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.

147 lines
3.9 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
  1. #include <windows.h>
  2. #include "glew.h"
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include "utils.h"
  6. #include "GPUProgram.h"
  7. #include "ObjModel.h"
  8. #include "FBO.h"
  9. #include "FullScreenQuad.h"
  10. #include "Glm/glm.hpp"
  11. #include "Glm/ext.hpp"
  12. #pragma comment(lib,"opengl32.lib")
  13. #pragma comment(lib,"glew32.lib")
  14. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  15. {
  16. switch (msg)
  17. {
  18. case WM_CLOSE:
  19. PostQuitMessage(0);
  20. break;
  21. }
  22. return DefWindowProc(hwnd,msg,wParam,lParam);
  23. }
  24. INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  25. {
  26. WNDCLASSEX wndClass;
  27. wndClass.cbClsExtra = 0;
  28. wndClass.cbSize = sizeof(WNDCLASSEX);
  29. wndClass.cbWndExtra = 0;
  30. wndClass.hbrBackground = NULL;
  31. wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
  32. wndClass.hIcon = NULL;
  33. wndClass.hIconSm = NULL;
  34. wndClass.hInstance = hInstance;
  35. wndClass.lpfnWndProc=GLWindowProc;
  36. wndClass.lpszClassName = "OpenGL";
  37. wndClass.lpszMenuName = NULL;
  38. wndClass.style = CS_VREDRAW | CS_HREDRAW;
  39. ATOM atom = RegisterClassEx(&wndClass);
  40. RECT rect;
  41. rect.left = 0;
  42. rect.top = 0;
  43. rect.right = 1280;
  44. rect.bottom = 720;
  45. AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
  46. HWND hwnd = CreateWindowEx(NULL, "OpenGL", "RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL);
  47. HDC dc = GetDC(hwnd);
  48. PIXELFORMATDESCRIPTOR pfd;
  49. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  50. pfd.nVersion = 1;
  51. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DOUBLEBUFFER;
  52. pfd.iLayerType = PFD_MAIN_PLANE;
  53. pfd.iPixelType = PFD_TYPE_RGBA;
  54. pfd.cColorBits = 32;
  55. pfd.cDepthBits = 24;
  56. pfd.cStencilBits = 8;
  57. int pixelFormatID = ChoosePixelFormat(dc, &pfd);
  58. SetPixelFormat(dc,pixelFormatID,&pfd);
  59. HGLRC rc = wglCreateContext(dc);
  60. wglMakeCurrent(dc, rc);
  61. GetClientRect(hwnd, &rect);
  62. int viewportWidth = rect.right - rect.left, viewportHeight = rect.bottom - rect.top;
  63. glewInit();
  64. //gpu program
  65. GPUProgram gpuProgram;
  66. gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/skybox.vs");
  67. gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/skybox.fs");
  68. gpuProgram.Link();
  69. gpuProgram.DetectAttribute("pos");
  70. gpuProgram.DetectAttribute("texcoord");
  71. gpuProgram.DetectAttribute("normal");
  72. gpuProgram.DetectUniform("M");
  73. gpuProgram.DetectUniform("V");
  74. gpuProgram.DetectUniform("P");
  75. //init 3d model
  76. ObjModel cube;
  77. cube.Init("res/model/Sphere.obj");
  78. float identity[] = {
  79. 1.0f,0,0,0,
  80. 0,1.0f,0,0,
  81. 0,0,1.0f,0,
  82. 0,0,0,1.0f
  83. };
  84. glm::mat4 cubeModelMatrix;
  85. glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
  86. GLuint mainTexture = SOIL_load_OGL_cubemap(
  87. "res/image/right.bmp",
  88. "res/image/left.bmp",
  89. "res/image/top.bmp",
  90. "res/image/bottom.bmp",
  91. "res/image/back.bmp",
  92. "res/image/front.bmp",
  93. 0, 0, SOIL_FLAG_POWER_OF_TWO
  94. );
  95. ShowWindow(hwnd, SW_SHOW);
  96. UpdateWindow(hwnd);
  97. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  98. MSG msg;
  99. while (true)
  100. {
  101. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  102. {
  103. if (msg.message==WM_QUIT)
  104. {
  105. break;
  106. }
  107. TranslateMessage(&msg);
  108. DispatchMessage(&msg);
  109. }
  110. glDisable(GL_DEPTH_TEST);
  111. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  112. glUseProgram(gpuProgram.mProgram);
  113. glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, identity);
  114. glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  115. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(cubeModelMatrix));
  116. glBindTexture(GL_TEXTURE_CUBE_MAP, mainTexture);
  117. glUniform1i(gpuProgram.GetLocation("U_MainTexture"), 0);
  118. cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  119. cube.Draw();
  120. glUseProgram(0);
  121. glEnable(GL_DEPTH_TEST);
  122. glFlush();
  123. SwapBuffers(dc);
  124. }
  125. return 0;
  126. }