Dbg.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * @file
  3. *
  4. * Debugging stage
  5. */
  6. #include "Renderer.h"
  7. #include "App.h"
  8. #include "Scene.h"
  9. #include "SkelNode.h"
  10. #include "Camera.h"
  11. #include "LightProps.h"
  12. //======================================================================================================================
  13. // Statics =
  14. //======================================================================================================================
  15. RsrcPtr<ShaderProg> Renderer::Dbg::sProg;
  16. Mat4 Renderer::Dbg::viewProjectionMat;
  17. const ShaderProg::UniVar* Renderer::Dbg::colorUniVar;
  18. const ShaderProg::UniVar* Renderer::Dbg::modelViewProjectionMat;
  19. //======================================================================================================================
  20. // Constructor =
  21. //======================================================================================================================
  22. Renderer::Dbg::Dbg(Renderer& r_):
  23. RenderingStage(r_),
  24. showAxisEnabled(false),
  25. showLightsEnabled(true),
  26. showSkeletonsEnabled(false),
  27. showCamerasEnabled(true)
  28. {
  29. }
  30. //======================================================================================================================
  31. // drawLine =
  32. //======================================================================================================================
  33. void Renderer::Dbg::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
  34. {
  35. float posBuff [] = {from.x, from.y, from.z, to.x, to.y, to.z};
  36. setColor(color);
  37. setModelMat(Mat4::getIdentity());
  38. glEnableVertexAttribArray(0);
  39. glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, posBuff);
  40. glDrawArrays(GL_LINES, 0, 2);
  41. glDisableVertexAttribArray(0);
  42. }
  43. //======================================================================================================================
  44. // renderGrid =
  45. //======================================================================================================================
  46. void Renderer::Dbg::renderGrid()
  47. {
  48. float col0[] = { 0.5, 0.5, 0.5 };
  49. float col1[] = { 0.0, 0.0, 1.0 };
  50. float col2[] = { 1.0, 0.0, 0.0 };
  51. glDisable(GL_TEXTURE_2D);
  52. glDisable(GL_LIGHTING);
  53. glDisable(GL_LINE_STIPPLE);
  54. //glLineWidth(1.0);
  55. glColor3fv(col0);
  56. const float space = 1.0; // space between lines
  57. const int num = 57; // lines number. must be odd
  58. float opt = ((num-1)*space/2);
  59. glBegin(GL_LINES);
  60. for(int x=0; x<num; x++)
  61. {
  62. if(x==num/2) // if the middle line then change color
  63. glColor3fv(col1);
  64. else if(x==(num/2)+1) // if the next line after the middle one change back to default col
  65. glColor3fv(col0);
  66. float opt1 = (x*space);
  67. // line in z
  68. glVertex3f(opt1-opt, 0.0, -opt);
  69. glVertex3f(opt1-opt, 0.0, opt);
  70. if(x==num/2) // if middle line change col so you can highlight the x-axis
  71. glColor3fv(col2);
  72. // line in the x
  73. glVertex3f(-opt, 0.0, opt1-opt);
  74. glVertex3f(opt, 0.0, opt1-opt);
  75. }
  76. glEnd();
  77. }
  78. //======================================================================================================================
  79. // drawSphere =
  80. //======================================================================================================================
  81. void Renderer::Dbg::drawSphere(float radius, const Transform& trf, const Vec4& col, int complexity)
  82. {
  83. setColor(col);
  84. const float twopi = M::PI*2;
  85. const float pidiv2 = M::PI/2;
  86. float theta1 = 0.0;
  87. float theta2 = 0.0;
  88. float theta3 = 0.0;
  89. float ex = 0.0;
  90. float ey = 0.0;
  91. float ez = 0.0;
  92. float px = 0.0;
  93. float py = 0.0;
  94. float pz = 0.0;
  95. Vec<Vec3> positions;
  96. Vec<Vec3> normals;
  97. Vec<Vec2> texCoodrs;
  98. for(int i = 0; i < complexity/2; ++i)
  99. {
  100. theta1 = i * twopi / complexity - pidiv2;
  101. theta2 = (i + 1) * twopi / complexity - pidiv2;
  102. for(int j = complexity; j >= 0; --j)
  103. {
  104. theta3 = j * twopi / complexity;
  105. float sintheta1, costheta1;
  106. sinCos(theta1, sintheta1, costheta1);
  107. float sintheta2, costheta2;
  108. sinCos(theta2, sintheta2, costheta2);
  109. float sintheta3, costheta3;
  110. sinCos(theta3, sintheta3, costheta3);
  111. ex = costheta2 * costheta3;
  112. ey = sintheta2;
  113. ez = costheta2 * sintheta3;
  114. px = radius * ex;
  115. py = radius * ey;
  116. pz = radius * ez;
  117. positions.push_back(Vec3(px, py, pz));
  118. normals.push_back(Vec3(ex, ey, ez));
  119. texCoodrs.push_back(Vec2(-(j/(float)complexity), 2*(i+1)/(float)complexity));
  120. ex = costheta1 * costheta3;
  121. ey = sintheta1;
  122. ez = costheta1 * sintheta3;
  123. px = radius * ex;
  124. py = radius * ey;
  125. pz = radius * ez;
  126. positions.push_back(Vec3(px, py, pz));
  127. normals.push_back(Vec3(ex, ey, ez));
  128. texCoodrs.push_back(Vec2(-(j/(float)complexity), 2*i/(float)complexity));
  129. }
  130. }
  131. setModelMat(Mat4(trf));
  132. glEnableVertexAttribArray(0);
  133. glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &(positions[0][0]));
  134. glDrawArrays(GL_QUAD_STRIP, 0, positions.size());
  135. glDisableVertexAttribArray(0);
  136. }
  137. //======================================================================================================================
  138. // renderCube =
  139. //======================================================================================================================
  140. void Renderer::Dbg::renderCube(float size)
  141. {
  142. Vec3 maxPos = Vec3(0.5 * size);
  143. Vec3 minPos = Vec3(-0.5 * size);
  144. Vec3 points [] = {
  145. Vec3(maxPos.x, maxPos.y, maxPos.z), // right top front
  146. Vec3(minPos.x, maxPos.y, maxPos.z), // left top front
  147. Vec3(minPos.x, minPos.y, maxPos.z), // left bottom front
  148. Vec3(maxPos.x, minPos.y, maxPos.z), // right bottom front
  149. Vec3(maxPos.x, maxPos.y, minPos.z), // right top back
  150. Vec3(minPos.x, maxPos.y, minPos.z), // left top back
  151. Vec3(minPos.x, minPos.y, minPos.z), // left bottom back
  152. Vec3(maxPos.x, minPos.y, minPos.z) // right bottom back
  153. };
  154. const ushort indeces [] = { 0, 1, 2, 3, 4, 0, 3, 7, 1, 5, 6, 2, 5, 4, 7, 6, 0, 4, 5, 1, 3, 2, 6, 7 };
  155. glEnableVertexAttribArray(0);
  156. glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &(points[0][0]));
  157. glDrawElements(GL_QUADS, sizeof(indeces)/sizeof(ushort), GL_UNSIGNED_SHORT, indeces);
  158. glDisableVertexAttribArray(0);
  159. }
  160. //======================================================================================================================
  161. // init =
  162. //======================================================================================================================
  163. void Renderer::Dbg::init()
  164. {
  165. // create FBO Captain Blood
  166. fbo.create();
  167. fbo.bind();
  168. // inform in what buffers we draw
  169. fbo.setNumOfColorAttachements(1);
  170. // attach the textures
  171. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, r.pps.postPassFai.getGlId(), 0);
  172. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, r.ms.depthFai.getGlId(), 0);
  173. // test if success
  174. if(!fbo.isGood())
  175. FATAL("Cannot create debug FBO");
  176. // unbind
  177. fbo.unbind();
  178. // shader
  179. if(sProg.get() == NULL)
  180. {
  181. sProg.loadRsrc("shaders/Dbg.glsl");
  182. colorUniVar = sProg->findUniVar("color");
  183. modelViewProjectionMat = sProg->findUniVar("modelViewProjectionMat");
  184. }
  185. }
  186. //======================================================================================================================
  187. // runStage =
  188. //======================================================================================================================
  189. void Renderer::Dbg::run()
  190. {
  191. if(!enabled) return;
  192. const Camera& cam = *r.cam;
  193. fbo.bind();
  194. sProg->bind();
  195. viewProjectionMat = cam.getProjectionMatrix() * cam.getViewMatrix();
  196. // OGL stuff
  197. Renderer::setViewport(0, 0, r.width, r.height);
  198. glEnable(GL_DEPTH_TEST);
  199. glDisable(GL_BLEND);
  200. sProg->bind();
  201. //R::renderGrid();
  202. for(uint i=0; i<app->getScene()->nodes.size(); i++)
  203. {
  204. SceneNode* node = app->getScene()->nodes[i];
  205. if
  206. (
  207. (node->type == SceneNode::SNT_LIGHT && showLightsEnabled) ||
  208. (node->type == SceneNode::SNT_CAMERA && showCamerasEnabled) ||
  209. node->type == SceneNode::SNT_PARTICLE_EMITTER
  210. )
  211. {
  212. node->render();
  213. }
  214. else if(app->getScene()->nodes[i]->type == SceneNode::SNT_SKELETON && showSkeletonsEnabled)
  215. {
  216. SkelNode* skelNode = static_cast<SkelNode*>(node);
  217. glDisable(GL_DEPTH_TEST);
  218. skelNode->render();
  219. glEnable(GL_DEPTH_TEST);
  220. }
  221. }
  222. // Physics
  223. glPolygonMode(GL_FRONT, GL_LINE);
  224. setModelMat(Mat4::getIdentity());
  225. app->getScene()->getPhysics()->getDynamicsWorld()->debugDrawWorld();
  226. glPolygonMode(GL_FRONT, GL_FILL);
  227. }
  228. //======================================================================================================================
  229. // setColor =
  230. //======================================================================================================================
  231. void Renderer::Dbg::setColor(const Vec4& color)
  232. {
  233. colorUniVar->setVec4(&color);
  234. }
  235. //======================================================================================================================
  236. // setModelMat =
  237. //======================================================================================================================
  238. void Renderer::Dbg::setModelMat(const Mat4& modelMat)
  239. {
  240. Mat4 pmv = viewProjectionMat * modelMat;
  241. modelViewProjectionMat->setMat4(&pmv);
  242. }