Dbg.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include "Dbg.h"
  2. #include "Renderer.h"
  3. #include "Scene/RenderableNode.h"
  4. #include "Scene/Scene.h"
  5. #include "Scene/Camera.h"
  6. #include "Scene/Light.h"
  7. #include "Scene/ParticleEmitterNode.h"
  8. #include "RendererInitializer.h"
  9. #include "Drawers/SceneDbgDrawer.h"
  10. #include "Scene/SkinNode.h"
  11. #include "Scene/SpotLight.h"
  12. #include <boost/foreach.hpp>
  13. namespace R {
  14. //==============================================================================
  15. // Constructor =
  16. //==============================================================================
  17. Dbg::Dbg(Renderer& r_):
  18. RenderingPass(r_),
  19. showAxisEnabled(false),
  20. showLightsEnabled(true),
  21. showSkeletonsEnabled(true),
  22. showCamerasEnabled(true),
  23. showVisibilityBoundingShapesFlag(true),
  24. sceneDbgDrawer(*this),
  25. collisionDbgDrawer(*this)
  26. {}
  27. //==============================================================================
  28. // drawLine =
  29. //==============================================================================
  30. void Dbg::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
  31. {
  32. setColor(color);
  33. begin();
  34. pushBackVertex(from);
  35. pushBackVertex(to);
  36. end();
  37. }
  38. //==============================================================================
  39. // renderGrid =
  40. //==============================================================================
  41. void Dbg::renderGrid()
  42. {
  43. Vec4 col0(0.5, 0.5, 0.5, 1.0);
  44. Vec4 col1(0.0, 0.0, 1.0, 1.0);
  45. Vec4 col2(1.0, 0.0, 0.0, 1.0);
  46. const float SPACE = 1.0; // space between lines
  47. const int NUM = 57; // lines number. must be odd
  48. const float GRID_HALF_SIZE = ((NUM - 1) * SPACE / 2);
  49. setColor(col0);
  50. begin();
  51. for(int x = - NUM / 2 * SPACE; x < NUM / 2 * SPACE; x += SPACE)
  52. {
  53. setColor(col0);
  54. // if the middle line then change color
  55. if(x == 0)
  56. {
  57. setColor(col1);
  58. }
  59. // line in z
  60. pushBackVertex(Vec3(x, 0.0, -GRID_HALF_SIZE));
  61. pushBackVertex(Vec3(x, 0.0, GRID_HALF_SIZE));
  62. // if middle line change col so you can highlight the x-axis
  63. if(x == 0)
  64. {
  65. setColor(col2);
  66. }
  67. // line in the x
  68. pushBackVertex(Vec3(-GRID_HALF_SIZE, 0.0, x));
  69. pushBackVertex(Vec3(GRID_HALF_SIZE, 0.0, x));
  70. }
  71. // render
  72. end();
  73. }
  74. //==============================================================================
  75. // drawSphere =
  76. //==============================================================================
  77. void Dbg::drawSphere(float radius, int complexity)
  78. {
  79. Vec<Vec3>* sphereLines;
  80. //
  81. // Pre-calculate the sphere points5
  82. //
  83. std::map<uint, Vec<Vec3> >::iterator it =
  84. complexityToPreCalculatedSphere.find(complexity);
  85. if(it != complexityToPreCalculatedSphere.end()) // Found
  86. {
  87. sphereLines = &(it->second);
  88. }
  89. else // Not found
  90. {
  91. complexityToPreCalculatedSphere[complexity] = Vec<Vec3>();
  92. sphereLines = &complexityToPreCalculatedSphere[complexity];
  93. float fi = M::PI / complexity;
  94. Vec3 prev(1.0, 0.0, 0.0);
  95. for(float th = fi; th < M::PI * 2.0 + fi; th += fi)
  96. {
  97. Vec3 p = Mat3(Euler(0.0, th, 0.0)) * Vec3(1.0, 0.0, 0.0);
  98. for(float th2 = 0.0; th2 < M::PI; th2 += fi)
  99. {
  100. Mat3 rot(Euler(th2, 0.0, 0.0));
  101. Vec3 rotPrev = rot * prev;
  102. Vec3 rotP = rot * p;
  103. sphereLines->push_back(rotPrev);
  104. sphereLines->push_back(rotP);
  105. Mat3 rot2(Euler(0.0, 0.0, M::PI / 2));
  106. sphereLines->push_back(rot2 * rotPrev);
  107. sphereLines->push_back(rot2 * rotP);
  108. }
  109. prev = p;
  110. }
  111. }
  112. //
  113. // Render
  114. //
  115. modelMat = modelMat * Mat4(Vec3(0.0), Mat3::getIdentity(), radius);
  116. begin();
  117. BOOST_FOREACH(const Vec3& p, *sphereLines)
  118. {
  119. if(pointIndex >= MAX_POINTS_PER_DRAW)
  120. {
  121. end();
  122. begin();
  123. }
  124. pushBackVertex(p);
  125. }
  126. end();
  127. }
  128. //==============================================================================
  129. // renderCube =
  130. //==============================================================================
  131. void Dbg::drawCube(float size)
  132. {
  133. Vec3 maxPos = Vec3(0.5 * size);
  134. Vec3 minPos = Vec3(-0.5 * size);
  135. boost::array<Vec3, 8> points = {{
  136. Vec3(maxPos.x(), maxPos.y(), maxPos.z()), // right top front
  137. Vec3(minPos.x(), maxPos.y(), maxPos.z()), // left top front
  138. Vec3(minPos.x(), minPos.y(), maxPos.z()), // left bottom front
  139. Vec3(maxPos.x(), minPos.y(), maxPos.z()), // right bottom front
  140. Vec3(maxPos.x(), maxPos.y(), minPos.z()), // right top back
  141. Vec3(minPos.x(), maxPos.y(), minPos.z()), // left top back
  142. Vec3(minPos.x(), minPos.y(), minPos.z()), // left bottom back
  143. Vec3(maxPos.x(), minPos.y(), minPos.z()) // right bottom back
  144. }};
  145. boost::array<uint, 24> indeces = {{0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6,
  146. 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7}};
  147. begin();
  148. BOOST_FOREACH(uint id, indeces)
  149. {
  150. pushBackVertex(points[id]);
  151. }
  152. end();
  153. }
  154. //==============================================================================
  155. // init =
  156. //==============================================================================
  157. void Dbg::init(const RendererInitializer& initializer)
  158. {
  159. enabled = initializer.dbg.enabled;
  160. //
  161. // FBO
  162. //
  163. try
  164. {
  165. fbo.create();
  166. fbo.bind();
  167. fbo.setNumOfColorAttachements(1);
  168. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  169. GL_TEXTURE_2D, r.getPps().getPostPassFai().getGlId(), 0);
  170. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  171. GL_TEXTURE_2D, r.getMs().getDepthFai().getGlId(), 0);
  172. fbo.checkIfGood();
  173. fbo.unbind();
  174. }
  175. catch(std::exception& e)
  176. {
  177. throw EXCEPTION("Cannot create debug FBO: " + e.what());
  178. }
  179. //
  180. // Shader prog
  181. //
  182. sProg.loadRsrc("shaders/Dbg.glsl");
  183. //
  184. // VAO & VBOs
  185. //
  186. positionsVbo.create(GL_ARRAY_BUFFER, sizeof(positions), NULL,
  187. GL_DYNAMIC_DRAW);
  188. colorsVbo.create(GL_ARRAY_BUFFER, sizeof(colors), NULL, GL_DYNAMIC_DRAW);
  189. vao.create();
  190. const int positionAttribLoc = 0;
  191. vao.attachArrayBufferVbo(positionsVbo, positionAttribLoc, 3, GL_FLOAT,
  192. GL_FALSE, 0, NULL);
  193. const int colorAttribLoc = 1;
  194. vao.attachArrayBufferVbo(colorsVbo, colorAttribLoc, 3, GL_FLOAT, GL_FALSE,
  195. 0, NULL);
  196. //
  197. // Rest
  198. //
  199. pointIndex = 0;
  200. ON_GL_FAIL_THROW_EXCEPTION();
  201. modelMat.setIdentity();
  202. crntCol = Vec3(1.0, 0.0, 0.0);
  203. }
  204. //==============================================================================
  205. // runStage =
  206. //==============================================================================
  207. void Dbg::run()
  208. {
  209. if(!enabled)
  210. {
  211. return;
  212. }
  213. fbo.bind();
  214. sProg->bind();
  215. // OGL stuff
  216. GlStateMachineSingleton::getInstance().setViewport(0, 0,
  217. r.getWidth(), r.getHeight());
  218. GlStateMachineSingleton::getInstance().enable(GL_DEPTH_TEST, true);
  219. GlStateMachineSingleton::getInstance().enable(GL_BLEND, false);
  220. setModelMat(Mat4::getIdentity());
  221. renderGrid();
  222. BOOST_FOREACH(const SceneNode* node,
  223. SceneSingleton::getInstance().getAllNodes())
  224. {
  225. /*if(!node->isVisible())
  226. {
  227. continue;
  228. }*/
  229. switch(node->getSceneNodeType())
  230. {
  231. case SceneNode::SNT_CAMERA:
  232. sceneDbgDrawer.drawCamera(static_cast<const Camera&>(*node));
  233. break;
  234. case SceneNode::SNT_LIGHT:
  235. sceneDbgDrawer.drawLight(static_cast<const Light&>(*node));
  236. break;
  237. case SceneNode::SNT_PARTICLE_EMITTER:
  238. sceneDbgDrawer.drawParticleEmitter(
  239. static_cast<const ParticleEmitterNode&>(*node));
  240. break;
  241. case SceneNode::SNT_RENDERABLE:
  242. /*if(showVisibilityBoundingShapesFlag)
  243. {
  244. const RenderableNode& rnode =
  245. static_cast<const RenderableNode&>(*node);
  246. collisionDbgDrawer.draw(rnode. getVisibilityShapeWSpace());
  247. }*/
  248. break;
  249. case SceneNode::SNT_SKIN:
  250. {
  251. const SkinNode& node_ = static_cast<const SkinNode&>(*node);
  252. sceneDbgDrawer.drawSkinNodeSkeleton(node_);
  253. if(showVisibilityBoundingShapesFlag)
  254. {
  255. collisionDbgDrawer.draw(node_.getVisibilityShapeWSpace());
  256. }
  257. break;
  258. }
  259. default:
  260. break;
  261. }
  262. }
  263. ///////////////
  264. /*setColor(Vec3(1));
  265. Obb obb(Vec3(0.0), Mat3::getIdentity(), Vec3(1.0, 2.0, 1.0));
  266. Obb obb2(Vec3(0.0), Mat3::getIdentity(), Vec3(1.0, 1.5, 1.0));
  267. obb = obb.getTransformed(SceneSingleton::getInstance().getAllNodes()[1]->
  268. getWorldTransform());
  269. collisionDbgDrawer.draw(obb.getCompoundShape(obb2));
  270. collisionDbgDrawer.draw(obb);
  271. collisionDbgDrawer.draw(obb2);
  272. setModelMat(Mat4::getIdentity());
  273. boost::array<Vec3, 8> points;
  274. obb.getExtremePoints(points);
  275. setColor(Vec3(1, 0, 0));
  276. begin();
  277. enum
  278. {
  279. RTF,
  280. LTF,
  281. LBF,
  282. RBF,
  283. RTB,
  284. LTB,
  285. LBB,
  286. RBB
  287. };
  288. Vec3 xAxis = obb.getRotation().getColumn(0);
  289. Vec3 yAxis = obb.getRotation().getColumn(1);
  290. Vec3 zAxis = obb.getRotation().getColumn(2);
  291. Vec3 er = obb.getRotation() * obb.getExtend();
  292. boost::array<uint, 24> indeces = {{0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6,
  293. 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7}};
  294. BOOST_FOREACH(uint id, indeces)
  295. {
  296. pushBackVertex(points[id]);
  297. }
  298. end();*/
  299. ///////////////
  300. SceneSingleton::getInstance().getPhysMasterContainer().getWorld().
  301. debugDrawWorld();
  302. // Physics
  303. /*glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  304. setModelMat(Mat4::getIdentity());
  305. app->getScene().getPhysics().debugDraw();
  306. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);*/
  307. }
  308. //==============================================================================
  309. // setModelMat =
  310. //==============================================================================
  311. void Dbg::setModelMat(const Mat4& modelMat_)
  312. {
  313. ASSERT(pointIndex == 0); // This means that the func called after begin
  314. // and before end
  315. modelMat = modelMat_;
  316. }
  317. //==============================================================================
  318. // begin =
  319. //==============================================================================
  320. void Dbg::begin()
  321. {
  322. ASSERT(pointIndex == 0);
  323. }
  324. //==============================================================================
  325. // end =
  326. //==============================================================================
  327. void Dbg::end()
  328. {
  329. ASSERT(pointIndex != 0);
  330. positionsVbo.write(&positions[0], 0, sizeof(Vec3) * pointIndex);
  331. colorsVbo.write(&colors[0], 0, sizeof(Vec3) * pointIndex);
  332. Mat4 pmv = r.getViewProjectionMat() * modelMat;
  333. sProg->getUniformVariable("modelViewProjectionMat").set(&pmv);
  334. vao.bind();
  335. glDrawArrays(GL_LINES, 0, pointIndex);
  336. vao.unbind();
  337. // Cleanup
  338. pointIndex = 0;
  339. }
  340. //==============================================================================
  341. // pushBackVertex =
  342. //==============================================================================
  343. void Dbg::pushBackVertex(const Vec3& pos)
  344. {
  345. positions[pointIndex] = pos;
  346. colors[pointIndex] = crntCol;
  347. ++pointIndex;
  348. }
  349. } // end namespace