r_is.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. #include "renderer.hpp"
  3. #include "Light.h"
  4. #include "Scene.h"
  5. float renderer_t::illumination_stage_t::point_light_pass_t::smo_uvs_coords [] = { -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 };
  6. uint renderer_t::illumination_stage_t::point_light_pass_t::smo_uvs_vbo_id = NULL; // stencil masking opt uv sphere vertex buffer object id
  7. //=====================================================================================================================================
  8. // Stencil Masking Opt Uv Sphere =
  9. //=====================================================================================================================================
  10. // init stencil masking optimization UV sphere
  11. void renderer_t::illumination_stage_t::point_light_pass_t::InitSMOUVS()
  12. {
  13. glGenBuffers( 1, &smo_uvs_vbo_id );
  14. glBindBuffer( GL_ARRAY_BUFFER, smo_uvs_vbo_id );
  15. glBufferData( GL_ARRAY_BUFFER, sizeof(smo_uvs_coords), smo_uvs_coords, GL_STATIC_DRAW );
  16. glBindBuffer( GL_ARRAY_BUFFER, 0 );
  17. }
  18. void renderer_t::illumination_stage_t::point_light_pass_t::DrawSMOUVS( const PointLight& light )
  19. {
  20. const float scale = 1.2;
  21. renderer.matrices.model = Mat4( light.translationWspace, Mat3::getIdentity(), light.radius*scale );
  22. shader_progs.smouvs.bind();
  23. renderer.UpdateMatrices();
  24. glBindBuffer( GL_ARRAY_BUFFER, smo_uvs_vbo_id );
  25. glEnableClientState( GL_VERTEX_ARRAY );
  26. glVertexPointer( 3, GL_FLOAT, 0, 0 );
  27. glDrawArrays( GL_TRIANGLES, 0, sizeof(smo_uvs_coords)/sizeof(float)/3 );
  28. glDisableClientState( GL_VERTEX_ARRAY );
  29. glBindBuffer( GL_ARRAY_BUFFER, 0 );
  30. }
  31. //=====================================================================================================================================
  32. // CalcViewVector =
  33. //=====================================================================================================================================
  34. /// calc the view vector that we will use inside the shader to calculate the frag pos in view space
  35. void renderer_t::illumination_stage_t::CalcViewVector()
  36. {
  37. int _w = renderer.width;
  38. int _h = renderer.height;
  39. int pixels[4][2]={ {_w,_h}, {0,_h}, { 0,0 }, {_w,0} }; // from righ up and CC wise to right down, Just like we render the quad
  40. int viewport[4]={ 0, 0, _w, _h };
  41. for( int i=0; i<4; i++ )
  42. {
  43. Original Code:
  44. R::UnProject( pixels[i][0], pixels[i][1], 10, cam.getViewMatrix(), cam.getProjectionMatrix(), viewport,
  45. view_vectors[i].x, view_vectors[i].y, view_vectors[i].z );
  46. view_vectors[i] = cam.getViewMatrix() * view_vectors[i];
  47. The original code is the above 3 lines. The optimized follows:
  48. Vec3 vec;
  49. vec.x = (2.0*(pixels[i][0]-viewport[0]))/viewport[2] - 1.0;
  50. vec.y = (2.0*(pixels[i][1]-viewport[1]))/viewport[3] - 1.0;
  51. vec.z = 1.0;
  52. view_vectors[i] = vec.getTransformed( renderer.camera->getInvProjectionMatrix() );
  53. // end of optimized code
  54. }
  55. }
  56. //=====================================================================================================================================
  57. // CalcPlanes =
  58. //=====================================================================================================================================
  59. /// calc the planes that we will use inside the shader to calculate the frag pos in view space
  60. void renderer_t::illumination_stage_t::CalcPlanes()
  61. {
  62. const float& zFar = renderer.camera->getZFar();
  63. const float& zNear = renderer.camera->getZNear();
  64. planes.x = -zFar / (zFar - zNear);
  65. planes.y = -zFar * zNear / (zFar - zNear);
  66. }
  67. //=====================================================================================================================================
  68. // init ambient pass =
  69. //=====================================================================================================================================
  70. void renderer_t::illumination_stage_t::ambient_pass_t::init()
  71. {
  72. shaderProg.customLoad( "shaders/is_ap.glsl", "" );
  73. }
  74. //=====================================================================================================================================
  75. // Run ambient pass =
  76. //=====================================================================================================================================
  77. void renderer_t::illumination_stage_t::ambient_pass_t::Run() const
  78. {
  79. glDisable( GL_BLEND );
  80. // set the shader
  81. shaderProg.bind();
  82. // set the uniforms
  83. glUniform3fv( shaderProg.getUniLoc(0), 1, &((Vec3)Scene::getAmbientColor())[0] );
  84. shaderProg.locTexUnit( shaderProg.getUniLoc(1), renderer.ms.fais.diffuse, 0 );
  85. // Draw quad
  86. renderer.DrawQuad();
  87. }
  88. //=====================================================================================================================================
  89. // SetStencilMask [point light] =
  90. //=====================================================================================================================================
  91. /// clears the stencil buffer and draws a shape in the stencil buffer (in this case the shape is a UV shpere)
  92. void renderer_t::illumination_stage_t::point_light_pass_t::SetStencilMask( const PointLight& light ) const
  93. {
  94. glEnable( GL_STENCIL_TEST );
  95. glClear( GL_STENCIL_BUFFER_BIT );
  96. glColorMask( false, false, false, false );
  97. glStencilFunc( GL_ALWAYS, 0x1, 0x1 );
  98. glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
  99. glDisable( GL_CULL_FACE );
  100. // set matrices
  101. glMatrixMode( GL_MODELVIEW );
  102. glPushMatrix();
  103. glMatrixMode( GL_PROJECTION );
  104. glPushMatrix();
  105. R::setProjectionViewMatrices( cam );
  106. // render sphere to stencil buffer
  107. DrawSMOUVS( light );
  108. // restore matrices
  109. glMatrixMode( GL_PROJECTION );
  110. glPopMatrix();
  111. glMatrixMode( GL_MODELVIEW );
  112. glPopMatrix();
  113. glEnable( GL_CULL_FACE );
  114. glColorMask( true, true, true, true );
  115. // change the stencil func so that the light pass will only write in the masked area
  116. glStencilFunc( GL_EQUAL, 0x1, 0x1 );
  117. glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
  118. }
  119. */