#include "glad.h" #include "shader.h" #include <Windows.h> #include <iostream>
#define STB_IMAGE_IMPLEMENTATION #include "stb_image.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
#if _DEBUG #pragma comment(linker, "/subsystem:console") int main(int argc, const char **argv) { return WinMain(GetModuleHandle(NULL), NULL, GetCommandLineA(), SW_SHOWDEFAULT); } #else #pragma comment(linker, "/subsystem:windows") #endif
#pragma comment(lib, "opengl32.lib")
void initOpenGL() { Shader ourShader("C:\\Users\\zhaoys-c\\source\\repos\\Dionysen\\LearnOpenGL\\OpenGL\\\\OpenGL\\shaders\\shader.vs", "C:\\Users\\zhaoys-c\\source\\repos\\Dionysen\\LearnOpenGL\\OpenGL\\\\OpenGL\\shaders\\shader.fs"); float vertices[] = { 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f };
unsigned int indices[] = { 0, 1, 3, 1, 2, 3 }; unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float))); glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float))); glEnableVertexAttribArray(2);
unsigned int texture1, texture2; glGenTextures(1, &texture1); glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
int width, height, nrChannels; stbi_set_flip_vertically_on_load(true); unsigned char *data = stbi_load("C:\\Users\\zhaoys-c\\source\\repos\\Dionysen\\LearnOpenGL\\OpenGL\\OpenGL\\Resources\\wall.jpg", &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout << "Failed to load texture1" << std::endl; }
stbi_image_free(data);
glGenTextures(1, &texture2); glBindTexture(GL_TEXTURE_2D, texture2); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); data = stbi_load("C:\\Users\\zhaoys-c\\source\\repos\\Dionysen\\LearnOpenGL\\OpenGL\\OpenGL\\Resources\\awesomeface.png", &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout << "Failed to load texture2" << std::endl; } stbi_image_free(data);
ourShader.use(); glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0); ourShader.setInt("texture2", 1);
ourShader.setFloat("visible", 0.1f); ourShader.use(); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture2); glBindVertexArray(VAO); }
void render() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); }
int WINAPI WinMain(_In_ HINSTANCE hinstance, _In_opt_ HINSTANCE hPrevInstance, _In_ PSTR szCmdLine, _In_ int ICmdShow) { WNDCLASSEX wndclass{}; wndclass.cbSize = sizeof(WNDCLASSEX); wndclass.style = 0; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hinstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); wndclass.lpszMenuName = 0; wndclass.lpszClassName = "OpenGL Window";
RegisterClassEx(&wndclass);
HWND hwnd = CreateWindowEx( 0, wndclass.lpszClassName, "OpenGL Window", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinstance, szCmdLine);
HDC hdc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 32; pfd.cStencilBits = 8;
int pixelFormat = ChoosePixelFormat(hdc, &pfd); SetPixelFormat(hdc, pixelFormat, &pfd);
HGLRC hrc = wglCreateContext(hdc); if (hrc) { wglMakeCurrent(hdc, hrc); } else { std::cout << "ERROR::HRC::CREATE_FAILED\n"; }
if (!gladLoadGL()) { std::cout << "Could not initialize GLAD \n"; } else { std::cout << "OpenGL Version " << GLVersion.major << std::endl; }
initOpenGL();
ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd);
MSG msg; while (true) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); render(); SwapBuffers(hdc); } } return (int)msg.wParam; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_SIZE: glViewport(0, 0, LOWORD(lParam), HIWORD(lParam)); break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: HDC hdc = GetDC(hwnd); HGLRC hglrc = wglGetCurrentContext();
glBindVertexArray(0);
wglMakeCurrent(NULL, NULL); wglDeleteContext(hglrc); ReleaseDC(hwnd, hdc); PostQuitMessage(0); break; }
return DefWindowProc(hwnd, iMsg, wParam, lParam); }
|