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.

192 lines
6.0 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
  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. auto what = [&]()->void {
  123. glm::mat4 normalMatrix = glm::inverseTranspose(model);
  124. glUseProgram(program);
  125. glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
  126. glUniformMatrix4fv(VLocation, 1, GL_FALSE, identify);
  127. glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection));
  128. glUniformMatrix4fv(NMLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix));
  129. glBindTexture(GL_TEXTURE_2D, mainTexture);
  130. glUniform1i(textureLocation, 0);
  131. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  132. glEnableVertexAttribArray(posLocation);
  133. glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0);
  134. glEnableVertexAttribArray(texcoordLocation);
  135. glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3));
  136. glEnableVertexAttribArray(normalLocation);
  137. glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5));
  138. glBindBuffer(GL_ARRAY_BUFFER, 0);
  139. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  140. glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
  141. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  142. glUseProgram(0);
  143. };
  144. while (true) {
  145. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  146. if (msg.message == WM_QUIT) {
  147. break;
  148. }
  149. TranslateMessage(&msg);
  150. DispatchMessage(&msg);
  151. }
  152. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  153. glEnable(GL_SCISSOR_TEST);
  154. glScissor(400,300, 100, 100);
  155. what();
  156. SwapBuffers(dc);
  157. }
  158. return 0;
  159. }