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.

157 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
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. HWND hwnd = CreateWindowEx(NULL, "OpenGL", "RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
  41. HDC dc = GetDC(hwnd);
  42. PIXELFORMATDESCRIPTOR pfd;
  43. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  44. pfd.nVersion = 1;
  45. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DOUBLEBUFFER;
  46. pfd.iLayerType = PFD_MAIN_PLANE;
  47. pfd.iPixelType = PFD_TYPE_RGBA;
  48. pfd.cColorBits = 32;
  49. pfd.cDepthBits = 24;
  50. pfd.cStencilBits = 8;
  51. int pixelFormatID = ChoosePixelFormat(dc, &pfd);
  52. SetPixelFormat(dc,pixelFormatID,&pfd);
  53. HGLRC rc = wglCreateContext(dc);
  54. wglMakeCurrent(dc, rc);
  55. glewInit();
  56. //init gpu program
  57. GPUProgram gpuProgram;
  58. gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/test.vs");
  59. gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/test.fs");
  60. gpuProgram.Link();
  61. gpuProgram.DetectAttribute("pos");
  62. gpuProgram.DetectAttribute("texcoord");
  63. gpuProgram.DetectAttribute("normal");
  64. gpuProgram.DetectUniform("M");
  65. gpuProgram.DetectUniform("V");
  66. gpuProgram.DetectUniform("P");
  67. gpuProgram.DetectUniform("NM");
  68. gpuProgram.DetectUniform("U_AmbientLightColor");
  69. gpuProgram.DetectUniform("U_AmbientMaterial");
  70. gpuProgram.DetectUniform("U_DiffuseLightColor");
  71. gpuProgram.DetectUniform("U_DiffuseMaterial");
  72. gpuProgram.DetectUniform("U_LightPos");
  73. gpuProgram.DetectUniform("U_LightDirection");
  74. gpuProgram.DetectUniform("U_Cutoff");
  75. gpuProgram.DetectUniform("U_SpecularLightColor");
  76. gpuProgram.DetectUniform("U_SpecularMaterial");
  77. gpuProgram.DetectUniform("U_EyePos");
  78. //init 3d model
  79. ObjModel model;
  80. model.Init("res/model/Quad.obj");
  81. float identity[] = {
  82. 1.0f,0,0,0,
  83. 0,1.0f,0,0,
  84. 0,0,1.0f,0,
  85. 0,0,0,1.0f
  86. };
  87. float ambientLightColor[] = {0.4f,0.4f,0.4f,1.0f};
  88. float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };
  89. float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  90. float diffuseMaterial[] = { 0.6f,0.6f,0.6f,1.0f };
  91. float lightPos[] = {0.0f,1.5f,-3.0f,0.0f};
  92. float spotLightDirection[] = {0.0f, -1.0f, 0.0f, 64.0f};//������һλ�ij�......
  93. float spotLightCutoff = 30.0f;
  94. float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  95. float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };
  96. float eyePos[] = { 0.0f,0.0f,0.0f };
  97. glm::mat4 modelMatrix = glm::translate<float>(0.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f)*glm::scale(2.0f,2.0f,2.0f);
  98. glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
  99. glm::mat4 normalMatrix = glm::inverseTranspose(modelMatrix);
  100. glClearColor(41.0f/255.0f, 71.0f/255.0f, 121.0f / 255.0f, 1.0f);
  101. ShowWindow(hwnd, SW_SHOW);
  102. UpdateWindow(hwnd);
  103. MSG msg;
  104. while (true)
  105. {
  106. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  107. {
  108. if (msg.message==WM_QUIT)
  109. {
  110. break;
  111. }
  112. TranslateMessage(&msg);
  113. DispatchMessage(&msg);
  114. }
  115. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  116. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  117. glEnable(GL_DEPTH_TEST);
  118. glUseProgram(gpuProgram.mProgram);
  119. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1,GL_FALSE, glm::value_ptr(modelMatrix));
  120. glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, identity);
  121. glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  122. glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
  123. glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1,ambientLightColor);
  124. glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);
  125. glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
  126. glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
  127. glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
  128. glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
  129. glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
  130. glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
  131. glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
  132. glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
  133. model.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  134. model.Draw();
  135. glUseProgram(0);
  136. glFinish();
  137. SwapBuffers(dc);
  138. }
  139. return 0;
  140. }