Ms.cpp 3.6 KB

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