DebugDrawer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  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/FinalComposite.h>
  11. #include <anki/renderer/GBuffer.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::DebugDrawer()
  19. {
  20. }
  21. DebugDrawer::~DebugDrawer()
  22. {
  23. }
  24. Error DebugDrawer::init(Renderer* r)
  25. {
  26. m_r = r;
  27. GrManager& gr = r->getGrManager();
  28. // Create the prog and shaders
  29. ANKI_CHECK(r->getResourceManager().loadResource("shaders/Dbg.vert.glsl", m_vert));
  30. ANKI_CHECK(r->getResourceManager().loadResource("shaders/Dbg.frag.glsl", m_frag));
  31. m_prog = gr.newInstance<ShaderProgram>(m_vert->getGrShader(), m_frag->getGrShader());
  32. // Create the vert buffs
  33. for(BufferPtr& v : m_vertBuff)
  34. {
  35. v = gr.newInstance<Buffer>(
  36. sizeof(Vertex) * MAX_VERTS_PER_FRAME, BufferUsageBit::VERTEX, BufferMapAccessBit::WRITE);
  37. }
  38. m_mMat.setIdentity();
  39. m_vpMat.setIdentity();
  40. m_mvpMat.setIdentity();
  41. return ErrorCode::NONE;
  42. }
  43. void DebugDrawer::prepareFrame(CommandBufferPtr& jobs)
  44. {
  45. m_cmdb = jobs;
  46. U frame = m_r->getFrameCount() % MAX_FRAMES_IN_FLIGHT;
  47. void* mapped = m_vertBuff[frame]->map(0, MAX_VERTS_PER_FRAME * sizeof(Vertex), BufferMapAccessBit::WRITE);
  48. m_clientVerts = WeakArray<Vertex>(static_cast<Vertex*>(mapped), MAX_VERTS_PER_FRAME);
  49. m_cmdb->bindVertexBuffer(0, m_vertBuff[frame], 0, 2 * sizeof(Vec4));
  50. m_cmdb->setVertexAttribute(0, 0, PixelFormat(ComponentFormat::R32G32B32A32, TransformFormat::FLOAT), 0);
  51. m_cmdb->setVertexAttribute(1, 0, PixelFormat(ComponentFormat::R32G32B32A32, TransformFormat::FLOAT), sizeof(Vec4));
  52. m_cmdb->bindShaderProgram(m_prog);
  53. m_frameVertCount = 0;
  54. m_crntDrawVertCount = 0;
  55. }
  56. void DebugDrawer::finishFrame()
  57. {
  58. U frame = m_r->getFrameCount() % MAX_FRAMES_IN_FLIGHT;
  59. m_vertBuff[frame]->unmap();
  60. flush();
  61. // Restore state
  62. m_cmdb->setDepthCompareOperation(CompareOperation::ALWAYS);
  63. m_cmdb = CommandBufferPtr(); // Release command buffer
  64. }
  65. void DebugDrawer::setModelMatrix(const Mat4& m)
  66. {
  67. m_mMat = m;
  68. m_mvpMat = m_vpMat * m_mMat;
  69. }
  70. void DebugDrawer::setViewProjectionMatrix(const Mat4& m)
  71. {
  72. m_vpMat = m;
  73. m_mvpMat = m_vpMat * m_mMat;
  74. }
  75. void DebugDrawer::begin(PrimitiveTopology topology)
  76. {
  77. ANKI_ASSERT(topology == PrimitiveTopology::LINES || topology == PrimitiveTopology::TRIANGLES);
  78. if(topology != m_primitive)
  79. {
  80. flush();
  81. }
  82. m_primitive = topology;
  83. }
  84. void DebugDrawer::end()
  85. {
  86. }
  87. void DebugDrawer::pushBackVertex(const Vec3& pos)
  88. {
  89. if(m_frameVertCount < MAX_VERTS_PER_FRAME)
  90. {
  91. m_clientVerts[m_frameVertCount].m_position = m_mvpMat * Vec4(pos, 1.0);
  92. m_clientVerts[m_frameVertCount].m_color = Vec4(m_crntCol, 1.0);
  93. ++m_frameVertCount;
  94. ++m_crntDrawVertCount;
  95. }
  96. else
  97. {
  98. ANKI_R_LOGW("Increase DebugDrawer::MAX_VERTS_PER_FRAME");
  99. }
  100. }
  101. void DebugDrawer::flush()
  102. {
  103. if(m_crntDrawVertCount > 0)
  104. {
  105. if(m_primitive == PrimitiveTopology::LINES)
  106. {
  107. ANKI_ASSERT((m_crntDrawVertCount % 2) == 0);
  108. }
  109. else
  110. {
  111. ANKI_ASSERT((m_crntDrawVertCount % 3) == 0);
  112. }
  113. m_cmdb->setDepthCompareOperation((m_depthTestEnabled) ? CompareOperation::LESS : CompareOperation::ALWAYS);
  114. U firstVert = m_frameVertCount - m_crntDrawVertCount;
  115. m_cmdb->drawArrays(m_primitive, m_crntDrawVertCount, 1, firstVert);
  116. m_crntDrawVertCount = 0;
  117. }
  118. }
  119. void DebugDrawer::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
  120. {
  121. setColor(color);
  122. begin(PrimitiveTopology::LINES);
  123. pushBackVertex(from);
  124. pushBackVertex(to);
  125. end();
  126. }
  127. void DebugDrawer::drawGrid()
  128. {
  129. Vec4 col0(0.5, 0.5, 0.5, 1.0);
  130. Vec4 col1(0.0, 0.0, 1.0, 1.0);
  131. Vec4 col2(1.0, 0.0, 0.0, 1.0);
  132. const F32 SPACE = 1.0; // space between lines
  133. const I NUM = 57; // lines number. must be odd
  134. const F32 GRID_HALF_SIZE = ((NUM - 1) * SPACE / 2);
  135. setColor(col0);
  136. begin(PrimitiveTopology::LINES);
  137. for(I x = -NUM / 2 * SPACE; x < NUM / 2 * SPACE; x += SPACE)
  138. {
  139. setColor(col0);
  140. // if the middle line then change color
  141. if(x == 0)
  142. {
  143. setColor(col0 * 0.5 + col1 * 0.5);
  144. pushBackVertex(Vec3(x, 0.0, -GRID_HALF_SIZE));
  145. pushBackVertex(Vec3(x, 0.0, 0.0));
  146. setColor(col1);
  147. pushBackVertex(Vec3(x, 0.0, 0.0));
  148. pushBackVertex(Vec3(x, 0.0, GRID_HALF_SIZE));
  149. }
  150. else
  151. {
  152. // line in z
  153. pushBackVertex(Vec3(x, 0.0, -GRID_HALF_SIZE));
  154. pushBackVertex(Vec3(x, 0.0, GRID_HALF_SIZE));
  155. }
  156. // if middle line change col so you can highlight the x-axis
  157. if(x == 0)
  158. {
  159. setColor(col0 * 0.5 + col2 * 0.5);
  160. pushBackVertex(Vec3(-GRID_HALF_SIZE, 0.0, x));
  161. pushBackVertex(Vec3(0.0, 0.0, x));
  162. setColor(col2);
  163. pushBackVertex(Vec3(0.0, 0.0, x));
  164. pushBackVertex(Vec3(GRID_HALF_SIZE, 0.0, x));
  165. }
  166. else
  167. {
  168. // line in the x
  169. pushBackVertex(Vec3(-GRID_HALF_SIZE, 0.0, x));
  170. pushBackVertex(Vec3(GRID_HALF_SIZE, 0.0, x));
  171. }
  172. }
  173. // render
  174. end();
  175. }
  176. void DebugDrawer::drawSphere(F32 radius, I complexity)
  177. {
  178. #if 1
  179. Mat4 oldMMat = m_mMat;
  180. Mat4 oldVpMat = m_vpMat;
  181. setModelMatrix(m_mMat * Mat4(Vec4(0.0, 0.0, 0.0, 1.0), Mat3::getIdentity(), radius));
  182. begin(PrimitiveTopology::LINES);
  183. // Pre-calculate the sphere points5
  184. F32 fi = PI / complexity;
  185. Vec3 prev(1.0, 0.0, 0.0);
  186. for(F32 th = fi; th < PI * 2.0 + fi; th += fi)
  187. {
  188. Vec3 p = Mat3(Euler(0.0, th, 0.0)) * Vec3(1.0, 0.0, 0.0);
  189. for(F32 th2 = 0.0; th2 < PI; th2 += fi)
  190. {
  191. Mat3 rot(Euler(th2, 0.0, 0.0));
  192. Vec3 rotPrev = rot * prev;
  193. Vec3 rotP = rot * p;
  194. pushBackVertex(rotPrev);
  195. pushBackVertex(rotP);
  196. Mat3 rot2(Euler(0.0, 0.0, PI / 2));
  197. pushBackVertex(rot2 * rotPrev);
  198. pushBackVertex(rot2 * rotP);
  199. }
  200. prev = p;
  201. }
  202. end();
  203. m_mMat = oldMMat;
  204. m_vpMat = oldVpMat;
  205. #endif
  206. }
  207. void DebugDrawer::drawCube(F32 size)
  208. {
  209. Vec3 maxPos = Vec3(0.5 * size);
  210. Vec3 minPos = Vec3(-0.5 * size);
  211. Array<Vec3, 8> points = {{
  212. Vec3(maxPos.x(), maxPos.y(), maxPos.z()), // right top front
  213. Vec3(minPos.x(), maxPos.y(), maxPos.z()), // left top front
  214. Vec3(minPos.x(), minPos.y(), maxPos.z()), // left bottom front
  215. Vec3(maxPos.x(), minPos.y(), maxPos.z()), // right bottom front
  216. Vec3(maxPos.x(), maxPos.y(), minPos.z()), // right top back
  217. Vec3(minPos.x(), maxPos.y(), minPos.z()), // left top back
  218. Vec3(minPos.x(), minPos.y(), minPos.z()), // left bottom back
  219. Vec3(maxPos.x(), minPos.y(), minPos.z()) // right bottom back
  220. }};
  221. static const Array<U32, 24> indeces = {{0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7}};
  222. begin(PrimitiveTopology::LINES);
  223. for(U32 id : indeces)
  224. {
  225. pushBackVertex(points[id]);
  226. }
  227. end();
  228. }
  229. void CollisionDebugDrawer::visit(const Sphere& sphere)
  230. {
  231. m_dbg->setModelMatrix(Mat4(sphere.getCenter().xyz1(), Mat3::getIdentity(), 1.0));
  232. m_dbg->drawSphere(sphere.getRadius());
  233. }
  234. void CollisionDebugDrawer::visit(const Obb& obb)
  235. {
  236. Mat4 scale(Mat4::getIdentity());
  237. scale(0, 0) = obb.getExtend().x();
  238. scale(1, 1) = obb.getExtend().y();
  239. scale(2, 2) = obb.getExtend().z();
  240. Mat4 tsl(Transform(obb.getCenter(), obb.getRotation(), 1.0));
  241. m_dbg->setModelMatrix(tsl * scale);
  242. m_dbg->drawCube(2.0);
  243. }
  244. void CollisionDebugDrawer::visit(const Plane& plane)
  245. {
  246. Vec3 n = plane.getNormal().xyz();
  247. const F32& o = plane.getOffset();
  248. Quat q;
  249. q.setFrom2Vec3(Vec3(0.0, 0.0, 1.0), n);
  250. Mat3 rot(q);
  251. rot.rotateXAxis(PI / 2.0);
  252. Mat4 trf(Vec4(n * o, 1.0), rot);
  253. m_dbg->setModelMatrix(trf);
  254. m_dbg->drawGrid();
  255. }
  256. void CollisionDebugDrawer::visit(const LineSegment& ls)
  257. {
  258. m_dbg->setModelMatrix(Mat4::getIdentity());
  259. m_dbg->begin(PrimitiveTopology::LINES);
  260. m_dbg->pushBackVertex(ls.getOrigin().xyz());
  261. m_dbg->pushBackVertex((ls.getOrigin() + ls.getDirection()).xyz());
  262. m_dbg->end();
  263. }
  264. void CollisionDebugDrawer::visit(const Aabb& aabb)
  265. {
  266. Vec3 min = aabb.getMin().xyz();
  267. Vec3 max = aabb.getMax().xyz();
  268. Mat4 trf = Mat4::getIdentity();
  269. // Scale
  270. for(U32 i = 0; i < 3; ++i)
  271. {
  272. trf(i, i) = max[i] - min[i];
  273. }
  274. // Translation
  275. trf.setTranslationPart(Vec4((max + min) / 2.0, 1.0));
  276. m_dbg->setModelMatrix(trf);
  277. m_dbg->drawCube();
  278. }
  279. void CollisionDebugDrawer::visit(const Frustum& f)
  280. {
  281. switch(f.getType())
  282. {
  283. case FrustumType::ORTHOGRAPHIC:
  284. visit(static_cast<const OrthographicFrustum&>(f).getObb());
  285. break;
  286. case FrustumType::PERSPECTIVE:
  287. {
  288. const PerspectiveFrustum& pf = static_cast<const PerspectiveFrustum&>(f);
  289. F32 camLen = pf.getFar();
  290. F32 tmp0 = camLen / tan((PI - pf.getFovX()) * 0.5) + 0.001;
  291. F32 tmp1 = camLen * tan(pf.getFovY() * 0.5) + 0.001;
  292. Vec3 points[] = {
  293. Vec3(0.0, 0.0, 0.0), // 0: eye point
  294. Vec3(-tmp0, tmp1, -camLen), // 1: top left
  295. Vec3(-tmp0, -tmp1, -camLen), // 2: bottom left
  296. Vec3(tmp0, -tmp1, -camLen), // 3: bottom right
  297. Vec3(tmp0, tmp1, -camLen) // 4: top right
  298. };
  299. const U32 indeces[] = {0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 2, 3, 3, 4, 4, 1};
  300. m_dbg->begin(PrimitiveTopology::LINES);
  301. for(U32 i = 0; i < sizeof(indeces) / sizeof(U32); i++)
  302. {
  303. m_dbg->pushBackVertex(points[indeces[i]]);
  304. }
  305. m_dbg->end();
  306. break;
  307. }
  308. }
  309. }
  310. void CollisionDebugDrawer::visit(const CompoundShape& cs)
  311. {
  312. CollisionDebugDrawer* self = this;
  313. Error err = cs.iterateShapes([&](const CollisionShape& a) -> Error {
  314. a.accept(*self);
  315. return ErrorCode::NONE;
  316. });
  317. (void)err;
  318. }
  319. void CollisionDebugDrawer::visit(const ConvexHullShape& hull)
  320. {
  321. m_dbg->setModelMatrix(Mat4(hull.getTransform()));
  322. m_dbg->begin(PrimitiveTopology::LINES);
  323. const Vec4* points = hull.getPoints() + 1;
  324. const Vec4* end = hull.getPoints() + hull.getPointsCount();
  325. for(; points != end; ++points)
  326. {
  327. m_dbg->pushBackVertex(hull.getPoints()->xyz());
  328. m_dbg->pushBackVertex(points->xyz());
  329. }
  330. m_dbg->end();
  331. }
  332. void PhysicsDebugDrawer::drawLines(const Vec3* lines, const U32 linesCount, const Vec4& color)
  333. {
  334. m_dbg->begin(PrimitiveTopology::LINES);
  335. m_dbg->setColor(color);
  336. for(U i = 0; i < linesCount * 2; ++i)
  337. {
  338. m_dbg->pushBackVertex(lines[i]);
  339. }
  340. m_dbg->end();
  341. }
  342. void SceneDebugDrawer::draw(const RenderableQueueElement& r) const
  343. {
  344. // TODO
  345. }
  346. void SceneDebugDrawer::draw(const PointLightQueueElement& light) const
  347. {
  348. m_dbg->setColor(light.m_diffuseColor);
  349. CollisionDebugDrawer coldraw(m_dbg);
  350. Sphere sphere(light.m_worldPosition.xyz0(), light.m_radius);
  351. sphere.accept(coldraw);
  352. }
  353. void SceneDebugDrawer::draw(const SpotLightQueueElement& light) const
  354. {
  355. // TODO
  356. }
  357. } // end namespace anki