2
0

Renderer.cpp 11 KB

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