DebugRenderer.cpp 24 KB

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