You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #include <windows.h>
  2. #include "glew.h"
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include "utils.h"
  6. #include "GPUProgram.h"
  7. #include "ObjModel.h"
  8. #include "FBO.h"
  9. #include "FullScreenQuad.h"
  10. #include "Glm/glm.hpp"
  11. #include "Glm/ext.hpp"
  12. #pragma comment(lib,"opengl32.lib")
  13. #pragma comment(lib,"glew32.lib")
  14. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  15. {
  16. switch (msg)
  17. {
  18. case WM_CLOSE:
  19. PostQuitMessage(0);
  20. break;
  21. }
  22. return DefWindowProc(hwnd,msg,wParam,lParam);
  23. }
  24. INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  25. {
  26. WNDCLASSEX wndClass;
  27. wndClass.cbClsExtra = 0;
  28. wndClass.cbSize = sizeof(WNDCLASSEX);
  29. wndClass.cbWndExtra = 0;
  30. wndClass.hbrBackground = NULL;
  31. wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
  32. wndClass.hIcon = NULL;
  33. wndClass.hIconSm = NULL;
  34. wndClass.hInstance = hInstance;
  35. wndClass.lpfnWndProc=GLWindowProc;
  36. wndClass.lpszClassName = "OpenGL";
  37. wndClass.lpszMenuName = NULL;
  38. wndClass.style = CS_VREDRAW | CS_HREDRAW;
  39. ATOM atom = RegisterClassEx(&wndClass);
  40. RECT rect;
  41. rect.left = 0;
  42. rect.top = 0;
  43. rect.right = 256;
  44. rect.bottom = 256;
  45. AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
  46. HWND hwnd = CreateWindowEx(NULL, "OpenGL", "RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL);
  47. HDC dc = GetDC(hwnd);
  48. PIXELFORMATDESCRIPTOR pfd;
  49. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  50. pfd.nVersion = 1;
  51. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DOUBLEBUFFER;
  52. pfd.iLayerType = PFD_MAIN_PLANE;
  53. pfd.iPixelType = PFD_TYPE_RGBA;
  54. pfd.cColorBits = 32;
  55. pfd.cDepthBits = 24;
  56. pfd.cStencilBits = 8;
  57. int pixelFormatID = ChoosePixelFormat(dc, &pfd);
  58. SetPixelFormat(dc,pixelFormatID,&pfd);
  59. HGLRC rc = wglCreateContext(dc);
  60. wglMakeCurrent(dc, rc);
  61. GetClientRect(hwnd, &rect);
  62. int viewportWidth = rect.right - rect.left, viewportHeight = rect.bottom - rect.top;
  63. glewInit();
  64. //init fsqgpu program
  65. GPUProgram originalProgram;
  66. originalProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  67. originalProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad.fs");
  68. originalProgram.Link();
  69. originalProgram.DetectAttribute("pos");
  70. originalProgram.DetectAttribute("texcoord");
  71. originalProgram.DetectUniform("U_MainTexture");
  72. //��˹ģ��
  73. GPUProgram gaussianProgram;
  74. gaussianProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  75. gaussianProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad_gaussian.fs");
  76. gaussianProgram.Link();
  77. gaussianProgram.DetectAttribute("pos");
  78. gaussianProgram.DetectAttribute("texcoord");
  79. gaussianProgram.DetectUniform("U_MainTexture");
  80. //combine program
  81. GPUProgram combineProgram;
  82. combineProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  83. combineProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/normal_blend.fs");
  84. combineProgram.Link();
  85. combineProgram.DetectAttribute("pos");
  86. combineProgram.DetectAttribute("texcoord");
  87. combineProgram.DetectUniform("U_BaseTexture");
  88. combineProgram.DetectUniform("U_BlendTexture");
  89. //init 3d model
  90. ObjModel quad;
  91. quad.Init("res/model/Quad.obj");
  92. float identity[] = {
  93. 1.0f,0,0,0,
  94. 0,1.0f,0,0,
  95. 0,0,1.0f,0,
  96. 0,0,0,1.0f
  97. };
  98. glm::mat4 quadModel = glm::translate<float>(0.0f, 0.0f, -2.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(8.0f, 8.0f, 8.0f);
  99. glm::mat4 quadNormalMatrix = glm::inverseTranspose(quadModel);
  100. 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));
  101. glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
  102. //��ʼ��fsq
  103. FullScreenQuad fsq;
  104. fsq.Init();
  105. //��ʼ��FBO
  106. FBO fbo[2];
  107. fbo[0].AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  108. fbo[0].AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  109. fbo[1].AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  110. fbo[1].AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  111. GLuint head = CreateTextureFromFile("res/image/head.png");
  112. GLuint grass = CreateTextureFromFile("res/image/grass.png");
  113. ShowWindow(hwnd, SW_SHOW);
  114. UpdateWindow(hwnd);
  115. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  116. MSG msg;
  117. while (true)
  118. {
  119. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  120. {
  121. if (msg.message==WM_QUIT)
  122. {
  123. break;
  124. }
  125. TranslateMessage(&msg);
  126. DispatchMessage(&msg);
  127. }
  128. glEnable(GL_DEPTH_TEST);
  129. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  130. glEnable(GL_BLEND);
  131. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  132. glActiveTexture(GL_TEXTURE0);
  133. glUseProgram(originalProgram.mProgram);
  134. glBindTexture(GL_TEXTURE_2D, head);
  135. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  136. fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  137. glUseProgram(originalProgram.mProgram);
  138. glBindTexture(GL_TEXTURE_2D, grass);
  139. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  140. fsq.DrawToRightTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  141. glUseProgram(combineProgram.mProgram);
  142. glActiveTexture(GL_TEXTURE0);
  143. glBindTexture(GL_TEXTURE_2D, head);
  144. glUniform1i(combineProgram.GetLocation("U_BaseTexture"), 0);
  145. glActiveTexture(GL_TEXTURE1);
  146. glBindTexture(GL_TEXTURE_2D, grass);
  147. glUniform1i(combineProgram.GetLocation("U_BlendTexture"), 1);
  148. fsq.DrawToRightBottom(combineProgram.GetLocation("pos"), combineProgram.GetLocation("texcoord"));
  149. glDisable(GL_BLEND);
  150. glFlush();
  151. SwapBuffers(dc);
  152. }
  153. return 0;
  154. }