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.

334 lines
12 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
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[2];
  164. fbo[0].AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  165. fbo[0].AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  166. fbo[1].AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  167. fbo[1].AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  168. FBO HDRfbo;
  169. HDRfbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  170. HDRfbo.AttachColorBuffer("hdrcolor", GL_COLOR_ATTACHMENT1, GL_RGBA16F, viewportWidth, viewportHeight);
  171. HDRfbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  172. HDRfbo.Finish();
  173. ShowWindow(hwnd, SW_SHOW);
  174. UpdateWindow(hwnd);
  175. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  176. MSG msg;
  177. while (true)
  178. {
  179. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  180. {
  181. if (msg.message==WM_QUIT)
  182. {
  183. break;
  184. }
  185. TranslateMessage(&msg);
  186. DispatchMessage(&msg);
  187. }
  188. glEnable(GL_DEPTH_TEST);
  189. HDRfbo.Bind();
  190. glUseProgram(gpuProgram.mProgram);
  191. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  192. glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1, ambientLightColor);
  193. glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);
  194. glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
  195. glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
  196. glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
  197. glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
  198. glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
  199. glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
  200. glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
  201. glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
  202. glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
  203. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
  204. glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  205. glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  206. glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
  207. obj.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  208. obj.Draw();
  209. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
  210. glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(quadNormalMatrix));
  211. quad.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  212. quad.Draw();
  213. glUseProgram(lightSourceProgram.mProgram);
  214. glUniformMatrix4fv(lightSourceProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(sphereModel));
  215. glUniformMatrix4fv(lightSourceProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  216. glUniformMatrix4fv(lightSourceProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  217. sphere.Bind(lightSourceProgram.GetLocation("pos"), lightSourceProgram.GetLocation("texcoord"), lightSourceProgram.GetLocation("normal"));
  218. sphere.Draw();
  219. glUseProgram(0);
  220. HDRfbo.Unbind();
  221. /* blur start */
  222. fbo[0].Bind();
  223. glUseProgram(gaussianProgram.mProgram);
  224. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  225. glActiveTexture(GL_TEXTURE0);
  226. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("hdrcolor"));
  227. glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
  228. fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
  229. fbo[0].Unbind();
  230. fbo[1].Bind();
  231. glUseProgram(gaussianProgram.mProgram);
  232. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  233. glActiveTexture(GL_TEXTURE0);
  234. glBindTexture(GL_TEXTURE_2D, fbo[0].GetBuffer("color"));
  235. glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
  236. fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
  237. fbo[1].Unbind();
  238. fbo[0].Bind();
  239. glUseProgram(gaussianProgram.mProgram);
  240. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  241. glActiveTexture(GL_TEXTURE0);
  242. glBindTexture(GL_TEXTURE_2D, fbo[1].GetBuffer("color"));
  243. glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
  244. fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
  245. fbo[0].Unbind();
  246. fbo[1].Bind();
  247. glUseProgram(gaussianProgram.mProgram);
  248. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  249. glActiveTexture(GL_TEXTURE0);
  250. glBindTexture(GL_TEXTURE_2D, fbo[0].GetBuffer("color"));
  251. glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
  252. fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
  253. fbo[1].Unbind();
  254. /* blur end */
  255. glFlush();
  256. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  257. glActiveTexture(GL_TEXTURE0);
  258. glUseProgram(originalProgram.mProgram);
  259. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("color"));
  260. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  261. fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  262. glUseProgram(hdrProgram.mProgram);
  263. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("hdrcolor"));
  264. glUniform1i(hdrProgram.GetLocation("U_MainTexture"), 0);
  265. fsq.DrawToRightTop(hdrProgram.GetLocation("pos"), hdrProgram.GetLocation("texcoord"));
  266. glUseProgram(gaussianProgram.mProgram);
  267. glBindTexture(GL_TEXTURE_2D, fbo[1].GetBuffer("color"));
  268. glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
  269. fsq.DrawToLeftBottom(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
  270. glUseProgram(combineProgram.mProgram);
  271. glActiveTexture(GL_TEXTURE0);
  272. glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("color"));
  273. glUniform1i(combineProgram.GetLocation("U_MainTexture"), 0);
  274. glActiveTexture(GL_TEXTURE1);
  275. glBindTexture(GL_TEXTURE_2D, fbo[1].GetBuffer("color"));
  276. glUniform1i(combineProgram.GetLocation("U_HDRTexture"), 1);
  277. fsq.DrawToRightBottom(combineProgram.GetLocation("pos"), combineProgram.GetLocation("texcoord"));
  278. SwapBuffers(dc);
  279. }
  280. return 0;
  281. }