Ms.cpp 3.8 KB

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