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.

302 lines
11 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
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 = 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. //��˹ģ��
  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. //combine program
  89. GPUProgram combineProgram;
  90. combineProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  91. combineProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/combine.fs");
  92. combineProgram.Link();
  93. combineProgram.DetectAttribute("pos");
  94. combineProgram.DetectAttribute("texcoord");
  95. combineProgram.DetectUniform("U_MainTexture");
  96. combineProgram.DetectUniform("U_HDRTexture");
  97. //init light source program
  98. GPUProgram lightSourceProgram;
  99. lightSourceProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/lightSource.vs");
  100. lightSourceProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/lightSource.fs");
  101. lightSourceProgram.Link();
  102. lightSourceProgram.DetectAttribute("pos");
  103. lightSourceProgram.DetectUniform("M");
  104. lightSourceProgram.DetectUniform("V");
  105. lightSourceProgram.DetectUniform("P");
  106. //init gpu program
  107. GPUProgram gpuProgram;
  108. gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/test.vs");
  109. gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/hdr.fs");
  110. gpuProgram.Link();
  111. gpuProgram.DetectAttribute("pos");
  112. gpuProgram.DetectAttribute("texcoord");
  113. gpuProgram.DetectAttribute("normal");
  114. gpuProgram.DetectUniform("M");
  115. gpuProgram.DetectUniform("V");
  116. gpuProgram.DetectUniform("P");
  117. gpuProgram.DetectUniform("NM");
  118. gpuProgram.DetectUniform("U_AmbientLightColor");
  119. gpuProgram.DetectUniform("U_AmbientMaterial");
  120. gpuProgram.DetectUniform("U_DiffuseLightColor");
  121. gpuProgram.DetectUniform("U_DiffuseMaterial");
  122. gpuProgram.DetectUniform("U_DiffuseIntensity");
  123. gpuProgram.DetectUniform("U_LightPos");
  124. gpuProgram.DetectUniform("U_LightDirection");
  125. gpuProgram.DetectUniform("U_Cutoff");
  126. gpuProgram.DetectUniform("U_SpecularLightColor");
  127. gpuProgram.DetectUniform("U_SpecularMaterial");
  128. gpuProgram.DetectUniform("U_EyePos");
  129. //init 3d model
  130. ObjModel obj,quad,sphere;
  131. obj.Init("res/model/Cube.obj");
  132. quad.Init("res/model/Quad.obj");
  133. sphere.Init("res/model/Sphere.obj");
  134. float identity[] = {
  135. 1.0f,0,0,0,
  136. 0,1.0f,0,0,
  137. 0,0,1.0f,0,
  138. 0,0,0,1.0f
  139. };
  140. float ambientLightColor[] = { 0.4f,0.4f,0.4f,1.0f };
  141. float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };
  142. float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  143. float diffuseMaterial[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  144. float lightPos[] = { 0.1f,3.0f, -2.2f,1.0f };
  145. float diffuseIntensity = 3.0f;
  146. float spotLightDirection[] = { 0.0f, -1.0f, 0.0f, 128.0f };//������һλ�ij�......
  147. float spotLightCutoff = 0.0f;
  148. float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  149. float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };
  150. float eyePos[] = { 0.0f, 0.0f, 0.0f };
  151. glm::mat4 model = glm::translate<float>(0.0f, 0.0f, -2.0f) * glm::rotate(-60.0f, 1.0f, 1.0f, 1.0f);
  152. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  153. 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);
  154. glm::mat4 quadNormalMatrix = glm::inverseTranspose(quadModel);
  155. glm::mat4 sphereModel = glm::translate<float>(0.1f, 3.0f, -2.2f) * glm::scale(0.2f, 0.2f, 0.2f);
  156. glm::mat4 sphereNormalMatrix = glm::inverseTranspose(sphereModel);
  157. 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));
  158. glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
  159. //��ʼ��fsq
  160. FullScreenQuad fsq;
  161. fsq.Init();
  162. //��ʼ��FBO
  163. FBO fbo;
  164. fbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  165. fbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  166. FBO HDRfbo;
  167. HDRfbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  168. HDRfbo.AttachColorBuffer("hdrcolor", GL_COLOR_ATTACHMENT1, GL_RGBA16F, viewportWidth, viewportHeight);
  169. HDRfbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  170. HDRfbo.Finish();
  171. ShowWindow(hwnd, SW_SHOW);
  172. UpdateWindow(hwnd);
  173. glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  174. MSG msg;
  175. while (true)
  176. {
  177. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  178. {
  179. if (msg.message==WM_QUIT)
  180. {
  181. break;
  182. }
  183. TranslateMessage(&msg);
  184. DispatchMessage(&msg);
  185. }
  186. glEnable(GL_DEPTH_TEST);
  187. HDRfbo.Bind();
  188. glUseProgram(gpuProgram.mProgram);
  189. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  190. glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1, ambientLightColor);
  191. glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);
  192. glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
  193. glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
  194. glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
  195. glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
  196. glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
  197. glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
  198. glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
  199. glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
  200. glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
  201. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
  202. glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  203. glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  204. glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
  205. obj.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  206. obj.Draw();
  207. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
  208. glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(quadNormalMatrix));
  209. quad.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  210. quad.Draw();
  211. glUseProgram(lightSourceProgram.mProgram);
  212. glUniformMatrix4fv(lightSourceProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(sphereModel));
  213. glUniformMatrix4fv(lightSourceProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  214. glUniformMatrix4fv(lightSourceProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  215. sphere.Bind(lightSourceProgram.GetLocation("pos"), lightSourceProgram.GetLocation("texcoord"), lightSourceProgram.GetLocation("normal"));
  216. sphere.Draw();
  217. glUseProgram(0);
  218. HDRfbo.Unbind();
  219. fbo.Bind();
  220. glUseProgram(gaussianProgram.mProgram);
  221. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  222. glActiveTexture(GL_TEXTURE0);
  223. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("hdrcolor"));
  224. glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
  225. fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
  226. fbo.Unbind();
  227. glFlush();
  228. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  229. glActiveTexture(GL_TEXTURE0);
  230. glUseProgram(originalProgram.mProgram);
  231. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("color"));
  232. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  233. fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  234. glUseProgram(hdrProgram.mProgram);
  235. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("hdrcolor"));
  236. glUniform1i(hdrProgram.GetLocation("U_MainTexture"), 0);
  237. fsq.DrawToRightTop(hdrProgram.GetLocation("pos"), hdrProgram.GetLocation("texcoord"));
  238. glUseProgram(gaussianProgram.mProgram);
  239. glBindTexture(GL_TEXTURE_2D, fbo.GetBuffer("color"));
  240. glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
  241. fsq.DrawToLeftBottom(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
  242. glUseProgram(combineProgram.mProgram);
  243. glActiveTexture(GL_TEXTURE0);
  244. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("color"));
  245. glUniform1i(combineProgram.GetLocation("U_MainTexture"), 0);
  246. glActiveTexture(GL_TEXTURE1);
  247. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("hdrcolor"));
  248. glUniform1i(combineProgram.GetLocation("U_HDRTexture"), 1);
  249. fsq.DrawToRightBottom(combineProgram.GetLocation("pos"), combineProgram.GetLocation("texcoord"));
  250. SwapBuffers(dc);
  251. }
  252. return 0;
  253. }