Dbg.cpp 9.4 KB

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