Ms.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <boost/foreach.hpp>
  2. #include "Ms.h"
  3. #include "Renderer.h"
  4. #include "Camera.h"
  5. #include "Ez.h"
  6. #include "RenderableNode.h"
  7. //======================================================================================================================
  8. // Constructor =
  9. //======================================================================================================================
  10. Ms::Ms(Renderer& r_):
  11. RenderingPass(r_),
  12. ez(r_)
  13. {}
  14. //======================================================================================================================
  15. // init =
  16. //======================================================================================================================
  17. void Ms::init(const RendererInitializer& initializer)
  18. {
  19. try
  20. {
  21. // create FBO
  22. fbo.create();
  23. fbo.bind();
  24. // inform in what buffers we draw
  25. fbo.setNumOfColorAttachements(3);
  26. // create the FAIs
  27. normalFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RG16F, GL_RG, GL_FLOAT);
  28. diffuseFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGB16F, GL_RGB, GL_FLOAT);
  29. specularFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGBA16F, GL_RGBA, GL_FLOAT);
  30. depthFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8);
  31. normalFai.setRepeat(false);
  32. diffuseFai.setRepeat(false);
  33. specularFai.setRepeat(false);
  34. depthFai.setRepeat(false);
  35. // attach the buffers to the FBO
  36. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, normalFai.getGlId(), 0);
  37. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, diffuseFai.getGlId(), 0);
  38. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, specularFai.getGlId(), 0);
  39. //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
  40. //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
  41. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
  42. // test if success
  43. fbo.checkIfGood();
  44. // unbind
  45. fbo.unbind();
  46. }
  47. catch(std::exception& e)
  48. {
  49. throw EXCEPTION("Cannot create deferred shading material stage FBO: " + e.what());
  50. }
  51. ez.init(initializer);
  52. }
  53. //======================================================================================================================
  54. // run =
  55. //======================================================================================================================
  56. void Ms::run()
  57. {
  58. if(ez.isEnabled())
  59. {
  60. ez.run();
  61. }
  62. fbo.bind();
  63. if(!ez.isEnabled())
  64. {
  65. glClear(GL_DEPTH_BUFFER_BIT);
  66. }
  67. Renderer::setViewport(0, 0, r.getWidth(), r.getHeight());
  68. //glEnable(GL_DEPTH_TEST);
  69. //app->getScene().skybox.Render(cam.getViewMatrix().getRotationPart());
  70. //glDepthFunc(GL_LEQUAL);
  71. // if ez then change the default depth test and disable depth writing
  72. if(ez.isEnabled())
  73. {
  74. glDepthMask(false);
  75. glDepthFunc(GL_EQUAL);
  76. }
  77. glClear(GL_COLOR_BUFFER_BIT);
  78. // render all
  79. BOOST_FOREACH(const RenderableNode* node, r.getCamera().getVisibleMsRenderableNodes())
  80. {
  81. r.getSceneDrawer().renderRenderableNode(*node, r.getCamera(), SceneDrawer::RPT_COLOR);
  82. }
  83. // restore depth
  84. if(ez.isEnabled())
  85. {
  86. glDepthMask(true);
  87. glDepthFunc(GL_LESS);
  88. }
  89. fbo.unbind();
  90. ON_GL_FAIL_THROW_EXCEPTION();
  91. }