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.

239 lines
8.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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 = 800;
  44. rect.bottom = 600;
  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. //init hdr program
  73. GPUProgram hdrProgram;
  74. hdrProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  75. hdrProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/hdrrender.fs");
  76. hdrProgram.Link();
  77. hdrProgram.DetectAttribute("pos");
  78. hdrProgram.DetectAttribute("texcoord");
  79. hdrProgram.DetectUniform("U_MainTexture");
  80. //init dilation program
  81. GPUProgram gaussianProgram;
  82. gaussianProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  83. gaussianProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad_gaussian.fs");
  84. gaussianProgram.Link();
  85. gaussianProgram.DetectAttribute("pos");
  86. gaussianProgram.DetectAttribute("texcoord");
  87. gaussianProgram.DetectUniform("U_MainTexture");
  88. //init gpu program
  89. GPUProgram gpuProgram;
  90. gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/test.vs");
  91. gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/hdr.fs");
  92. gpuProgram.Link();
  93. gpuProgram.DetectAttribute("pos");
  94. gpuProgram.DetectAttribute("texcoord");
  95. gpuProgram.DetectAttribute("normal");
  96. gpuProgram.DetectUniform("M");
  97. gpuProgram.DetectUniform("V");
  98. gpuProgram.DetectUniform("P");
  99. gpuProgram.DetectUniform("NM");
  100. gpuProgram.DetectUniform("U_AmbientLightColor");
  101. gpuProgram.DetectUniform("U_AmbientMaterial");
  102. gpuProgram.DetectUniform("U_DiffuseLightColor");
  103. gpuProgram.DetectUniform("U_DiffuseMaterial");
  104. gpuProgram.DetectUniform("U_DiffuseIntensity");
  105. gpuProgram.DetectUniform("U_LightPos");
  106. gpuProgram.DetectUniform("U_LightDirection");
  107. gpuProgram.DetectUniform("U_Cutoff");
  108. gpuProgram.DetectUniform("U_SpecularLightColor");
  109. gpuProgram.DetectUniform("U_SpecularMaterial");
  110. gpuProgram.DetectUniform("U_EyePos");
  111. //init 3d model
  112. ObjModel obj;
  113. obj.Init("res/model/Cube.obj");
  114. float identity[] = {
  115. 1.0f,0,0,0,
  116. 0,1.0f,0,0,
  117. 0,0,1.0f,0,
  118. 0,0,0,1.0f
  119. };
  120. float ambientLightColor[] = { 0.4f,0.4f,0.4f,1.0f };
  121. float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };
  122. float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  123. float diffuseMaterial[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  124. float lightPos[] = { 0.1f,3.0f, -2.2f,1.0f };
  125. float diffuseIntensity = 4.0f;
  126. float spotLightDirection[] = { 0.0f, -1.0f, 0.0f, 128.0f };//������һλ�ij�......
  127. float spotLightCutoff = 15.0f;
  128. float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  129. float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };
  130. float eyePos[] = { 0.0f,0.0f,0.0f };
  131. glm::mat4 model = glm::translate<float>(0.0f, 0.0f, -2.5f) * glm::rotate(-60.0f, 1.0f, 1.0f, 1.0f);
  132. glm::mat4 viewMatrix = glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
  133. glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
  134. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  135. //��ʼ��fsq
  136. FullScreenQuad fsq;
  137. fsq.Init();
  138. //��ʼ��FBO
  139. FBO fbo;
  140. fbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  141. fbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  142. fbo.Finish();
  143. FBO HDRfbo;
  144. HDRfbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA16F, viewportWidth, viewportHeight);
  145. HDRfbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  146. HDRfbo.Finish();
  147. ShowWindow(hwnd, SW_SHOW);
  148. UpdateWindow(hwnd);
  149. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  150. MSG msg;
  151. while (true)
  152. {
  153. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  154. {
  155. if (msg.message==WM_QUIT)
  156. {
  157. break;
  158. }
  159. TranslateMessage(&msg);
  160. DispatchMessage(&msg);
  161. }
  162. glEnable(GL_DEPTH_TEST);
  163. HDRfbo.Bind();
  164. glUseProgram(gpuProgram.mProgram);
  165. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  166. glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1, ambientLightColor);
  167. glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);
  168. glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
  169. glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
  170. glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
  171. glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
  172. glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
  173. glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
  174. glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
  175. glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
  176. glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
  177. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
  178. glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  179. glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  180. glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
  181. obj.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  182. obj.Draw();
  183. glUseProgram(0);
  184. HDRfbo.Unbind();
  185. glFlush();
  186. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  187. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  188. glActiveTexture(GL_TEXTURE0);
  189. glUseProgram(originalProgram.mProgram);
  190. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("color"));
  191. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  192. fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  193. glUseProgram(hdrProgram.mProgram);
  194. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("color"));
  195. glUniform1i(hdrProgram.GetLocation("U_MainTexture"), 0);
  196. fsq.DrawToRightTop(hdrProgram.GetLocation("pos"), hdrProgram.GetLocation("texcoord"));
  197. glUseProgram(gaussianProgram.mProgram);
  198. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("color"));
  199. glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
  200. fsq.DrawToRightBottom(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
  201. SwapBuffers(dc);
  202. }
  203. return 0;
  204. }