IsShadows.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "Renderer.h"
  2. #include "Texture.h"
  3. #include "Scene.h"
  4. #include "Resource.h"
  5. #include "Fbo.h"
  6. #include "Material.h"
  7. #include "MeshNode.h"
  8. namespace R {
  9. namespace Is {
  10. namespace Shad {
  11. /*
  12. =======================================================================================================================================
  13. DATA VARS =
  14. =======================================================================================================================================
  15. */
  16. bool pcf = true;
  17. bool bilinear = true;
  18. static Fbo fbo;
  19. // exportable vars
  20. int shadowResolution = 512;
  21. Texture shadowMap;
  22. /*
  23. =======================================================================================================================================
  24. init =
  25. =======================================================================================================================================
  26. */
  27. void init()
  28. {
  29. // create FBO
  30. fbo.create();
  31. fbo.bind();
  32. // texture
  33. shadowMap.createEmpty2D( shadowResolution, shadowResolution, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT );
  34. shadowMap.texParameter( GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  35. if( bilinear ) shadowMap.texParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  36. else shadowMap.texParameter( GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  37. shadowMap.texParameter( GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE );
  38. shadowMap.texParameter( GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
  39. /// If you dont want to use the FFP for comparing the shadwomap (the above two lines) then you can make the comparision inside the
  40. /// glsl shader. The GL_LEQUAL means that: shadow = ( R <= Dt ) ? 1.0 : 0.0; . The R is given by: R = _tex_coord2.z/_tex_coord2.w;
  41. /// and the Dt = shadow2D(shadow_depth_map, _shadow_uv ).r (see lp_generic.frag). Hardware filters like GL_LINEAR cannot be applied.
  42. // inform the we wont write to color buffers
  43. fbo.setNumOfColorAttachements(0);
  44. // attach the texture
  45. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadowMap.getGlId(), 0 );
  46. // test if success
  47. if( !fbo.isGood() )
  48. FATAL( "Cannot create shadow FBO" );
  49. // unbind
  50. fbo.unbind();
  51. }
  52. /*
  53. =======================================================================================================================================
  54. runPass =
  55. render Scene only with depth and store the result in the shadow map =
  56. =======================================================================================================================================
  57. */
  58. void runPass( const Camera& cam )
  59. {
  60. // FBO
  61. fbo.bind();
  62. // matrix
  63. glMatrixMode( GL_PROJECTION );
  64. glPushMatrix();
  65. glMatrixMode( GL_MODELVIEW );
  66. glPushMatrix();
  67. glPushAttrib( GL_VIEWPORT_BIT );
  68. glClear( GL_DEPTH_BUFFER_BIT );
  69. R::setProjectionViewMatrices( cam );
  70. R::setViewport( 0, 0, shadowResolution, shadowResolution );
  71. // disable color & blend & enable depth test
  72. glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
  73. glEnable( GL_DEPTH_TEST );
  74. glDisable( GL_BLEND );
  75. // for artifacts
  76. glPolygonOffset( 2.0, 2.0 ); // keep the values as low as possible!!!!
  77. glEnable( GL_POLYGON_OFFSET_FILL );
  78. // render all meshes
  79. for( uint i=0; i<Scene::meshNodes.size(); i++ )
  80. {
  81. MeshNode* mesh_node = Scene::meshNodes[i];
  82. if( mesh_node->material->blends || mesh_node->material->refracts ) continue;
  83. DEBUG_ERR( mesh_node->material->dpMtl == NULL );
  84. //meshNode->material->dpMtl->setup();
  85. //meshNode->renderDepth();
  86. mesh_node->material->setup();
  87. mesh_node->render();
  88. }
  89. glDisable( GL_POLYGON_OFFSET_FILL );
  90. glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  91. glPopAttrib();
  92. glMatrixMode( GL_MODELVIEW );
  93. glPopMatrix();
  94. glMatrixMode( GL_PROJECTION );
  95. glPopMatrix();
  96. // FBO
  97. fbo.unbind();
  98. }
  99. }}} // end namespaces