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.

89 lines
3.0 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
  1. #include "scene.h"
  2. #include "ggl.h"
  3. #include "utils.h"
  4. #include "ground.h"
  5. #include "shader.h"
  6. #include "model.h"
  7. #include "skybox.h"
  8. #include "particleSystem.h"
  9. #include "framebufferobject.h"
  10. glm::mat4 modelMatrix, viewMatrix, projectionMatrix;
  11. glm::vec3 cameraPos(10.0f, 10.0f, 10.0f);
  12. Ground ground;
  13. Model model, niutou, sphere;
  14. SkyBox skybox;
  15. ParticleSystem ps;
  16. FrameBufferObject *fbo;
  17. VertexBuffer *fsqVertex;
  18. Shader *fsqShader;
  19. glm::mat4 fsqViewMatrix;
  20. void Init() {
  21. viewMatrix = glm::lookAt(cameraPos, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f,1.0f,0.0f));
  22. ground.Init();
  23. model.Init("Res/Sphere.obj");
  24. model.SetTexture("Res/earth.bmp");
  25. model.SetPosition(0.0f, 0.0f, 0.0f);
  26. skybox.Init("Res/");
  27. niutou.Init("Res/niutou.obj");
  28. niutou.SetTexture("Res/niutou.bmp");
  29. niutou.mModelMatrix = glm::translate(-5.0f, 0.0f, 4.0f)*glm::scale(0.05f, 0.05f, 0.05f);
  30. ps.Init(0.0f, 0.0f, 0.0f);
  31. }
  32. void SetViewPortSize(float width, float height) {
  33. projectionMatrix = glm::perspective(60.0f, width / height, 0.1f, 1000.0f);
  34. fbo = new FrameBufferObject;
  35. fbo->AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, (int)width, (int)height);
  36. fbo->AttachColorBuffer("color1", GL_COLOR_ATTACHMENT1, (int)width, (int)height);
  37. fbo->AttachDepthBuffer("depth", (int)width, (int)height);
  38. fbo->Finish();
  39. sphere.Init("Res/Sphere.obj");
  40. sphere.SetTexture(fbo->GetBuffer("color"));
  41. sphere.mModelMatrix = glm::scale(4.0f, 4.0f, 4.0f)*glm::rotate(150.0f, 0.0f, 1.0f, 0.0f);
  42. float aspect = width / height;
  43. float halfFOV = 60.0f / 2.0f;
  44. float randianHalfFOV = 3.14f*halfFOV / 180.0f;
  45. float tanHalfFOV = sinf(randianHalfFOV) / cosf(randianHalfFOV);
  46. float y = tanHalfFOV*0.2f;
  47. float x = y*aspect;
  48. fsqVertex = new VertexBuffer;
  49. fsqVertex->SetSize(4);
  50. fsqVertex->SetPosition(0, -x, -y, -0.2f);
  51. fsqVertex->SetTexcoord(0, 0.0f, 0.0f);
  52. fsqVertex->SetPosition(1, x, -y, -0.2f);
  53. fsqVertex->SetTexcoord(1, 1.0f, 0.0f);
  54. fsqVertex->SetPosition(2, -x, y, -0.2f);
  55. fsqVertex->SetTexcoord(2, 0.0f, 1.0f);
  56. fsqVertex->SetPosition(3, x, y, -0.2f);
  57. fsqVertex->SetTexcoord(3, 1.0f, 1.0f);
  58. fsqShader = new Shader;
  59. fsqShader->Init("Res/texture.vs", "Res/texture.fs");
  60. fsqShader->SetTexture("U_Texture", fbo->GetBuffer("color"));
  61. }
  62. void Draw() {
  63. float frameTime = GetFrameTime();
  64. glClearColor(0.1f, 0.4f, 0.6f, 1.0f);
  65. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  66. fbo->Bind();
  67. skybox.Draw(viewMatrix, projectionMatrix, cameraPos.x, cameraPos.y, cameraPos.z);
  68. ground.Draw(viewMatrix, projectionMatrix);
  69. model.Draw(viewMatrix, projectionMatrix, cameraPos.x, cameraPos.y, cameraPos.z);
  70. niutou.Draw(viewMatrix, projectionMatrix, cameraPos.x, cameraPos.y, cameraPos.z);
  71. ps.Update(frameTime);
  72. ps.Draw(viewMatrix, projectionMatrix);
  73. fbo->Unbind();
  74. //sphere.Draw(viewMatrix, projectionMatrix, cameraPos.x, cameraPos.y, cameraPos.z);
  75. fsqVertex->Bind();
  76. fsqShader->Bind(glm::value_ptr(modelMatrix), glm::value_ptr(fsqViewMatrix), glm::value_ptr(projectionMatrix));
  77. glDrawArrays(GL_TRIANGLE_STRIP, 0, fsqVertex->mVertexCount);
  78. fsqVertex->Unbind();
  79. }