#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 = 512; rect.bottom = 512; 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(); //combine program GPUProgram combineProgram1; combineProgram1.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); combineProgram1.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad.fs"); combineProgram1.Link(); combineProgram1.DetectAttribute("pos"); combineProgram1.DetectAttribute("texcoord"); combineProgram1.DetectUniform("U_MainTexture"); //combine program GPUProgram combineProgram2; combineProgram2.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); combineProgram2.AttachShader(GL_FRAGMENT_SHADER, "res/shader/smooth.fs"); combineProgram2.Link(); combineProgram2.DetectAttribute("pos"); combineProgram2.DetectAttribute("texcoord"); combineProgram2.DetectUniform("U_MainTexture"); //combine program GPUProgram combineProgram3; combineProgram3.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); combineProgram3.AttachShader(GL_FRAGMENT_SHADER, "res/shader/sharpen.fs"); combineProgram3.Link(); combineProgram3.DetectAttribute("pos"); combineProgram3.DetectAttribute("texcoord"); combineProgram3.DetectUniform("U_MainTexture"); //combine program GPUProgram combineProgram4; combineProgram4.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); combineProgram4.AttachShader(GL_FRAGMENT_SHADER, "res/shader/edge_detect.fs"); combineProgram4.Link(); combineProgram4.DetectAttribute("pos"); combineProgram4.DetectAttribute("texcoord"); combineProgram4.DetectUniform("U_MainTexture"); //init 3d model ObjModel quad; quad.Init("res/model/Quad.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 quadModel = glm::translate(0.0f, 0.0f, -2.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(8.0f, 8.0f, 8.0f); glm::mat4 quadNormalMatrix = glm::inverseTranspose(quadModel); glm::mat4 viewMatrix = glm::lookAt(glm::vec3(1.0f, 0.5f, -10.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f); //³õʼ»¯fsq FullScreenQuad fsq; fsq.Init(); GLuint head = CreateTextureFromFile("res/image/wood.bmp"); GLuint grass = CreateTextureFromFile("res/image/earth.bmp"); 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); } glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glActiveTexture(GL_TEXTURE0); glUseProgram(combineProgram1.mProgram); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, head); glUniform1i(combineProgram1.GetLocation("U_MainTexture"), 0); fsq.DrawToLeftTop(combineProgram1.GetLocation("pos"), combineProgram1.GetLocation("texcoord")); glUseProgram(combineProgram2.mProgram); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, head); glUniform1i(combineProgram2.GetLocation("U_MainTexture"), 0); fsq.DrawToRightTop(combineProgram2.GetLocation("pos"), combineProgram2.GetLocation("texcoord")); glUseProgram(combineProgram3.mProgram); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, head); glUniform1i(combineProgram3.GetLocation("U_MainTexture"), 0); fsq.DrawToLeftBottom(combineProgram3.GetLocation("pos"), combineProgram3.GetLocation("texcoord")); glUseProgram(combineProgram4.mProgram); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, head); glUniform1i(combineProgram4.GetLocation("U_MainTexture"), 0); fsq.DrawToRightBottom(combineProgram4.GetLocation("pos"), combineProgram4.GetLocation("texcoord")); glDisable(GL_BLEND); glFlush(); SwapBuffers(dc); } return 0; }