DebugRenderer.cpp 22 KB

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