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.

278 lines
7.8 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
  1. #include <windows.h>
  2. #include "glew.h"
  3. #include "Glm/glm.hpp"
  4. #include "Glm/ext.hpp"
  5. #include <stdio.h>
  6. #pragma comment(lib,"opengl32.lib")
  7. #pragma comment(lib, "glew32.lib")
  8. struct Vertex {
  9. float pos[3];
  10. float color[4];
  11. };
  12. /**
  13. * @hwnd ϢĴ
  14. * @msg Ϣ
  15. */
  16. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  17. switch (msg) {
  18. case WM_CLOSE:
  19. PostQuitMessage(0);
  20. return 0;
  21. }
  22. return DefWindowProc(hwnd, msg, wParam, lParam);//������Ϣ����windowĬ�ϴ�������
  23. }
  24. char* LoadFileContent(const char *path) {
  25. FILE* pFile = fopen(path, "rb");
  26. if (pFile) {
  27. fseek(pFile, 0, SEEK_END);
  28. int nLen = ftell(pFile);
  29. char* buffer = new char[nLen+1];
  30. rewind(pFile);
  31. fread(buffer, nLen, 1, pFile);
  32. buffer[nLen] = '\0';
  33. fclose(pFile);
  34. return buffer;
  35. }
  36. fclose(pFile);
  37. return nullptr;
  38. }
  39. /**
  40. * һGPU
  41. */
  42. GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath) {
  43. //����shader
  44. GLuint vsShader = glCreateShader(GL_VERTEX_SHADER);
  45. GLuint fsShader = glCreateShader(GL_FRAGMENT_SHADER);
  46. //��ȡshader����
  47. const char* vsCode = LoadFileContent(vsShaderPath);
  48. const char* fsCode = LoadFileContent(fsShaderPath);
  49. //��shader���� ���ڴ洫���Դ�
  50. glShaderSource(vsShader, 1, &vsCode, nullptr);
  51. glShaderSource(fsShader, 1, &fsCode, nullptr);
  52. //����shader
  53. glCompileShader(vsShader);
  54. GLint compileResult = GL_TRUE;
  55. glGetShaderiv(vsShader, GL_COMPILE_STATUS, &compileResult);
  56. if (compileResult == GL_FALSE) {
  57. char szLog[1024] = { 0 };
  58. GLsizei logLen = 0;//ʵ�ʴ�����־����
  59. glGetShaderInfoLog(vsShader, 1024, &logLen, szLog);
  60. printf("Compile shader fail error log is : %s \n shader code :\n %s \n ", szLog, vsCode);
  61. glDeleteShader(vsShader);
  62. }
  63. glCompileShader(fsShader);
  64. compileResult = GL_TRUE;
  65. glGetShaderiv(fsShader, GL_COMPILE_STATUS, &compileResult);
  66. if (compileResult == GL_FALSE) {
  67. char szLog[1024] = { 0 };
  68. GLsizei logLen = 0;//ʵ�ʴ�����־����
  69. glGetShaderInfoLog(fsShader, 1024, &logLen, szLog);
  70. printf("Compile shader fail error log is : %s \n shader code :\n %s \n ", szLog, fsCode);
  71. glDeleteShader(fsShader);
  72. }
  73. //����program
  74. GLuint program = glCreateProgram();
  75. //����shader
  76. glAttachShader(program, vsShader);
  77. glAttachShader(program, fsShader);
  78. //����
  79. glLinkProgram(program);
  80. GLint nResult;
  81. glGetProgramiv(program, GL_LINK_STATUS, &nResult);
  82. if (nResult == GL_FALSE) {
  83. char log[1024] = { 0 };
  84. GLsizei writed = 0;
  85. glGetProgramInfoLog(program, 1024, &writed, log);
  86. printf("Create CPU program fail error %s\n", log);
  87. glDeleteProgram(program);
  88. program = 0;
  89. }
  90. //����shader
  91. glDetachShader(program, vsShader);
  92. glDetachShader(program, fsShader);
  93. //ɾ��shader
  94. glDeleteShader(vsShader);
  95. glDeleteShader(fsShader);
  96. return program;
  97. }
  98. /**
  99. * @hinstance Ӧóʵ
  100. * @hPrevInstance һӦóʽʵ
  101. * @IpCmdLine еIJ
  102. * @ShowCmd ôʾ
  103. */
  104. INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  105. {
  106. /*ע�ᴰ��*/
  107. WNDCLASSEX wndclass;
  108. wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
  109. wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
  110. wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
  111. wndclass.hbrBackground = NULL; //���ڱ���
  112. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
  113. wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
  114. wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
  115. wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
  116. wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
  117. wndclass.lpszClassName = L"GLWindow";//��������
  118. wndclass.lpszMenuName = NULL;//�˵�����
  119. wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
  120. ATOM atom = RegisterClassEx(&wndclass);
  121. if (!atom) {
  122. MessageBox(NULL, L"Register failed", L"Error", MB_OK);
  123. return 0;
  124. }
  125. /*��������*/
  126. //�������ڴ�С
  127. RECT rect;
  128. rect.left = 0;
  129. rect.right = 800;
  130. rect.top = 0;
  131. rect.bottom = 600;
  132. AdjustWindowRect(&rect, WS_EX_OVERLAPPEDWINDOW, NULL);
  133. int windowWidth = rect.right - rect.left;
  134. int windowHeight = rect.bottom - rect.top;
  135. //��������һ��Ҫ�͸ղ�ע�ᴰ�ڵı���һ��
  136. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
  137. //������Ⱦ����
  138. HDC dc = GetDC(hwnd);//��ȡ�豸������
  139. PIXELFORMATDESCRIPTOR pfd;
  140. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  141. pfd.nVersion = 1;
  142. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  143. pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
  144. pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
  145. pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
  146. pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
  147. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; //��ʾ������������������
  148. //�������ظ�ʽ
  149. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  150. SetPixelFormat(dc, pixelFormat, &pfd);
  151. //����OpenGL��Ⱦ����
  152. HGLRC rc = wglCreateContext(dc);
  153. wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
  154. /*glew��ʼ��*/
  155. glewInit();
  156. /*����program*/
  157. GLuint program = CreateGPUProgram("test.vs", "test.fs");
  158. GLuint posLocation, colorLocation, MLocation, VLocation, PLocation;
  159. posLocation = glGetAttribLocation(program, "pos");
  160. colorLocation = glGetAttribLocation(program, "color");
  161. MLocation = glGetUniformLocation(program, "M");
  162. VLocation = glGetUniformLocation(program, "V");
  163. PLocation = glGetUniformLocation(program, "P");
  164. /*����vbo*/
  165. Vertex vertex[3];
  166. vertex[0].pos[0] = 0;
  167. vertex[0].pos[1] = 0;
  168. vertex[0].pos[2] = -100.0f;
  169. vertex[0].color[0] = 0.0f;
  170. vertex[0].color[1] = 0.0f;
  171. vertex[0].color[2] = 0.0f;
  172. vertex[0].color[3] = 1.0f;
  173. vertex[1].pos[0] = 10;
  174. vertex[1].pos[1] = 0;
  175. vertex[1].pos[2] = -100.0f;
  176. vertex[1].color[0] = 0.0f;
  177. vertex[1].color[1] = 0.0f;
  178. vertex[1].color[2] = 0.0f;
  179. vertex[1].color[3] = 1.0f;
  180. vertex[2].pos[0] = 0;
  181. vertex[2].pos[1] = 10;
  182. vertex[2].pos[2] = -100.0f;
  183. vertex[2].color[0] = 0.0f;
  184. vertex[2].color[1] = 0.0f;
  185. vertex[2].color[2] = 0.0f;
  186. vertex[2].color[3] = 1.0f;
  187. GLuint vbo;
  188. glGenBuffers(1, &vbo);
  189. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  190. glBufferData(GL_ARRAY_BUFFER, sizeof(float)*7*3, vertex, GL_STATIC_DRAW);
  191. glBindBuffer(GL_ARRAY_BUFFER, 0);
  192. /*����IBO*/
  193. unsigned int indexes[] = {0,1,2};
  194. GLuint ibo;
  195. glGenBuffers(1, &ibo);
  196. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  197. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 3, indexes, GL_STATIC_DRAW);
  198. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  199. glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
  200. //����һ����λ������һ��ͶӰ���󣬺����������ݵ�shader
  201. float identify[] = {
  202. 1,0,0,0,
  203. 0,1,0,0,
  204. 0,0,1,0,
  205. 0,0,0,1
  206. };
  207. glm::mat4 projection = glm::perspective(45.0f, 800.0f/600.0f, 0.1f, 1000.0f);
  208. /*��ʾ����*/
  209. ShowWindow(hwnd, SW_SHOW);
  210. UpdateWindow(hwnd);
  211. /*�����������û�����*/
  212. MSG msg;
  213. while (true) {
  214. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  215. if (msg.message == WM_QUIT) {
  216. break;
  217. }
  218. TranslateMessage(&msg);
  219. DispatchMessage(&msg);
  220. }
  221. glClear(GL_COLOR_BUFFER_BIT);
  222. glUseProgram(program);
  223. //����mvp������shader
  224. glUniformMatrix4fv(MLocation, 1, GL_FALSE, identify);
  225. glUniformMatrix4fv(VLocation, 1, GL_FALSE, identify);
  226. glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection));
  227. //��shader��posָ�����ݺ�color����
  228. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  229. glEnableVertexAttribArray(posLocation);
  230. glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
  231. glEnableVertexAttribArray(colorLocation);
  232. glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(float) * 3));
  233. glBindBuffer(GL_ARRAY_BUFFER, 0);
  234. //glDrawArrays(GL_TRIANGLES, 0, 3);
  235. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  236. glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
  237. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  238. glUseProgram(0);
  239. SwapBuffers(dc);
  240. }
  241. return 0;
  242. }