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.

175 lines
5.3 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
  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. //reflect program
  76. GPUProgram reflectProgram;
  77. reflectProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/skybox_reflect.vs");
  78. reflectProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/skybox_reflect.fs");
  79. reflectProgram.Link();
  80. reflectProgram.DetectAttribute("pos");
  81. reflectProgram.DetectAttribute("texcoord");
  82. reflectProgram.DetectAttribute("normal");
  83. reflectProgram.DetectUniform("M");
  84. reflectProgram.DetectUniform("V");
  85. reflectProgram.DetectUniform("P");
  86. reflectProgram.DetectUniform("NM");
  87. reflectProgram.DetectUniform("U_MainTexture");
  88. //init 3d model
  89. ObjModel cube,sphere;
  90. cube.Init("res/model/Cube.obj");
  91. sphere.Init("res/model/Sphere.obj");
  92. float identity[] = {
  93. 1.0f,0,0,0,
  94. 0,1.0f,0,0,
  95. 0,0,1.0f,0,
  96. 0,0,0,1.0f
  97. };
  98. glm::mat4 cubeModelMatrix;
  99. glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
  100. glm::mat4 sphereModelMatrix = glm::translate(0.0f, 0.0f, -4.0f);
  101. glm::mat4 sphereNormalMatrix = glm::inverseTranspose(sphereModelMatrix);
  102. GLuint mainTexture = SOIL_load_OGL_cubemap(
  103. "res/image/right.bmp",
  104. "res/image/left.bmp",
  105. "res/image/top.bmp",
  106. "res/image/bottom.bmp",
  107. "res/image/back.bmp",
  108. "res/image/front.bmp",
  109. 0, 0, SOIL_FLAG_POWER_OF_TWO
  110. );
  111. ShowWindow(hwnd, SW_SHOW);
  112. UpdateWindow(hwnd);
  113. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  114. MSG msg;
  115. while (true)
  116. {
  117. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  118. {
  119. if (msg.message==WM_QUIT)
  120. {
  121. break;
  122. }
  123. TranslateMessage(&msg);
  124. DispatchMessage(&msg);
  125. }
  126. glDisable(GL_DEPTH_TEST);
  127. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  128. glUseProgram(gpuProgram.mProgram);
  129. glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, identity);
  130. glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  131. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(cubeModelMatrix));
  132. glBindTexture(GL_TEXTURE_CUBE_MAP, mainTexture);
  133. glUniform1i(gpuProgram.GetLocation("U_MainTexture"), 0);
  134. cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  135. cube.Draw();
  136. glUseProgram(0);
  137. glEnable(GL_DEPTH_TEST);
  138. glUseProgram(reflectProgram.mProgram);
  139. glUniformMatrix4fv(reflectProgram.GetLocation("V"), 1, GL_FALSE, identity);
  140. glUniformMatrix4fv(reflectProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  141. glUniformMatrix4fv(reflectProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(sphereModelMatrix));
  142. glUniformMatrix4fv(reflectProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(sphereNormalMatrix));
  143. glBindTexture(GL_TEXTURE_CUBE_MAP, mainTexture);
  144. glUniform1i(reflectProgram.GetLocation("U_MainTexture"), 0);
  145. sphere.Bind(reflectProgram.GetLocation("pos"), reflectProgram.GetLocation("texcoord"), reflectProgram.GetLocation("normal"));
  146. sphere.Draw();
  147. glUseProgram(0);
  148. glFlush();
  149. SwapBuffers(dc);
  150. }
  151. return 0;
  152. }