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.

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