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.

176 lines
5.7 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
  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 = 800;
  44. rect.bottom = 600;
  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. //combine program
  65. GPUProgram gpuProgram;
  66. gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/forg.vs");
  67. gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/forg.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. gpuProgram.DetectUniform("NM");
  76. gpuProgram.DetectUniform("U_AmbientLightColor");
  77. gpuProgram.DetectUniform("U_AmbientMaterial");
  78. gpuProgram.DetectUniform("U_DiffuseLightColor");
  79. gpuProgram.DetectUniform("U_DiffuseMaterial");
  80. gpuProgram.DetectUniform("U_DiffuseIntensity");
  81. gpuProgram.DetectUniform("U_LightPos");
  82. gpuProgram.DetectUniform("U_LightDirection");
  83. gpuProgram.DetectUniform("U_Cutoff");
  84. gpuProgram.DetectUniform("U_SpecularLightColor");
  85. gpuProgram.DetectUniform("U_SpecularMaterial");
  86. gpuProgram.DetectUniform("U_EyePos");
  87. //init 3d model
  88. ObjModel cube;
  89. cube.Init("res/model/Cube.obj");
  90. float identity[] = {
  91. 1.0f,0,0,0,
  92. 0,1.0f,0,0,
  93. 0,0,1.0f,0,
  94. 0,0,0,1.0f
  95. };
  96. float ambientLightColor[] = { 0.4f,0.4f,0.4f,1.0f };
  97. float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };
  98. float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  99. float diffuseMaterial[] = { 0.6f, 0.6f, 0.6f, 1.0f };
  100. float lightPos[] = { 0.0f,1.0f,0.0f,0.0f };
  101. float diffuseIntensity = 2.5f;
  102. float spotLightDirection[] = { 0.0f, -1.0f, 0.0f, 628.0f };
  103. float spotLightCutoff = 5.0f;
  104. float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  105. float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };
  106. float eyePos[] = { 0.0f,0.0f,0.0f };
  107. glm::mat4 cubeModelMatrix = glm::translate<float>(-2.0f, 0.0f, -6.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
  108. glm::mat4 cubeNormalMatrix = glm::inverseTranspose(cubeModelMatrix);
  109. glm::mat4 viewMatrix = glm::lookAt(glm::vec3(1.0f, 0.5f, -10.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
  110. glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
  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. glEnable(GL_DEPTH_TEST);
  127. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  128. glUseProgram(gpuProgram.mProgram);
  129. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(cubeModelMatrix));
  130. glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, identity);
  131. glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  132. glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(cubeNormalMatrix));
  133. glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1, ambientLightColor);
  134. glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);
  135. glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
  136. glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
  137. glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
  138. glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
  139. glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
  140. glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
  141. glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
  142. glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
  143. glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
  144. cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  145. cube.Draw();
  146. glUseProgram(0);
  147. glFlush();
  148. SwapBuffers(dc);
  149. }
  150. return 0;
  151. }