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.

262 lines
8.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
5 years ago
  1. #include <windows.h>
  2. #include "glew.h"
  3. #include "Glm/glm.hpp"
  4. #include "Glm/ext.hpp"
  5. #include <stdio.h>
  6. #include "misc.h"
  7. #include "model.h"
  8. #include "timer.h"
  9. #include "frustum.h"
  10. #pragma comment(lib,"opengl32.lib")
  11. #pragma comment(lib, "glew32.lib")
  12. /**
  13. * @hwnd ϢĴ
  14. * @msg Ϣ
  15. */
  16. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  17. switch (msg) {
  18. case WM_CLOSE:
  19. PostQuitMessage(0);
  20. return 0;
  21. }
  22. return DefWindowProc(hwnd, msg, wParam, lParam);//������Ϣ����windowĬ�ϴ�������
  23. }
  24. /**
  25. * @hinstance Ӧóʵ
  26. * @hPrevInstance һӦóʽʵ
  27. * @IpCmdLine еIJ
  28. * @ShowCmd ôʾ
  29. */
  30. INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  31. {
  32. /*ע�ᴰ��*/
  33. WNDCLASSEX wndclass;
  34. wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
  35. wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
  36. wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
  37. wndclass.hbrBackground = NULL; //���ڱ���
  38. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
  39. wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
  40. wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
  41. wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
  42. wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
  43. wndclass.lpszClassName = L"GLWindow";//��������
  44. wndclass.lpszMenuName = NULL;//�˵�����
  45. wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
  46. ATOM atom = RegisterClassEx(&wndclass);
  47. if (!atom) {
  48. MessageBox(NULL, L"Register failed", L"Error", MB_OK);
  49. return 0;
  50. }
  51. /*��������*/
  52. int windowWidth = 800;
  53. int windowHeight = 600;
  54. RECT rect;
  55. rect.left = 0;
  56. rect.right = windowWidth;
  57. rect.bottom = windowHeight;
  58. rect.top = 0;
  59. AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
  60. //��������һ��Ҫ�͸ղ�ע�ᴰ�ڵı���һ��
  61. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
  62. //������Ⱦ����
  63. HDC dc = GetDC(hwnd);//��ȡ�豸������
  64. PIXELFORMATDESCRIPTOR pfd;
  65. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  66. pfd.nVersion = 1;
  67. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  68. pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
  69. pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
  70. pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
  71. pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
  72. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; //��ʾ������������������
  73. //�������ظ�ʽ
  74. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  75. SetPixelFormat(dc, pixelFormat, &pfd);
  76. //����OpenGL��Ⱦ����
  77. HGLRC rc = wglCreateContext(dc);
  78. wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
  79. /*glew��ʼ��*/
  80. glewInit();
  81. /*����program*/
  82. GLuint program = CreateGPUProgram("res/shader/SimpleTexture.vs", "res/shader/SimpleTexture.fs");
  83. GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, textureLocation;
  84. posLocation = glGetAttribLocation(program, "pos");
  85. texcoordLocation = glGetAttribLocation(program, "texcoord");
  86. normalLocation = glGetAttribLocation(program, "normal");
  87. MLocation = glGetUniformLocation(program, "M");
  88. VLocation = glGetUniformLocation(program, "V");
  89. PLocation = glGetUniformLocation(program, "P");
  90. textureLocation = glGetUniformLocation(program, "U_MainTexture");
  91. /*load model*/
  92. unsigned int *indexes = nullptr;
  93. int vertexCount = 0, indexCount = 0;
  94. Timer t;
  95. t.Start();
  96. VertexData* vertexes = LoadObjModel("res/model/niutou.obj", &indexes, vertexCount, indexCount);
  97. printf("load model cost %fs %d\n", t.GetPassedTime(), t.GetPassedTickers());
  98. if (vertexes == nullptr) {
  99. printf("load obj model fail\n");
  100. }
  101. /*����vbo*/
  102. GLuint vao = CreateVAOWithVBOSettings([&]()->void
  103. {
  104. GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes);
  105. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  106. glEnableVertexAttribArray(posLocation);
  107. glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0);
  108. glEnableVertexAttribArray(texcoordLocation);
  109. glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3));
  110. });
  111. /*����IBO*/
  112. GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes);
  113. glBindBuffer(GL_ARRAY_BUFFER, 0);
  114. /*����fsqprogram*/
  115. GLuint fsqprogram = CreateGPUProgram("res/shader/UI FullScreemQuad.vs", "res/shader/RenderDepthBuffer.fs");
  116. GLuint fsqposLocation, fsqtexcoordLocation, fsqnormalLocation, fsqMLocation, fsqVLocation, fsqPLocation, fsqtextureLocation;
  117. fsqposLocation = glGetAttribLocation(fsqprogram, "pos");
  118. fsqtexcoordLocation = glGetAttribLocation(fsqprogram, "texcoord");
  119. fsqnormalLocation = glGetAttribLocation(fsqprogram, "normal");
  120. fsqMLocation = glGetUniformLocation(fsqprogram, "M");
  121. fsqVLocation = glGetUniformLocation(fsqprogram, "V");
  122. fsqPLocation = glGetUniformLocation(fsqprogram, "P");
  123. textureLocation = glGetUniformLocation(program, "U_MainTexture");
  124. /*load model*/
  125. unsigned int *fsqindexes = nullptr;
  126. int fsqvertexCount = 0, fsqindexCount = 0;
  127. Timer t2;
  128. t2.Start();
  129. VertexData* fsqvertexes = LoadObjModel("res/model/Quad.obj", &fsqindexes, fsqvertexCount, fsqindexCount);
  130. printf("load model cost %fs %d\n", t2.GetPassedTime(), t2.GetPassedTickers());
  131. if (fsqvertexes == nullptr) {
  132. printf("load obj model fail\n");
  133. }
  134. /*����fsqvbo*/
  135. GLuint fsqvao = CreateVAOWithVBOSettings([&]()->void
  136. {
  137. GLuint fsqvbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * fsqvertexCount, GL_STATIC_DRAW, fsqvertexes);
  138. glBindBuffer(GL_ARRAY_BUFFER, fsqvbo);
  139. glEnableVertexAttribArray(fsqposLocation);
  140. glVertexAttribPointer(fsqposLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0);
  141. glEnableVertexAttribArray(fsqtexcoordLocation);
  142. glVertexAttribPointer(fsqtexcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3));
  143. });
  144. /*����fsqIBO*/
  145. GLuint fsqibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * fsqindexCount, GL_STATIC_DRAW, fsqindexes);
  146. glBindBuffer(GL_ARRAY_BUFFER, 0);
  147. /*��������*/
  148. GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp");
  149. GLuint colorBuffer, depthBuffer;
  150. GLuint fbo = CreateFramebufferObject(colorBuffer, depthBuffer, windowWidth, windowHeight);
  151. glViewport(0, 0, windowWidth, windowHeight);
  152. GL_CALL(glClearColor(0.1f, 0.4f, 0.7f, 1.0f));
  153. glEnable(GL_DEPTH_TEST);
  154. //����һ����λ������һ��ͶӰ���󣬺����������ݵ�shader
  155. float identify[] = {
  156. 1,0,0,0,
  157. 0,1,0,0,
  158. 0,0,1,0,
  159. 0,0,0,1
  160. };
  161. glm::mat4 model = glm::translate(0.0f, -0.5f, -2.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f,0.01f,0.01f);
  162. glm::mat4 projection = glm::perspective(45.0f, (float)windowWidth / (float)windowHeight, 0.1f, 1000.0f);
  163. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  164. /*��ʾ����*/
  165. ShowWindow(hwnd, SW_SHOW);
  166. UpdateWindow(hwnd);
  167. /*�����������û�����*/
  168. MSG msg;
  169. auto what = [&]()->void {
  170. glUseProgram(program);
  171. glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
  172. glUniformMatrix4fv(VLocation, 1, GL_FALSE, identify);
  173. glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection));
  174. glBindTexture(GL_TEXTURE_2D, mainTexture);
  175. glUniform1i(textureLocation, 0);
  176. glBindVertexArray(vao);
  177. glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
  178. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  179. glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
  180. glBindVertexArray(0);
  181. glUseProgram(0);
  182. };
  183. auto RenderFullScreenQuad = [&]()->void {
  184. glUseProgram(fsqprogram);
  185. glBindVertexArray(fsqvao);
  186. glBindTexture(GL_TEXTURE_2D, depthBuffer);
  187. glUniform1i(fsqtextureLocation, 0);
  188. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fsqibo);
  189. glDrawElements(GL_TRIANGLES, fsqindexCount, GL_UNSIGNED_INT, 0);
  190. glBindVertexArray(0);
  191. glUseProgram(0);
  192. };
  193. /*ʹ�ù̶����߻���һ���ı��Σ�����fbo��������*/
  194. glMatrixMode(GL_PROJECTION);
  195. glLoadMatrixf(glm::value_ptr(projection));
  196. glMatrixMode(GL_MODELVIEW);
  197. glLoadIdentity();
  198. while (true) {
  199. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  200. if (msg.message == WM_QUIT) {
  201. break;
  202. }
  203. TranslateMessage(&msg);
  204. DispatchMessage(&msg);
  205. }
  206. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  207. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  208. what();
  209. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  210. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  211. RenderFullScreenQuad();
  212. glFlush();
  213. SwapBuffers(dc);
  214. }
  215. return 0;
  216. }