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.

193 lines
5.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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 = 512;
  44. rect.bottom = 512;
  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 combineProgram1;
  66. combineProgram1.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  67. combineProgram1.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad.fs");
  68. combineProgram1.Link();
  69. combineProgram1.DetectAttribute("pos");
  70. combineProgram1.DetectAttribute("texcoord");
  71. combineProgram1.DetectUniform("U_MainTexture");
  72. //combine program
  73. GPUProgram combineProgram2;
  74. combineProgram2.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  75. combineProgram2.AttachShader(GL_FRAGMENT_SHADER, "res/shader/smooth.fs");
  76. combineProgram2.Link();
  77. combineProgram2.DetectAttribute("pos");
  78. combineProgram2.DetectAttribute("texcoord");
  79. combineProgram2.DetectUniform("U_MainTexture");
  80. //combine program
  81. GPUProgram combineProgram3;
  82. combineProgram3.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  83. combineProgram3.AttachShader(GL_FRAGMENT_SHADER, "res/shader/sharpen.fs");
  84. combineProgram3.Link();
  85. combineProgram3.DetectAttribute("pos");
  86. combineProgram3.DetectAttribute("texcoord");
  87. combineProgram3.DetectUniform("U_MainTexture");
  88. //combine program
  89. GPUProgram combineProgram4;
  90. combineProgram4.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
  91. combineProgram4.AttachShader(GL_FRAGMENT_SHADER, "res/shader/edge_detect.fs");
  92. combineProgram4.Link();
  93. combineProgram4.DetectAttribute("pos");
  94. combineProgram4.DetectAttribute("texcoord");
  95. combineProgram4.DetectUniform("U_MainTexture");
  96. //init 3d model
  97. ObjModel quad;
  98. quad.Init("res/model/Quad.obj");
  99. float identity[] = {
  100. 1.0f,0,0,0,
  101. 0,1.0f,0,0,
  102. 0,0,1.0f,0,
  103. 0,0,0,1.0f
  104. };
  105. glm::mat4 quadModel = glm::translate<float>(0.0f, 0.0f, -2.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(8.0f, 8.0f, 8.0f);
  106. glm::mat4 quadNormalMatrix = glm::inverseTranspose(quadModel);
  107. 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));
  108. glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
  109. //��ʼ��fsq
  110. FullScreenQuad fsq;
  111. fsq.Init();
  112. GLuint head = CreateTextureFromFile("res/image/wood.bmp");
  113. GLuint grass = CreateTextureFromFile("res/image/earth.bmp");
  114. ShowWindow(hwnd, SW_SHOW);
  115. UpdateWindow(hwnd);
  116. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  117. MSG msg;
  118. while (true)
  119. {
  120. if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  121. {
  122. if (msg.message==WM_QUIT)
  123. {
  124. break;
  125. }
  126. TranslateMessage(&msg);
  127. DispatchMessage(&msg);
  128. }
  129. glEnable(GL_DEPTH_TEST);
  130. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  131. glEnable(GL_BLEND);
  132. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  133. glActiveTexture(GL_TEXTURE0);
  134. glUseProgram(combineProgram1.mProgram);
  135. glActiveTexture(GL_TEXTURE0);
  136. glBindTexture(GL_TEXTURE_2D, head);
  137. glUniform1i(combineProgram1.GetLocation("U_MainTexture"), 0);
  138. fsq.DrawToLeftTop(combineProgram1.GetLocation("pos"), combineProgram1.GetLocation("texcoord"));
  139. glUseProgram(combineProgram2.mProgram);
  140. glActiveTexture(GL_TEXTURE0);
  141. glBindTexture(GL_TEXTURE_2D, head);
  142. glUniform1i(combineProgram2.GetLocation("U_MainTexture"), 0);
  143. fsq.DrawToRightTop(combineProgram2.GetLocation("pos"), combineProgram2.GetLocation("texcoord"));
  144. glUseProgram(combineProgram3.mProgram);
  145. glActiveTexture(GL_TEXTURE0);
  146. glBindTexture(GL_TEXTURE_2D, head);
  147. glUniform1i(combineProgram3.GetLocation("U_MainTexture"), 0);
  148. fsq.DrawToLeftBottom(combineProgram3.GetLocation("pos"), combineProgram3.GetLocation("texcoord"));
  149. glUseProgram(combineProgram4.mProgram);
  150. glActiveTexture(GL_TEXTURE0);
  151. glBindTexture(GL_TEXTURE_2D, head);
  152. glUniform1i(combineProgram4.GetLocation("U_MainTexture"), 0);
  153. fsq.DrawToRightBottom(combineProgram4.GetLocation("pos"), combineProgram4.GetLocation("texcoord"));
  154. glDisable(GL_BLEND);
  155. glFlush();
  156. SwapBuffers(dc);
  157. }
  158. return 0;
  159. }