Is.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. The file contains functions and vars used for the deferred shading illumination stage.
  3. */
  4. #include "Renderer.h"
  5. #include "Camera.h"
  6. #include "Scene.h"
  7. #include "Mesh.h"
  8. #include "Light.h"
  9. #include "Resource.h"
  10. #include "Scene.h"
  11. #include "Fbo.h"
  12. #include "LightProps.h"
  13. namespace R {
  14. namespace Is {
  15. /*
  16. =======================================================================================================================================
  17. VARS =
  18. =======================================================================================================================================
  19. */
  20. static Fbo fbo;
  21. Texture fai; // illuminated Scene
  22. static uint stencil_rb; // framebuffer render buffer for stencil optimizations
  23. // shaders
  24. static ShaderProg* ambientSProg;
  25. static ShaderProg* pointLightSProg;
  26. static ShaderProg* spotLightNoShadowSProg;
  27. static ShaderProg* spotLightShadowSProg;
  28. // the bellow are used to speedup the calculation of the frag pos (view space) inside the shader. This is done by precompute the
  29. // view vectors each for one corner of the screen and tha planes used to compute the frag_pos_view_space.z from the depth value.
  30. static Vec3 view_vectors[4];
  31. static Vec2 planes;
  32. /*
  33. =======================================================================================================================================
  34. Stencil Masking Opt Uv Sphere =
  35. =======================================================================================================================================
  36. */
  37. static float 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 };
  38. static uint smo_uvs_vbo_id = 0; // stencil masking opt uv sphere vertex buffer object id
  39. // init stencil masking optimization UV sphere
  40. static void initSMOUVS()
  41. {
  42. glGenBuffers( 1, &smo_uvs_vbo_id );
  43. glBindBuffer( GL_ARRAY_BUFFER, smo_uvs_vbo_id );
  44. glBufferData( GL_ARRAY_BUFFER, sizeof(smo_uvs_coords), smo_uvs_coords, GL_STATIC_DRAW );
  45. glBindBuffer( GL_ARRAY_BUFFER, 0 );
  46. }
  47. static void DrawSMOUVS( const PointLight& light )
  48. {
  49. const float scale = 1.2;
  50. R::multMatrix( Mat4( light.translationWspace, Mat3::getIdentity(), light.radius*scale ) );
  51. R::noShaders();
  52. glBindBuffer( GL_ARRAY_BUFFER, smo_uvs_vbo_id );
  53. glEnableClientState( GL_VERTEX_ARRAY );
  54. glVertexPointer( 3, GL_FLOAT, 0, 0 );
  55. glDrawArrays( GL_TRIANGLES, 0, sizeof(smo_uvs_coords)/sizeof(float)/3 );
  56. glDisableClientState( GL_VERTEX_ARRAY );
  57. glBindBuffer( GL_ARRAY_BUFFER, 0 );
  58. }
  59. //=====================================================================================================================================
  60. // CalcViewVector =
  61. //=====================================================================================================================================
  62. /// Calc the view vector that we will use inside the shader to calculate the frag pos in view space
  63. static void CalcViewVector( const Camera& cam )
  64. {
  65. int _w = R::w;
  66. int _h = R::h;
  67. 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
  68. int viewport[4]={ 0, 0, _w, _h };
  69. for( int i=0; i<4; i++ )
  70. {
  71. /* Original Code:
  72. R::UnProject( pixels[i][0], pixels[i][1], 10, cam.getViewMatrix(), cam.getProjectionMatrix(), viewport,
  73. view_vectors[i].x, view_vectors[i].y, view_vectors[i].z );
  74. view_vectors[i] = cam.getViewMatrix() * view_vectors[i];
  75. The original code is the above 3 lines. The optimized follows:*/
  76. Vec3 vec;
  77. vec.x = (2.0*(pixels[i][0]-viewport[0]))/viewport[2] - 1.0;
  78. vec.y = (2.0*(pixels[i][1]-viewport[1]))/viewport[3] - 1.0;
  79. vec.z = 1.0;
  80. view_vectors[i] = vec.getTransformed( cam.getInvProjectionMatrix() );
  81. // end of optimized code
  82. }
  83. }
  84. //=====================================================================================================================================
  85. // CalcPlanes =
  86. //=====================================================================================================================================
  87. /// Calc the planes that we will use inside the shader to calculate the frag pos in view space
  88. static void CalcPlanes( const Camera& cam )
  89. {
  90. planes.x = -cam.getZFar() / (cam.getZFar() - cam.getZNear());
  91. planes.y = -cam.getZFar() * cam.getZNear() / (cam.getZFar() - cam.getZNear());
  92. }
  93. /*
  94. =======================================================================================================================================
  95. initStageFBO =
  96. =======================================================================================================================================
  97. */
  98. static void initStageFBO()
  99. {
  100. // create FBO
  101. fbo.Create();
  102. fbo.bind();
  103. // init the stencil render buffer
  104. glGenRenderbuffers( 1, &stencil_rb );
  105. glBindRenderbuffer( GL_RENDERBUFFER, stencil_rb );
  106. glRenderbufferStorage( GL_RENDERBUFFER, GL_STENCIL_INDEX, R::w, R::h );
  107. glFramebufferRenderbufferEXT( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencil_rb );
  108. // inform in what buffers we draw
  109. fbo.setNumOfColorAttachements(1);
  110. // create the txtrs
  111. if( !fai.createEmpty2D( R::w, R::h, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE ) )
  112. {
  113. FATAL( "See prev error" );
  114. }
  115. // attach
  116. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fai.getGlId(), 0 );
  117. // test if success
  118. if( !fbo.isGood() )
  119. FATAL( "Cannot create deferred shading illumination stage FBO" );
  120. // unbind
  121. fbo.Unbind();
  122. }
  123. /*
  124. =======================================================================================================================================
  125. init =
  126. =======================================================================================================================================
  127. */
  128. void init()
  129. {
  130. // load the shaders
  131. ambientSProg = rsrc::shaders.load( "shaders/is_ap.glsl" );
  132. pointLightSProg = rsrc::shaders.load( "shaders/is_lp_point.glsl" );
  133. spotLightNoShadowSProg = rsrc::shaders.load( "shaders/is_lp_spot.glsl" );
  134. spotLightShadowSProg = rsrc::shaders.load( "shaders/is_lp_spot_shad.glsl" );
  135. // init the rest
  136. initStageFBO();
  137. initSMOUVS();
  138. R::Is::Shad::init();
  139. }
  140. /*
  141. =======================================================================================================================================
  142. AmbientPass =
  143. =======================================================================================================================================
  144. */
  145. static void AmbientPass( const Camera& /*cam*/, const Vec3& color )
  146. {
  147. glDisable( GL_BLEND );
  148. // set the shader
  149. ambientSProg->bind();
  150. // set the uniforms
  151. glUniform3fv( ambientSProg->getUniVar("ambientCol").getLoc(), 1, &((Vec3)color)[0] );
  152. ambientSProg->locTexUnit( ambientSProg->getUniVar("sceneColMap").getLoc(), R::Ms::diffuseFai, 0 );
  153. // Draw quad
  154. R::DrawQuad( 0 );
  155. }
  156. //=====================================================================================================================================
  157. // SetStencilMask [point light] =
  158. //=====================================================================================================================================
  159. /// Clears the stencil buffer and draws a shape in the stencil buffer (in this case the shape is a UV shpere)
  160. static void SetStencilMask( const Camera& cam, const PointLight& light )
  161. {
  162. glEnable( GL_STENCIL_TEST );
  163. glClear( GL_STENCIL_BUFFER_BIT );
  164. glColorMask( false, false, false, false );
  165. glStencilFunc( GL_ALWAYS, 0x1, 0x1 );
  166. glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
  167. glDisable( GL_CULL_FACE );
  168. // set matrices
  169. glMatrixMode( GL_MODELVIEW );
  170. glPushMatrix();
  171. glMatrixMode( GL_PROJECTION );
  172. glPushMatrix();
  173. R::setProjectionViewMatrices( cam );
  174. // render sphere to stencil buffer
  175. DrawSMOUVS( light );
  176. // restore matrices
  177. glMatrixMode( GL_PROJECTION );
  178. glPopMatrix();
  179. glMatrixMode( GL_MODELVIEW );
  180. glPopMatrix();
  181. glEnable( GL_CULL_FACE );
  182. glColorMask( true, true, true, true );
  183. // change the stencil func so that the light pass will only write in the masked area
  184. glStencilFunc( GL_EQUAL, 0x1, 0x1 );
  185. glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
  186. }
  187. /*
  188. =======================================================================================================================================
  189. SetStencilMask [spot light] =
  190. see above =
  191. =======================================================================================================================================
  192. */
  193. static void SetStencilMask( const Camera& cam, const SpotLight& light )
  194. {
  195. glEnable( GL_STENCIL_TEST );
  196. glClear( GL_STENCIL_BUFFER_BIT );
  197. glColorMask( false, false, false, false );
  198. glStencilFunc( GL_ALWAYS, 0x1, 0x1 );
  199. glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
  200. glDisable( GL_CULL_FACE );
  201. // set matrices
  202. glMatrixMode( GL_MODELVIEW );
  203. glPushMatrix();
  204. glMatrixMode( GL_PROJECTION );
  205. glPushMatrix();
  206. R::setProjectionViewMatrices( cam );
  207. // render camera's shape to stencil buffer
  208. R::noShaders();
  209. const Camera& lcam = light.camera;
  210. float x = lcam.getZFar() / tan( (PI-lcam.getFovX())/2 );
  211. float y = tan( lcam.getFovY()/2 ) * lcam.getZFar();
  212. float z = -lcam.getZFar();
  213. const int tris_num = 6;
  214. float verts[tris_num][3][3] = {
  215. { { 0.0, 0.0, 0.0 }, { x, -y, z }, { x, y, z } }, // right triangle
  216. { { 0.0, 0.0, 0.0 }, { x, y, z }, {-x, y, z } }, // top
  217. { { 0.0, 0.0, 0.0 }, {-x, y, z }, {-x, -y, z } }, // left
  218. { { 0.0, 0.0, 0.0 }, {-x, -y, z }, { x, -y, z } }, // bottom
  219. { { x, -y, z }, {-x, y, z }, { x, y, z } }, // front up right
  220. { { x, -y, z }, {-x, -y, z }, {-x, y, z } }, // front bottom left
  221. };
  222. R::multMatrix( lcam.transformationWspace );
  223. glEnableClientState( GL_VERTEX_ARRAY );
  224. glVertexPointer( 3, GL_FLOAT, 0, verts );
  225. glDrawArrays( GL_TRIANGLES, 0, tris_num*3 );
  226. glDisableClientState( GL_VERTEX_ARRAY );
  227. // restore matrices
  228. glMatrixMode( GL_PROJECTION );
  229. glPopMatrix();
  230. glMatrixMode( GL_MODELVIEW );
  231. glPopMatrix();
  232. glEnable( GL_CULL_FACE );
  233. glColorMask( true, true, true, true );
  234. // change the stencil func so that the light pass will only write in the masked area
  235. glStencilFunc( GL_EQUAL, 0x1, 0x1 );
  236. glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
  237. }
  238. /*
  239. =======================================================================================================================================
  240. PointLightPass =
  241. =======================================================================================================================================
  242. */
  243. static void PointLightPass( const Camera& cam, const PointLight& light )
  244. {
  245. //** make a check wether the point light passes the frustum test **
  246. bsphere_t sphere( light.translationWspace, light.radius );
  247. if( !cam.insideFrustum( sphere ) ) return;
  248. //** set the scissors **
  249. //int n = SetScissors( cam.getViewMatrix()*light.translationWspace, light.radius );
  250. //if( n < 1 ) return;
  251. //** stencil optimization **
  252. SetStencilMask( cam, light );
  253. //** bind the shader **
  254. const ShaderProg& shader = *pointLightSProg; // I dont want to type
  255. shader.bind();
  256. // bind the material stage framebuffer attachable images
  257. shader.locTexUnit( shader.getUniVar("msNormalFai").getLoc(), R::Ms::normalFai, 0 );
  258. shader.locTexUnit( shader.getUniVar("msDiffuseFai").getLoc(), R::Ms::diffuseFai, 1 );
  259. shader.locTexUnit( shader.getUniVar("msSpecularFai").getLoc(), R::Ms::specularFai, 2 );
  260. shader.locTexUnit( shader.getUniVar("msDepthFai").getLoc(), R::Ms::depthFai, 3 );
  261. glUniform2fv( shader.getUniVar("planes").getLoc(), 1, &planes[0] );
  262. Vec3 light_pos_eye_space = light.translationWspace.getTransformed( cam.getViewMatrix() );
  263. glUniform3fv( shader.getUniVar("lightPos").getLoc(), 1, &light_pos_eye_space[0] );
  264. glUniform1f( shader.getUniVar("lightInvRadius").getLoc(), 1.0/light.radius );
  265. glUniform3fv( shader.getUniVar("lightDiffuseCol").getLoc(), 1, &Vec3(light.lightProps->getDiffuseColor())[0] );
  266. glUniform3fv( shader.getUniVar("lightSpecularCol").getLoc(), 1, &Vec3(light.lightProps->getSpecularColor())[0] );
  267. //** render quad **
  268. glEnableVertexAttribArray( 0 );
  269. glEnableVertexAttribArray( 1 );
  270. glVertexAttribPointer( 0, 2, GL_FLOAT, false, 0, &R::quad_vert_cords[0] );
  271. glVertexAttribPointer( 1, 3, GL_FLOAT, false, 0, &view_vectors[0] );
  272. glDrawArrays( GL_QUADS, 0, 4 );
  273. glDisableVertexAttribArray( 0 );
  274. glDisableVertexAttribArray( 1 );
  275. //glDisable( GL_SCISSOR_TEST );
  276. glDisable( GL_STENCIL_TEST );
  277. }
  278. /*
  279. =======================================================================================================================================
  280. SpotLightPass =
  281. =======================================================================================================================================
  282. */
  283. static void SpotLightPass( const Camera& cam, const SpotLight& light )
  284. {
  285. //** first of all check if the light's camera is inside the frustum **
  286. if( !cam.insideFrustum( light.camera ) ) return;
  287. //** stencil optimization **
  288. SetStencilMask( cam, light );
  289. //** generate the shadow map (if needed) **
  290. if( light.castsShadow )
  291. {
  292. R::Is::Shad::runPass( light.camera );
  293. // restore the IS FBO
  294. fbo.bind();
  295. // and restore blending and depth test
  296. glEnable( GL_BLEND );
  297. glBlendFunc( GL_ONE, GL_ONE );
  298. glDisable( GL_DEPTH_TEST );
  299. }
  300. //** set the shader and uniforms **
  301. const ShaderProg* shdr; // because of the huge name
  302. if( light.castsShadow ) shdr = spotLightShadowSProg;
  303. else shdr = spotLightNoShadowSProg;
  304. shdr->bind();
  305. // bind the framebuffer attachable images
  306. shdr->locTexUnit( shdr->getUniVar("msNormalFai").getLoc(), R::Ms::normalFai, 0 );
  307. shdr->locTexUnit( shdr->getUniVar("msDiffuseFai").getLoc(), R::Ms::diffuseFai, 1 );
  308. shdr->locTexUnit( shdr->getUniVar("msSpecularFai").getLoc(), R::Ms::specularFai, 2 );
  309. shdr->locTexUnit( shdr->getUniVar("msDepthFai").getLoc(), R::Ms::depthFai, 3 );
  310. if( light.lightProps->getTexture() == NULL )
  311. ERROR( "No texture is attached to the light. light_props name: " << light.lightProps->getRsrcName() );
  312. // the planes
  313. //glUniform2fv( shdr->getUniLoc("planes"), 1, &planes[0] );
  314. glUniform2fv( shdr->getUniVar("planes").getLoc(), 1, &planes[0] );
  315. // the light params
  316. Vec3 light_pos_eye_space = light.translationWspace.getTransformed( cam.getViewMatrix() );
  317. glUniform3fv( shdr->getUniVar("lightPos").getLoc(), 1, &light_pos_eye_space[0] );
  318. glUniform1f( shdr->getUniVar("lightInvRadius").getLoc(), 1.0/light.getDistance() );
  319. glUniform3fv( shdr->getUniVar("lightDiffuseCol").getLoc(), 1, &Vec3(light.lightProps->getDiffuseColor())[0] );
  320. glUniform3fv( shdr->getUniVar("lightSpecularCol").getLoc(), 1, &Vec3(light.lightProps->getSpecularColor())[0] );
  321. // set the light texture
  322. shdr->locTexUnit( shdr->getUniVar("lightTex").getLoc(), *light.lightProps->getTexture(), 4 );
  323. // before we render disable anisotropic in the light.texture because it produces artefacts. ToDo: see if this is unececeary in future drivers
  324. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  325. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  326. //** set texture matrix for shadowmap projection **
  327. // Bias * P_light * V_light * inv( V_cam )
  328. //const float mBias[] = {0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0};
  329. static Mat4 bias_m4( 0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0 );
  330. Mat4 tex_projection_mat;
  331. tex_projection_mat = bias_m4 * light.camera.getProjectionMatrix() * light.camera.getViewMatrix() * cam.transformationWspace;
  332. glUniformMatrix4fv( shdr->getUniVar("texProjectionMat").getLoc(), 1, true, &tex_projection_mat[0] );
  333. /*
  334. const float mBias[] = {0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0};
  335. glActiveTexture( GL_TEXTURE0 );
  336. glMatrixMode( GL_TEXTURE );
  337. glloadMatrixf( mBias );
  338. R::multMatrix( light.camera.getProjectionMatrix() );
  339. R::multMatrix( light.camera.getViewMatrix() );
  340. R::multMatrix( cam.transformationWspace );
  341. glMatrixMode(GL_MODELVIEW);*/
  342. // the shadow stuff
  343. // render depth to texture and then bind it
  344. if( light.castsShadow )
  345. {
  346. shdr->locTexUnit( shdr->getUniVar("shadowMap").getLoc(), R::Is::Shad::shadowMap, 5 );
  347. }
  348. //** render quad **
  349. glEnableVertexAttribArray( 0 );
  350. glEnableVertexAttribArray( 1 );
  351. glVertexAttribPointer( 0, 2, GL_FLOAT, false, 0, &R::quad_vert_cords[0] );
  352. glVertexAttribPointer( 1, 3, GL_FLOAT, false, 0, &view_vectors[0] );
  353. glDrawArrays( GL_QUADS, 0, 4 );
  354. glDisableVertexAttribArray( 0 );
  355. glDisableVertexAttribArray( 1 );
  356. // restore texture matrix
  357. glMatrixMode( GL_TEXTURE );
  358. glLoadIdentity();
  359. glMatrixMode(GL_MODELVIEW);
  360. glDisable( GL_STENCIL_TEST );
  361. }
  362. /*
  363. =======================================================================================================================================
  364. runStage =
  365. =======================================================================================================================================
  366. */
  367. void runStage( const Camera& cam )
  368. {
  369. // FBO
  370. fbo.bind();
  371. // OGL stuff
  372. R::setViewport( 0, 0, R::w, R::h );
  373. glMatrixMode( GL_MODELVIEW );
  374. glLoadIdentity();
  375. glDisable( GL_DEPTH_TEST );
  376. // ambient pass
  377. AmbientPass( cam, Scene::getAmbientColor() );
  378. // light passes
  379. glEnable( GL_BLEND );
  380. glBlendFunc( GL_ONE, GL_ONE );
  381. CalcViewVector( cam );
  382. CalcPlanes( cam );
  383. // for all lights
  384. for( uint i=0; i<Scene::lights.size(); i++ )
  385. {
  386. const Light& light = *Scene::lights[i];
  387. switch( light.type )
  388. {
  389. case Light::LT_POINT:
  390. {
  391. const PointLight& pointl = static_cast<const PointLight&>(light);
  392. PointLightPass( cam, pointl );
  393. break;
  394. }
  395. case Light::LT_SPOT:
  396. {
  397. const SpotLight& projl = static_cast<const SpotLight&>(light);
  398. SpotLightPass( cam, projl );
  399. break;
  400. }
  401. default:
  402. DEBUG_ERR( 1 );
  403. }
  404. }
  405. // FBO
  406. fbo.Unbind();
  407. }
  408. }} // end namespaces