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.

240 lines
8.4 KiB

5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 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
5 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
5 years ago
4 years ago
5 years ago
4 years ago
5 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
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 = 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 projector program
  73. GPUProgram sampleProgram;
  74. sampleProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/sample.vs");
  75. sampleProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/sample.fs");
  76. sampleProgram.Link();
  77. sampleProgram.DetectAttribute("pos");
  78. sampleProgram.DetectAttribute("texcoord");
  79. sampleProgram.DetectUniform("M");
  80. sampleProgram.DetectUniform("V");
  81. sampleProgram.DetectUniform("P");
  82. sampleProgram.DetectUniform("U_MainTexture");
  83. //init projective texture program
  84. GPUProgram projectedTextureProgram;
  85. projectedTextureProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/projector.vs");
  86. projectedTextureProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/projector.fs");
  87. projectedTextureProgram.Link();
  88. projectedTextureProgram.DetectAttribute("pos");
  89. projectedTextureProgram.DetectAttribute("texcoord");
  90. projectedTextureProgram.DetectUniform("M");
  91. projectedTextureProgram.DetectUniform("V");
  92. projectedTextureProgram.DetectUniform("P");
  93. projectedTextureProgram.DetectUniform("U_ShadowMap");
  94. projectedTextureProgram.DetectUniform("U_MainTexture");
  95. projectedTextureProgram.DetectUniform("U_ProjectiveTexture");
  96. projectedTextureProgram.DetectUniform("U_ProjectorMatrix");
  97. //init 3d model
  98. ObjModel obj, quad, sphere;
  99. obj.Init("res/model/Cube.obj");
  100. quad.Init("res/model/Quad.obj");
  101. sphere.Init("res/model/Sphere.obj");
  102. float identity[] = {
  103. 1.0f,0,0,0,
  104. 0,1.0f,0,0,
  105. 0,0,1.0f,0,
  106. 0,0,0,1.0f
  107. };
  108. glm::mat4 model = glm::translate<float>(6.0f, 0.0f, -6.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
  109. glm::mat4 quadModel = glm::translate<float>(6.0f, -1.5f, -6.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(10.0f, 10.0f, 10.0f);
  110. glm::mat4 sphereModel = glm::translate<float>(0.0f, 0.0f, 0.0f) * glm::scale(0.3f, 0.3f, 0.3f);
  111. 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));
  112. glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
  113. glm::mat4 projectorMatrix = glm::translate<float>(0.5f, 0.5f, 0.5f)*glm::scale(0.5f, 0.5f, 0.5f) * projectionMatrix * viewMatrix;
  114. //��ʼ��fsq
  115. FullScreenQuad fsq;
  116. fsq.Init();
  117. //��ʼ��FBO
  118. FBO originalFbo;
  119. originalFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  120. originalFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  121. originalFbo.Finish();
  122. FBO projectiveTextureFbo;
  123. projectiveTextureFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
  124. projectiveTextureFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
  125. projectiveTextureFbo.Finish();
  126. GLuint mainTexture = SOIL_load_OGL_texture("res/image/stone.bmp", 0, 0, SOIL_FLAG_POWER_OF_TWO);
  127. GLuint projectiveTexture = SOIL_load_OGL_texture("res/image/grass.png", 0, 0, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_INVERT_Y);
  128. ShowWindow(hwnd, SW_SHOW);
  129. UpdateWindow(hwnd);
  130. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  131. glEnable(GL_DEPTH_TEST);
  132. originalFbo.Bind();
  133. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  134. glUseProgram(sampleProgram.mProgram);
  135. glUniformMatrix4fv(sampleProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  136. glUniformMatrix4fv(sampleProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  137. glUniformMatrix4fv(sampleProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
  138. glBindTexture(GL_TEXTURE_2D, mainTexture);
  139. glUniform1i(sampleProgram.GetLocation("U_MainTexture"), 0);
  140. obj.Bind(sampleProgram.GetLocation("pos"), sampleProgram.GetLocation("texcoord"), sampleProgram.GetLocation("normal"));
  141. obj.Draw();
  142. glUniformMatrix4fv(sampleProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
  143. quad.Bind(sampleProgram.GetLocation("pos"), sampleProgram.GetLocation("texcoord"), sampleProgram.GetLocation("normal"));
  144. quad.Draw();
  145. originalFbo.Unbind();
  146. projectiveTextureFbo.Bind();
  147. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  148. glUseProgram(projectedTextureProgram.mProgram);
  149. glUniformMatrix4fv(projectedTextureProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  150. glUniformMatrix4fv(projectedTextureProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
  151. glUniformMatrix4fv(projectedTextureProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
  152. glUniformMatrix4fv(projectedTextureProgram.GetLocation("U_ProjectorMatrix"), 1, GL_FALSE, glm::value_ptr(projectorMatrix));
  153. glActiveTexture(GL_TEXTURE0);
  154. glBindTexture(GL_TEXTURE_2D, mainTexture);
  155. glUniform1i(projectedTextureProgram.GetLocation("U_MainTexture"), 0);
  156. glActiveTexture(GL_TEXTURE1);
  157. glBindTexture(GL_TEXTURE_2D, projectiveTexture);
  158. glUniform1i(projectedTextureProgram.GetLocation("U_ProjectiveTexture"), 1);
  159. obj.Bind(projectedTextureProgram.GetLocation("pos"), projectedTextureProgram.GetLocation("texcoord"), projectedTextureProgram.GetLocation("normal"));
  160. obj.Draw();
  161. glUniformMatrix4fv(projectedTextureProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
  162. quad.Bind(projectedTextureProgram.GetLocation("pos"), projectedTextureProgram.GetLocation("texcoord"), projectedTextureProgram.GetLocation("normal"));
  163. quad.Draw();
  164. projectiveTextureFbo.Unbind();
  165. MSG msg;
  166. while (true)
  167. {
  168. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  169. {
  170. if (msg.message == WM_QUIT)
  171. {
  172. break;
  173. }
  174. TranslateMessage(&msg);
  175. DispatchMessage(&msg);
  176. }
  177. glClearColor(0.1f, 0.4f, 0.7f, 1.0f);
  178. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  179. glUseProgram(originalProgram.mProgram);
  180. glActiveTexture(GL_TEXTURE0);
  181. glBindTexture(GL_TEXTURE_2D, originalFbo.GetBuffer("color"));
  182. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  183. fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  184. glUseProgram(originalProgram.mProgram);
  185. glActiveTexture(GL_TEXTURE0);
  186. glBindTexture(GL_TEXTURE_2D, projectiveTextureFbo.GetBuffer("color"));
  187. glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
  188. fsq.DrawToRightTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
  189. glFlush();
  190. SwapBuffers(dc);
  191. }
  192. return 0;
  193. }