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.

140 lines
4.3 KiB

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