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.

205 lines
5.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <windows.h>
  3. #include "glew.h"
  4. #include <gl/GL.h>
  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. char* fileContent = nullptr;
  26. int filesize = 0;
  27. FILE*pFile = fopen(path, "rb");
  28. if (pFile) {
  29. fseek(pFile, 0, SEEK_END);
  30. int nLen = ftell(pFile);
  31. if (nLen > 0) {
  32. rewind(pFile);
  33. fileContent = new char[nLen + 1];
  34. fread(fileContent, sizeof(char), nLen, pFile);
  35. fileContent[nLen] = '\0';
  36. filesize = nLen;
  37. }
  38. fclose(pFile);
  39. }
  40. return fileContent;
  41. }
  42. /**
  43. * һGPU
  44. */
  45. GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath) {
  46. //����program
  47. GLuint program = glCreateProgram();
  48. //����shader
  49. GLuint vsShader = glCreateShader(GL_VERTEX_SHADER);
  50. GLuint fsShader = glCreateShader(GL_FRAGMENT_SHADER);
  51. //��ȡshader����
  52. const char* vsCode = LoadFileContent(vsShaderPath);
  53. const char* fsCode = LoadFileContent(fsShaderPath);
  54. //��shader���� ���ڴ洫���Դ�
  55. glShaderSource(vsShader, 1, &vsCode, nullptr);
  56. glShaderSource(fsShader, 1, &fsCode, nullptr);
  57. //����shader
  58. glCompileShader(vsShader);
  59. glCompileShader(fsShader);
  60. //����shader
  61. glAttachShader(program, vsShader);
  62. glAttachShader(program, fsShader);
  63. //����
  64. glLinkProgram(program);
  65. //����shader
  66. glDetachShader(program, vsShader);
  67. glDetachShader(program, fsShader);
  68. //ɾ��shader
  69. glDeleteShader(vsShader);
  70. glDeleteShader(fsShader);
  71. return program;
  72. }
  73. /**
  74. * @hinstance Ӧóʵ
  75. * @hPrevInstance һӦóʽʵ
  76. * @IpCmdLine еIJ
  77. * @ShowCmd ôʾ
  78. */
  79. INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  80. {
  81. /*ע�ᴰ��*/
  82. WNDCLASSEX wndclass;
  83. wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
  84. wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
  85. wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
  86. wndclass.hbrBackground = NULL; //���ڱ���
  87. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
  88. wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
  89. wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
  90. wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
  91. wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
  92. wndclass.lpszClassName = L"GLWindow";//��������
  93. wndclass.lpszMenuName = NULL;//�˵�����
  94. wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
  95. ATOM atom = RegisterClassEx(&wndclass);
  96. if (!atom) {
  97. MessageBox(NULL, L"Register failed", L"Error", MB_OK);
  98. return 0;
  99. }
  100. /*��������*/
  101. //�������ڴ�С
  102. RECT rect;
  103. rect.left = 0;
  104. rect.right = 800;
  105. rect.top = 0;
  106. rect.bottom = 600;
  107. AdjustWindowRect(&rect, WS_EX_OVERLAPPEDWINDOW, NULL);
  108. int windowWidth = rect.right - rect.left;
  109. int windowHeight = rect.bottom - rect.top;
  110. //��������һ��Ҫ�͸ղ�ע�ᴰ�ڵı���һ��
  111. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
  112. //������Ⱦ����
  113. HDC dc = GetDC(hwnd);//��ȡ�豸������
  114. PIXELFORMATDESCRIPTOR pfd;
  115. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  116. pfd.nVersion = 1;
  117. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  118. pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
  119. pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
  120. pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
  121. pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
  122. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; //��ʾ������������������
  123. //�������ظ�ʽ
  124. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  125. SetPixelFormat(dc, pixelFormat, &pfd);
  126. //����OpenGL��Ⱦ����
  127. HGLRC rc = wglCreateContext(dc);
  128. wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
  129. /*glew��ʼ��*/
  130. glewInit();
  131. /*����program*/
  132. GLuint program = CreateGPUProgram("test.vs", "test.fs");
  133. /*����vbo*/
  134. Vertex vertex[3];
  135. vertex[0].pos[0] = 0;
  136. vertex[0].pos[1] = 0;
  137. vertex[0].pos[2] = -100.0f;
  138. vertex[0].color[0] = 1.0f;
  139. vertex[0].color[1] = 1.0f;
  140. vertex[0].color[2] = 1.0f;
  141. vertex[0].color[3] = 1.0f;
  142. vertex[1].pos[0] = 10;
  143. vertex[1].pos[1] = 0;
  144. vertex[1].pos[2] = -100.0f;
  145. vertex[1].color[0] = 1.0f;
  146. vertex[1].color[1] = 1.0f;
  147. vertex[1].color[2] = 1.0f;
  148. vertex[1].color[3] = 1.0f;
  149. vertex[2].pos[0] = 0;
  150. vertex[2].pos[1] = 10;
  151. vertex[2].pos[2] = -100.0f;
  152. vertex[2].color[0] = 1.0f;
  153. vertex[2].color[1] = 1.0f;
  154. vertex[2].color[2] = 1.0f;
  155. vertex[2].color[3] = 1.0f;
  156. GLuint vbo;
  157. glGenBuffers(1, &vbo);
  158. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  159. glBufferData(GL_ARRAY_BUFFER, sizeof(float)*7*3, vertex, GL_STATIC_DRAW);
  160. glBindBuffer(GL_ARRAY_BUFFER, 0);
  161. glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
  162. /*��ʾ����*/
  163. ShowWindow(hwnd, SW_SHOW);
  164. UpdateWindow(hwnd);
  165. /*�����������û�����*/
  166. MSG msg;
  167. while (true) {
  168. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  169. if (msg.message == WM_QUIT) {
  170. break;
  171. }
  172. TranslateMessage(&msg);
  173. DispatchMessage(&msg);
  174. }
  175. glClear(GL_COLOR_BUFFER_BIT);
  176. SwapBuffers(dc);
  177. }
  178. return 0;
  179. }