Renderer.cpp 9.4 KB

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