#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 = 256; rect.bottom = 256; 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(); //init fsqgpu program GPUProgram originalProgram; originalProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); originalProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad.fs"); originalProgram.Link(); originalProgram.DetectAttribute("pos"); originalProgram.DetectAttribute("texcoord"); originalProgram.DetectUniform("U_MainTexture"); //高斯模糊 GPUProgram gaussianProgram; gaussianProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); gaussianProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad_gaussian.fs"); gaussianProgram.Link(); gaussianProgram.DetectAttribute("pos"); gaussianProgram.DetectAttribute("texcoord"); gaussianProgram.DetectUniform("U_MainTexture"); //combine program GPUProgram combineProgram; combineProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); combineProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/normal_blend.fs"); combineProgram.Link(); combineProgram.DetectAttribute("pos"); combineProgram.DetectAttribute("texcoord"); combineProgram.DetectUniform("U_BaseTexture"); combineProgram.DetectUniform("U_BlendTexture"); //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(); //初始化FBO FBO fbo[2]; fbo[0].AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight); fbo[0].AttachDepthBuffer("depth", viewportWidth, viewportHeight); fbo[1].AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight); fbo[1].AttachDepthBuffer("depth", viewportWidth, viewportHeight); GLuint head = CreateTextureFromFile("res/image/head.png"); GLuint grass = CreateTextureFromFile("res/image/grass.png"); 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(originalProgram.mProgram); glBindTexture(GL_TEXTURE_2D, head); glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0); fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord")); glUseProgram(originalProgram.mProgram); glBindTexture(GL_TEXTURE_2D, grass); glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0); fsq.DrawToRightTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord")); glUseProgram(combineProgram.mProgram); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, head); glUniform1i(combineProgram.GetLocation("U_BaseTexture"), 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, grass); glUniform1i(combineProgram.GetLocation("U_BlendTexture"), 1); fsq.DrawToRightBottom(combineProgram.GetLocation("pos"), combineProgram.GetLocation("texcoord")); glDisable(GL_BLEND); glFlush(); SwapBuffers(dc); } return 0; }