DebugDrawer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. // Copyright (C) 2009-2015, 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/renderer/Renderer.h"
  7. #include "anki/resource/ShaderResource.h"
  8. #include "anki/resource/TextureResource.h"
  9. #include "anki/renderer/Renderer.h"
  10. #include "anki/renderer/Pps.h"
  11. #include "anki/renderer/Ms.h"
  12. #include "anki/util/Logger.h"
  13. #include "anki/physics/PhysicsWorld.h"
  14. #include "anki/Collision.h"
  15. #include "anki/Scene.h"
  16. namespace anki {
  17. //==============================================================================
  18. // DebugDrawer =
  19. //==============================================================================
  20. //==============================================================================
  21. DebugDrawer::DebugDrawer()
  22. {}
  23. //==============================================================================
  24. DebugDrawer::~DebugDrawer()
  25. {}
  26. //==============================================================================
  27. Error DebugDrawer::create(Renderer* r)
  28. {
  29. GrManager& gl = r->getGrManager();
  30. ANKI_CHECK(m_vert.load("shaders/Dbg.vert.glsl", &r->getResourceManager()));
  31. ANKI_CHECK(m_frag.load("shaders/Dbg.frag.glsl", &r->getResourceManager()));
  32. PipelinePtr::Initializer init;
  33. init.m_vertex.m_bindingCount = 1;
  34. init.m_vertex.m_bindings[0].m_stride = 2 * sizeof(Vec4);
  35. init.m_vertex.m_attributeCount = 2;
  36. init.m_vertex.m_attributes[0].m_format =
  37. PixelFormat(ComponentFormat::R32G32B32A32, TransformFormat::FLOAT);
  38. init.m_vertex.m_attributes[0].m_offset = 0;
  39. init.m_vertex.m_attributes[0].m_binding = 0;
  40. init.m_vertex.m_attributes[1].m_format =
  41. PixelFormat(ComponentFormat::R32G32B32A32, TransformFormat::FLOAT);
  42. init.m_vertex.m_attributes[1].m_offset = sizeof(Vec4);
  43. init.m_vertex.m_attributes[1].m_binding = 0;
  44. init.m_inputAssembler.m_topology = PrimitiveTopology::LINES;
  45. init.m_depthStencil.m_depthWriteEnabled = false;
  46. init.m_depthStencil.m_format = Ms::DEPTH_RT_PIXEL_FORMAT;
  47. init.m_color.m_attachmentCount = 1;
  48. init.m_color.m_attachments[0].m_format = Pps::RT_PIXEL_FORMAT;
  49. init.m_shaders[U(ShaderType::VERTEX)] = m_vert->getGrShader();
  50. init.m_shaders[U(ShaderType::FRAGMENT)] = m_frag->getGrShader();
  51. m_pplineLinesDepth.create(&gl, init);
  52. init.m_depthStencil.m_depthCompareFunction = CompareOperation::ALWAYS;
  53. m_pplineLinesNoDepth.create(&gl, init);
  54. m_vertBuff.create(&gl, GL_ARRAY_BUFFER, nullptr,
  55. sizeof(m_clientLineVerts), GL_DYNAMIC_STORAGE_BIT);
  56. m_lineVertCount = 0;
  57. m_triVertCount = 0;
  58. m_mMat.setIdentity();
  59. m_vpMat.setIdentity();
  60. m_mvpMat.setIdentity();
  61. m_crntCol = Vec3(1.0, 0.0, 0.0);
  62. return ErrorCode::NONE;
  63. }
  64. //==============================================================================
  65. void DebugDrawer::setModelMatrix(const Mat4& m)
  66. {
  67. m_mMat = m;
  68. m_mvpMat = m_vpMat * m_mMat;
  69. }
  70. //==============================================================================
  71. void DebugDrawer::setViewProjectionMatrix(const Mat4& m)
  72. {
  73. m_vpMat = m;
  74. m_mvpMat = m_vpMat * m_mMat;
  75. }
  76. //==============================================================================
  77. void DebugDrawer::begin(GLenum primitive)
  78. {
  79. ANKI_ASSERT(primitive == GL_TRIANGLES || primitive == GL_LINES);
  80. m_primitive = primitive;
  81. }
  82. //==============================================================================
  83. void DebugDrawer::end()
  84. {
  85. if(m_primitive == GL_LINES)
  86. {
  87. if(m_lineVertCount % 2 != 0)
  88. {
  89. pushBackVertex(Vec3(0.0));
  90. ANKI_LOGW("Forgot to close the line loop");
  91. }
  92. }
  93. else
  94. {
  95. if(m_triVertCount % 3 != 0)
  96. {
  97. pushBackVertex(Vec3(0.0));
  98. pushBackVertex(Vec3(0.0));
  99. ANKI_LOGW("Forgot to close the line loop");
  100. }
  101. }
  102. }
  103. //==============================================================================
  104. Error DebugDrawer::flush()
  105. {
  106. Error err = flushInternal(GL_LINES);
  107. if(!err)
  108. {
  109. err = flushInternal(GL_TRIANGLES);
  110. }
  111. return err;
  112. }
  113. //==============================================================================
  114. Error DebugDrawer::flushInternal(GLenum primitive)
  115. {
  116. Error err = ErrorCode::NONE;
  117. if((primitive == GL_LINES && m_lineVertCount == 0)
  118. || (primitive == GL_TRIANGLES && m_triVertCount == 0))
  119. {
  120. // Early exit
  121. return ErrorCode::NONE;
  122. }
  123. U clientVerts;
  124. void* vertBuff;
  125. if(primitive == GL_LINES)
  126. {
  127. clientVerts = m_lineVertCount;
  128. vertBuff = &m_clientLineVerts[0];
  129. m_lineVertCount = 0;
  130. }
  131. else
  132. {
  133. clientVerts = m_triVertCount;
  134. vertBuff = &m_clientTriVerts[0];
  135. m_triVertCount = 0;
  136. }
  137. U size = sizeof(Vertex) * clientVerts;
  138. m_vertBuff.write(m_cmdb, vertBuff, size, 0, 0, size);
  139. if(m_depthTestEnabled)
  140. {
  141. m_pplineLinesDepth.bind(m_cmdb);
  142. }
  143. else
  144. {
  145. m_pplineLinesNoDepth.bind(m_cmdb);
  146. }
  147. m_cmdb.bindVertexBuffer(0, m_vertBuff, 0);
  148. m_cmdb.drawArrays(primitive, clientVerts);
  149. return err;
  150. }
  151. //==============================================================================
  152. void DebugDrawer::pushBackVertex(const Vec3& pos)
  153. {
  154. U32* vertCount;
  155. Vertex* vertBuff;
  156. if(m_primitive == GL_LINES)
  157. {
  158. vertCount = &m_lineVertCount;
  159. vertBuff = &m_clientLineVerts[0];
  160. }
  161. else
  162. {
  163. vertCount = &m_triVertCount;
  164. vertBuff = &m_clientTriVerts[0];
  165. }
  166. vertBuff[*vertCount].m_position = m_mvpMat * Vec4(pos, 1.0);
  167. vertBuff[*vertCount].m_color = Vec4(m_crntCol, 1.0);
  168. ++(*vertCount);
  169. if(*vertCount == MAX_POINTS_PER_DRAW)
  170. {
  171. Error err = flush();
  172. // TODO Don't ignore
  173. (void)err;
  174. }
  175. }
  176. //==============================================================================
  177. void DebugDrawer::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
  178. {
  179. setColor(color);
  180. begin(GL_LINES);
  181. pushBackVertex(from);
  182. pushBackVertex(to);
  183. end();
  184. }
  185. //==============================================================================
  186. void DebugDrawer::drawGrid()
  187. {
  188. Vec4 col0(0.5, 0.5, 0.5, 1.0);
  189. Vec4 col1(0.0, 0.0, 1.0, 1.0);
  190. Vec4 col2(1.0, 0.0, 0.0, 1.0);
  191. const F32 SPACE = 1.0; // space between lines
  192. const U NUM = 57; // lines number. must be odd
  193. const F32 GRID_HALF_SIZE = ((NUM - 1) * SPACE / 2);
  194. setColor(col0);
  195. begin(GL_LINES);
  196. for(U x = - NUM / 2 * SPACE; x < NUM / 2 * SPACE; x += SPACE)
  197. {
  198. setColor(col0);
  199. // if the middle line then change color
  200. if(x == 0)
  201. {
  202. setColor(col1);
  203. }
  204. // line in z
  205. pushBackVertex(Vec3(x, 0.0, -GRID_HALF_SIZE));
  206. pushBackVertex(Vec3(x, 0.0, GRID_HALF_SIZE));
  207. // if middle line change col so you can highlight the x-axis
  208. if(x == 0)
  209. {
  210. setColor(col2);
  211. }
  212. // line in the x
  213. pushBackVertex(Vec3(-GRID_HALF_SIZE, 0.0, x));
  214. pushBackVertex(Vec3(GRID_HALF_SIZE, 0.0, x));
  215. }
  216. // render
  217. end();
  218. }
  219. //==============================================================================
  220. void DebugDrawer::drawSphere(F32 radius, I complexity)
  221. {
  222. #if 1
  223. Mat4 oldMMat = m_mMat;
  224. Mat4 oldVpMat = m_vpMat;
  225. setModelMatrix(m_mMat * Mat4(Vec4(0.0, 0.0, 0.0, 1.0),
  226. Mat3::getIdentity(), radius));
  227. begin(GL_LINES);
  228. // Pre-calculate the sphere points5
  229. F32 fi = getPi<F32>() / complexity;
  230. Vec3 prev(1.0, 0.0, 0.0);
  231. for(F32 th = fi; th < getPi<F32>() * 2.0 + fi; th += fi)
  232. {
  233. Vec3 p = Mat3(Euler(0.0, th, 0.0)) * Vec3(1.0, 0.0, 0.0);
  234. for(F32 th2 = 0.0; th2 < getPi<F32>(); th2 += fi)
  235. {
  236. Mat3 rot(Euler(th2, 0.0, 0.0));
  237. Vec3 rotPrev = rot * prev;
  238. Vec3 rotP = rot * p;
  239. pushBackVertex(rotPrev);
  240. pushBackVertex(rotP);
  241. Mat3 rot2(Euler(0.0, 0.0, getPi<F32>() / 2));
  242. pushBackVertex(rot2 * rotPrev);
  243. pushBackVertex(rot2 * rotP);
  244. }
  245. prev = p;
  246. }
  247. end();
  248. m_mMat = oldMMat;
  249. m_vpMat = oldVpMat;
  250. #endif
  251. }
  252. //==============================================================================
  253. void DebugDrawer::drawCube(F32 size)
  254. {
  255. Vec3 maxPos = Vec3(0.5 * size);
  256. Vec3 minPos = Vec3(-0.5 * size);
  257. Array<Vec3, 8> points = {{
  258. Vec3(maxPos.x(), maxPos.y(), maxPos.z()), // right top front
  259. Vec3(minPos.x(), maxPos.y(), maxPos.z()), // left top front
  260. Vec3(minPos.x(), minPos.y(), maxPos.z()), // left bottom front
  261. Vec3(maxPos.x(), minPos.y(), maxPos.z()), // right bottom front
  262. Vec3(maxPos.x(), maxPos.y(), minPos.z()), // right top back
  263. Vec3(minPos.x(), maxPos.y(), minPos.z()), // left top back
  264. Vec3(minPos.x(), minPos.y(), minPos.z()), // left bottom back
  265. Vec3(maxPos.x(), minPos.y(), minPos.z()) // right bottom back
  266. }};
  267. static const Array<U32, 24> indeces = {{
  268. 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6,
  269. 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7}};
  270. begin(GL_LINES);
  271. for(U32 id : indeces)
  272. {
  273. pushBackVertex(points[id]);
  274. }
  275. end();
  276. }
  277. //==============================================================================
  278. // CollisionDebugDrawer =
  279. //==============================================================================
  280. //==============================================================================
  281. void CollisionDebugDrawer::visit(const Sphere& sphere)
  282. {
  283. m_dbg->setModelMatrix(
  284. Mat4(sphere.getCenter().xyz1(), Mat3::getIdentity(), 1.0));
  285. m_dbg->drawSphere(sphere.getRadius());
  286. }
  287. //==============================================================================
  288. void CollisionDebugDrawer::visit(const Obb& obb)
  289. {
  290. Mat4 scale(Mat4::getIdentity());
  291. scale(0, 0) = obb.getExtend().x();
  292. scale(1, 1) = obb.getExtend().y();
  293. scale(2, 2) = obb.getExtend().z();
  294. Mat4 tsl(Transform(obb.getCenter(), obb.getRotation(), 1.0));
  295. m_dbg->setModelMatrix(tsl * scale);
  296. m_dbg->drawCube(2.0);
  297. }
  298. //==============================================================================
  299. void CollisionDebugDrawer::visit(const Plane& plane)
  300. {
  301. Vec3 n = plane.getNormal().xyz();
  302. const F32& o = plane.getOffset();
  303. Quat q;
  304. q.setFrom2Vec3(Vec3(0.0, 0.0, 1.0), n);
  305. Mat3 rot(q);
  306. rot.rotateXAxis(getPi<F32>() / 2.0);
  307. Mat4 trf(Vec4(n * o, 1.0), rot);
  308. m_dbg->setModelMatrix(trf);
  309. m_dbg->drawGrid();
  310. }
  311. //==============================================================================
  312. void CollisionDebugDrawer::visit(const LineSegment& ls)
  313. {
  314. m_dbg->setModelMatrix(Mat4::getIdentity());
  315. m_dbg->begin(GL_LINES);
  316. m_dbg->pushBackVertex(ls.getOrigin().xyz());
  317. m_dbg->pushBackVertex((ls.getOrigin() + ls.getDirection()).xyz());
  318. m_dbg->end();
  319. }
  320. //==============================================================================
  321. void CollisionDebugDrawer::visit(const Aabb& aabb)
  322. {
  323. Vec3 min = aabb.getMin().xyz();
  324. Vec3 max = aabb.getMax().xyz();
  325. Mat4 trf = Mat4::getIdentity();
  326. // Scale
  327. for(U32 i = 0; i < 3; ++i)
  328. {
  329. trf(i, i) = max[i] - min[i];
  330. }
  331. // Translation
  332. trf.setTranslationPart(Vec4((max + min) / 2.0, 1.0));
  333. m_dbg->setModelMatrix(trf);
  334. m_dbg->drawCube();
  335. }
  336. //==============================================================================
  337. void CollisionDebugDrawer::visit(const Frustum& f)
  338. {
  339. switch(f.getType())
  340. {
  341. case Frustum::Type::ORTHOGRAPHIC:
  342. visit(static_cast<const OrthographicFrustum&>(f).getObb());
  343. break;
  344. case Frustum::Type::PERSPECTIVE:
  345. {
  346. const PerspectiveFrustum& pf =
  347. static_cast<const PerspectiveFrustum&>(f);
  348. F32 camLen = pf.getFar();
  349. F32 tmp0 = camLen / tan((getPi<F32>() - pf.getFovX()) * 0.5)
  350. + 0.001;
  351. F32 tmp1 = camLen * tan(pf.getFovY() * 0.5) + 0.001;
  352. Vec3 points[] = {
  353. Vec3(0.0, 0.0, 0.0), // 0: eye point
  354. Vec3(-tmp0, tmp1, -camLen), // 1: top left
  355. Vec3(-tmp0, -tmp1, -camLen), // 2: bottom left
  356. Vec3(tmp0, -tmp1, -camLen), // 3: bottom right
  357. Vec3(tmp0, tmp1, -camLen) // 4: top right
  358. };
  359. const U32 indeces[] = {0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 2,
  360. 3, 3, 4, 4, 1};
  361. m_dbg->begin(GL_LINES);
  362. for(U32 i = 0; i < sizeof(indeces) / sizeof(U32); i++)
  363. {
  364. m_dbg->pushBackVertex(points[indeces[i]]);
  365. }
  366. m_dbg->end();
  367. break;
  368. }
  369. }
  370. }
  371. //==============================================================================
  372. void CollisionDebugDrawer::visit(const CompoundShape& cs)
  373. {
  374. CollisionDebugDrawer* self = this;
  375. Error err = cs.iterateShapes([&](const CollisionShape& a) -> Error
  376. {
  377. a.accept(*self);
  378. return ErrorCode::NONE;
  379. });
  380. (void) err;
  381. }
  382. //==============================================================================
  383. void CollisionDebugDrawer::visit(const ConvexHullShape& hull)
  384. {
  385. m_dbg->setModelMatrix(Mat4(hull.getTransform()));
  386. m_dbg->begin(GL_LINES);
  387. const Vec4* points = hull.getPoints() + 1;
  388. const Vec4* end = hull.getPoints() + hull.getPointsCount();
  389. for(; points != end; ++points)
  390. {
  391. m_dbg->pushBackVertex(hull.getPoints()->xyz());
  392. m_dbg->pushBackVertex(points->xyz());
  393. }
  394. m_dbg->end();
  395. }
  396. //==============================================================================
  397. // PhysicsDebugDrawer =
  398. //==============================================================================
  399. void PhysicsDebugDrawer::drawLines(
  400. const Vec3* lines,
  401. const U32 linesCount,
  402. const Vec4& color)
  403. {
  404. m_dbg->begin(GL_LINES);
  405. m_dbg->setColor(color);
  406. for(U i = 0; i < linesCount * 2; ++i)
  407. {
  408. m_dbg->pushBackVertex(lines[i]);
  409. }
  410. m_dbg->end();
  411. }
  412. //==============================================================================
  413. // SceneDebugDrawer =
  414. //==============================================================================
  415. //==============================================================================
  416. void SceneDebugDrawer::draw(SceneNode& node)
  417. {
  418. MoveComponent* mv = node.tryGetComponent<MoveComponent>();
  419. if(mv)
  420. {
  421. m_dbg->setModelMatrix(Mat4(mv->getWorldTransform()));
  422. }
  423. else
  424. {
  425. m_dbg->setModelMatrix(Mat4::getIdentity());
  426. }
  427. FrustumComponent* fr = node.tryGetComponent<FrustumComponent>();
  428. if(fr)
  429. {
  430. draw(*fr);
  431. }
  432. Error err = node.iterateComponentsOfType<SpatialComponent>(
  433. [&](SpatialComponent& sp) -> Error
  434. {
  435. draw(sp);
  436. return ErrorCode::NONE;
  437. });
  438. PortalSectorComponent* ps = node.tryGetComponent<PortalSectorComponent>();
  439. if(ps)
  440. {
  441. draw(*ps);
  442. }
  443. (void)err;
  444. }
  445. //==============================================================================
  446. void SceneDebugDrawer::draw(FrustumComponent& fr) const
  447. {
  448. const Frustum& fs = fr.getFrustum();
  449. m_dbg->setColor(Vec3(1.0, 1.0, 0.0));
  450. CollisionDebugDrawer coldraw(m_dbg);
  451. fs.accept(coldraw);
  452. }
  453. //==============================================================================
  454. void SceneDebugDrawer::draw(SpatialComponent& x) const
  455. {
  456. if(!x.getVisibleByCamera())
  457. {
  458. return;
  459. }
  460. m_dbg->setColor(Vec3(1.0, 0.0, 1.0));
  461. CollisionDebugDrawer coldraw(m_dbg);
  462. x.getAabb().accept(coldraw);
  463. }
  464. //==============================================================================
  465. void SceneDebugDrawer::draw(const PortalSectorComponent& c) const
  466. {
  467. const SceneNode& node = c.getSceneNode();
  468. const PortalSectorBase& psnode = static_cast<const PortalSectorBase&>(node);
  469. m_dbg->setColor(Vec3(0.0, 0.0, 1.0));
  470. m_dbg->begin(GL_LINES);
  471. const auto& verts = psnode.getVertices();
  472. ANKI_ASSERT((psnode.getVertexIndices().getSize() % 3) == 0);
  473. for(U i = 0; i < psnode.getVertexIndices().getSize(); i += 3)
  474. {
  475. I id0 = psnode.getVertexIndices()[i];
  476. I id1 = psnode.getVertexIndices()[i + 1];
  477. I id2 = psnode.getVertexIndices()[i + 2];
  478. m_dbg->pushBackVertex(verts[id0].xyz());
  479. m_dbg->pushBackVertex(verts[id1].xyz());
  480. m_dbg->pushBackVertex(verts[id1].xyz());
  481. m_dbg->pushBackVertex(verts[id2].xyz());
  482. m_dbg->pushBackVertex(verts[id2].xyz());
  483. m_dbg->pushBackVertex(verts[id0].xyz());
  484. }
  485. m_dbg->end();
  486. }
  487. //==============================================================================
  488. void SceneDebugDrawer::drawPath(const Path& path) const
  489. {
  490. /*const U count = path.getPoints().size();
  491. m_dbg->setColor(Vec3(1.0, 1.0, 0.0));
  492. m_dbg->begin();
  493. for(U i = 0; i < count - 1; i++)
  494. {
  495. m_dbg->pushBackVertex(path.getPoints()[i].getPosition());
  496. m_dbg->pushBackVertex(path.getPoints()[i + 1].getPosition());
  497. }
  498. m_dbg->end();*/
  499. }
  500. } // end namespace anki