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.

186 lines
5.9 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 "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. int windowWidth = 800;
  51. int windowHeight = 600;
  52. RECT rect;
  53. rect.left = 0;
  54. rect.right = windowWidth;
  55. rect.bottom = windowHeight;
  56. rect.top = 0;
  57. AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
  58. //��������һ��Ҫ�͸ղ�ע�ᴰ�ڵı���һ��
  59. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
  60. //������Ⱦ����
  61. HDC dc = GetDC(hwnd);//��ȡ�豸������
  62. PIXELFORMATDESCRIPTOR pfd;
  63. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  64. pfd.nVersion = 1;
  65. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  66. pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
  67. pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
  68. pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
  69. pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
  70. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; //��ʾ������������������
  71. //�������ظ�ʽ
  72. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  73. SetPixelFormat(dc, pixelFormat, &pfd);
  74. //����OpenGL��Ⱦ����
  75. HGLRC rc = wglCreateContext(dc);
  76. wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
  77. /*glew��ʼ��*/
  78. glewInit();
  79. /*����program*/
  80. GLuint program = CreateGPUProgram("res/shader/UI FullScreemQuad.vs", "res/shader/UI.fs");
  81. GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation;
  82. posLocation = glGetAttribLocation(program, "pos");
  83. texcoordLocation = glGetAttribLocation(program, "texcoord");
  84. normalLocation = glGetAttribLocation(program, "normal");
  85. MLocation = glGetUniformLocation(program, "M");
  86. VLocation = glGetUniformLocation(program, "V");
  87. PLocation = glGetUniformLocation(program, "P");
  88. NMLocation = glGetUniformLocation(program, "NM");
  89. textureLocation = glGetUniformLocation(program, "U_MainTexture");
  90. /*load model*/
  91. unsigned int *indexes = nullptr;
  92. int vertexCount = 0, indexCount = 0;
  93. VertexData* vertexes = LoadObjModel("res/model/Quad.obj", &indexes, vertexCount, indexCount);
  94. if (vertexes == nullptr) {
  95. printf("load obj model fail\n");
  96. }
  97. /*����vbo*/
  98. GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes);
  99. /*����IBO*/
  100. GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes);
  101. /*��������*/
  102. GLuint mainTexture = CreateTextureFromFile("res/image/stone.jpg");
  103. GLuint secondTexture = CreateTextureFromDds("res/image/150001.dds");
  104. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  105. glEnable(GL_DEPTH_TEST);
  106. glViewport(0,0, windowWidth, windowHeight);
  107. //����һ����λ������һ��ͶӰ���󣬺����������ݵ�shader
  108. float identify[] = {
  109. 1,0,0,0,
  110. 0,1,0,0,
  111. 0,0,1,0,
  112. 0,0,0,1
  113. };
  114. glm::mat4 model = glm::translate(0.0f, 0.0f, -4.0f);
  115. glm::mat4 projection = glm::perspective(45.0f, (float)windowWidth / (float)windowHeight, 0.1f, 1000.0f);
  116. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  117. /*��ʾ����*/
  118. ShowWindow(hwnd, SW_SHOW);
  119. UpdateWindow(hwnd);
  120. /*�����������û�����*/
  121. MSG msg;
  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. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  131. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  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_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
  149. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  150. glUseProgram(0);
  151. SwapBuffers(dc);
  152. }
  153. return 0;
  154. }