DebugDrawer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // Copyright (C) 2014, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "anki/renderer/DebugDrawer.h"
  6. #include "anki/resource/ProgramResource.h"
  7. #include "anki/Collision.h"
  8. #include "anki/Scene.h"
  9. #include "anki/resource/TextureResource.h"
  10. #include "anki/renderer/Renderer.h"
  11. #include "anki/core/Logger.h"
  12. namespace anki {
  13. //==============================================================================
  14. // DebugDrawer =
  15. //==============================================================================
  16. //==============================================================================
  17. DebugDrawer::DebugDrawer()
  18. {
  19. GlManager& gl = GlManagerSingleton::get();
  20. m_vert.load("shaders/Dbg.vert.glsl");
  21. m_frag.load("shaders/Dbg.frag.glsl");
  22. GlJobChainHandle jobs(&gl);
  23. m_ppline = GlProgramPipelineHandle(jobs,
  24. {m_vert->getGlProgram(), m_frag->getGlProgram()});
  25. m_vertBuff = GlBufferHandle(jobs, GL_ARRAY_BUFFER,
  26. sizeof(m_clientLineVerts), GL_DYNAMIC_STORAGE_BIT);
  27. m_lineVertCount = 0;
  28. m_triVertCount = 0;
  29. m_mMat.setIdentity();
  30. m_vpMat.setIdentity();
  31. m_mvpMat.setIdentity();
  32. m_crntCol = Vec3(1.0, 0.0, 0.0);
  33. jobs.finish();
  34. }
  35. //==============================================================================
  36. DebugDrawer::~DebugDrawer()
  37. {}
  38. //==============================================================================
  39. void DebugDrawer::setModelMatrix(const Mat4& m)
  40. {
  41. m_mMat = m;
  42. m_mvpMat = m_vpMat * m_mMat;
  43. }
  44. //==============================================================================
  45. void DebugDrawer::setViewProjectionMatrix(const Mat4& m)
  46. {
  47. m_vpMat = m;
  48. m_mvpMat = m_vpMat * m_mMat;
  49. }
  50. //==============================================================================
  51. void DebugDrawer::begin(GLenum primitive)
  52. {
  53. ANKI_ASSERT(primitive == GL_TRIANGLES || primitive == GL_LINES);
  54. m_primitive = primitive;
  55. }
  56. //==============================================================================
  57. void DebugDrawer::end()
  58. {
  59. if(m_primitive == GL_LINES)
  60. {
  61. if(m_lineVertCount % 2 != 0)
  62. {
  63. pushBackVertex(Vec3(0.0));
  64. ANKI_LOGW("Forgot to close the line loop");
  65. }
  66. }
  67. else
  68. {
  69. if(m_triVertCount % 3 != 0)
  70. {
  71. pushBackVertex(Vec3(0.0));
  72. pushBackVertex(Vec3(0.0));
  73. ANKI_LOGW("Forgot to close the line loop");
  74. }
  75. }
  76. }
  77. //==============================================================================
  78. void DebugDrawer::flush()
  79. {
  80. flushInternal(GL_LINES);
  81. flushInternal(GL_TRIANGLES);
  82. }
  83. //==============================================================================
  84. void DebugDrawer::flushInternal(GLenum primitive)
  85. {
  86. if((primitive == GL_LINES && m_lineVertCount == 0)
  87. || (primitive == GL_TRIANGLES && m_triVertCount == 0))
  88. {
  89. // Early exit
  90. return;
  91. }
  92. U clientVerts;
  93. void* vertBuff;
  94. if(primitive == GL_LINES)
  95. {
  96. clientVerts = m_lineVertCount;
  97. vertBuff = &m_clientLineVerts[0];
  98. m_lineVertCount = 0;
  99. }
  100. else
  101. {
  102. clientVerts = m_triVertCount;
  103. vertBuff = &m_clientTriVerts[0];
  104. m_triVertCount = 0;
  105. }
  106. U size = sizeof(Vertex) * clientVerts;
  107. GlClientBufferHandle tmpBuff(m_jobs, size, nullptr);
  108. memcpy(tmpBuff.getBaseAddress(), vertBuff, size);
  109. m_vertBuff.write(m_jobs, tmpBuff, 0, 0, size);
  110. m_ppline.bind(m_jobs);
  111. m_vertBuff.bindVertexBuffer(m_jobs,
  112. 4, GL_FLOAT, false, sizeof(Vertex), 0, 0); // Pos
  113. m_vertBuff.bindVertexBuffer(m_jobs,
  114. 4, GL_FLOAT, true, sizeof(Vertex), sizeof(Vec4), 1); // Color
  115. GlDrawcallArrays dc(primitive, clientVerts);
  116. dc.draw(m_jobs);
  117. }
  118. //==============================================================================
  119. void DebugDrawer::pushBackVertex(const Vec3& pos)
  120. {
  121. U32* vertCount;
  122. Vertex* vertBuff;
  123. if(m_primitive == GL_LINES)
  124. {
  125. vertCount = &m_lineVertCount;
  126. vertBuff = &m_clientLineVerts[0];
  127. }
  128. else
  129. {
  130. vertCount = &m_triVertCount;
  131. vertBuff = &m_clientTriVerts[0];
  132. }
  133. vertBuff[*vertCount].m_position = m_mvpMat * Vec4(pos, 1.0);
  134. vertBuff[*vertCount].m_color = Vec4(m_crntCol, 1.0);
  135. ++(*vertCount);
  136. if(*vertCount == MAX_POINTS_PER_DRAW)
  137. {
  138. flush();
  139. }
  140. }
  141. //==============================================================================
  142. void DebugDrawer::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
  143. {
  144. setColor(color);
  145. begin(GL_LINES);
  146. pushBackVertex(from);
  147. pushBackVertex(to);
  148. end();
  149. }
  150. //==============================================================================
  151. void DebugDrawer::drawGrid()
  152. {
  153. Vec4 col0(0.5, 0.5, 0.5, 1.0);
  154. Vec4 col1(0.0, 0.0, 1.0, 1.0);
  155. Vec4 col2(1.0, 0.0, 0.0, 1.0);
  156. const F32 SPACE = 1.0; // space between lines
  157. const U NUM = 57; // lines number. must be odd
  158. const F32 GRID_HALF_SIZE = ((NUM - 1) * SPACE / 2);
  159. setColor(col0);
  160. begin(GL_LINES);
  161. for(U x = - NUM / 2 * SPACE; x < NUM / 2 * SPACE; x += SPACE)
  162. {
  163. setColor(col0);
  164. // if the middle line then change color
  165. if(x == 0)
  166. {
  167. setColor(col1);
  168. }
  169. // line in z
  170. pushBackVertex(Vec3(x, 0.0, -GRID_HALF_SIZE));
  171. pushBackVertex(Vec3(x, 0.0, GRID_HALF_SIZE));
  172. // if middle line change col so you can highlight the x-axis
  173. if(x == 0)
  174. {
  175. setColor(col2);
  176. }
  177. // line in the x
  178. pushBackVertex(Vec3(-GRID_HALF_SIZE, 0.0, x));
  179. pushBackVertex(Vec3(GRID_HALF_SIZE, 0.0, x));
  180. }
  181. // render
  182. end();
  183. }
  184. //==============================================================================
  185. void DebugDrawer::drawSphere(F32 radius, I complexity)
  186. {
  187. Vector<Vec3>* sphereLines;
  188. // Pre-calculate the sphere points5
  189. //
  190. std::unordered_map<U32, Vector<Vec3>>::iterator it =
  191. m_complexityToPreCalculatedSphere.find(complexity);
  192. if(it != m_complexityToPreCalculatedSphere.end()) // Found
  193. {
  194. sphereLines = &(it->second);
  195. }
  196. else // Not found
  197. {
  198. m_complexityToPreCalculatedSphere[complexity] = Vector<Vec3>();
  199. sphereLines = &m_complexityToPreCalculatedSphere[complexity];
  200. F32 fi = getPi<F32>() / complexity;
  201. Vec3 prev(1.0, 0.0, 0.0);
  202. for(F32 th = fi; th < getPi<F32>() * 2.0 + fi; th += fi)
  203. {
  204. Vec3 p = Mat3(Euler(0.0, th, 0.0)) * Vec3(1.0, 0.0, 0.0);
  205. for(F32 th2 = 0.0; th2 < getPi<F32>(); th2 += fi)
  206. {
  207. Mat3 rot(Euler(th2, 0.0, 0.0));
  208. Vec3 rotPrev = rot * prev;
  209. Vec3 rotP = rot * p;
  210. sphereLines->push_back(rotPrev);
  211. sphereLines->push_back(rotP);
  212. Mat3 rot2(Euler(0.0, 0.0, getPi<F32>() / 2));
  213. sphereLines->push_back(rot2 * rotPrev);
  214. sphereLines->push_back(rot2 * rotP);
  215. }
  216. prev = p;
  217. }
  218. }
  219. // Render
  220. //
  221. Mat4 oldMMat = m_mMat;
  222. Mat4 oldVpMat = m_vpMat;
  223. setModelMatrix(m_mMat * Mat4(Vec4(0.0, 0.0, 0.0, 1.0),
  224. Mat3::getIdentity(), radius));
  225. begin(GL_LINES);
  226. for(const Vec3& p : *sphereLines)
  227. {
  228. pushBackVertex(p);
  229. }
  230. end();
  231. // restore
  232. m_mMat = oldMMat;
  233. m_vpMat = oldVpMat;
  234. }
  235. //==============================================================================
  236. void DebugDrawer::drawCube(F32 size)
  237. {
  238. Vec3 maxPos = Vec3(0.5 * size);
  239. Vec3 minPos = Vec3(-0.5 * size);
  240. Array<Vec3, 8> points = {{
  241. Vec3(maxPos.x(), maxPos.y(), maxPos.z()), // right top front
  242. Vec3(minPos.x(), maxPos.y(), maxPos.z()), // left top front
  243. Vec3(minPos.x(), minPos.y(), maxPos.z()), // left bottom front
  244. Vec3(maxPos.x(), minPos.y(), maxPos.z()), // right bottom front
  245. Vec3(maxPos.x(), maxPos.y(), minPos.z()), // right top back
  246. Vec3(minPos.x(), maxPos.y(), minPos.z()), // left top back
  247. Vec3(minPos.x(), minPos.y(), minPos.z()), // left bottom back
  248. Vec3(maxPos.x(), minPos.y(), minPos.z()) // right bottom back
  249. }};
  250. static const Array<U32, 24> indeces = {{
  251. 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6,
  252. 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7}};
  253. begin(GL_LINES);
  254. for(U32 id : indeces)
  255. {
  256. pushBackVertex(points[id]);
  257. }
  258. end();
  259. }
  260. //==============================================================================
  261. // CollisionDebugDrawer =
  262. //==============================================================================
  263. //==============================================================================
  264. void CollisionDebugDrawer::visit(const Sphere& sphere)
  265. {
  266. m_dbg->setModelMatrix(
  267. Mat4(sphere.getCenter().xyz1(), Mat3::getIdentity(), 1.0));
  268. m_dbg->drawSphere(sphere.getRadius());
  269. }
  270. //==============================================================================
  271. void CollisionDebugDrawer::visit(const Obb& obb)
  272. {
  273. Mat4 scale(Mat4::getIdentity());
  274. scale(0, 0) = obb.getExtend().x();
  275. scale(1, 1) = obb.getExtend().y();
  276. scale(2, 2) = obb.getExtend().z();
  277. Mat4 tsl(Transform(obb.getCenter(), obb.getRotation(), 1.0));
  278. m_dbg->setModelMatrix(tsl * scale);
  279. m_dbg->drawCube(2.0);
  280. }
  281. //==============================================================================
  282. void CollisionDebugDrawer::visit(const Plane& plane)
  283. {
  284. Vec3 n = plane.getNormal().xyz();
  285. const F32& o = plane.getOffset();
  286. Quat q;
  287. q.setFrom2Vec3(Vec3(0.0, 0.0, 1.0), n);
  288. Mat3 rot(q);
  289. rot.rotateXAxis(getPi<F32>() / 2.0);
  290. Mat4 trf(Vec4(n * o, 1.0), rot);
  291. m_dbg->setModelMatrix(trf);
  292. m_dbg->drawGrid();
  293. }
  294. //==============================================================================
  295. void CollisionDebugDrawer::visit(const LineSegment& ls)
  296. {
  297. m_dbg->setModelMatrix(Mat4::getIdentity());
  298. m_dbg->begin(GL_LINES);
  299. m_dbg->pushBackVertex(ls.getOrigin().xyz());
  300. m_dbg->pushBackVertex((ls.getOrigin() + ls.getDirection()).xyz());
  301. m_dbg->end();
  302. }
  303. //==============================================================================
  304. void CollisionDebugDrawer::visit(const Aabb& aabb)
  305. {
  306. Vec3 min = aabb.getMin().xyz();
  307. Vec3 max = aabb.getMax().xyz();
  308. Mat4 trf = Mat4::getIdentity();
  309. // Scale
  310. for(U32 i = 0; i < 3; ++i)
  311. {
  312. trf(i, i) = max[i] - min[i];
  313. }
  314. // Translation
  315. trf.setTranslationPart(Vec4((max + min) / 2.0, 1.0));
  316. m_dbg->setModelMatrix(trf);
  317. m_dbg->drawCube();
  318. }
  319. //==============================================================================
  320. void CollisionDebugDrawer::visit(const Frustum& f)
  321. {
  322. switch(f.getType())
  323. {
  324. case Frustum::Type::ORTHOGRAPHIC:
  325. visit(static_cast<const OrthographicFrustum&>(f).getObb());
  326. break;
  327. case Frustum::Type::PERSPECTIVE:
  328. {
  329. const PerspectiveFrustum& pf =
  330. static_cast<const PerspectiveFrustum&>(f);
  331. F32 camLen = pf.getFar();
  332. F32 tmp0 = camLen / tan((getPi<F32>() - pf.getFovX()) * 0.5)
  333. + 0.001;
  334. F32 tmp1 = camLen * tan(pf.getFovY() * 0.5) + 0.001;
  335. Vec3 points[] = {
  336. Vec3(0.0, 0.0, 0.0), // 0: eye point
  337. Vec3(-tmp0, tmp1, -camLen), // 1: top left
  338. Vec3(-tmp0, -tmp1, -camLen), // 2: bottom left
  339. Vec3(tmp0, -tmp1, -camLen), // 3: bottom right
  340. Vec3(tmp0, tmp1, -camLen) // 4: top right
  341. };
  342. const U32 indeces[] = {0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 2,
  343. 3, 3, 4, 4, 1};
  344. m_dbg->begin(GL_LINES);
  345. for(U32 i = 0; i < sizeof(indeces) / sizeof(U32); i++)
  346. {
  347. m_dbg->pushBackVertex(points[indeces[i]]);
  348. }
  349. m_dbg->end();
  350. break;
  351. }
  352. }
  353. }
  354. //==============================================================================
  355. // SceneDebugDrawer =
  356. //==============================================================================
  357. //==============================================================================
  358. void SceneDebugDrawer::draw(SceneNode& node)
  359. {
  360. MoveComponent* mv = node.tryGetComponent<MoveComponent>();
  361. if(mv)
  362. {
  363. m_dbg->setModelMatrix(Mat4(mv->getWorldTransform()));
  364. }
  365. else
  366. {
  367. m_dbg->setModelMatrix(Mat4::getIdentity());
  368. }
  369. FrustumComponent* fr = node.tryGetComponent<FrustumComponent>();
  370. if(fr)
  371. {
  372. draw(*fr);
  373. }
  374. node.iterateComponentsOfType<SpatialComponent>([&](SpatialComponent& sp)
  375. {
  376. draw(sp);
  377. });
  378. }
  379. //==============================================================================
  380. void SceneDebugDrawer::draw(FrustumComponent& fr) const
  381. {
  382. const Frustum& fs = fr.getFrustum();
  383. m_dbg->setColor(Vec3(1.0, 1.0, 0.0));
  384. CollisionDebugDrawer coldraw(m_dbg);
  385. fs.accept(coldraw);
  386. }
  387. //==============================================================================
  388. void SceneDebugDrawer::draw(SpatialComponent& x) const
  389. {
  390. if(!x.bitsEnabled(SpatialComponent::SF_VISIBLE_CAMERA))
  391. {
  392. return;
  393. }
  394. m_dbg->setColor(Vec3(1.0, 0.0, 1.0));
  395. CollisionDebugDrawer coldraw(m_dbg);
  396. x.getAabb().accept(coldraw);
  397. }
  398. //==============================================================================
  399. void SceneDebugDrawer::draw(const Sector& sector)
  400. {
  401. // Draw the sector
  402. if(sector.getVisibleByMask() == VB_NONE)
  403. {
  404. m_dbg->setColor(Vec3(1.0, 0.5, 0.5));
  405. }
  406. else
  407. {
  408. if(sector.getVisibleByMask() & VB_CAMERA)
  409. {
  410. m_dbg->setColor(Vec3(0.5, 1.0, 0.5));
  411. }
  412. else
  413. {
  414. m_dbg->setColor(Vec3(0.5, 0.5, 1.0));
  415. }
  416. }
  417. CollisionDebugDrawer v(m_dbg);
  418. sector.getAabb().accept(v);
  419. // Draw the portals
  420. m_dbg->setColor(Vec3(0.0, 0.0, 1.0));
  421. for(const Portal* portal : sector.getSectorGroup().getPortals())
  422. {
  423. if(portal->sectors[0] == &sector || portal->sectors[1] == &sector)
  424. {
  425. portal->shape.accept(v);
  426. }
  427. }
  428. }
  429. //==============================================================================
  430. void SceneDebugDrawer::drawPath(const Path& path) const
  431. {
  432. /*const U count = path.getPoints().size();
  433. m_dbg->setColor(Vec3(1.0, 1.0, 0.0));
  434. m_dbg->begin();
  435. for(U i = 0; i < count - 1; i++)
  436. {
  437. m_dbg->pushBackVertex(path.getPoints()[i].getPosition());
  438. m_dbg->pushBackVertex(path.getPoints()[i + 1].getPosition());
  439. }
  440. m_dbg->end();*/
  441. }
  442. } // end namespace anki