DebugRenderer.cpp 25 KB

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