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.

209 lines
6.5 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
  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 = 512;
  44. rect.bottom = 512;
  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. //combine program
  65. GPUProgram combineProgram1;
  66. combineProgram1.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  67. combineProgram1.AttachShader(GL_FRAGMENT_SHADER, "res/shader/zpdd.fs");
  68. combineProgram1.Link();
  69. combineProgram1.DetectAttribute("pos");
  70. combineProgram1.DetectAttribute("texcoord");
  71. combineProgram1.DetectUniform("U_BaseTexture");
  72. combineProgram1.DetectUniform("U_BlendTexture");
  73. //combine program
  74. GPUProgram combineProgram2;
  75. combineProgram2.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  76. combineProgram2.AttachShader(GL_FRAGMENT_SHADER, "res/shader/zpdd_inverse.fs");
  77. combineProgram2.Link();
  78. combineProgram2.DetectAttribute("pos");
  79. combineProgram2.DetectAttribute("texcoord");
  80. combineProgram2.DetectUniform("U_BaseTexture");
  81. combineProgram2.DetectUniform("U_BlendTexture");
  82. //combine program
  83. GPUProgram combineProgram3;
  84. combineProgram3.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  85. combineProgram3.AttachShader(GL_FRAGMENT_SHADER, "res/shader/zpdd_dark.fs");
  86. combineProgram3.Link();
  87. combineProgram3.DetectAttribute("pos");
  88. combineProgram3.DetectAttribute("texcoord");
  89. combineProgram3.DetectUniform("U_BaseTexture");
  90. combineProgram3.DetectUniform("U_BlendTexture");
  91. //combine program
  92. GPUProgram combineProgram4;
  93. combineProgram4.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  94. combineProgram4.AttachShader(GL_FRAGMENT_SHADER, "res/shader/zpdd_light.fs");
  95. combineProgram4.Link();
  96. combineProgram4.DetectAttribute("pos");
  97. combineProgram4.DetectAttribute("texcoord");
  98. combineProgram4.DetectUniform("U_BaseTexture");
  99. combineProgram4.DetectUniform("U_BlendTexture");
  100. //init 3d model
  101. ObjModel quad;
  102. quad.Init("res/model/Quad.obj");
  103. float identity[] = {
  104. 1.0f,0,0,0,
  105. 0,1.0f,0,0,
  106. 0,0,1.0f,0,
  107. 0,0,0,1.0f
  108. };
  109. 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);
  110. glm::mat4 quadNormalMatrix = glm::inverseTranspose(quadModel);
  111. 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));
  112. glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
  113. //��ʼ��fsq
  114. FullScreenQuad fsq;
  115. fsq.Init();
  116. GLuint head = CreateTextureFromFile("res/image/wood.bmp");
  117. GLuint grass = CreateTextureFromFile("res/image/earth.bmp");
  118. ShowWindow(hwnd, SW_SHOW);
  119. UpdateWindow(hwnd);
  120. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  121. MSG msg;
  122. while (true)
  123. {
  124. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  125. {
  126. if (msg.message==WM_QUIT)
  127. {
  128. break;
  129. }
  130. TranslateMessage(&msg);
  131. DispatchMessage(&msg);
  132. }
  133. glEnable(GL_DEPTH_TEST);
  134. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  135. glEnable(GL_BLEND);
  136. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  137. glActiveTexture(GL_TEXTURE0);
  138. glUseProgram(combineProgram1.mProgram);
  139. glActiveTexture(GL_TEXTURE0);
  140. glBindTexture(GL_TEXTURE_2D, head);
  141. glUniform1i(combineProgram1.GetLocation("U_BaseTexture"), 0);
  142. glActiveTexture(GL_TEXTURE1);
  143. glBindTexture(GL_TEXTURE_2D, grass);
  144. glUniform1i(combineProgram1.GetLocation("U_BlendTexture"), 1);
  145. fsq.DrawToLeftTop(combineProgram1.GetLocation("pos"), combineProgram1.GetLocation("texcoord"));
  146. glUseProgram(combineProgram2.mProgram);
  147. glActiveTexture(GL_TEXTURE0);
  148. glBindTexture(GL_TEXTURE_2D, head);
  149. glUniform1i(combineProgram2.GetLocation("U_BaseTexture"), 0);
  150. glActiveTexture(GL_TEXTURE1);
  151. glBindTexture(GL_TEXTURE_2D, grass);
  152. glUniform1i(combineProgram2.GetLocation("U_BlendTexture"), 1);
  153. fsq.DrawToRightTop(combineProgram2.GetLocation("pos"), combineProgram2.GetLocation("texcoord"));
  154. glUseProgram(combineProgram3.mProgram);
  155. glActiveTexture(GL_TEXTURE0);
  156. glBindTexture(GL_TEXTURE_2D, head);
  157. glUniform1i(combineProgram3.GetLocation("U_BaseTexture"), 0);
  158. glActiveTexture(GL_TEXTURE1);
  159. glBindTexture(GL_TEXTURE_2D, grass);
  160. glUniform1i(combineProgram3.GetLocation("U_BlendTexture"), 1);
  161. fsq.DrawToLeftBottom(combineProgram3.GetLocation("pos"), combineProgram3.GetLocation("texcoord"));
  162. glUseProgram(combineProgram4.mProgram);
  163. glActiveTexture(GL_TEXTURE0);
  164. glBindTexture(GL_TEXTURE_2D, head);
  165. glUniform1i(combineProgram4.GetLocation("U_BaseTexture"), 0);
  166. glActiveTexture(GL_TEXTURE1);
  167. glBindTexture(GL_TEXTURE_2D, grass);
  168. glUniform1i(combineProgram4.GetLocation("U_BlendTexture"), 1);
  169. fsq.DrawToRightBottom(combineProgram4.GetLocation("pos"), combineProgram4.GetLocation("texcoord"));
  170. glDisable(GL_BLEND);
  171. glFlush();
  172. SwapBuffers(dc);
  173. }
  174. return 0;
  175. }