DebugRenderer.cpp 18 KB

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