Renderer.cpp 10 KB

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