Renderer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "Renderer.h"
  2. #include "Camera.h" /// @todo remove this
  3. #include "RendererInitializer.h"
  4. #include "Material.h"
  5. //======================================================================================================================
  6. // Statics =
  7. //======================================================================================================================
  8. float Renderer::quadVertCoords [][2] = { {1.0,1.0}, {0.0,1.0}, {0.0,0.0}, {1.0,0.0} };
  9. int Renderer::maxColorAtachments = -1;
  10. //======================================================================================================================
  11. // Constructor =
  12. //======================================================================================================================
  13. Renderer::Renderer():
  14. width( 640 ),
  15. height( 480 ),
  16. ms( *this ),
  17. is( *this ),
  18. pps( *this ),
  19. dbg( *this )
  20. {
  21. }
  22. //======================================================================================================================
  23. // init =
  24. //======================================================================================================================
  25. void Renderer::init( const RendererInitializer& initializer )
  26. {
  27. // set from the initializer
  28. ms.ez.enabled = initializer.ms.ez.enabled;
  29. is.sm.enabled = initializer.is.sm.enabled;
  30. is.sm.pcfEnabled = initializer.is.sm.pcfEnabled;
  31. is.sm.bilinearEnabled = initializer.is.sm.bilinearEnabled;
  32. is.sm.resolution = initializer.is.sm.resolution;
  33. pps.hdr.enabled = initializer.pps.hdr.enabled;
  34. pps.hdr.renderingQuality = initializer.pps.hdr.renderingQuality;
  35. pps.ssao.enabled = initializer.pps.ssao.enabled;
  36. pps.ssao.renderingQuality = initializer.pps.ssao.renderingQuality;
  37. pps.ssao.bluringQuality = initializer.pps.ssao.bluringQuality;
  38. dbg.enabled = initializer.dbg.enabled;
  39. width = initializer.width;
  40. height = initializer.height;
  41. aspectRatio = float(width)/height;
  42. // a few sanity checks
  43. if( width < 10 || height < 10 )
  44. {
  45. FATAL( "Incorrect width" );
  46. }
  47. // init the stages. Careful with the order!!!!!!!!!!
  48. ms.init();
  49. is.init();
  50. pps.init();
  51. dbg.init();
  52. }
  53. //======================================================================================================================
  54. // render =
  55. //======================================================================================================================
  56. void Renderer::render( Camera& cam_ )
  57. {
  58. cam = &cam_;
  59. ms.run();
  60. is.run();
  61. pps.run();
  62. dbg.run();
  63. ++framesNum;
  64. }
  65. //======================================================================================================================
  66. // drawQuad =
  67. //======================================================================================================================
  68. void Renderer::drawQuad( int vertCoordsUniLoc )
  69. {
  70. DEBUG_ERR( vertCoordsUniLoc == -1 );
  71. glEnableVertexAttribArray( vertCoordsUniLoc );
  72. glVertexAttribPointer( vertCoordsUniLoc, 2, GL_FLOAT, false, 0, quadVertCoords );
  73. glDrawArrays( GL_QUADS, 0, 4 );
  74. glDisableVertexAttribArray( vertCoordsUniLoc );
  75. }
  76. //======================================================================================================================
  77. // setupMaterial =
  78. //======================================================================================================================
  79. void Renderer::setupMaterial( const Material& mtl, const SceneNode& sceneNode, const Camera& cam )
  80. {
  81. mtl.shaderProg->bind();
  82. uint textureUnit = 0;
  83. //
  84. // FFP stuff
  85. //
  86. if( mtl.blends )
  87. {
  88. glEnable( GL_BLEND );
  89. //glDisable( GL_BLEND );
  90. glBlendFunc( mtl.blendingSfactor, mtl.blendingDfactor );
  91. }
  92. else
  93. glDisable( GL_BLEND );
  94. if( mtl.depthTesting )
  95. glEnable( GL_DEPTH_TEST );
  96. else
  97. glDisable( GL_DEPTH_TEST );
  98. if( mtl.wireframe )
  99. glPolygonMode( GL_FRONT, GL_LINE );
  100. else
  101. glPolygonMode( GL_FRONT, GL_FILL );
  102. //
  103. // matrices
  104. //
  105. Mat4 modelMat( sceneNode.getWorldTransform() );
  106. const Mat4& projectionMat = cam.getProjectionMatrix();
  107. const Mat4& viewMat = cam.getViewMatrix();
  108. Mat4 modelViewMat;
  109. Mat3 normalMat;
  110. Mat4 modelViewProjectionMat;
  111. // should I calculate the modelViewMat ?
  112. if( mtl.stdUniVars[ Material::SUV_MODELVIEW_MAT ] ||
  113. mtl.stdUniVars[ Material::SUV_MODELVIEWPROJECTION_MAT ] ||
  114. mtl.stdUniVars[ Material::SUV_NORMAL_MAT ] )
  115. {
  116. modelViewMat = Mat4::combineTransformations( viewMat, modelMat );
  117. }
  118. // set all the matrices
  119. if( mtl.stdUniVars[ Material::SUV_MODEL_MAT ] )
  120. mtl.stdUniVars[ Material::SUV_MODEL_MAT ]->setMat4( &modelMat );
  121. if( mtl.stdUniVars[ Material::SUV_VIEW_MAT ] )
  122. mtl.stdUniVars[ Material::SUV_VIEW_MAT ]->setMat4( &viewMat );
  123. if( mtl.stdUniVars[ Material::SUV_PROJECTION_MAT ] )
  124. mtl.stdUniVars[ Material::SUV_PROJECTION_MAT ]->setMat4( &projectionMat );
  125. if( mtl.stdUniVars[ Material::SUV_MODELVIEW_MAT ] )
  126. mtl.stdUniVars[ Material::SUV_MODELVIEW_MAT ]->setMat4( &modelViewMat );
  127. if( mtl.stdUniVars[ Material::SUV_NORMAL_MAT ] )
  128. {
  129. normalMat = modelViewMat.getRotationPart();
  130. mtl.stdUniVars[ Material::SUV_NORMAL_MAT ]->setMat3( &normalMat );
  131. }
  132. if( mtl.stdUniVars[ Material::SUV_MODELVIEWPROJECTION_MAT ] )
  133. {
  134. modelViewProjectionMat = projectionMat * modelViewMat;
  135. mtl.stdUniVars[ Material::SUV_MODELVIEWPROJECTION_MAT ]->setMat4( &modelViewProjectionMat );
  136. }
  137. //
  138. // FAis
  139. //
  140. if( mtl.stdUniVars[ Material::SUV_MS_NORMAL_FAI ] )
  141. mtl.stdUniVars[ Material::SUV_MS_NORMAL_FAI ]->setTexture( ms.normalFai, textureUnit++ );
  142. if( mtl.stdUniVars[ Material::SUV_MS_DIFFUSE_FAI ] )
  143. mtl.stdUniVars[ Material::SUV_MS_DIFFUSE_FAI ]->setTexture( ms.diffuseFai, textureUnit++ );
  144. if( mtl.stdUniVars[ Material::SUV_MS_SPECULAR_FAI ] )
  145. mtl.stdUniVars[ Material::SUV_MS_SPECULAR_FAI ]->setTexture( ms.specularFai, textureUnit++ );
  146. if( mtl.stdUniVars[ Material::SUV_MS_DEPTH_FAI ] )
  147. mtl.stdUniVars[ Material::SUV_MS_DEPTH_FAI ]->setTexture( ms.depthFai, textureUnit++ );
  148. if( mtl.stdUniVars[ Material::SUV_IS_FAI ] )
  149. mtl.stdUniVars[ Material::SUV_IS_FAI ]->setTexture( is.fai, textureUnit++ );
  150. if( mtl.stdUniVars[ Material::SUV_PPS_FAI ] )
  151. mtl.stdUniVars[ Material::SUV_PPS_FAI ]->setTexture( pps.fai, textureUnit++ );
  152. //
  153. // Other
  154. //
  155. if( mtl.stdUniVars[ Material::SUV_RENDERER_SIZE ] )
  156. {
  157. Vec2 v( width, height );
  158. mtl.stdUniVars[ Material::SUV_RENDERER_SIZE ]->setVec2( &v );
  159. }
  160. //
  161. // now loop all the user defined vars and set them
  162. //
  163. for( uint i=0; i<mtl.userDefinedVars.size(); i++ )
  164. {
  165. const Material::UserDefinedUniVar* udv = &mtl.userDefinedVars[i];
  166. switch( udv->sProgVar->getGlDataType() )
  167. {
  168. // texture
  169. case GL_SAMPLER_2D:
  170. udv->sProgVar->setTexture( *udv->value.texture, textureUnit++ );
  171. break;
  172. // float
  173. case GL_FLOAT:
  174. udv->sProgVar->setFloat( udv->value.float_ );
  175. break;
  176. // vec2
  177. case GL_FLOAT_VEC2:
  178. udv->sProgVar->setVec2( &udv->value.vec2 );
  179. break;
  180. // vec3
  181. case GL_FLOAT_VEC3:
  182. udv->sProgVar->setVec3( &udv->value.vec3 );
  183. break;
  184. // vec4
  185. case GL_FLOAT_VEC4:
  186. udv->sProgVar->setVec4( &udv->value.vec4 );
  187. break;
  188. }
  189. }
  190. }
  191. //======================================================================================================================
  192. // setProjectionMatrix =
  193. //======================================================================================================================
  194. void Renderer::setProjectionMatrix( const Camera& cam )
  195. {
  196. glMatrixMode( GL_PROJECTION );
  197. loadMatrix( cam.getProjectionMatrix() );
  198. }
  199. //======================================================================================================================
  200. // setViewMatrix =
  201. //======================================================================================================================
  202. void Renderer::setViewMatrix( const Camera& cam )
  203. {
  204. glMatrixMode( GL_MODELVIEW );
  205. loadMatrix( cam.getViewMatrix() );
  206. }
  207. //======================================================================================================================
  208. // unproject =
  209. //======================================================================================================================
  210. Vec3 Renderer::unproject( const Vec3& windowCoords, const Mat4& modelViewMat, const Mat4& projectionMat,
  211. const int view[4] )
  212. {
  213. Mat4 invPm = projectionMat * modelViewMat;
  214. invPm.invert();
  215. // the vec is in NDC space meaning: -1<=vec.x<=1 -1<=vec.y<=1 -1<=vec.z<=1
  216. Vec4 vec;
  217. vec.x = (2.0*(windowCoords.x-view[0]))/view[2] - 1.0;
  218. vec.y = (2.0*(windowCoords.y-view[1]))/view[3] - 1.0;
  219. vec.z = 2.0*windowCoords.z - 1.0;
  220. vec.w = 1.0;
  221. Vec4 final = invPm * vec;
  222. final /= final.w;
  223. return Vec3( final );
  224. }
  225. //======================================================================================================================
  226. // ortho =
  227. //======================================================================================================================
  228. Mat4 Renderer::ortho( float left, float right, float bottom, float top, float near, float far )
  229. {
  230. float difx = right-left;
  231. float dify = top-bottom;
  232. float difz = far-near;
  233. float tx = -(right+left) / difx;
  234. float ty = -(top+bottom) / dify;
  235. float tz = -(far+near) / difz;
  236. Mat4 m;
  237. m(0,0) = 2.0 / difx;
  238. m(0,1) = 0.0;
  239. m(0,2) = 0.0;
  240. m(0,3) = tx;
  241. m(1,0) = 0.0;
  242. m(1,1) = 2.0 / dify;
  243. m(1,2) = 0.0;
  244. m(1,3) = ty;
  245. m(2,0) = 0.0;
  246. m(2,1) = 0.0;
  247. m(2,2) = -2.0 / difz;
  248. m(2,3) = tz;
  249. m(3,0) = 0.0;
  250. m(3,1) = 0.0;
  251. m(3,2) = 0.0;
  252. m(3,3) = 1.0;
  253. return m;
  254. }
  255. //======================================================================================================================
  256. // getLastError =
  257. //======================================================================================================================
  258. const uchar* Renderer::getLastError()
  259. {
  260. return gluErrorString( glGetError() );
  261. }
  262. //======================================================================================================================
  263. // printLastError =
  264. //======================================================================================================================
  265. void Renderer::printLastError()
  266. {
  267. GLenum errid = glGetError();
  268. if( errid != GL_NO_ERROR )
  269. ERROR( "OpenGL Error: " << gluErrorString( errid ) );
  270. }