Sm.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "Sm.h"
  2. #include "Renderer.h"
  3. #include "App.h"
  4. #include "Scene.h"
  5. #include "MeshNode.h"
  6. #include "LightProps.h"
  7. #include "Camera.h"
  8. #include "RendererInitializer.h"
  9. //======================================================================================================================
  10. // init =
  11. //======================================================================================================================
  12. void Sm::init(const RendererInitializer& initializer)
  13. {
  14. enabled = initializer.is.sm.enabled;
  15. if(!enabled)
  16. return;
  17. pcfEnabled = initializer.is.sm.pcfEnabled;
  18. bilinearEnabled = initializer.is.sm.bilinearEnabled;
  19. resolution = initializer.is.sm.resolution;
  20. // create FBO
  21. fbo.create();
  22. fbo.bind();
  23. // texture
  24. shadowMap.createEmpty2D(resolution, resolution, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT);
  25. shadowMap.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  26. if(bilinearEnabled)
  27. {
  28. shadowMap.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  29. }
  30. else
  31. {
  32. shadowMap.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  33. }
  34. shadowMap.setTexParameter(GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  35. shadowMap.setTexParameter(GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  36. /*
  37. * If you dont want to use the FFP for comparing the shadowmap (the above two lines) then you can make the comparison
  38. * inside the glsl shader. The GL_LEQUAL means that: shadow = (R <= Dt) ? 1.0 : 0.0; . The R is given by:
  39. * R = _tex_coord2.z/_tex_coord2.w; and the Dt = shadow2D(shadow_depth_map, _shadow_uv).r (see lp_generic.frag).
  40. * Hardware filters like GL_LINEAR cannot be applied.
  41. */
  42. // inform the we wont write to color buffers
  43. fbo.setNumOfColorAttachements(0);
  44. // attach the texture
  45. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowMap.getGlId(), 0);
  46. // test if success
  47. if(!fbo.isGood())
  48. FATAL("Cannot create shadowmapping FBO");
  49. // unbind
  50. fbo.unbind();
  51. }
  52. //======================================================================================================================
  53. // run =
  54. //======================================================================================================================
  55. void Sm::run(const Camera& cam)
  56. {
  57. DEBUG_ERR(!enabled);
  58. // FBO
  59. fbo.bind();
  60. // set GL
  61. Renderer::setViewport(0, 0, resolution, resolution);
  62. glClear(GL_DEPTH_BUFFER_BIT);
  63. // disable color & blend & enable depth test
  64. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  65. glEnable(GL_DEPTH_TEST);
  66. glDisable(GL_BLEND);
  67. // for artifacts
  68. glPolygonOffset(2.0, 2.0); // keep the values as low as possible!!!!
  69. glEnable(GL_POLYGON_OFFSET_FILL);
  70. // render all meshes
  71. for(Vec<MeshNode*>::iterator it=app->getScene().meshNodes.begin(); it!=app->getScene().meshNodes.end(); it++)
  72. {
  73. MeshNode* meshNode = (*it);
  74. if(meshNode->mesh->material->blends)
  75. continue;
  76. DEBUG_ERR(meshNode->mesh->material->dpMtl.get() == NULL);
  77. r.setupMaterial(*meshNode->mesh->material->dpMtl, *meshNode, cam);
  78. meshNode->renderDepth();
  79. }
  80. // restore GL
  81. glDisable(GL_POLYGON_OFFSET_FILL);
  82. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  83. // FBO
  84. fbo.unbind();
  85. }