Ms.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * The file contains functions and vars used for the deferred shading material stage.
  3. */
  4. #include "Renderer.h"
  5. #include "Camera.h"
  6. #include "Scene.h"
  7. #include "Mesh.h"
  8. #include "Fbo.h"
  9. #include "Material.h"
  10. #include "MeshNode.h"
  11. namespace R {
  12. namespace Ms {
  13. /*
  14. =======================================================================================================================================
  15. VARS =
  16. =======================================================================================================================================
  17. */
  18. static Fbo fbo;
  19. Texture normalFai, diffuseFai, specularFai, depthFai;
  20. //=====================================================================================================================================
  21. // init =
  22. //=====================================================================================================================================
  23. void init()
  24. {
  25. // create FBO
  26. fbo.create();
  27. fbo.bind();
  28. // inform in what buffers we draw
  29. fbo.setNumOfColorAttachements(3);
  30. // create the FAIs
  31. const int internal_format = GL_RGBA16F_ARB;
  32. if( !normalFai.createEmpty2D( R::w, R::h, internal_format, GL_RGBA ) ||
  33. !diffuseFai.createEmpty2D( R::w, R::h, internal_format, GL_RGBA ) ||
  34. !specularFai.createEmpty2D( R::w, R::h, internal_format, GL_RGBA ) ||
  35. !depthFai.createEmpty2D( R::w, R::h, GL_DEPTH24_STENCIL8_EXT, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT ) )
  36. {
  37. FATAL( "See prev error" );
  38. }
  39. // you could use the above for SSAO but the difference is very little.
  40. //depthFai.texParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  41. //depthFai.texParameter( GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  42. // attach the buffers to the FBO
  43. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, normalFai.getGlId(), 0 );
  44. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, diffuseFai.getGlId(), 0 );
  45. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, specularFai.getGlId(), 0 );
  46. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthFai.getGlId(), 0 );
  47. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, depthFai.getGlId(), 0 );
  48. // test if success
  49. if( !fbo.isGood() )
  50. FATAL( "Cannot create deferred shading material pass FBO" );
  51. // unbind
  52. fbo.unbind();
  53. #if defined( _EARLY_Z_ )
  54. R::Ms::earlyz::init();
  55. #endif
  56. }
  57. //=====================================================================================================================================
  58. // runStage =
  59. //=====================================================================================================================================
  60. void runStage( const Camera& cam )
  61. {
  62. #if defined( _EARLY_Z_ )
  63. // run the early z pass
  64. R::Ms::earlyz::runPass( cam );
  65. #endif
  66. fbo.bind();
  67. #if !defined( _EARLY_Z_ )
  68. glClear( GL_DEPTH_BUFFER_BIT );
  69. #endif
  70. R::setProjectionViewMatrices( cam );
  71. R::setViewport( 0, 0, R::w, R::h );
  72. //glEnable( GL_DEPTH_TEST );
  73. Scene::skybox.Render( cam.getViewMatrix().getRotationPart() );
  74. //glDepthFunc( GL_LEQUAL );
  75. #if defined( _EARLY_Z_ )
  76. glDepthMask( false );
  77. glDepthFunc( GL_EQUAL );
  78. #endif
  79. // render the meshes
  80. for( uint i=0; i<Scene::meshNodes.size(); i++ )
  81. {
  82. MeshNode* mesh_node = Scene::meshNodes[i];
  83. DEBUG_ERR( mesh_node->material == NULL );
  84. if( mesh_node->material->blends || mesh_node->material->refracts ) continue;
  85. mesh_node->material->setup();
  86. mesh_node->render();
  87. }
  88. glPolygonMode( GL_FRONT, GL_FILL ); // the rendering above fucks the polygon mode
  89. #if defined( _EARLY_Z_ )
  90. glDepthMask( true );
  91. glDepthFunc( GL_LESS );
  92. #endif
  93. fbo.unbind();
  94. }
  95. }} // end namespaces