r_bs.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. =======================================================================================================================================
  18. VARS =
  19. =======================================================================================================================================
  20. */
  21. static fbo_t b_fbo; ///< blending models FBO
  22. static fbo_t r_fbo; ///< refracting models FBO
  23. static texture_t r_fai; ///< RGB for color and A for mask (0 doesnt pass, 1 pass)
  24. static shader_prog_t* r2b_shdr;
  25. /*
  26. =======================================================================================================================================
  27. InitBFBO =
  28. =======================================================================================================================================
  29. */
  30. static void InitB()
  31. {
  32. // create FBO
  33. b_fbo.Create();
  34. b_fbo.Bind();
  35. // inform FBO about the color buffers
  36. b_fbo.SetNumOfColorAttachements(1);
  37. // attach the texes
  38. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r::is::fai.GetGLID(), 0 );
  39. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, r::ms::depth_fai.GetGLID(), 0 );
  40. // test if success
  41. if( !b_fbo.IsGood() )
  42. FATAL( "Cannot create deferred shading blending stage FBO" );
  43. // unbind
  44. b_fbo.Unbind();
  45. }
  46. /*
  47. =======================================================================================================================================
  48. InitRFBO =
  49. =======================================================================================================================================
  50. */
  51. static void InitR()
  52. {
  53. // create FBO
  54. r_fbo.Create();
  55. r_fbo.Bind();
  56. // inform FBO about the color buffers
  57. r_fbo.SetNumOfColorAttachements(1);
  58. // texture
  59. r_fai.CreateEmpty2D( r::w * r::rendering_quality, r::h * r::rendering_quality, GL_RGBA, GL_RGBA );
  60. // attach the texes
  61. glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, r_fai.GetGLID(), 0 );
  62. glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, r::ms::depth_fai.GetGLID(), 0 );
  63. // test if success
  64. if( !r_fbo.IsGood() )
  65. FATAL( "Cannot create deferred shading blending stage FBO" );
  66. // unbind
  67. r_fbo.Unbind();
  68. //r2b_shdr =
  69. }
  70. //=====================================================================================================================================
  71. // Init =
  72. //=====================================================================================================================================
  73. void Init()
  74. {
  75. InitB();
  76. InitR();
  77. }
  78. //=====================================================================================================================================
  79. // RunStage =
  80. //=====================================================================================================================================
  81. void RunStage( const camera_t& cam )
  82. {
  83. // OGL stuff
  84. r::SetProjectionViewMatrices( cam );
  85. r::SetViewport( 0, 0, r::w*r::rendering_quality, r::h*r::rendering_quality );
  86. glEnable( GL_DEPTH_TEST );
  87. glDepthMask( false );
  88. // render the meshes
  89. for( uint i=0; i<scene::mesh_nodes.size(); i++ )
  90. {
  91. mesh_node_t* mesh_node = scene::mesh_nodes[i];
  92. if( mesh_node->material->blends )
  93. {
  94. b_fbo.Bind();
  95. mesh_node->material->Setup();
  96. mesh_node->Render();
  97. }
  98. else if( mesh_node->material->refracts )
  99. {
  100. // write to the rFbo
  101. r_fbo.Bind();
  102. glClear( GL_COLOR_BUFFER_BIT );
  103. mesh_node->material->Setup();
  104. mesh_node->Render();
  105. b_fbo.Bind();
  106. }
  107. }
  108. // render the smodels
  109. /*for( uint i=0; i<scene::models.size(); i++ )
  110. Render<model_t, true>( scene::models[i] );*/
  111. // restore a few things
  112. glDepthMask( true );
  113. fbo_t::Unbind();
  114. }
  115. }} // end namespaces