Smo.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file
  3. *
  4. * Illumination stage stencil masking optimization passes
  5. */
  6. #include "Renderer.h"
  7. #include "Light.h"
  8. //======================================================================================================================
  9. // Statics =
  10. //======================================================================================================================
  11. float Renderer::Is::Smo::sMOUvSCoords[] = { -0.000000, 0.000000, -1.000000, 0.500000, 0.500000, -0.707107, 0.707107, 0.000000, -0.707107, 0.500000, 0.500000, 0.707107, 0.000000, 0.000000, 1.000000, 0.707107, 0.000000, 0.707107, -0.000000, 0.707107, 0.707107, 0.000000, 0.000000, 1.000000, 0.500000, 0.500000, 0.707107, -0.000000, 0.000000, -1.000000, -0.000000, 0.707107, -0.707107, 0.500000, 0.500000, -0.707107, -0.000000, 0.000000, -1.000000, -0.500000, 0.500000, -0.707107, -0.000000, 0.707107, -0.707107, -0.500000, 0.500000, 0.707107, 0.000000, 0.000000, 1.000000, -0.000000, 0.707107, 0.707107, -0.707107, -0.000000, 0.707107, 0.000000, 0.000000, 1.000000, -0.500000, 0.500000, 0.707107, -0.000000, 0.000000, -1.000000, -0.707107, -0.000000, -0.707107, -0.500000, 0.500000, -0.707107, -0.000000, 0.000000, -1.000000, -0.500000, -0.500000, -0.707107, -0.707107, -0.000000, -0.707107, -0.500000, -0.500000, 0.707107, 0.000000, 0.000000, 1.000000, -0.707107, -0.000000, 0.707107, 0.000000, -0.707107, 0.707107, 0.000000, 0.000000, 1.000000, -0.500000, -0.500000, 0.707107, -0.000000, 0.000000, -1.000000, 0.000000, -0.707107, -0.707107, -0.500000, -0.500000, -0.707107, -0.000000, 0.000000, -1.000000, 0.500000, -0.500000, -0.707107, 0.000000, -0.707107, -0.707107, 0.500000, -0.500000, 0.707107, 0.000000, 0.000000, 1.000000, 0.000000, -0.707107, 0.707107, 0.707107, 0.000000, 0.707107, 0.000000, 0.000000, 1.000000, 0.500000, -0.500000, 0.707107, -0.000000, 0.000000, -1.000000, 0.707107, 0.000000, -0.707107, 0.500000, -0.500000, -0.707107, 0.500000, -0.500000, -0.707107, 0.707107, 0.000000, -0.707107, 1.000000, 0.000000, -0.000000, 0.500000, -0.500000, -0.707107, 1.000000, 0.000000, -0.000000, 0.707107, -0.707107, 0.000000, 0.707107, -0.707107, 0.000000, 1.000000, 0.000000, -0.000000, 0.707107, 0.000000, 0.707107, 0.707107, -0.707107, 0.000000, 0.707107, 0.000000, 0.707107, 0.500000, -0.500000, 0.707107, 0.000000, -1.000000, 0.000000, 0.707107, -0.707107, 0.000000, 0.500000, -0.500000, 0.707107, 0.000000, -1.000000, 0.000000, 0.500000, -0.500000, 0.707107, 0.000000, -0.707107, 0.707107, 0.000000, -0.707107, -0.707107, 0.500000, -0.500000, -0.707107, 0.707107, -0.707107, 0.000000, 0.000000, -0.707107, -0.707107, 0.707107, -0.707107, 0.000000, 0.000000, -1.000000, 0.000000, -0.500000, -0.500000, -0.707107, 0.000000, -0.707107, -0.707107, -0.707107, -0.707107, 0.000000, 0.000000, -0.707107, -0.707107, 0.000000, -1.000000, 0.000000, -0.707107, -0.707107, 0.000000, -0.707107, -0.707107, 0.000000, 0.000000, -1.000000, 0.000000, 0.000000, -0.707107, 0.707107, -0.707107, -0.707107, 0.000000, 0.000000, -0.707107, 0.707107, -0.500000, -0.500000, 0.707107, -1.000000, -0.000000, 0.000000, -0.707107, -0.707107, 0.000000, -0.500000, -0.500000, 0.707107, -1.000000, -0.000000, 0.000000, -0.500000, -0.500000, 0.707107, -0.707107, -0.000000, 0.707107, -0.707107, -0.000000, -0.707107, -0.500000, -0.500000, -0.707107, -0.707107, -0.707107, 0.000000, -0.707107, -0.000000, -0.707107, -0.707107, -0.707107, 0.000000, -1.000000, -0.000000, 0.000000, -0.500000, 0.500000, -0.707107, -0.707107, -0.000000, -0.707107, -1.000000, -0.000000, 0.000000, -0.500000, 0.500000, -0.707107, -1.000000, -0.000000, 0.000000, -0.707107, 0.707107, 0.000000, -0.707107, 0.707107, 0.000000, -1.000000, -0.000000, 0.000000, -0.707107, -0.000000, 0.707107, -0.707107, 0.707107, 0.000000, -0.707107, -0.000000, 0.707107, -0.500000, 0.500000, 0.707107, -0.000000, 1.000000, 0.000000, -0.707107, 0.707107, 0.000000, -0.500000, 0.500000, 0.707107, -0.000000, 1.000000, 0.000000, -0.500000, 0.500000, 0.707107, -0.000000, 0.707107, 0.707107, -0.000000, 0.707107, -0.707107, -0.500000, 0.500000, -0.707107, -0.707107, 0.707107, 0.000000, -0.000000, 0.707107, -0.707107, -0.707107, 0.707107, 0.000000, -0.000000, 1.000000, 0.000000, 0.500000, 0.500000, -0.707107, -0.000000, 0.707107, -0.707107, -0.000000, 1.000000, 0.000000, 0.500000, 0.500000, -0.707107, -0.000000, 1.000000, 0.000000, 0.707107, 0.707107, 0.000000, 0.707107, 0.707107, 0.000000, -0.000000, 1.000000, 0.000000, -0.000000, 0.707107, 0.707107, 0.707107, 0.707107, 0.000000, -0.000000, 0.707107, 0.707107, 0.500000, 0.500000, 0.707107, 1.000000, 0.000000, -0.000000, 0.707107, 0.707107, 0.000000, 0.500000, 0.500000, 0.707107, 1.000000, 0.000000, -0.000000, 0.500000, 0.500000, 0.707107, 0.707107, 0.000000, 0.707107, 0.707107, 0.000000, -0.707107, 0.500000, 0.500000, -0.707107, 0.707107, 0.707107, 0.000000, 0.707107, 0.000000, -0.707107, 0.707107, 0.707107, 0.000000, 1.000000, 0.000000, -0.000000 };
  12. Vbo Renderer::Is::Smo::sMOUvSVbo;
  13. Renderer::Is::Smo::SmoShaderProg Renderer::Is::Smo::sProg;
  14. //======================================================================================================================
  15. // init =
  16. //======================================================================================================================
  17. void Renderer::Is::Smo::init()
  18. {
  19. if(sMOUvSVbo.isCreated()) return;
  20. sMOUvSVbo.create(GL_ARRAY_BUFFER, sizeof(sMOUvSCoords), sMOUvSCoords, GL_STATIC_DRAW);
  21. sProg.customLoad("shaders/IsSmo.glsl");
  22. sProg.uniVars.modelViewProjectionMat = sProg.findUniVar("modelViewProjectionMat");
  23. }
  24. //======================================================================================================================
  25. // run [PointLight] =
  26. //======================================================================================================================
  27. void Renderer::Is::Smo::run(const PointLight& light)
  28. {
  29. // set GL
  30. glClear(GL_STENCIL_BUFFER_BIT);
  31. glStencilFunc(GL_ALWAYS, 0x1, 0x1);
  32. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  33. glColorMask(false, false, false, false);
  34. glDisable(GL_CULL_FACE);
  35. // set shared prog
  36. const float scale = 1.2; // we scale the sphere a little
  37. sProg.bind();
  38. Mat4 modelMat = Mat4(light.getWorldTransform().getOrigin(), Mat3::getIdentity(), light.radius*scale);
  39. Mat4 trf = r.cam->getProjectionMatrix() * Mat4::combineTransformations(r.cam->getViewMatrix(), modelMat);
  40. sProg.uniVars.modelViewProjectionMat->setMat4(&trf);
  41. // render sphere to the stencil buffer
  42. sMOUvSVbo.bind();
  43. const int loc = 0;
  44. glEnableVertexAttribArray(loc);
  45. glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  46. glDrawArrays(GL_TRIANGLES, 0, sizeof(sMOUvSCoords)/sizeof(float)/3);
  47. glDisableVertexAttribArray(loc);
  48. sMOUvSVbo.unbind();
  49. // restore GL
  50. glEnable(GL_CULL_FACE);
  51. glColorMask(true, true, true, true);
  52. glStencilFunc(GL_EQUAL, 0x1, 0x1);
  53. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  54. }
  55. //======================================================================================================================
  56. // run [SpotLight] =
  57. //======================================================================================================================
  58. void Renderer::Is::Smo::run(const SpotLight& light)
  59. {
  60. // set GL state
  61. glStencilFunc(GL_ALWAYS, 0x1, 0x1);
  62. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  63. glClear(GL_STENCIL_BUFFER_BIT);
  64. glColorMask(false, false, false, false);
  65. glDisable(GL_CULL_FACE);
  66. // calc camera shape
  67. const Camera& lcam = light.camera;
  68. float x = lcam.getZFar() / tan((PI-lcam.getFovX())/2);
  69. float y = tan(lcam.getFovY()/2) * lcam.getZFar();
  70. float z = -lcam.getZFar();
  71. const int trisNum = 6;
  72. float verts[trisNum][3][3] = {
  73. { { 0.0, 0.0, 0.0 }, { x, -y, z }, { x, y, z } }, // right triangle
  74. { { 0.0, 0.0, 0.0 }, { x, y, z }, {-x, y, z } }, // top
  75. { { 0.0, 0.0, 0.0 }, {-x, y, z }, {-x, -y, z } }, // left
  76. { { 0.0, 0.0, 0.0 }, {-x, -y, z }, { x, -y, z } }, // bottom
  77. { { x, -y, z }, {-x, y, z }, { x, y, z } }, // front up right
  78. { { x, -y, z }, {-x, -y, z }, {-x, y, z } }, // front bottom left
  79. };
  80. // shader prog
  81. sProg.bind();
  82. Mat4 modelMat = Mat4(lcam.getWorldTransform());
  83. Mat4 trf = r.cam->getProjectionMatrix() * Mat4::combineTransformations(r.cam->getViewMatrix(), modelMat);
  84. sProg.uniVars.modelViewProjectionMat->setMat4(&trf);
  85. // render camera shape to stencil buffer
  86. const int loc = 0;
  87. glEnableVertexAttribArray(loc);
  88. glVertexAttribPointer(loc, 3, GL_FLOAT, false, 0, verts);
  89. glDrawArrays(GL_TRIANGLES, 0, trisNum*3);
  90. glDisableVertexAttribArray(loc);
  91. // restore GL state
  92. glEnable(GL_CULL_FACE);
  93. glColorMask(true, true, true, true);
  94. glStencilFunc(GL_EQUAL, 0x1, 0x1);
  95. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  96. }