r_bs.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * The file contains functions and vars used for the deferred shading blending stage stage.
  3. * The blending stage comes after the illumination stage. All the objects that are transculent will be drawn here.
  4. */
  5. #include "renderer.h"
  6. #include "camera.h"
  7. #include "scene.h"
  8. #include "mesh.h"
  9. #include "r_private.h"
  10. #include "resource.h"
  11. #include "fbo.h"
  12. #include "mesh_node.h"
  13. #include "material.h"
  14. namespace r {
  15. namespace bs {
  16. //=====================================================================================================================================
  17. // VARS =
  18. //=====================================================================================================================================
  19. static fbo_t b_fbo; ///< blending models FBO
  20. static fbo_t r_fbo; ///< refracting models FBO
  21. /*static*/ texture_t r_fai; ///< RGB for color and A for mask (0 doesnt pass, 1 pass)
  22. static shader_prog_t* r2b_shdr;
  23. //=====================================================================================================================================
  24. // InitB =
  25. //=====================================================================================================================================
  26. static void InitB()
  27. {
  28. // create FBO
  29. b_fbo.Create();
  30. b_fbo.Bind();
  31. // inform FBO about the color buffers
  32. b_fbo.SetNumOfColorAttachements(1);
  33. // attach the texes
  34. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r::is::fai.GetGLID(), 0 );
  35. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, r::ms::depth_fai.GetGLID(), 0 );
  36. // test if success
  37. if( !b_fbo.IsGood() )
  38. FATAL( "Cannot create deferred shading blending stage FBO" );
  39. // unbind
  40. b_fbo.Unbind();
  41. }
  42. //=====================================================================================================================================
  43. // InitR =
  44. //=====================================================================================================================================
  45. static void InitR()
  46. {
  47. // create FBO
  48. r_fbo.Create();
  49. r_fbo.Bind();
  50. // inform FBO about the color buffers
  51. r_fbo.SetNumOfColorAttachements(1);
  52. // texture
  53. r_fai.CreateEmpty2D( r::w * r::rendering_quality, r::h * r::rendering_quality, GL_RGBA, GL_RGBA );
  54. // attach the texes
  55. glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, r_fai.GetGLID(), 0 );
  56. glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, r::ms::depth_fai.GetGLID(), 0 );
  57. // test if success
  58. if( !r_fbo.IsGood() )
  59. FATAL( "Cannot create deferred shading blending stage FBO" );
  60. // unbind
  61. r_fbo.Unbind();
  62. r2b_shdr = rsrc::shaders.Load( "shaders/bs_refract.glsl" );
  63. }
  64. //=====================================================================================================================================
  65. // Init =
  66. //=====================================================================================================================================
  67. void Init()
  68. {
  69. InitB();
  70. InitR();
  71. }
  72. //=====================================================================================================================================
  73. // RunStage =
  74. //=====================================================================================================================================
  75. void RunStage( const camera_t& cam )
  76. {
  77. // OGL stuff
  78. r::SetProjectionViewMatrices( cam );
  79. r::SetViewport( 0, 0, r::w*r::rendering_quality, r::h*r::rendering_quality );
  80. glEnable( GL_DEPTH_TEST );
  81. glDepthMask( false );
  82. // render the meshes
  83. for( uint i=0; i<scene::mesh_nodes.size(); i++ )
  84. {
  85. mesh_node_t* mesh_node = scene::mesh_nodes[i];
  86. if( mesh_node->material->refracts )
  87. {
  88. // write to the rFbo
  89. r_fbo.Bind();
  90. glClear( GL_COLOR_BUFFER_BIT );
  91. mesh_node->material->Setup();
  92. mesh_node->Render();
  93. b_fbo.Bind();
  94. glDisable( GL_DEPTH_TEST );
  95. r2b_shdr->Bind();
  96. r2b_shdr->LocTexUnit( r2b_shdr->GetUniformLocation(0), r_fai, 0 );
  97. r::DrawQuad( r2b_shdr->GetAttributeLocation(0) );
  98. }
  99. else if( mesh_node->material->blends )
  100. {
  101. b_fbo.Bind();
  102. mesh_node->material->Setup();
  103. mesh_node->Render();
  104. }
  105. }
  106. // restore a few things
  107. glDepthMask( true );
  108. fbo_t::Unbind();
  109. }
  110. }} // end namespaces