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.

365 lines
15 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
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 = 1280;
  44. rect.bottom = 720;
  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 depth render program
  73. GPUProgram depthRenderProgram;
  74. depthRenderProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  75. depthRenderProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/depthrender.fs");
  76. depthRenderProgram.Link();
  77. depthRenderProgram.DetectAttribute("pos");
  78. depthRenderProgram.DetectAttribute("texcoord");
  79. depthRenderProgram.DetectUniform("U_MainTexture");
  80. //init depth program
  81. GPUProgram depthProgram;
  82. depthProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/sample_depth_buffer.vs");
  83. depthProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/sample_depth_buffer.fs");
  84. depthProgram.Link();
  85. depthProgram.DetectAttribute("pos");
  86. depthProgram.DetectUniform("M");
  87. depthProgram.DetectUniform("V");
  88. depthProgram.DetectUniform("P");
  89. //init projector program
  90. GPUProgram sampleProgram;
  91. sampleProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/sample.vs");
  92. sampleProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/sample.fs");
  93. sampleProgram.Link();
  94. sampleProgram.DetectAttribute("pos");
  95. sampleProgram.DetectAttribute("texcoord");
  96. sampleProgram.DetectUniform("M");
  97. sampleProgram.DetectUniform("V");
  98. sampleProgram.DetectUniform("P");
  99. sampleProgram.DetectUniform("U_MainTexture");
  100. //init projective texture program
  101. GPUProgram projectedTextureProgram;
  102. projectedTextureProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/projector.vs");
  103. projectedTextureProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/projector_shadow.fs");
  104. projectedTextureProgram.Link();
  105. projectedTextureProgram.DetectAttribute("pos");
  106. projectedTextureProgram.DetectAttribute("texcoord");
  107. projectedTextureProgram.DetectUniform("M");
  108. projectedTextureProgram.DetectUniform("V");
  109. projectedTextureProgram.DetectUniform("P");
  110. projectedTextureProgram.DetectUniform("U_ShadowMap");
  111. projectedTextureProgram.DetectUniform("U_MainTexture");
  112. projectedTextureProgram.DetectUniform("U_ProjectiveTexture");
  113. projectedTextureProgram.DetectUniform("U_ProjectorMatrix");
  114. projectedTextureProgram.DetectUniform("U_ProjectorNoTransScaleMatrix");
  115. //init projective texture program2
  116. GPUProgram projectedTextureProgram2;
  117. projectedTextureProgram2.AttachShader(GL_VERTEX_SHADER, "res/shader/projector.vs");
  118. projectedTextureProgram2.AttachShader(GL_FRAGMENT_SHADER, "res/shader/projector_shadow2.fs");
  119. projectedTextureProgram2.Link();
  120. projectedTextureProgram2.DetectAttribute("pos");
  121. projectedTextureProgram2.DetectAttribute("texcoord");
  122. projectedTextureProgram2.DetectUniform("M");
  123. projectedTextureProgram2.DetectUniform("V");
  124. projectedTextureProgram2.DetectUniform("P");
  125. projectedTextureProgram2.DetectUniform("U_ShadowMap");
  126. projectedTextureProgram2.DetectUniform("U_MainTexture");
  127. projectedTextureProgram2.DetectUniform("U_ProjectiveTexture");
  128. projectedTextureProgram2.DetectUniform("U_ProjectorMatrix");
  129. projectedTextureProgram2.DetectUniform("U_ProjectorNoTransScaleMatrix");
  130. //init 3d model
  131. ObjModel obj, quad, sphere;
  132. obj.Init("res/model/Cube.obj");
  133. quad.Init("res/model/Quad.obj");
  134. sphere.Init("res/model/Sphere.obj");
  135. float identity[] = {
  136. 1.0f,0,0,0,
  137. 0,1.0f,0,0,
  138. 0,0,1.0f,0,
  139. 0,0,0,1.0f
  140. };
  141. glm::mat4 model = glm::translate<float>(6.0f, 0.0f, -6.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
  142. glm::mat4 quadModel = glm::translate<float>(6.0f, -1.5f, -6.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(20.0f, 20.0f, 20.0f);
  143. glm::mat4 sphereModel = glm::translate<float>(0.0f, 0.0f, 0.0f) * glm::scale(0.3f, 0.3f, 0.3f);
  144. glm::mat4 viewMatrix = glm::lookAt(glm::vec3(10.0f, 3.0f, 0.0f), glm::vec3(6.0f, -1.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
  145. glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
  146. glm::mat4 projectorViewMatrix = glm::lookAt(glm::vec3(0.0f, 4.0f, 0.0f), glm::vec3(6.0f, -1.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
  147. glm::mat4 projectorProjectionMatrix = glm::perspective(20.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
  148. glm::mat4 projectorMatrix = glm::translate<float>(0.5f, 0.5f, 0.5f)*glm::scale(0.5f, 0.5f, 0.5f) * projectorProjectionMatrix * projectorViewMatrix;
  149. glm::mat4 projectorNoTransScaleMatrix = projectorProjectionMatrix * projectorViewMatrix;
  150. //��ʼ��fsq
  151. FullScreenQuad fsq;
  152. fsq.Init();
  153. //��ʼ��FBO
  154. FBO originalFbo;
  155. originalFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  156. originalFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  157. originalFbo.Finish();
  158. FBO projectiveTextureFbo;
  159. projectiveTextureFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  160. projectiveTextureFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  161. projectiveTextureFbo.Finish();
  162. FBO projectiveTextureFbo2;
  163. projectiveTextureFbo2.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  164. projectiveTextureFbo2.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  165. projectiveTextureFbo2.Finish();
  166. FBO depthFbo;
  167. depthFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGB, viewportWidth, viewportHeight);
  168. depthFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  169. depthFbo.Finish();
  170. GLuint mainTexture = SOIL_load_OGL_texture("res/image/stone.bmp", 0, 0, SOIL_FLAG_POWER_OF_TWO);
  171. GLuint projectiveTexture = SOIL_load_OGL_texture("res/image/head.png", 0, 0, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_INVERT_Y);
  172. ShowWindow(hwnd, SW_SHOW);
  173. UpdateWindow(hwnd);
  174. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  175. glEnable(GL_DEPTH_TEST);
  176. glEnable(GL_CULL_FACE);
  177. glCullFace(GL_FRONT);
  178. depthFbo.Bind();
  179. glViewport(0, 0, viewportWidth, viewportHeight);
  180. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  181. glUseProgram(depthProgram.mProgram);
  182. glUniformMatrix4fv(depthProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectorProjectionMatrix));
  183. glUniformMatrix4fv(depthProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
  184. glUniformMatrix4fv(depthProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(projectorViewMatrix));
  185. obj.Bind(depthProgram.GetLocation("pos"), depthProgram.GetLocation("texcoord"), depthProgram.GetLocation("normal"));
  186. obj.Draw();
  187. glUniformMatrix4fv(depthProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
  188. quad.Bind(depthProgram.GetLocation("pos"), depthProgram.GetLocation("texcoord"), depthProgram.GetLocation("normal"));
  189. quad.Draw();
  190. depthFbo.Unbind();
  191. glCullFace(GL_BACK);
  192. /*ԭͼfbo*/
  193. originalFbo.Bind();
  194. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  195. glUseProgram(sampleProgram.mProgram);
  196. glUniformMatrix4fv(sampleProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  197. glUniformMatrix4fv(sampleProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  198. glUniformMatrix4fv(sampleProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
  199. glBindTexture(GL_TEXTURE_2D, mainTexture);
  200. glUniform1i(sampleProgram.GetLocation("U_MainTexture"), 0);
  201. obj.Bind(sampleProgram.GetLocation("pos"), sampleProgram.GetLocation("texcoord"), sampleProgram.GetLocation("normal"));
  202. obj.Draw();
  203. glUniformMatrix4fv(sampleProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
  204. quad.Bind(sampleProgram.GetLocation("pos"), sampleProgram.GetLocation("texcoord"), sampleProgram.GetLocation("normal"));
  205. quad.Draw();
  206. originalFbo.Unbind();
  207. /*ͶӰfbo*/
  208. glEnable(GL_BLEND);
  209. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  210. projectiveTextureFbo.Bind();
  211. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  212. glUseProgram(projectedTextureProgram.mProgram);
  213. glUniformMatrix4fv(projectedTextureProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  214. glUniformMatrix4fv(projectedTextureProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  215. glUniformMatrix4fv(projectedTextureProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
  216. glUniformMatrix4fv(projectedTextureProgram.GetLocation("U_ProjectorMatrix"), 1, GL_FALSE, glm::value_ptr(projectorMatrix));
  217. glUniformMatrix4fv(projectedTextureProgram.GetLocation("U_ProjectorNoTransScaleMatrix"), 1, GL_FALSE, glm::value_ptr(projectorNoTransScaleMatrix));
  218. glActiveTexture(GL_TEXTURE0);
  219. glBindTexture(GL_TEXTURE_2D, mainTexture);
  220. glUniform1i(projectedTextureProgram.GetLocation("U_MainTexture"), 0);
  221. glActiveTexture(GL_TEXTURE1);
  222. glBindTexture(GL_TEXTURE_2D, projectiveTexture);
  223. glUniform1i(projectedTextureProgram.GetLocation("U_ProjectiveTexture"), 1);
  224. glActiveTexture(GL_TEXTURE2);
  225. glBindTexture(GL_TEXTURE_2D, depthFbo.GetBuffer("depth"));
  226. glUniform1i(projectedTextureProgram.GetLocation("U_ShadowMap"), 2);
  227. obj.Bind(projectedTextureProgram.GetLocation("pos"), projectedTextureProgram.GetLocation("texcoord"), projectedTextureProgram.GetLocation("normal"));
  228. obj.Draw();
  229. glUniformMatrix4fv(projectedTextureProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
  230. quad.Bind(projectedTextureProgram.GetLocation("pos"), projectedTextureProgram.GetLocation("texcoord"), projectedTextureProgram.GetLocation("normal"));
  231. quad.Draw();
  232. projectiveTextureFbo.Unbind();
  233. glDisable(GL_BLEND);
  234. /*ͶӰfbo ����Ӱ*/
  235. glEnable(GL_BLEND);
  236. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  237. projectiveTextureFbo2.Bind();
  238. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  239. glUseProgram(projectedTextureProgram2.mProgram);
  240. glUniformMatrix4fv(projectedTextureProgram2.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  241. glUniformMatrix4fv(projectedTextureProgram2.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  242. glUniformMatrix4fv(projectedTextureProgram2.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
  243. glUniformMatrix4fv(projectedTextureProgram2.GetLocation("U_ProjectorMatrix"), 1, GL_FALSE, glm::value_ptr(projectorMatrix));
  244. glUniformMatrix4fv(projectedTextureProgram2.GetLocation("U_ProjectorNoTransScaleMatrix"), 1, GL_FALSE, glm::value_ptr(projectorNoTransScaleMatrix));
  245. glActiveTexture(GL_TEXTURE0);
  246. glBindTexture(GL_TEXTURE_2D, mainTexture);
  247. glUniform1i(projectedTextureProgram2.GetLocation("U_MainTexture"), 0);
  248. glActiveTexture(GL_TEXTURE1);
  249. glBindTexture(GL_TEXTURE_2D, projectiveTexture);
  250. glUniform1i(projectedTextureProgram2.GetLocation("U_ProjectiveTexture"), 1);
  251. glActiveTexture(GL_TEXTURE2);
  252. glBindTexture(GL_TEXTURE_2D, depthFbo.GetBuffer("depth"));
  253. glUniform1i(projectedTextureProgram2.GetLocation("U_ShadowMap"), 2);
  254. obj.Bind(projectedTextureProgram2.GetLocation("pos"), projectedTextureProgram2.GetLocation("texcoord"), projectedTextureProgram2.GetLocation("normal"));
  255. obj.Draw();
  256. glUniformMatrix4fv(projectedTextureProgram2.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
  257. quad.Bind(projectedTextureProgram2.GetLocation("pos"), projectedTextureProgram2.GetLocation("texcoord"), projectedTextureProgram2.GetLocation("normal"));
  258. quad.Draw();
  259. projectiveTextureFbo2.Unbind();
  260. glDisable(GL_BLEND);
  261. MSG msg;
  262. while (true)
  263. {
  264. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  265. {
  266. if (msg.message == WM_QUIT)
  267. {
  268. break;
  269. }
  270. TranslateMessage(&msg);
  271. DispatchMessage(&msg);
  272. }
  273. glClearColor(0.1f, 0.4f, 0.7f, 1.0f);
  274. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  275. glUseProgram(originalProgram.mProgram);
  276. glActiveTexture(GL_TEXTURE0);
  277. glBindTexture(GL_TEXTURE_2D, originalFbo.GetBuffer("color"));
  278. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  279. fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  280. glUseProgram(originalProgram.mProgram);
  281. glActiveTexture(GL_TEXTURE0);
  282. glBindTexture(GL_TEXTURE_2D, projectiveTextureFbo.GetBuffer("color"));
  283. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  284. fsq.DrawToRightTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  285. glUseProgram(depthRenderProgram.mProgram);
  286. glActiveTexture(GL_TEXTURE0);
  287. glBindTexture(GL_TEXTURE_2D, depthFbo.GetBuffer("depth"));
  288. glUniform1i(depthRenderProgram.GetLocation("U_MainTexture"), 0);
  289. fsq.DrawToRightBottom(depthRenderProgram.GetLocation("pos"), depthRenderProgram.GetLocation("texcoord"));
  290. glUseProgram(originalProgram.mProgram);
  291. glActiveTexture(GL_TEXTURE0);
  292. glBindTexture(GL_TEXTURE_2D, projectiveTextureFbo2.GetBuffer("color"));
  293. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  294. fsq.DrawToLeftBottom(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  295. glFlush();
  296. SwapBuffers(dc);
  297. }
  298. return 0;
  299. }