#include #include "glew.h" #include #include #include "utils.h" #include "GPUProgram.h" #include "ObjModel.h" #include "FBO.h" #include "FullScreenQuad.h" #include "Glm/glm.hpp" #include "Glm/ext.hpp" #pragma comment(lib,"opengl32.lib") #pragma comment(lib,"glew32.lib") LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CLOSE: PostQuitMessage(0); break; } return DefWindowProc(hwnd,msg,wParam,lParam); } INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) { WNDCLASSEX wndClass; wndClass.cbClsExtra = 0; wndClass.cbSize = sizeof(WNDCLASSEX); wndClass.cbWndExtra = 0; wndClass.hbrBackground = NULL; wndClass.hCursor = LoadCursor(NULL,IDC_ARROW); wndClass.hIcon = NULL; wndClass.hIconSm = NULL; wndClass.hInstance = hInstance; wndClass.lpfnWndProc=GLWindowProc; wndClass.lpszClassName = "OpenGL"; wndClass.lpszMenuName = NULL; wndClass.style = CS_VREDRAW | CS_HREDRAW; ATOM atom = RegisterClassEx(&wndClass); RECT rect; rect.left = 0; rect.top = 0; rect.right = 1280; rect.bottom = 720; AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE); HWND hwnd = CreateWindowEx(NULL, "OpenGL", "RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL); HDC dc = GetDC(hwnd); PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DOUBLEBUFFER; pfd.iLayerType = PFD_MAIN_PLANE; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 24; pfd.cStencilBits = 8; int pixelFormatID = ChoosePixelFormat(dc, &pfd); SetPixelFormat(dc,pixelFormatID,&pfd); HGLRC rc = wglCreateContext(dc); wglMakeCurrent(dc, rc); GetClientRect(hwnd, &rect); int viewportWidth = rect.right - rect.left, viewportHeight = rect.bottom - rect.top; glewInit(); //gpu program GPUProgram gpuProgram; gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/skybox.vs"); gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/skybox.fs"); gpuProgram.Link(); gpuProgram.DetectAttribute("pos"); gpuProgram.DetectAttribute("texcoord"); gpuProgram.DetectAttribute("normal"); gpuProgram.DetectUniform("M"); gpuProgram.DetectUniform("V"); gpuProgram.DetectUniform("P"); //init 3d model ObjModel cube; cube.Init("res/model/Sphere.obj"); float identity[] = { 1.0f,0,0,0, 0,1.0f,0,0, 0,0,1.0f,0, 0,0,0,1.0f }; glm::mat4 cubeModelMatrix; glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f); GLuint mainTexture = SOIL_load_OGL_cubemap( "res/image/right.bmp", "res/image/left.bmp", "res/image/top.bmp", "res/image/bottom.bmp", "res/image/back.bmp", "res/image/front.bmp", 0, 0, SOIL_FLAG_POWER_OF_TWO ); ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); MSG msg; while (true) { if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE)) { if (msg.message==WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); } glDisable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(gpuProgram.mProgram); glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, identity); glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix)); glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(cubeModelMatrix)); glBindTexture(GL_TEXTURE_CUBE_MAP, mainTexture); glUniform1i(gpuProgram.GetLocation("U_MainTexture"), 0); cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal")); cube.Draw(); glUseProgram(0); glEnable(GL_DEPTH_TEST); glFlush(); SwapBuffers(dc); } return 0; }