DebugRenderer.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Core/CoreEvents.h"
  25. #include "../Core/Profiler.h"
  26. #include "../Graphics/Camera.h"
  27. #include "../Graphics/DebugRenderer.h"
  28. #include "../Graphics/Graphics.h"
  29. #include "../Graphics/Light.h"
  30. #include "../Graphics/ShaderVariation.h"
  31. #include "../Graphics/VertexBuffer.h"
  32. #include "../Math/Polyhedron.h"
  33. #include "../Resource/ResourceCache.h"
  34. #include "../DebugNew.h"
  35. #ifdef ATOMIC_3D
  36. #include "../Atomic3D/AnimatedModel.h"
  37. #else
  38. #include "../Scene/Node.h"
  39. #endif
  40. namespace Atomic
  41. {
  42. extern const char* SUBSYSTEM_CATEGORY;
  43. // Cap the amount of lines to prevent crash when eg. debug rendering large heightfields
  44. static const unsigned MAX_LINES = 1000000;
  45. // Cap the amount of triangles to prevent crash.
  46. static const unsigned MAX_TRIANGLES = 100000;
  47. DebugRenderer::DebugRenderer(Context* context) :
  48. Component(context),
  49. position1_(0, 0, 0),
  50. position2_(0, 0, 0),
  51. position3_(0, 0, 0),
  52. numGridLines_(100),
  53. scale_(0),
  54. lineLength_(0),
  55. offset_(0),
  56. scaleIncrement_(10)
  57. {
  58. vertexBuffer_ = new VertexBuffer(context_);
  59. SubscribeToEvent(E_ENDFRAME, HANDLER(DebugRenderer, HandleEndFrame));
  60. }
  61. DebugRenderer::~DebugRenderer()
  62. {
  63. }
  64. void DebugRenderer::RegisterObject(Context* context)
  65. {
  66. context->RegisterFactory<DebugRenderer>(SUBSYSTEM_CATEGORY);
  67. }
  68. void DebugRenderer::SetView(Camera* camera)
  69. {
  70. if (!camera)
  71. return;
  72. view_ = camera->GetView();
  73. projection_ = camera->GetProjection();
  74. frustum_ = camera->GetFrustum();
  75. }
  76. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest)
  77. {
  78. AddLine(start, end, color.ToUInt(), depthTest);
  79. }
  80. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest)
  81. {
  82. if (lines_.Size() + noDepthLines_.Size() >= MAX_LINES)
  83. return;
  84. if (depthTest)
  85. lines_.Push(DebugLine(start, end, color));
  86. else
  87. noDepthLines_.Push(DebugLine(start, end, color));
  88. }
  89. void DebugRenderer::AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest)
  90. {
  91. AddTriangle(v1, v2, v3, color.ToUInt(), depthTest);
  92. }
  93. void DebugRenderer::AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest)
  94. {
  95. if (triangles_.Size() + noDepthTriangles_.Size() >= MAX_TRIANGLES)
  96. return;
  97. if (depthTest)
  98. triangles_.Push(DebugTriangle(v1, v2, v3, color));
  99. else
  100. noDepthTriangles_.Push(DebugTriangle(v1, v2, v3, color));
  101. }
  102. void DebugRenderer::AddNode(Node* node, float scale, bool depthTest)
  103. {
  104. if (!node)
  105. return;
  106. Vector3 start = node->GetWorldPosition();
  107. Quaternion rotation = node->GetWorldRotation();
  108. AddLine(start, start + rotation * (scale * Vector3::RIGHT), Color::RED.ToUInt(), depthTest);
  109. AddLine(start, start + rotation * (scale * Vector3::UP), Color::GREEN.ToUInt(), depthTest);
  110. AddLine(start, start + rotation * (scale * Vector3::FORWARD), Color::BLUE.ToUInt(), depthTest);
  111. }
  112. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest)
  113. {
  114. const Vector3& min = box.min_;
  115. const Vector3& max = box.max_;
  116. Vector3 v1(max.x_, min.y_, min.z_);
  117. Vector3 v2(max.x_, max.y_, min.z_);
  118. Vector3 v3(min.x_, max.y_, min.z_);
  119. Vector3 v4(min.x_, min.y_, max.z_);
  120. Vector3 v5(max.x_, min.y_, max.z_);
  121. Vector3 v6(min.x_, max.y_, max.z_);
  122. unsigned uintColor = color.ToUInt();
  123. AddLine(min, v1, uintColor, depthTest);
  124. AddLine(v1, v2, uintColor, depthTest);
  125. AddLine(v2, v3, uintColor, depthTest);
  126. AddLine(v3, min, uintColor, depthTest);
  127. AddLine(v4, v5, uintColor, depthTest);
  128. AddLine(v5, max, uintColor, depthTest);
  129. AddLine(max, v6, uintColor, depthTest);
  130. AddLine(v6, v4, uintColor, depthTest);
  131. AddLine(min, v4, uintColor, depthTest);
  132. AddLine(v1, v5, uintColor, depthTest);
  133. AddLine(v2, max, uintColor, depthTest);
  134. AddLine(v3, v6, uintColor, depthTest);
  135. }
  136. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest)
  137. {
  138. const Vector3& min = box.min_;
  139. const Vector3& max = box.max_;
  140. Vector3 v0(transform * min);
  141. Vector3 v1(transform * Vector3(max.x_, min.y_, min.z_));
  142. Vector3 v2(transform * Vector3(max.x_, max.y_, min.z_));
  143. Vector3 v3(transform * Vector3(min.x_, max.y_, min.z_));
  144. Vector3 v4(transform * Vector3(min.x_, min.y_, max.z_));
  145. Vector3 v5(transform * Vector3(max.x_, min.y_, max.z_));
  146. Vector3 v6(transform * Vector3(min.x_, max.y_, max.z_));
  147. Vector3 v7(transform * max);
  148. unsigned uintColor = color.ToUInt();
  149. AddLine(v0, v1, uintColor, depthTest);
  150. AddLine(v1, v2, uintColor, depthTest);
  151. AddLine(v2, v3, uintColor, depthTest);
  152. AddLine(v3, v0, uintColor, depthTest);
  153. AddLine(v4, v5, uintColor, depthTest);
  154. AddLine(v5, v7, uintColor, depthTest);
  155. AddLine(v7, v6, uintColor, depthTest);
  156. AddLine(v6, v4, uintColor, depthTest);
  157. AddLine(v0, v4, uintColor, depthTest);
  158. AddLine(v1, v5, uintColor, depthTest);
  159. AddLine(v2, v7, uintColor, depthTest);
  160. AddLine(v3, v6, uintColor, depthTest);
  161. }
  162. void DebugRenderer::AddFrustum(const Frustum& frustum, const Color& color, bool depthTest)
  163. {
  164. const Vector3* vertices = frustum.vertices_;
  165. unsigned uintColor = color.ToUInt();
  166. AddLine(vertices[0], vertices[1], uintColor, depthTest);
  167. AddLine(vertices[1], vertices[2], uintColor, depthTest);
  168. AddLine(vertices[2], vertices[3], uintColor, depthTest);
  169. AddLine(vertices[3], vertices[0], uintColor, depthTest);
  170. AddLine(vertices[4], vertices[5], uintColor, depthTest);
  171. AddLine(vertices[5], vertices[6], uintColor, depthTest);
  172. AddLine(vertices[6], vertices[7], uintColor, depthTest);
  173. AddLine(vertices[7], vertices[4], uintColor, depthTest);
  174. AddLine(vertices[0], vertices[4], uintColor, depthTest);
  175. AddLine(vertices[1], vertices[5], uintColor, depthTest);
  176. AddLine(vertices[2], vertices[6], uintColor, depthTest);
  177. AddLine(vertices[3], vertices[7], uintColor, depthTest);
  178. }
  179. void DebugRenderer::AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest)
  180. {
  181. unsigned uintColor = color.ToUInt();
  182. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  183. {
  184. const PODVector<Vector3>& face = poly.faces_[i];
  185. if (face.Size() >= 3)
  186. {
  187. for (unsigned j = 0; j < face.Size(); ++j)
  188. AddLine(face[j], face[(j + 1) % face.Size()], uintColor, depthTest);
  189. }
  190. }
  191. }
  192. static Vector3 PointOnSphere(const Sphere& sphere, unsigned theta, unsigned phi)
  193. {
  194. return Vector3(
  195. sphere.center_.x_ + sphere.radius_ * Sin((float)theta) * Sin((float)phi),
  196. sphere.center_.y_ + sphere.radius_ * Cos((float)phi),
  197. sphere.center_.z_ + sphere.radius_ * Cos((float)theta) * Sin((float)phi)
  198. );
  199. }
  200. void DebugRenderer::AddSphere(const Sphere& sphere, const Color& color, bool depthTest)
  201. {
  202. unsigned uintColor = color.ToUInt();
  203. for (unsigned j = 0; j < 180; j += 45)
  204. {
  205. for (unsigned i = 0; i < 360; i += 45)
  206. {
  207. Vector3 p1 = PointOnSphere(sphere, i, j);
  208. Vector3 p2 = PointOnSphere(sphere, i + 45, j);
  209. Vector3 p3 = PointOnSphere(sphere, i, j + 45);
  210. Vector3 p4 = PointOnSphere(sphere, i + 45, j + 45);
  211. AddLine(p1, p2, uintColor, depthTest);
  212. AddLine(p3, p4, uintColor, depthTest);
  213. AddLine(p1, p3, uintColor, depthTest);
  214. AddLine(p2, p4, uintColor, depthTest);
  215. }
  216. }
  217. }
  218. void DebugRenderer::AddCylinder(const Vector3& position, float radius, float height, const Color& color, bool depthTest)
  219. {
  220. Sphere sphere(position, radius);
  221. Vector3 heightVec(0, height, 0);
  222. Vector3 offsetXVec(radius, 0, 0);
  223. Vector3 offsetZVec(0, 0, radius);
  224. for (unsigned i = 0; i < 360; i += 45)
  225. {
  226. Vector3 p1 = PointOnSphere(sphere, i, 90);
  227. Vector3 p2 = PointOnSphere(sphere, i + 45, 90);
  228. AddLine(p1, p2, color, depthTest);
  229. AddLine(p1 + heightVec, p2 + heightVec, color, depthTest);
  230. }
  231. AddLine(position + offsetXVec, position + heightVec + offsetXVec, color, depthTest);
  232. AddLine(position - offsetXVec, position + heightVec - offsetXVec, color, depthTest);
  233. AddLine(position + offsetZVec, position + heightVec + offsetZVec, color, depthTest);
  234. AddLine(position - offsetZVec, position + heightVec - offsetZVec, color, depthTest);
  235. }
  236. #ifdef ATOMIC_3D
  237. void DebugRenderer::AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest)
  238. {
  239. const Vector<Bone>& bones = skeleton.GetBones();
  240. if (!bones.Size())
  241. return;
  242. unsigned uintColor = color.ToUInt();
  243. for (unsigned i = 0; i < bones.Size(); ++i)
  244. {
  245. // Skip if bone contains no skinned geometry
  246. if (bones[i].radius_ < M_EPSILON && bones[i].boundingBox_.Size().LengthSquared() < M_EPSILON)
  247. continue;
  248. Node* boneNode = bones[i].node_;
  249. if (!boneNode)
  250. continue;
  251. Vector3 start = boneNode->GetWorldPosition();
  252. Vector3 end;
  253. unsigned j = bones[i].parentIndex_;
  254. Node* parentNode = boneNode->GetParent();
  255. // If bone has a parent defined, and it also skins geometry, draw a line to it. Else draw the bone as a point
  256. if (parentNode && (bones[j].radius_ >= M_EPSILON || bones[j].boundingBox_.Size().LengthSquared() >= M_EPSILON))
  257. end = parentNode->GetWorldPosition();
  258. else
  259. end = start;
  260. AddLine(start, end, uintColor, depthTest);
  261. }
  262. }
  263. #endif
  264. void DebugRenderer::AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize,
  265. unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest)
  266. {
  267. unsigned uintColor = color.ToUInt();
  268. const unsigned char* srcData = (const unsigned char*)vertexData;
  269. // 16-bit indices
  270. if (indexSize == sizeof(unsigned short))
  271. {
  272. const unsigned short* indices = ((const unsigned short*)indexData) + indexStart;
  273. const unsigned short* indicesEnd = indices + indexCount;
  274. while (indices < indicesEnd)
  275. {
  276. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  277. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  278. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  279. AddLine(v0, v1, uintColor, depthTest);
  280. AddLine(v1, v2, uintColor, depthTest);
  281. AddLine(v2, v0, uintColor, depthTest);
  282. indices += 3;
  283. }
  284. }
  285. else
  286. {
  287. const unsigned* indices = ((const unsigned*)indexData) + indexStart;
  288. const unsigned* indicesEnd = indices + indexCount;
  289. while (indices < indicesEnd)
  290. {
  291. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  292. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  293. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  294. AddLine(v0, v1, uintColor, depthTest);
  295. AddLine(v1, v2, uintColor, depthTest);
  296. AddLine(v2, v0, uintColor, depthTest);
  297. indices += 3;
  298. }
  299. }
  300. }
  301. void DebugRenderer::CreateXAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z)
  302. {
  303. for (int i = 0; i < numGridLines_; i++)
  304. {
  305. position1_ = Vector3(x, y, z - offset_);
  306. position2_ = Vector3(position1_.x_ + lineLength_, y, z - offset_);
  307. position3_ = Vector3(position1_.x_ + -lineLength_, y, z - offset_);
  308. AddLine(position1_, position2_, gridColor, depthTest);
  309. AddLine(position3_, position1_, gridColor, depthTest);
  310. z += scale_;
  311. }
  312. }
  313. void DebugRenderer::CreateZAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z)
  314. {
  315. for (int j = 0; j < numGridLines_; j++)
  316. {
  317. position1_ = Vector3(x - offset_, y, z);
  318. position2_ = Vector3(x - offset_, y, position1_.z_ + lineLength_);
  319. position3_ = Vector3(x - offset_, y, position1_.z_ + -lineLength_);
  320. AddLine(position1_, position2_, gridColor, depthTest);
  321. AddLine(position3_, position1_, gridColor, depthTest);
  322. x += scale_;
  323. }
  324. }
  325. void DebugRenderer::CreateGrid(const Color& grid, bool depthTest, Vector3 position)
  326. {
  327. unsigned gridColor = grid.ToUInt();
  328. scale_ = position.y_ / scaleIncrement_;
  329. if (position.y_ < scaleIncrement_)
  330. scale_ = 1;
  331. lineLength_ = (numGridLines_ / 2) * scale_;
  332. offset_ = (numGridLines_ / 2) * scale_;
  333. CreateXAxisLines(gridColor, depthTest, 0, 0, 0);
  334. CreateZAxisLines(gridColor, depthTest, 0, 0, 0);
  335. }
  336. void DebugRenderer::Render()
  337. {
  338. if (!HasContent())
  339. return;
  340. Graphics* graphics = GetSubsystem<Graphics>();
  341. // Engine does not render when window is closed or device is lost
  342. assert(graphics && graphics->IsInitialized() && !graphics->IsDeviceLost());
  343. PROFILE(RenderDebugGeometry);
  344. ShaderVariation* vs = graphics->GetShader(VS, "Basic", "VERTEXCOLOR");
  345. ShaderVariation* ps = graphics->GetShader(PS, "Basic", "VERTEXCOLOR");
  346. unsigned numVertices = (lines_.Size() + noDepthLines_.Size()) * 2 + (triangles_.Size() + noDepthTriangles_.Size()) * 3;
  347. // Resize the vertex buffer if too small or much too large
  348. if (vertexBuffer_->GetVertexCount() < numVertices || vertexBuffer_->GetVertexCount() > numVertices * 2)
  349. vertexBuffer_->SetSize(numVertices, MASK_POSITION | MASK_COLOR, true);
  350. float* dest = (float*)vertexBuffer_->Lock(0, numVertices, true);
  351. if (!dest)
  352. return;
  353. for (unsigned i = 0; i < lines_.Size(); ++i)
  354. {
  355. const DebugLine& line = lines_[i];
  356. dest[0] = line.start_.x_;
  357. dest[1] = line.start_.y_;
  358. dest[2] = line.start_.z_;
  359. ((unsigned&)dest[3]) = line.color_;
  360. dest[4] = line.end_.x_;
  361. dest[5] = line.end_.y_;
  362. dest[6] = line.end_.z_;
  363. ((unsigned&)dest[7]) = line.color_;
  364. dest += 8;
  365. }
  366. for (unsigned i = 0; i < noDepthLines_.Size(); ++i)
  367. {
  368. const DebugLine& line = noDepthLines_[i];
  369. dest[0] = line.start_.x_;
  370. dest[1] = line.start_.y_;
  371. dest[2] = line.start_.z_;
  372. ((unsigned&)dest[3]) = line.color_;
  373. dest[4] = line.end_.x_;
  374. dest[5] = line.end_.y_;
  375. dest[6] = line.end_.z_;
  376. ((unsigned&)dest[7]) = line.color_;
  377. dest += 8;
  378. }
  379. for (unsigned i = 0; i < triangles_.Size(); ++i)
  380. {
  381. const DebugTriangle& triangle = triangles_[i];
  382. dest[0] = triangle.v1_.x_;
  383. dest[1] = triangle.v1_.y_;
  384. dest[2] = triangle.v1_.z_;
  385. ((unsigned&)dest[3]) = triangle.color_;
  386. dest[4] = triangle.v2_.x_;
  387. dest[5] = triangle.v2_.y_;
  388. dest[6] = triangle.v2_.z_;
  389. ((unsigned&)dest[7]) = triangle.color_;
  390. dest[8] = triangle.v3_.x_;
  391. dest[9] = triangle.v3_.y_;
  392. dest[10] = triangle.v3_.z_;
  393. ((unsigned&)dest[11]) = triangle.color_;
  394. dest += 12;
  395. }
  396. for (unsigned i = 0; i < noDepthTriangles_.Size(); ++i)
  397. {
  398. const DebugTriangle& triangle = noDepthTriangles_[i];
  399. dest[0] = triangle.v1_.x_;
  400. dest[1] = triangle.v1_.y_;
  401. dest[2] = triangle.v1_.z_;
  402. ((unsigned&)dest[3]) = triangle.color_;
  403. dest[4] = triangle.v2_.x_;
  404. dest[5] = triangle.v2_.y_;
  405. dest[6] = triangle.v2_.z_;
  406. ((unsigned&)dest[7]) = triangle.color_;
  407. dest[8] = triangle.v3_.x_;
  408. dest[9] = triangle.v3_.y_;
  409. dest[10] = triangle.v3_.z_;
  410. ((unsigned&)dest[11]) = triangle.color_;
  411. dest += 12;
  412. }
  413. vertexBuffer_->Unlock();
  414. graphics->SetBlendMode(BLEND_REPLACE);
  415. graphics->SetColorWrite(true);
  416. graphics->SetCullMode(CULL_NONE);
  417. graphics->SetDepthWrite(true);
  418. graphics->SetScissorTest(false);
  419. graphics->SetStencilTest(false);
  420. graphics->SetShaders(vs, ps);
  421. graphics->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  422. graphics->SetShaderParameter(VSP_VIEWPROJ, projection_ * view_);
  423. graphics->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  424. graphics->SetVertexBuffer(vertexBuffer_);
  425. unsigned start = 0;
  426. unsigned count = 0;
  427. if (lines_.Size())
  428. {
  429. count = lines_.Size() * 2;
  430. graphics->SetDepthTest(CMP_LESSEQUAL);
  431. graphics->Draw(LINE_LIST, start, count);
  432. start += count;
  433. }
  434. if (noDepthLines_.Size())
  435. {
  436. count = noDepthLines_.Size() * 2;
  437. graphics->SetDepthTest(CMP_ALWAYS);
  438. graphics->Draw(LINE_LIST, start, count);
  439. start += count;
  440. }
  441. graphics->SetBlendMode(BLEND_ALPHA);
  442. if (triangles_.Size())
  443. {
  444. count = triangles_.Size() * 3;
  445. graphics->SetDepthTest(CMP_LESSEQUAL);
  446. graphics->Draw(TRIANGLE_LIST, start, count);
  447. start += count;
  448. }
  449. if (noDepthTriangles_.Size())
  450. {
  451. count = noDepthTriangles_.Size() * 3;
  452. graphics->SetDepthTest(CMP_ALWAYS);
  453. graphics->Draw(TRIANGLE_LIST, start, count);
  454. }
  455. }
  456. bool DebugRenderer::IsInside(const BoundingBox& box) const
  457. {
  458. return frustum_.IsInsideFast(box) == INSIDE;
  459. }
  460. bool DebugRenderer::HasContent() const
  461. {
  462. return !(lines_.Empty() && noDepthLines_.Empty() && triangles_.Empty() && noDepthTriangles_.Empty());
  463. }
  464. void DebugRenderer::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  465. {
  466. // When the amount of debug geometry is reduced, release memory
  467. unsigned linesSize = lines_.Size();
  468. unsigned noDepthLinesSize = noDepthLines_.Size();
  469. unsigned trianglesSize = triangles_.Size();
  470. unsigned noDepthTrianglesSize = noDepthTriangles_.Size();
  471. lines_.Clear();
  472. noDepthLines_.Clear();
  473. triangles_.Clear();
  474. noDepthTriangles_.Clear();
  475. if (lines_.Capacity() > linesSize * 2)
  476. lines_.Reserve(linesSize);
  477. if (noDepthLines_.Capacity() > noDepthLinesSize * 2)
  478. noDepthLines_.Reserve(noDepthLinesSize);
  479. if (triangles_.Capacity() > trianglesSize * 2)
  480. triangles_.Reserve(trianglesSize);
  481. if (noDepthTriangles_.Capacity() > noDepthTrianglesSize * 2)
  482. noDepthTriangles_.Reserve(noDepthTrianglesSize);
  483. }
  484. }