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.

198 lines
6.2 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
  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. #pragma comment(lib,"opengl32.lib")
  10. #pragma comment(lib, "glew32.lib")
  11. /**
  12. * @hwnd ϢĴ
  13. * @msg Ϣ
  14. */
  15. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  16. switch (msg) {
  17. case WM_CLOSE:
  18. PostQuitMessage(0);
  19. return 0;
  20. }
  21. return DefWindowProc(hwnd, msg, wParam, lParam);//������Ϣ����windowĬ�ϴ�������
  22. }
  23. /**
  24. * @hinstance Ӧóʵ
  25. * @hPrevInstance һӦóʽʵ
  26. * @IpCmdLine еIJ
  27. * @ShowCmd ôʾ
  28. */
  29. INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  30. {
  31. /*ע�ᴰ��*/
  32. WNDCLASSEX wndclass;
  33. wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
  34. wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
  35. wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
  36. wndclass.hbrBackground = NULL; //���ڱ���
  37. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
  38. wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
  39. wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
  40. wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
  41. wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
  42. wndclass.lpszClassName = L"GLWindow";//��������
  43. wndclass.lpszMenuName = NULL;//�˵�����
  44. wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
  45. ATOM atom = RegisterClassEx(&wndclass);
  46. if (!atom) {
  47. MessageBox(NULL, L"Register failed", L"Error", MB_OK);
  48. return 0;
  49. }
  50. /*��������*/
  51. int windowWidth = 800;
  52. int windowHeight = 600;
  53. RECT rect;
  54. rect.left = 0;
  55. rect.right = windowWidth;
  56. rect.bottom = windowHeight;
  57. rect.top = 0;
  58. AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
  59. //��������һ��Ҫ�͸ղ�ע�ᴰ�ڵı���һ��
  60. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
  61. //������Ⱦ����
  62. HDC dc = GetDC(hwnd);//��ȡ�豸������
  63. PIXELFORMATDESCRIPTOR pfd;
  64. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  65. pfd.nVersion = 1;
  66. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  67. pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
  68. pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
  69. pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
  70. pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
  71. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; //��ʾ������������������
  72. //�������ظ�ʽ
  73. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  74. SetPixelFormat(dc, pixelFormat, &pfd);
  75. //����OpenGL��Ⱦ����
  76. HGLRC rc = wglCreateContext(dc);
  77. wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
  78. /*glew��ʼ��*/
  79. glewInit();
  80. /*����program*/
  81. GLuint program = CreateGPUProgram("res/shader/PointSprite.vs", "res/shader/PointSprite.fs");
  82. GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation;
  83. posLocation = glGetAttribLocation(program, "pos");
  84. texcoordLocation = glGetAttribLocation(program, "texcoord");
  85. normalLocation = glGetAttribLocation(program, "normal");
  86. MLocation = glGetUniformLocation(program, "M");
  87. VLocation = glGetUniformLocation(program, "V");
  88. PLocation = glGetUniformLocation(program, "P");
  89. NMLocation = glGetUniformLocation(program, "NM");
  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/Quad.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. vertexes[0].position[0] = 0.0f;
  102. vertexes[0].position[1] = 0.0f;
  103. /*����vbo*/
  104. GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes);
  105. /*����IBO*/
  106. GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes);
  107. /*��������*/
  108. GLuint mainTexture = CreateTextureFromFile("res/image/stone.jpg");
  109. GLuint secondTexture = CreateTextureFromDds("res/image/150001.dds");
  110. glClearColor(0.1f, 0.4f, 0.7f, 1.0f);
  111. glEnable(GL_DEPTH_TEST);
  112. glEnable(GL_POINT_SPRITE);
  113. glEnable(GL_PROGRAM_POINT_SIZE);
  114. glViewport(0,0, windowWidth, windowHeight);
  115. //����һ����λ������һ��ͶӰ���󣬺����������ݵ�shader
  116. float identify[] = {
  117. 1,0,0,0,
  118. 0,1,0,0,
  119. 0,0,1,0,
  120. 0,0,0,1
  121. };
  122. glm::mat4 model = glm::translate(0.0f, 0.0f, -4.0f);
  123. glm::mat4 projection = glm::perspective(45.0f, (float)windowWidth / (float)windowHeight, 0.1f, 1000.0f);
  124. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  125. /*��ʾ����*/
  126. ShowWindow(hwnd, SW_SHOW);
  127. UpdateWindow(hwnd);
  128. /*�����������û�����*/
  129. MSG msg;
  130. auto what = [&]()->void {
  131. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  132. glUseProgram(program);
  133. glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
  134. glUniformMatrix4fv(VLocation, 1, GL_FALSE, identify);
  135. glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection));
  136. glUniformMatrix4fv(NMLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix));
  137. glBindTexture(GL_TEXTURE_2D, mainTexture);
  138. glUniform1i(textureLocation, 0);
  139. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  140. glEnableVertexAttribArray(posLocation);
  141. glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0);
  142. glEnableVertexAttribArray(texcoordLocation);
  143. glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3));
  144. glEnableVertexAttribArray(normalLocation);
  145. glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5));
  146. glBindBuffer(GL_ARRAY_BUFFER, 0);
  147. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  148. glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, 0);
  149. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  150. glUseProgram(0);
  151. };
  152. while (true) {
  153. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  154. if (msg.message == WM_QUIT) {
  155. break;
  156. }
  157. TranslateMessage(&msg);
  158. DispatchMessage(&msg);
  159. }
  160. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  161. what();
  162. SwapBuffers(dc);
  163. }
  164. return 0;
  165. }