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.

190 lines
6.0 KiB

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 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
5 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
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. #pragma comment(lib,"opengl32.lib")
  9. #pragma comment(lib, "glew32.lib")
  10. /**
  11. * @hwnd ϢĴ
  12. * @msg Ϣ
  13. */
  14. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  15. switch (msg) {
  16. case WM_CLOSE:
  17. PostQuitMessage(0);
  18. return 0;
  19. }
  20. return DefWindowProc(hwnd, msg, wParam, lParam);//������Ϣ����windowĬ�ϴ�������
  21. }
  22. /**
  23. * @hinstance Ӧóʵ
  24. * @hPrevInstance һӦóʽʵ
  25. * @IpCmdLine еIJ
  26. * @ShowCmd ôʾ
  27. */
  28. INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  29. {
  30. /*ע�ᴰ��*/
  31. WNDCLASSEX wndclass;
  32. wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
  33. wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
  34. wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
  35. wndclass.hbrBackground = NULL; //���ڱ���
  36. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
  37. wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
  38. wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
  39. wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
  40. wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
  41. wndclass.lpszClassName = L"GLWindow";//��������
  42. wndclass.lpszMenuName = NULL;//�˵�����
  43. wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
  44. ATOM atom = RegisterClassEx(&wndclass);
  45. if (!atom) {
  46. MessageBox(NULL, L"Register failed", L"Error", MB_OK);
  47. return 0;
  48. }
  49. /*��������*/
  50. //�������ڴ�С
  51. RECT rect;
  52. rect.left = 0;
  53. rect.right = 800;
  54. rect.top = 0;
  55. rect.bottom = 600;
  56. AdjustWindowRect(&rect, WS_EX_OVERLAPPEDWINDOW, NULL);
  57. int windowWidth = rect.right - rect.left;
  58. int windowHeight = rect.bottom - rect.top;
  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/test.vs", "res/shader/test.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. VertexData* vertexes = LoadObjModel("res/model/Sphere.obj", &indexes, vertexCount, indexCount);
  95. if (vertexes == nullptr) {
  96. printf("load obj model fail\n");
  97. }
  98. /*����vbo*/
  99. GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes);
  100. /*����IBO*/
  101. GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes);
  102. /*��������*/
  103. GLuint mainTexture = CreateTextureFromFile("res/image/stone.jpg");
  104. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  105. glEnable(GL_DEPTH_TEST);
  106. //����һ����λ������һ��ͶӰ���󣬺����������ݵ�shader
  107. float identify[] = {
  108. 1,0,0,0,
  109. 0,1,0,0,
  110. 0,0,1,0,
  111. 0,0,0,1
  112. };
  113. glm::mat4 model = glm::translate(0.0f, 0.0f, -4.0f);
  114. glm::mat4 projection = glm::perspective(45.0f, 800.0f/600.0f, 0.1f, 1000.0f);
  115. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  116. /*��ʾ����*/
  117. ShowWindow(hwnd, SW_SHOW);
  118. UpdateWindow(hwnd);
  119. /*�����������û�����*/
  120. MSG msg;
  121. float angle = 0.0;
  122. while (true) {
  123. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  124. if (msg.message == WM_QUIT) {
  125. break;
  126. }
  127. TranslateMessage(&msg);
  128. DispatchMessage(&msg);
  129. }
  130. if (angle > 360) {
  131. angle = 0;
  132. }
  133. else {
  134. angle += 0.01f;
  135. }
  136. model = glm::translate(0.0f, 0.0f, -4.0f) * glm::rotate(angle, 0.0f, 1.0f, 0.0f);
  137. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  138. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  139. glUseProgram(program);
  140. glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
  141. glUniformMatrix4fv(VLocation, 1, GL_FALSE, identify);
  142. glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection));
  143. glUniformMatrix4fv(NMLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix));
  144. glBindTexture(GL_TEXTURE_2D, mainTexture);
  145. glUniform1i(textureLocation, 0);
  146. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  147. glEnableVertexAttribArray(posLocation);
  148. glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0);
  149. glEnableVertexAttribArray(texcoordLocation);
  150. glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3));
  151. glEnableVertexAttribArray(normalLocation);
  152. glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5));
  153. glBindBuffer(GL_ARRAY_BUFFER, 0);
  154. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  155. glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
  156. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  157. glUseProgram(0);
  158. SwapBuffers(dc);
  159. }
  160. return 0;
  161. }