Ms.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "Ms.h"
  2. #include "Renderer.h"
  3. #include "App.h"
  4. #include "Scene.h"
  5. #include "Camera.h"
  6. #include "MeshNode.h"
  7. //======================================================================================================================
  8. // init =
  9. //======================================================================================================================
  10. void Ms::init(const RendererInitializer& initializer)
  11. {
  12. try
  13. {
  14. // create FBO
  15. fbo.create();
  16. fbo.bind();
  17. // inform in what buffers we draw
  18. fbo.setNumOfColorAttachements(3);
  19. // create the FAIs
  20. normalFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RG16F, GL_RG, GL_FLOAT);
  21. diffuseFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGB16F, GL_RGB, GL_FLOAT);
  22. specularFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGBA16F, GL_RGBA, GL_FLOAT);
  23. depthFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8);
  24. normalFai.setRepeat(false);
  25. diffuseFai.setRepeat(false);
  26. specularFai.setRepeat(false);
  27. depthFai.setRepeat(false);
  28. // attach the buffers to the FBO
  29. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, normalFai.getGlId(), 0);
  30. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, diffuseFai.getGlId(), 0);
  31. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, specularFai.getGlId(), 0);
  32. //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
  33. //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
  34. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
  35. // test if success
  36. fbo.checkIfGood();
  37. // unbind
  38. fbo.unbind();
  39. }
  40. catch(std::exception& e)
  41. {
  42. throw EXCEPTION("Cannot create deferred shading material stage FBO: " + e.what());
  43. }
  44. ez.init(initializer);
  45. }
  46. //======================================================================================================================
  47. // run =
  48. //======================================================================================================================
  49. void Ms::run()
  50. {
  51. const Camera& cam = r.getCamera();
  52. if(ez.isEnabled())
  53. {
  54. ez.run();
  55. }
  56. fbo.bind();
  57. if(!ez.isEnabled())
  58. {
  59. glClear(GL_DEPTH_BUFFER_BIT);
  60. }
  61. Renderer::setViewport(0, 0, r.getWidth(), r.getHeight());
  62. //glEnable(GL_DEPTH_TEST);
  63. //app->getScene().skybox.Render(cam.getViewMatrix().getRotationPart());
  64. //glDepthFunc(GL_LEQUAL);
  65. // if ez then change the default depth test and disable depth writing
  66. if(ez.isEnabled())
  67. {
  68. glDepthMask(false);
  69. glDepthFunc(GL_EQUAL);
  70. }
  71. // render the meshes
  72. for(Vec<MeshNode*>::iterator it=app->getScene().meshNodes.begin(); it!=app->getScene().meshNodes.end(); it++)
  73. {
  74. MeshNode* meshNode = (*it);
  75. if(meshNode->mesh->material.get() == NULL)
  76. {
  77. throw EXCEPTION("Mesh \"" + meshNode->mesh->getRsrcName() + "\" doesnt have material");
  78. }
  79. if(meshNode->mesh->material->blends)
  80. {
  81. continue;
  82. }
  83. r.setupMaterial(*meshNode->mesh->material, *meshNode, cam);
  84. meshNode->render();
  85. }
  86. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // the rendering above fucks the polygon mode
  87. // restore depth
  88. if(ez.isEnabled())
  89. {
  90. glDepthMask(true);
  91. glDepthFunc(GL_LESS);
  92. }
  93. fbo.unbind();
  94. }