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.

166 lines
4.6 KiB

5 years ago
  1. #include "ggl.h"
  2. #include "scene.h"
  3. #include "utils.h"
  4. #pragma comment(lib, "opengl32.lib")
  5. #pragma comment(lib, "glu32.lib")
  6. #pragma comment(lib, "winmm.lib")
  7. POINT originalPos;//�����Ҽ�������ʱ��λ��
  8. bool rotateView = false;
  9. unsigned char* LoadFileContent(const char* path, int &filesize) {
  10. unsigned char* fileContent = nullptr;
  11. filesize = 0;
  12. FILE *pFile = fopen(path, "rb");
  13. if (pFile) {
  14. fseek(pFile, 0, SEEK_END);//���ļ�ָ���ƶ����ļ�ĩβ
  15. int nLen = ftell(pFile);//�ļ�λ��ָ�뵱ǰλ���������ļ��׵�ƫ���ֽ�����������ʵ���ǵõ����ļ����ֽڳ���
  16. if (nLen > 0) {
  17. rewind(pFile);//���ļ�ָ���Ƶ�ͷ��
  18. fileContent = new unsigned char[nLen + 1];
  19. fread(fileContent, sizeof(unsigned char), nLen, pFile);
  20. fileContent[nLen] = '\0';
  21. filesize = nLen;
  22. }
  23. fclose(pFile);
  24. }
  25. return fileContent;
  26. }
  27. /**
  28. * ȡÿ֡ʱ
  29. */
  30. float GetFrameTime() {
  31. static unsigned long lastTime = 0, timeSinceComputerStart = 0;
  32. timeSinceComputerStart = timeGetTime();
  33. unsigned long frameTime = lastTime == 0 ? 0 : timeSinceComputerStart - lastTime;
  34. lastTime = timeSinceComputerStart;
  35. return float(frameTime) / 1000.0f;
  36. }
  37. /**
  38. * @hwnd ϢĴ
  39. * @msg Ϣ
  40. */
  41. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  42. switch (msg) {
  43. case WM_RBUTTONDOWN:
  44. GetCursorPos(&originalPos);//��ȡ����λ��
  45. ShowCursor(false);//��������
  46. rotateView = true;
  47. return 0;
  48. case WM_RBUTTONUP:
  49. SetCursorPos(originalPos.x, originalPos.y);
  50. ShowCursor(true);
  51. rotateView = false;
  52. return 0;
  53. case WM_MOUSEMOVE:
  54. if (rotateView) {
  55. POINT currentPos;
  56. GetCursorPos(&currentPos);//��һ�λ�ȡ���굱ǰλ��
  57. int deltaX = currentPos.x - originalPos.x;
  58. int deltaY = currentPos.y - originalPos.y;
  59. OnMouseMove(deltaX, deltaY);
  60. SetCursorPos(originalPos.x, originalPos.y);
  61. }
  62. return 0;
  63. case WM_KEYDOWN:
  64. OnKeyDown(wParam);
  65. return 0;
  66. case WM_KEYUP:
  67. OnKeyUp(wParam);
  68. return 0;
  69. case WM_CLOSE:
  70. PostQuitMessage(0);
  71. return 0;
  72. }
  73. return DefWindowProc(hwnd, msg, wParam, lParam);//������Ϣ����windowĬ�ϴ�������
  74. }
  75. /**
  76. * @hinstance Ӧóʵ
  77. * @hPrevInstance һӦóʽʵ
  78. * @IpCmdLine еIJ
  79. * @ShowCmd ôʾ
  80. */
  81. INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR IpCmdLine, int ShowCmd) {
  82. /*ע�ᴰ��*/
  83. WNDCLASSEX wndclass;
  84. wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
  85. wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
  86. wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
  87. wndclass.hbrBackground = NULL; //���ڱ���
  88. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
  89. wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
  90. wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
  91. wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
  92. wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
  93. wndclass.lpszClassName = L"GLWindow";//��������
  94. wndclass.lpszMenuName = NULL;//�˵�����
  95. wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
  96. ATOM atom = RegisterClassEx(&wndclass);
  97. if (!atom) {
  98. MessageBox(NULL, L"Register failed", L"Error", MB_OK);
  99. return 0;
  100. }
  101. /*��������*/
  102. //�������ڴ�С
  103. RECT rect;
  104. rect.left = 0;
  105. rect.right = 800;
  106. rect.top = 0;
  107. rect.bottom = 600;
  108. AdjustWindowRect(&rect, WS_EX_OVERLAPPEDWINDOW, NULL);
  109. int windowWidth = rect.right - rect.left;
  110. int windowHeight = rect.bottom - rect.top;
  111. //��������һ��Ҫ�͸ղ�ע�ᴰ�ڵı���һ��
  112. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
  113. //������Ⱦ����
  114. HDC dc = GetDC(hwnd);//��ȡ�豸������
  115. PIXELFORMATDESCRIPTOR pfd;
  116. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  117. pfd.nVersion = 1;
  118. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  119. pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
  120. pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
  121. pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
  122. pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
  123. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER ; //��ʾ������������������
  124. //�������ظ�ʽ
  125. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  126. SetPixelFormat(dc, pixelFormat, &pfd);
  127. //����OpenGL��Ⱦ����
  128. HGLRC rc = wglCreateContext(dc);
  129. wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
  130. Init();
  131. /*��ʾ����*/
  132. ShowWindow(hwnd, SW_SHOW);
  133. UpdateWindow(hwnd);
  134. /*�����������û�����*/
  135. MSG msg;
  136. while (true) {
  137. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  138. if (msg.message == WM_QUIT) {
  139. break;
  140. }
  141. TranslateMessage(&msg);
  142. DispatchMessage(&msg);
  143. }
  144. Draw();
  145. SwapBuffers(dc);
  146. }
  147. return 0;
  148. }