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.

163 lines
4.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #include <windows.h>
  2. #include "glew.h"
  3. #include <gl/GL.h>
  4. #include <stdio.h>
  5. #pragma comment(lib,"opengl32.lib")
  6. #pragma comment(lib, "glew32.lib")
  7. /**
  8. * @hwnd ϢĴ
  9. * @msg Ϣ
  10. */
  11. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  12. switch (msg) {
  13. case WM_CLOSE:
  14. PostQuitMessage(0);
  15. return 0;
  16. }
  17. return DefWindowProc(hwnd, msg, wParam, lParam);//������Ϣ����windowĬ�ϴ�������
  18. }
  19. char* LoadFileContent(const char *path) {
  20. char* fileContent = nullptr;
  21. int filesize = 0;
  22. FILE*pFile = fopen(path, "rb");
  23. if (pFile) {
  24. fseek(pFile, 0, SEEK_END);
  25. int nLen = ftell(pFile);
  26. if (nLen > 0) {
  27. rewind(pFile);
  28. fileContent = new char[nLen + 1];
  29. fread(fileContent, sizeof(char), nLen, pFile);
  30. fileContent[nLen] = '\0';
  31. filesize = nLen;
  32. }
  33. fclose(pFile);
  34. }
  35. return fileContent;
  36. }
  37. GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath) {
  38. //����program
  39. GLuint program = glCreateProgram();
  40. //����shader
  41. GLuint vsShader = glCreateShader(GL_VERTEX_SHADER);
  42. GLuint fsShader = glCreateShader(GL_FRAGMENT_SHADER);
  43. //��ȡshader����
  44. const char* vsCode = LoadFileContent(vsShaderPath);
  45. const char* fsCode = LoadFileContent(fsShaderPath);
  46. //��shader���� ���ڴ洫���Դ�
  47. glShaderSource(vsShader, 1, &vsCode, nullptr);
  48. glShaderSource(fsShader, 1, &fsCode, nullptr);
  49. //����shader
  50. glCompileShader(vsShader);
  51. glCompileShader(fsShader);
  52. //����shader
  53. glAttachShader(program, vsShader);
  54. glAttachShader(program, fsShader);
  55. //����
  56. glLinkProgram(program);
  57. //����shader
  58. glDetachShader(program, vsShader);
  59. glDetachShader(program, fsShader);
  60. //ɾ��shader
  61. glDeleteShader(vsShader);
  62. glDeleteShader(fsShader);
  63. return program;
  64. }
  65. /**
  66. * @hinstance Ӧóʵ
  67. * @hPrevInstance һӦóʽʵ
  68. * @IpCmdLine еIJ
  69. * @ShowCmd ôʾ
  70. */
  71. INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  72. {
  73. /*ע�ᴰ��*/
  74. WNDCLASSEX wndclass;
  75. wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
  76. wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
  77. wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
  78. wndclass.hbrBackground = NULL; //���ڱ���
  79. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
  80. wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
  81. wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
  82. wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
  83. wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
  84. wndclass.lpszClassName = L"GLWindow";//��������
  85. wndclass.lpszMenuName = NULL;//�˵�����
  86. wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
  87. ATOM atom = RegisterClassEx(&wndclass);
  88. if (!atom) {
  89. MessageBox(NULL, L"Register failed", L"Error", MB_OK);
  90. return 0;
  91. }
  92. /*��������*/
  93. //�������ڴ�С
  94. RECT rect;
  95. rect.left = 0;
  96. rect.right = 800;
  97. rect.top = 0;
  98. rect.bottom = 600;
  99. AdjustWindowRect(&rect, WS_EX_OVERLAPPEDWINDOW, NULL);
  100. int windowWidth = rect.right - rect.left;
  101. int windowHeight = rect.bottom - rect.top;
  102. //��������һ��Ҫ�͸ղ�ע�ᴰ�ڵı���һ��
  103. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
  104. //������Ⱦ����
  105. HDC dc = GetDC(hwnd);//��ȡ�豸������
  106. PIXELFORMATDESCRIPTOR pfd;
  107. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  108. pfd.nVersion = 1;
  109. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  110. pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
  111. pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
  112. pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
  113. pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
  114. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; //��ʾ������������������
  115. //�������ظ�ʽ
  116. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  117. SetPixelFormat(dc, pixelFormat, &pfd);
  118. //����OpenGL��Ⱦ����
  119. HGLRC rc = wglCreateContext(dc);
  120. wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
  121. /*glew��ʼ��*/
  122. glewInit();
  123. GLuint program = CreateGPUProgram("test.vs", "test.fs");
  124. glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
  125. /*��ʾ����*/
  126. ShowWindow(hwnd, SW_SHOW);
  127. UpdateWindow(hwnd);
  128. /*�����������û�����*/
  129. MSG msg;
  130. while (true) {
  131. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  132. if (msg.message == WM_QUIT) {
  133. break;
  134. }
  135. TranslateMessage(&msg);
  136. DispatchMessage(&msg);
  137. }
  138. glClear(GL_COLOR_BUFFER_BIT);
  139. SwapBuffers(dc);
  140. }
  141. return 0;
  142. }