Bs.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <boost/ptr_container/ptr_vector.hpp>
  2. #include "Bs.h"
  3. #include "Renderer.h"
  4. #include "App.h"
  5. #include "Scene.h"
  6. #include "ShaderProg.h"
  7. #include "Model.h"
  8. #include "ModelNode.h"
  9. #include "Material.h"
  10. #include "Mesh.h"
  11. //======================================================================================================================
  12. // createFbo =
  13. //======================================================================================================================
  14. void Bs::createFbo()
  15. {
  16. try
  17. {
  18. fbo.create();
  19. fbo.bind();
  20. fbo.setNumOfColorAttachements(1);
  21. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  22. r.getPps().getPrePassFai().getGlId(), 0);
  23. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
  24. r.getMs().getDepthFai().getGlId(), 0);
  25. fbo.checkIfGood();
  26. fbo.unbind();
  27. }
  28. catch(std::exception& e)
  29. {
  30. throw EXCEPTION("Failed to create blending stage FBO");
  31. }
  32. }
  33. //======================================================================================================================
  34. // createRefractFbo =
  35. //======================================================================================================================
  36. void Bs::createRefractFbo()
  37. {
  38. try
  39. {
  40. refractFbo.create();
  41. refractFbo.bind();
  42. refractFbo.setNumOfColorAttachements(1);
  43. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, refractFai.getGlId(), 0);
  44. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
  45. r.getMs().getDepthFai().getGlId(), 0);
  46. refractFbo.checkIfGood();
  47. refractFbo.unbind();
  48. }
  49. catch(std::exception& e)
  50. {
  51. throw EXCEPTION("Failed to create blending stage refract FBO");
  52. }
  53. }
  54. //======================================================================================================================
  55. // init =
  56. //======================================================================================================================
  57. void Bs::init(const RendererInitializer& /*initializer*/)
  58. {
  59. createFbo();
  60. refractFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGBA8, GL_RGBA, GL_FLOAT);
  61. createRefractFbo();
  62. refractSProg.loadRsrc("shaders/BsRefract.glsl");
  63. }
  64. //======================================================================================================================
  65. // run =
  66. //======================================================================================================================
  67. void Bs::run()
  68. {
  69. Renderer::setViewport(0, 0, r.getWidth(), r.getHeight());
  70. glDepthMask(false);
  71. // render the models
  72. Vec<ModelNode*>::const_iterator it = AppSingleton::getInstance().getScene().modelNodes.begin();
  73. for(; it != AppSingleton::getInstance().getScene().modelNodes.end(); ++it)
  74. {
  75. const ModelNode& mn = *(*it);
  76. boost::ptr_vector<ModelNodePatch>::const_iterator it = mn.getModelNodePatches().begin();
  77. for(; it != mn.getModelNodePatches().end(); it++)
  78. {
  79. const ModelNodePatch& sm = *it;
  80. if(!sm.getCpMtl().renderInBlendingStage())
  81. {
  82. continue;
  83. }
  84. // refracts ?
  85. if(sm.getCpMtl().getStdUniVar(Material::SUV_PPS_PRE_PASS_FAI))
  86. {
  87. //
  88. // Stage 0: Render to the temp FAI
  89. //
  90. refractFbo.bind();
  91. glEnable(GL_STENCIL_TEST);
  92. glClear(GL_STENCIL_BUFFER_BIT);
  93. glStencilFunc(GL_ALWAYS, 0x1, 0x1);
  94. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  95. r.setupShaderProg(sm.getCpMtl(), mn, r.getCamera());
  96. glDisable(GL_BLEND); // a hack
  97. sm.getCpVao().bind();
  98. glDrawElements(GL_TRIANGLES, sm.getModelPatchRsrc().getMesh().getVertIdsNum(), GL_UNSIGNED_SHORT, 0);
  99. sm.getCpVao().unbind();
  100. //
  101. // Stage 1: Render the temp FAI to prePassFai
  102. //
  103. fbo.bind();
  104. glStencilFunc(GL_EQUAL, 0x1, 0x1);
  105. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  106. if(sm.getCpMtl().isBlendingEnabled())
  107. {
  108. glEnable(GL_BLEND);
  109. glBlendFunc(sm.getCpMtl().getBlendingSfactor(), sm.getCpMtl().getBlendingDfactor());
  110. }
  111. else
  112. {
  113. glDisable(GL_BLEND);
  114. }
  115. refractSProg->bind();
  116. refractSProg->findUniVar("fai")->setTexture(refractFai, 0);
  117. r.drawQuad();
  118. // cleanup
  119. glStencilFunc(GL_ALWAYS, 0x1, 0x1);
  120. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  121. glClear(GL_STENCIL_BUFFER_BIT);
  122. glDisable(GL_STENCIL_TEST);
  123. }
  124. // no rafraction
  125. else
  126. {
  127. r.setupShaderProg(sm.getCpMtl(), mn, r.getCamera());
  128. sm.getCpVao().bind();
  129. glDrawElements(GL_TRIANGLES, sm.getModelPatchRsrc().getMesh().getVertIdsNum(), GL_UNSIGNED_SHORT, 0);
  130. sm.getCpVao().unbind();
  131. }
  132. } // end for all subModels
  133. } // end for all modelNodes
  134. glDepthMask(true);
  135. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // the rendering above fucks the polygon mode
  136. Fbo::unbind();
  137. }