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.

167 lines
5.3 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. //�������ڴ�С
  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;
  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. /*load model*/
  90. unsigned int *indexes = nullptr;
  91. int vertexCount = 0, indexCount = 0;
  92. VertexData* vertexes = LoadObjModel("res/model/Sphere.obj", &indexes, vertexCount, indexCount);
  93. if (vertexes == nullptr) {
  94. printf("load obj model fail\n");
  95. }
  96. /*����vbo*/
  97. GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes);
  98. /*����IBO*/
  99. GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes);
  100. glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  101. //����һ����λ������һ��ͶӰ���󣬺����������ݵ�shader
  102. float identify[] = {
  103. 1,0,0,0,
  104. 0,1,0,0,
  105. 0,0,1,0,
  106. 0,0,0,1
  107. };
  108. glm::mat4 model = glm::translate(0.0f, 0.0f, -5.0f);
  109. glm::mat4 projection = glm::perspective(45.0f, 800.0f/600.0f, 0.1f, 1000.0f);
  110. /*��ʾ����*/
  111. ShowWindow(hwnd, SW_SHOW);
  112. UpdateWindow(hwnd);
  113. /*�����������û�����*/
  114. MSG msg;
  115. while (true) {
  116. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  117. if (msg.message == WM_QUIT) {
  118. break;
  119. }
  120. TranslateMessage(&msg);
  121. DispatchMessage(&msg);
  122. }
  123. glClear(GL_COLOR_BUFFER_BIT);
  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. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  129. glEnableVertexAttribArray(posLocation);
  130. glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0);
  131. glEnableVertexAttribArray(texcoordLocation);
  132. glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3));
  133. glEnableVertexAttribArray(normalLocation);
  134. glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5));
  135. glBindBuffer(GL_ARRAY_BUFFER, 0);
  136. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  137. glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
  138. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  139. glUseProgram(0);
  140. SwapBuffers(dc);
  141. }
  142. return 0;
  143. }