DebugRenderer.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. //
  2. // Copyright (c) 2008-2016 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/AnimatedModel.h"
  27. #include "../Graphics/Camera.h"
  28. #include "../Graphics/DebugRenderer.h"
  29. #include "../Graphics/Graphics.h"
  30. #include "../Graphics/Light.h"
  31. #include "../Graphics/ShaderVariation.h"
  32. #include "../Graphics/VertexBuffer.h"
  33. #include "../Math/Polyhedron.h"
  34. #include "../Resource/ResourceCache.h"
  35. #include "../DebugNew.h"
  36. namespace Atomic
  37. {
  38. extern const char* SUBSYSTEM_CATEGORY;
  39. // Cap the amount of lines to prevent crash when eg. debug rendering large heightfields
  40. static const unsigned MAX_LINES = 1000000;
  41. // Cap the amount of triangles to prevent crash.
  42. static const unsigned MAX_TRIANGLES = 100000;
  43. DebugRenderer::DebugRenderer(Context* context) :
  44. Component(context),
  45. // ATOMIC BEGIN
  46. position1_(0, 0, 0),
  47. position2_(0, 0, 0),
  48. position3_(0, 0, 0),
  49. numGridLines_(100),
  50. scale_(0),
  51. lineLength_(0),
  52. offset_(0),
  53. scaleIncrement_(10)
  54. // ATOMIC END
  55. {
  56. vertexBuffer_ = new VertexBuffer(context_);
  57. SubscribeToEvent(E_ENDFRAME, ATOMIC_HANDLER(DebugRenderer, HandleEndFrame));
  58. }
  59. DebugRenderer::~DebugRenderer()
  60. {
  61. }
  62. void DebugRenderer::RegisterObject(Context* context)
  63. {
  64. context->RegisterFactory<DebugRenderer>(SUBSYSTEM_CATEGORY);
  65. }
  66. void DebugRenderer::SetView(Camera* camera)
  67. {
  68. if (!camera)
  69. return;
  70. view_ = camera->GetView();
  71. projection_ = camera->GetProjection();
  72. frustum_ = camera->GetFrustum();
  73. }
  74. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest)
  75. {
  76. AddLine(start, end, color.ToUInt(), depthTest);
  77. }
  78. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest)
  79. {
  80. if (lines_.Size() + noDepthLines_.Size() >= MAX_LINES)
  81. return;
  82. if (depthTest)
  83. lines_.Push(DebugLine(start, end, color));
  84. else
  85. noDepthLines_.Push(DebugLine(start, end, color));
  86. }
  87. void DebugRenderer::AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest)
  88. {
  89. AddTriangle(v1, v2, v3, color.ToUInt(), depthTest);
  90. }
  91. void DebugRenderer::AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest)
  92. {
  93. if (triangles_.Size() + noDepthTriangles_.Size() >= MAX_TRIANGLES)
  94. return;
  95. if (depthTest)
  96. triangles_.Push(DebugTriangle(v1, v2, v3, color));
  97. else
  98. noDepthTriangles_.Push(DebugTriangle(v1, v2, v3, color));
  99. }
  100. void DebugRenderer::AddNode(Node* node, float scale, bool depthTest)
  101. {
  102. if (!node)
  103. return;
  104. Vector3 start = node->GetWorldPosition();
  105. Quaternion rotation = node->GetWorldRotation();
  106. AddLine(start, start + rotation * (scale * Vector3::RIGHT), Color::RED.ToUInt(), depthTest);
  107. AddLine(start, start + rotation * (scale * Vector3::UP), Color::GREEN.ToUInt(), depthTest);
  108. AddLine(start, start + rotation * (scale * Vector3::FORWARD), Color::BLUE.ToUInt(), depthTest);
  109. }
  110. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest)
  111. {
  112. const Vector3& min = box.min_;
  113. const Vector3& max = box.max_;
  114. Vector3 v1(max.x_, min.y_, min.z_);
  115. Vector3 v2(max.x_, max.y_, min.z_);
  116. Vector3 v3(min.x_, max.y_, min.z_);
  117. Vector3 v4(min.x_, min.y_, max.z_);
  118. Vector3 v5(max.x_, min.y_, max.z_);
  119. Vector3 v6(min.x_, max.y_, max.z_);
  120. unsigned uintColor = color.ToUInt();
  121. AddLine(min, v1, uintColor, depthTest);
  122. AddLine(v1, v2, uintColor, depthTest);
  123. AddLine(v2, v3, uintColor, depthTest);
  124. AddLine(v3, min, uintColor, depthTest);
  125. AddLine(v4, v5, uintColor, depthTest);
  126. AddLine(v5, max, uintColor, depthTest);
  127. AddLine(max, v6, uintColor, depthTest);
  128. AddLine(v6, v4, uintColor, depthTest);
  129. AddLine(min, v4, uintColor, depthTest);
  130. AddLine(v1, v5, uintColor, depthTest);
  131. AddLine(v2, max, uintColor, depthTest);
  132. AddLine(v3, v6, uintColor, depthTest);
  133. }
  134. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest)
  135. {
  136. const Vector3& min = box.min_;
  137. const Vector3& max = box.max_;
  138. Vector3 v0(transform * min);
  139. Vector3 v1(transform * Vector3(max.x_, min.y_, min.z_));
  140. Vector3 v2(transform * Vector3(max.x_, max.y_, min.z_));
  141. Vector3 v3(transform * Vector3(min.x_, max.y_, min.z_));
  142. Vector3 v4(transform * Vector3(min.x_, min.y_, max.z_));
  143. Vector3 v5(transform * Vector3(max.x_, min.y_, max.z_));
  144. Vector3 v6(transform * Vector3(min.x_, max.y_, max.z_));
  145. Vector3 v7(transform * max);
  146. unsigned uintColor = color.ToUInt();
  147. AddLine(v0, v1, uintColor, depthTest);
  148. AddLine(v1, v2, uintColor, depthTest);
  149. AddLine(v2, v3, uintColor, depthTest);
  150. AddLine(v3, v0, uintColor, depthTest);
  151. AddLine(v4, v5, uintColor, depthTest);
  152. AddLine(v5, v7, uintColor, depthTest);
  153. AddLine(v7, v6, uintColor, depthTest);
  154. AddLine(v6, v4, uintColor, depthTest);
  155. AddLine(v0, v4, uintColor, depthTest);
  156. AddLine(v1, v5, uintColor, depthTest);
  157. AddLine(v2, v7, uintColor, depthTest);
  158. AddLine(v3, v6, uintColor, depthTest);
  159. }
  160. void DebugRenderer::AddFrustum(const Frustum& frustum, const Color& color, bool depthTest)
  161. {
  162. const Vector3* vertices = frustum.vertices_;
  163. unsigned uintColor = color.ToUInt();
  164. AddLine(vertices[0], vertices[1], uintColor, depthTest);
  165. AddLine(vertices[1], vertices[2], uintColor, depthTest);
  166. AddLine(vertices[2], vertices[3], uintColor, depthTest);
  167. AddLine(vertices[3], vertices[0], uintColor, depthTest);
  168. AddLine(vertices[4], vertices[5], uintColor, depthTest);
  169. AddLine(vertices[5], vertices[6], uintColor, depthTest);
  170. AddLine(vertices[6], vertices[7], uintColor, depthTest);
  171. AddLine(vertices[7], vertices[4], uintColor, depthTest);
  172. AddLine(vertices[0], vertices[4], uintColor, depthTest);
  173. AddLine(vertices[1], vertices[5], uintColor, depthTest);
  174. AddLine(vertices[2], vertices[6], uintColor, depthTest);
  175. AddLine(vertices[3], vertices[7], uintColor, depthTest);
  176. }
  177. void DebugRenderer::AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest)
  178. {
  179. unsigned uintColor = color.ToUInt();
  180. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  181. {
  182. const PODVector<Vector3>& face = poly.faces_[i];
  183. if (face.Size() >= 3)
  184. {
  185. for (unsigned j = 0; j < face.Size(); ++j)
  186. AddLine(face[j], face[(j + 1) % face.Size()], uintColor, depthTest);
  187. }
  188. }
  189. }
  190. static Vector3 PointOnSphere(const Sphere& sphere, unsigned theta, unsigned phi)
  191. {
  192. return Vector3(
  193. sphere.center_.x_ + sphere.radius_ * Sin((float)theta) * Sin((float)phi),
  194. sphere.center_.y_ + sphere.radius_ * Cos((float)phi),
  195. sphere.center_.z_ + sphere.radius_ * Cos((float)theta) * Sin((float)phi)
  196. );
  197. }
  198. void DebugRenderer::AddSphere(const Sphere& sphere, const Color& color, bool depthTest)
  199. {
  200. unsigned uintColor = color.ToUInt();
  201. for (unsigned j = 0; j < 180; j += 45)
  202. {
  203. for (unsigned i = 0; i < 360; i += 45)
  204. {
  205. Vector3 p1 = PointOnSphere(sphere, i, j);
  206. Vector3 p2 = PointOnSphere(sphere, i + 45, j);
  207. Vector3 p3 = PointOnSphere(sphere, i, j + 45);
  208. Vector3 p4 = PointOnSphere(sphere, i + 45, j + 45);
  209. AddLine(p1, p2, uintColor, depthTest);
  210. AddLine(p3, p4, uintColor, depthTest);
  211. AddLine(p1, p3, uintColor, depthTest);
  212. AddLine(p2, p4, uintColor, depthTest);
  213. }
  214. }
  215. }
  216. void DebugRenderer::AddCylinder(const Vector3& position, float radius, float height, const Color& color, bool depthTest)
  217. {
  218. Sphere sphere(position, radius);
  219. Vector3 heightVec(0, height, 0);
  220. Vector3 offsetXVec(radius, 0, 0);
  221. Vector3 offsetZVec(0, 0, radius);
  222. for (unsigned i = 0; i < 360; i += 45)
  223. {
  224. Vector3 p1 = PointOnSphere(sphere, i, 90);
  225. Vector3 p2 = PointOnSphere(sphere, i + 45, 90);
  226. AddLine(p1, p2, color, depthTest);
  227. AddLine(p1 + heightVec, p2 + heightVec, color, depthTest);
  228. }
  229. AddLine(position + offsetXVec, position + heightVec + offsetXVec, color, depthTest);
  230. AddLine(position - offsetXVec, position + heightVec - offsetXVec, color, depthTest);
  231. AddLine(position + offsetZVec, position + heightVec + offsetZVec, color, depthTest);
  232. AddLine(position - offsetZVec, position + heightVec - offsetZVec, color, depthTest);
  233. }
  234. void DebugRenderer::AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest)
  235. {
  236. const Vector<Bone>& bones = skeleton.GetBones();
  237. if (!bones.Size())
  238. return;
  239. unsigned uintColor = color.ToUInt();
  240. for (unsigned i = 0; i < bones.Size(); ++i)
  241. {
  242. // Skip if bone contains no skinned geometry
  243. if (bones[i].radius_ < M_EPSILON && bones[i].boundingBox_.Size().LengthSquared() < M_EPSILON)
  244. continue;
  245. Node* boneNode = bones[i].node_;
  246. if (!boneNode)
  247. continue;
  248. Vector3 start = boneNode->GetWorldPosition();
  249. Vector3 end;
  250. unsigned j = bones[i].parentIndex_;
  251. Node* parentNode = boneNode->GetParent();
  252. // If bone has a parent defined, and it also skins geometry, draw a line to it. Else draw the bone as a point
  253. if (parentNode && (bones[j].radius_ >= M_EPSILON || bones[j].boundingBox_.Size().LengthSquared() >= M_EPSILON))
  254. end = parentNode->GetWorldPosition();
  255. else
  256. end = start;
  257. AddLine(start, end, uintColor, depthTest);
  258. }
  259. }
  260. void DebugRenderer::AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize,
  261. unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest)
  262. {
  263. unsigned uintColor = color.ToUInt();
  264. const unsigned char* srcData = (const unsigned char*)vertexData;
  265. // 16-bit indices
  266. if (indexSize == sizeof(unsigned short))
  267. {
  268. const unsigned short* indices = ((const unsigned short*)indexData) + indexStart;
  269. const unsigned short* indicesEnd = indices + indexCount;
  270. while (indices < indicesEnd)
  271. {
  272. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  273. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  274. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  275. AddLine(v0, v1, uintColor, depthTest);
  276. AddLine(v1, v2, uintColor, depthTest);
  277. AddLine(v2, v0, uintColor, depthTest);
  278. indices += 3;
  279. }
  280. }
  281. else
  282. {
  283. const unsigned* indices = ((const unsigned*)indexData) + indexStart;
  284. const unsigned* indicesEnd = indices + indexCount;
  285. while (indices < indicesEnd)
  286. {
  287. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  288. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  289. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  290. AddLine(v0, v1, uintColor, depthTest);
  291. AddLine(v1, v2, uintColor, depthTest);
  292. AddLine(v2, v0, uintColor, depthTest);
  293. indices += 3;
  294. }
  295. }
  296. }
  297. void DebugRenderer::AddCircle(const Vector3& center, const Vector3& normal, float radius, const Color& color, int steps, bool depthTest)
  298. {
  299. Quaternion orientation;
  300. orientation.FromRotationTo(Vector3::UP, normal.Normalized());
  301. Vector3 p = orientation * Vector3(radius, 0, 0) + center;
  302. unsigned uintColor = color.ToUInt();
  303. for(int i = 1; i <= steps; ++i)
  304. {
  305. const float angle = (float)i / (float)steps * 360.0f;
  306. Vector3 v(radius * Cos(angle), 0, radius * Sin(angle));
  307. Vector3 c = orientation * v + center;
  308. AddLine(p, c, uintColor, depthTest);
  309. p = c;
  310. }
  311. p = center + normal * (radius / 4.0f);
  312. AddLine(center, p, uintColor, depthTest);
  313. }
  314. void DebugRenderer::AddCross(const Vector3& center, float size, const Color& color, bool depthTest)
  315. {
  316. unsigned uintColor = color.ToUInt();
  317. float halfSize = size / 2.0f;
  318. for (int i = 0; i < 3; ++i)
  319. {
  320. float start[3] = { center.x_, center.y_, center.z_ };
  321. float end[3] = { center.x_, center.y_, center.z_ };
  322. start[i] -= halfSize;
  323. end[i] += halfSize;
  324. AddLine(Vector3(start), Vector3(end), uintColor, depthTest);
  325. }
  326. }
  327. void DebugRenderer::AddQuad(const Vector3& center, float width, float height, const Color& color, bool depthTest)
  328. {
  329. unsigned uintColor = color.ToUInt();
  330. Vector3 v0(center.x_ - width / 2, center.y_, center.z_ - height / 2);
  331. Vector3 v1(center.x_ + width / 2, center.y_, center.z_ - height / 2);
  332. Vector3 v2(center.x_ + width / 2, center.y_, center.z_ + height / 2);
  333. Vector3 v3(center.x_ - width / 2, center.y_, center.z_ + height / 2);
  334. AddLine(v0, v1, uintColor, depthTest);
  335. AddLine(v1, v2, uintColor, depthTest);
  336. AddLine(v2, v3, uintColor, depthTest);
  337. AddLine(v3, v0, uintColor, depthTest);
  338. }
  339. void DebugRenderer::Render()
  340. {
  341. if (!HasContent())
  342. return;
  343. Graphics* graphics = GetSubsystem<Graphics>();
  344. // Engine does not render when window is closed or device is lost
  345. assert(graphics && graphics->IsInitialized() && !graphics->IsDeviceLost());
  346. ATOMIC_PROFILE(RenderDebugGeometry);
  347. ShaderVariation* vs = graphics->GetShader(VS, "Basic", "VERTEXCOLOR");
  348. ShaderVariation* ps = graphics->GetShader(PS, "Basic", "VERTEXCOLOR");
  349. unsigned numVertices = (lines_.Size() + noDepthLines_.Size()) * 2 + (triangles_.Size() + noDepthTriangles_.Size()) * 3;
  350. // Resize the vertex buffer if too small or much too large
  351. if (vertexBuffer_->GetVertexCount() < numVertices || vertexBuffer_->GetVertexCount() > numVertices * 2)
  352. vertexBuffer_->SetSize(numVertices, MASK_POSITION | MASK_COLOR, true);
  353. float* dest = (float*)vertexBuffer_->Lock(0, numVertices, true);
  354. if (!dest)
  355. return;
  356. for (unsigned i = 0; i < lines_.Size(); ++i)
  357. {
  358. const DebugLine& line = lines_[i];
  359. dest[0] = line.start_.x_;
  360. dest[1] = line.start_.y_;
  361. dest[2] = line.start_.z_;
  362. ((unsigned&)dest[3]) = line.color_;
  363. dest[4] = line.end_.x_;
  364. dest[5] = line.end_.y_;
  365. dest[6] = line.end_.z_;
  366. ((unsigned&)dest[7]) = line.color_;
  367. dest += 8;
  368. }
  369. for (unsigned i = 0; i < noDepthLines_.Size(); ++i)
  370. {
  371. const DebugLine& line = noDepthLines_[i];
  372. dest[0] = line.start_.x_;
  373. dest[1] = line.start_.y_;
  374. dest[2] = line.start_.z_;
  375. ((unsigned&)dest[3]) = line.color_;
  376. dest[4] = line.end_.x_;
  377. dest[5] = line.end_.y_;
  378. dest[6] = line.end_.z_;
  379. ((unsigned&)dest[7]) = line.color_;
  380. dest += 8;
  381. }
  382. for (unsigned i = 0; i < triangles_.Size(); ++i)
  383. {
  384. const DebugTriangle& triangle = triangles_[i];
  385. dest[0] = triangle.v1_.x_;
  386. dest[1] = triangle.v1_.y_;
  387. dest[2] = triangle.v1_.z_;
  388. ((unsigned&)dest[3]) = triangle.color_;
  389. dest[4] = triangle.v2_.x_;
  390. dest[5] = triangle.v2_.y_;
  391. dest[6] = triangle.v2_.z_;
  392. ((unsigned&)dest[7]) = triangle.color_;
  393. dest[8] = triangle.v3_.x_;
  394. dest[9] = triangle.v3_.y_;
  395. dest[10] = triangle.v3_.z_;
  396. ((unsigned&)dest[11]) = triangle.color_;
  397. dest += 12;
  398. }
  399. for (unsigned i = 0; i < noDepthTriangles_.Size(); ++i)
  400. {
  401. const DebugTriangle& triangle = noDepthTriangles_[i];
  402. dest[0] = triangle.v1_.x_;
  403. dest[1] = triangle.v1_.y_;
  404. dest[2] = triangle.v1_.z_;
  405. ((unsigned&)dest[3]) = triangle.color_;
  406. dest[4] = triangle.v2_.x_;
  407. dest[5] = triangle.v2_.y_;
  408. dest[6] = triangle.v2_.z_;
  409. ((unsigned&)dest[7]) = triangle.color_;
  410. dest[8] = triangle.v3_.x_;
  411. dest[9] = triangle.v3_.y_;
  412. dest[10] = triangle.v3_.z_;
  413. ((unsigned&)dest[11]) = triangle.color_;
  414. dest += 12;
  415. }
  416. vertexBuffer_->Unlock();
  417. graphics->SetBlendMode(BLEND_REPLACE);
  418. graphics->SetColorWrite(true);
  419. graphics->SetCullMode(CULL_NONE);
  420. graphics->SetDepthWrite(true);
  421. graphics->SetScissorTest(false);
  422. graphics->SetStencilTest(false);
  423. graphics->SetShaders(vs, ps);
  424. graphics->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  425. graphics->SetShaderParameter(VSP_VIEW, view_);
  426. graphics->SetShaderParameter(VSP_VIEWINV, view_.Inverse());
  427. graphics->SetShaderParameter(VSP_VIEWPROJ, projection_ * view_);
  428. graphics->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  429. graphics->SetVertexBuffer(vertexBuffer_);
  430. unsigned start = 0;
  431. unsigned count = 0;
  432. if (lines_.Size())
  433. {
  434. count = lines_.Size() * 2;
  435. graphics->SetDepthTest(CMP_LESSEQUAL);
  436. graphics->Draw(LINE_LIST, start, count);
  437. start += count;
  438. }
  439. if (noDepthLines_.Size())
  440. {
  441. count = noDepthLines_.Size() * 2;
  442. graphics->SetDepthTest(CMP_ALWAYS);
  443. graphics->Draw(LINE_LIST, start, count);
  444. start += count;
  445. }
  446. graphics->SetBlendMode(BLEND_ALPHA);
  447. if (triangles_.Size())
  448. {
  449. count = triangles_.Size() * 3;
  450. graphics->SetDepthTest(CMP_LESSEQUAL);
  451. graphics->Draw(TRIANGLE_LIST, start, count);
  452. start += count;
  453. }
  454. if (noDepthTriangles_.Size())
  455. {
  456. count = noDepthTriangles_.Size() * 3;
  457. graphics->SetDepthTest(CMP_ALWAYS);
  458. graphics->Draw(TRIANGLE_LIST, start, count);
  459. }
  460. }
  461. bool DebugRenderer::IsInside(const BoundingBox& box) const
  462. {
  463. return frustum_.IsInsideFast(box) == INSIDE;
  464. }
  465. bool DebugRenderer::HasContent() const
  466. {
  467. return !(lines_.Empty() && noDepthLines_.Empty() && triangles_.Empty() && noDepthTriangles_.Empty());
  468. }
  469. void DebugRenderer::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  470. {
  471. // When the amount of debug geometry is reduced, release memory
  472. unsigned linesSize = lines_.Size();
  473. unsigned noDepthLinesSize = noDepthLines_.Size();
  474. unsigned trianglesSize = triangles_.Size();
  475. unsigned noDepthTrianglesSize = noDepthTriangles_.Size();
  476. lines_.Clear();
  477. noDepthLines_.Clear();
  478. triangles_.Clear();
  479. noDepthTriangles_.Clear();
  480. if (lines_.Capacity() > linesSize * 2)
  481. lines_.Reserve(linesSize);
  482. if (noDepthLines_.Capacity() > noDepthLinesSize * 2)
  483. noDepthLines_.Reserve(noDepthLinesSize);
  484. if (triangles_.Capacity() > trianglesSize * 2)
  485. triangles_.Reserve(trianglesSize);
  486. if (noDepthTriangles_.Capacity() > noDepthTrianglesSize * 2)
  487. noDepthTriangles_.Reserve(noDepthTrianglesSize);
  488. }
  489. // ATOMIC BEGIN
  490. void DebugRenderer::CreateXAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z)
  491. {
  492. for (int i = 0; i < numGridLines_; i++)
  493. {
  494. position1_ = Vector3(x, y, z - offset_);
  495. position2_ = Vector3(position1_.x_ + lineLength_, y, z - offset_);
  496. position3_ = Vector3(position1_.x_ + -lineLength_, y, z - offset_);
  497. AddLine(position1_, position2_, gridColor, depthTest);
  498. AddLine(position3_, position1_, gridColor, depthTest);
  499. z += scale_;
  500. }
  501. }
  502. void DebugRenderer::CreateZAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z)
  503. {
  504. for (int j = 0; j < numGridLines_; j++)
  505. {
  506. position1_ = Vector3(x - offset_, y, z);
  507. position2_ = Vector3(x - offset_, y, position1_.z_ + lineLength_);
  508. position3_ = Vector3(x - offset_, y, position1_.z_ + -lineLength_);
  509. AddLine(position1_, position2_, gridColor, depthTest);
  510. AddLine(position3_, position1_, gridColor, depthTest);
  511. x += scale_;
  512. }
  513. }
  514. void DebugRenderer::CreateGrid(const Color& grid, bool depthTest, Vector3 position)
  515. {
  516. unsigned gridColor = grid.ToUInt();
  517. scale_ = position.y_ / scaleIncrement_;
  518. if (position.y_ < scaleIncrement_)
  519. scale_ = 1;
  520. lineLength_ = (numGridLines_ / 2) * scale_;
  521. offset_ = (numGridLines_ / 2) * scale_;
  522. CreateXAxisLines(gridColor, depthTest, 0, 0, 0);
  523. CreateZAxisLines(gridColor, depthTest, 0, 0, 0);
  524. }
  525. // ATOMIC END
  526. }