Dbg.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "Dbg.h"
  2. #include "Renderer.h"
  3. #include "App.h"
  4. #include "Scene.h"
  5. #include "Camera.h"
  6. #include "Light.h"
  7. #include "RendererInitializer.h"
  8. #include "DbgDrawer.h"
  9. #include "ParticleEmitter.h"
  10. //======================================================================================================================
  11. // Constructor =
  12. //======================================================================================================================
  13. Dbg::Dbg(Renderer& r_, Object* parent):
  14. RenderingPass(r_, parent),
  15. showAxisEnabled(false),
  16. showLightsEnabled(true),
  17. showSkeletonsEnabled(true),
  18. showCamerasEnabled(true)
  19. {}
  20. //======================================================================================================================
  21. // drawLine =
  22. //======================================================================================================================
  23. void Dbg::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
  24. {
  25. setColor(color);
  26. begin();
  27. pushBackVertex(from);
  28. pushBackVertex(to);
  29. end();
  30. }
  31. //======================================================================================================================
  32. // renderGrid =
  33. //======================================================================================================================
  34. void Dbg::renderGrid()
  35. {
  36. Vec4 col0(0.5, 0.5, 0.5, 1.0);
  37. Vec4 col1(0.0, 0.0, 1.0, 1.0);
  38. Vec4 col2(1.0, 0.0, 0.0, 1.0);
  39. const float SPACE = 1.0; // space between lines
  40. const int NUM = 57; // lines number. must be odd
  41. const float GRID_HALF_SIZE = ((NUM - 1) * SPACE / 2);
  42. setColor(col0);
  43. begin();
  44. for(int x = - NUM / 2 * SPACE; x < NUM / 2 * SPACE; x += SPACE)
  45. {
  46. setColor(col0);
  47. // if the middle line then change color
  48. if(x == 0)
  49. {
  50. setColor(col1);
  51. }
  52. // line in z
  53. pushBackVertex(Vec3(x, 0.0, -GRID_HALF_SIZE));
  54. pushBackVertex(Vec3(x, 0.0, GRID_HALF_SIZE));
  55. // if middle line change col so you can highlight the x-axis
  56. if(x == 0)
  57. {
  58. setColor(col2);
  59. }
  60. // line in the x
  61. pushBackVertex(Vec3(-GRID_HALF_SIZE, 0.0, x));
  62. pushBackVertex(Vec3(GRID_HALF_SIZE, 0.0, x));
  63. }
  64. // render
  65. end();
  66. }
  67. //======================================================================================================================
  68. // drawSphere =
  69. //======================================================================================================================
  70. void Dbg::drawSphere(float radius, const Transform& trf, const Vec4& col, int complexity)
  71. {
  72. /*setColor(col);
  73. const float twopi = M::PI*2;
  74. const float pidiv2 = M::PI/2;
  75. float theta1 = 0.0;
  76. float theta2 = 0.0;
  77. float theta3 = 0.0;
  78. float ex = 0.0;
  79. float ey = 0.0;
  80. float ez = 0.0;
  81. float px = 0.0;
  82. float py = 0.0;
  83. float pz = 0.0;
  84. Vec<Vec3> positions;
  85. Vec<Vec3> normals;
  86. Vec<Vec2> texCoodrs;
  87. for(int i = 0; i < complexity/2; ++i)
  88. {
  89. theta1 = i * twopi / complexity - pidiv2;
  90. theta2 = (i + 1) * twopi / complexity - pidiv2;
  91. for(int j = complexity; j >= 0; --j)
  92. {
  93. theta3 = j * twopi / complexity;
  94. float sintheta1, costheta1;
  95. sinCos(theta1, sintheta1, costheta1);
  96. float sintheta2, costheta2;
  97. sinCos(theta2, sintheta2, costheta2);
  98. float sintheta3, costheta3;
  99. sinCos(theta3, sintheta3, costheta3);
  100. ex = costheta2 * costheta3;
  101. ey = sintheta2;
  102. ez = costheta2 * sintheta3;
  103. px = radius * ex;
  104. py = radius * ey;
  105. pz = radius * ez;
  106. positions.push_back(Vec3(px, py, pz));
  107. normals.push_back(Vec3(ex, ey, ez));
  108. texCoodrs.push_back(Vec2(-(j/(float)complexity), 2*(i+1)/(float)complexity));
  109. ex = costheta1 * costheta3;
  110. ey = sintheta1;
  111. ez = costheta1 * sintheta3;
  112. px = radius * ex;
  113. py = radius * ey;
  114. pz = radius * ez;
  115. positions.push_back(Vec3(px, py, pz));
  116. normals.push_back(Vec3(ex, ey, ez));
  117. texCoodrs.push_back(Vec2(-(j/(float)complexity), 2*i/(float)complexity));
  118. }
  119. }
  120. setModelMat(Mat4(trf));
  121. glEnableVertexAttribArray(0);
  122. glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &(positions[0][0]));
  123. glDrawArrays(GL_QUAD_STRIP, 0, positions.size());
  124. glDisableVertexAttribArray(0);*/
  125. }
  126. //======================================================================================================================
  127. // renderCube =
  128. //======================================================================================================================
  129. void Dbg::drawCube(float size)
  130. {
  131. Vec3 maxPos = Vec3(0.5 * size);
  132. Vec3 minPos = Vec3(-0.5 * size);
  133. Vec3 points[] = {
  134. Vec3(maxPos.x, maxPos.y, maxPos.z), // right top front
  135. Vec3(minPos.x, maxPos.y, maxPos.z), // left top front
  136. Vec3(minPos.x, minPos.y, maxPos.z), // left bottom front
  137. Vec3(maxPos.x, minPos.y, maxPos.z), // right bottom front
  138. Vec3(maxPos.x, maxPos.y, minPos.z), // right top back
  139. Vec3(minPos.x, maxPos.y, minPos.z), // left top back
  140. Vec3(minPos.x, minPos.y, minPos.z), // left bottom back
  141. Vec3(maxPos.x, minPos.y, minPos.z) // right bottom back
  142. };
  143. const uint 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};
  144. begin();
  145. for(const uint* p = indeces; p != indeces + sizeof(indeces); p++)
  146. {
  147. pushBackVertex(points[*p]);
  148. }
  149. end();
  150. }
  151. //======================================================================================================================
  152. // init =
  153. //======================================================================================================================
  154. void Dbg::init(const RendererInitializer& initializer)
  155. {
  156. enabled = initializer.dbg.enabled;
  157. //
  158. // FBO
  159. //
  160. try
  161. {
  162. fbo.create();
  163. fbo.bind();
  164. fbo.setNumOfColorAttachements(1);
  165. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  166. r.getPps().getPostPassFai().getGlId(), 0);
  167. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, r.getMs().getDepthFai().getGlId(), 0);
  168. fbo.checkIfGood();
  169. fbo.unbind();
  170. }
  171. catch(std::exception& e)
  172. {
  173. throw EXCEPTION("Cannot create debug FBO: " + e.what());
  174. }
  175. //
  176. // Shader prog
  177. //
  178. sProg.loadRsrc("shaders/Dbg.glsl");
  179. //
  180. // VAO & VBOs
  181. //
  182. positionsVbo = new Vbo(GL_ARRAY_BUFFER, sizeof(positions), NULL, GL_DYNAMIC_DRAW, this);
  183. colorsVbo = new Vbo(GL_ARRAY_BUFFER, sizeof(colors), NULL, GL_DYNAMIC_DRAW, this);
  184. vao = new Vao(this);
  185. const int positionAttribLoc = 0;
  186. vao->attachArrayBufferVbo(*positionsVbo, positionAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  187. const int colorAttribLoc = 1;
  188. vao->attachArrayBufferVbo(*colorsVbo, colorAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  189. //
  190. // Rest
  191. //
  192. pointIndex = 0;
  193. ON_GL_FAIL_THROW_EXCEPTION();
  194. modelMat.setIdentity();
  195. crntCol = Vec3(1.0, 0.0, 0.0);
  196. dbgDrawer = new DbgDrawer(*this, this);
  197. }
  198. //======================================================================================================================
  199. // runStage =
  200. //======================================================================================================================
  201. void Dbg::run()
  202. {
  203. if(!enabled)
  204. {
  205. return;
  206. }
  207. fbo.bind();
  208. sProg->bind();
  209. // OGL stuff
  210. Renderer::setViewport(0, 0, r.getWidth(), r.getHeight());
  211. glEnable(GL_DEPTH_TEST);
  212. glDisable(GL_BLEND);
  213. renderGrid();
  214. /// @todo Uncomment
  215. Vec<SceneNode*>::const_iterator it = app->getScene().nodes.begin();
  216. for(; it != app->getScene().nodes.end(); ++it)
  217. {
  218. const SceneNode& node = *(*it);
  219. switch(node.type)
  220. {
  221. case SceneNode::SNT_CAMERA:
  222. dbgDrawer->drawCamera(static_cast<const Camera&>(node));
  223. break;
  224. case SceneNode::SNT_LIGHT:
  225. dbgDrawer->drawLight(static_cast<const Light&>(node));
  226. break;
  227. case SceneNode::SNT_PARTICLE_EMITTER:
  228. dbgDrawer->drawParticleEmitter(static_cast<const ParticleEmitter&>(node));
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. /*for(uint i=0; i<app->getScene().nodes.size(); i++)
  235. {
  236. SceneNode* node = app->getScene().nodes[i];
  237. if
  238. (
  239. (node->type == SceneNode::SNT_LIGHT && showLightsEnabled) ||
  240. (node->type == SceneNode::SNT_CAMERA && showCamerasEnabled) ||
  241. node->type == SceneNode::SNT_PARTICLE_EMITTER
  242. )
  243. {
  244. node->render();
  245. }
  246. else if(app->getScene().nodes[i]->type == SceneNode::SNT_SKELETON && showSkeletonsEnabled)
  247. {
  248. SkelNode* skelNode = static_cast<SkelNode*>(node);
  249. glDisable(GL_DEPTH_TEST);
  250. skelNode->render();
  251. glEnable(GL_DEPTH_TEST);
  252. }
  253. }*/
  254. // Physics
  255. /*glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  256. setModelMat(Mat4::getIdentity());
  257. app->getScene().getPhysics().debugDraw();
  258. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);*/
  259. }
  260. //======================================================================================================================
  261. // setModelMat =
  262. //======================================================================================================================
  263. void Dbg::setModelMat(const Mat4& modelMat_)
  264. {
  265. RASSERT_THROW_EXCEPTION(pointIndex != 0); // This means that the func called after begin and before end
  266. modelMat = modelMat_;
  267. }
  268. //======================================================================================================================
  269. // begin =
  270. //======================================================================================================================
  271. void Dbg::begin()
  272. {
  273. RASSERT_THROW_EXCEPTION(pointIndex != 0);
  274. }
  275. //======================================================================================================================
  276. // end =
  277. //======================================================================================================================
  278. void Dbg::end()
  279. {
  280. RASSERT_THROW_EXCEPTION(pointIndex == 0);
  281. positionsVbo->write(&positions[0], 0, sizeof(Vec3) * pointIndex);
  282. colorsVbo->write(&colors[0], 0, sizeof(Vec3) * pointIndex);
  283. Mat4 pmv = r.getViewProjectionMat() * modelMat;
  284. sProg->findUniVar("modelViewProjectionMat")->setMat4(&pmv);
  285. vao->bind();
  286. glDrawArrays(GL_LINES, 0, pointIndex);
  287. vao->unbind();
  288. // Cleanup
  289. pointIndex = 0;
  290. sProg->findUniVar("modelViewProjectionMat")->setMat4(&Mat4::getIdentity());
  291. }
  292. //======================================================================================================================
  293. // pushBackVertex =
  294. //======================================================================================================================
  295. void Dbg::pushBackVertex(const Vec3& pos)
  296. {
  297. positions[pointIndex] = pos;
  298. colors[pointIndex] = crntCol;
  299. ++pointIndex;
  300. }