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.

151 lines
4.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
  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_SpecularLightColor");
  74. gpuProgram.DetectUniform("U_SpecularMaterial");
  75. gpuProgram.DetectUniform("U_EyePos");
  76. //init 3d model
  77. ObjModel model;
  78. model.Init("res/model/Sphere.obj");
  79. float identity[] = {
  80. 1.0f,0,0,0,
  81. 0,1.0f,0,0,
  82. 0,0,1.0f,0,
  83. 0,0,0,1.0f
  84. };
  85. float ambientLightColor[] = {0.4f,0.4f,0.4f,1.0f};
  86. float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };
  87. float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  88. float diffuseMaterial[] = { 0.6f,0.6f,0.6f,1.0f };
  89. float lightPos[] = {3.0f,3.0f,0.0f,1.0f};
  90. float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
  91. float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };
  92. float eyePos[] = { 0.0f,0.0f,0.0f };
  93. glm::mat4 modelMatrix = glm::translate<float>(0.0f,0.0f,-3.0f);
  94. glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
  95. glm::mat4 normalMatrix = glm::inverseTranspose(modelMatrix);
  96. glClearColor(41.0f/255.0f, 71.0f/255.0f, 121.0f / 255.0f, 1.0f);
  97. ShowWindow(hwnd, SW_SHOW);
  98. UpdateWindow(hwnd);
  99. MSG msg;
  100. while (true)
  101. {
  102. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  103. {
  104. if (msg.message==WM_QUIT)
  105. {
  106. break;
  107. }
  108. TranslateMessage(&msg);
  109. DispatchMessage(&msg);
  110. }
  111. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  112. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  113. glEnable(GL_DEPTH_TEST);
  114. glUseProgram(gpuProgram.mProgram);
  115. glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1,GL_FALSE, glm::value_ptr(modelMatrix));
  116. glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, identity);
  117. glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  118. glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
  119. glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1,ambientLightColor);
  120. glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);
  121. glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
  122. glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
  123. glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
  124. glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
  125. glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
  126. glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
  127. model.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
  128. model.Draw();
  129. glUseProgram(0);
  130. glFinish();
  131. SwapBuffers(dc);
  132. }
  133. return 0;
  134. }