DebugRenderer.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. //
  2. // Copyright (c) 2008-2014 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 "../Graphics/Camera.h"
  24. #include "../Core/Context.h"
  25. #include "../Core/CoreEvents.h"
  26. #include "../Graphics/DebugRenderer.h"
  27. #include "../Graphics/Graphics.h"
  28. #include "../Graphics/Light.h"
  29. #include "../Math/Polyhedron.h"
  30. #include "../Core/Profiler.h"
  31. #include "../Resource/ResourceCache.h"
  32. #include "../Graphics/ShaderVariation.h"
  33. #include "../Graphics/VertexBuffer.h"
  34. #include "../DebugNew.h"
  35. #include "../Scene/Node.h"
  36. #ifdef ATOMIC_3D
  37. #include "../Atomic3D/AnimatedModel.h"
  38. #endif
  39. namespace Atomic
  40. {
  41. extern const char* SUBSYSTEM_CATEGORY;
  42. // Cap the amount of lines to prevent crash when eg. debug rendering large heightfields
  43. static const unsigned MAX_LINES = 1000000;
  44. // Cap the amount of triangles to prevent crash.
  45. static const unsigned MAX_TRIANGLES = 100000;
  46. DebugRenderer::DebugRenderer(Context* context) :
  47. Component(context)
  48. {
  49. vertexBuffer_ = new VertexBuffer(context_);
  50. SubscribeToEvent(E_ENDFRAME, HANDLER(DebugRenderer, HandleEndFrame));
  51. }
  52. DebugRenderer::~DebugRenderer()
  53. {
  54. }
  55. void DebugRenderer::RegisterObject(Context* context)
  56. {
  57. context->RegisterFactory<DebugRenderer>(SUBSYSTEM_CATEGORY);
  58. }
  59. void DebugRenderer::SetView(Camera* camera)
  60. {
  61. if (!camera)
  62. return;
  63. view_ = camera->GetView();
  64. projection_ = camera->GetProjection();
  65. frustum_ = camera->GetFrustum();
  66. }
  67. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest)
  68. {
  69. AddLine(start, end, color.ToUInt(), depthTest);
  70. }
  71. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest)
  72. {
  73. if (lines_.Size() + noDepthLines_.Size() >= MAX_LINES)
  74. return;
  75. if (depthTest)
  76. lines_.Push(DebugLine(start, end, color));
  77. else
  78. noDepthLines_.Push(DebugLine(start, end, color));
  79. }
  80. void DebugRenderer::AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest)
  81. {
  82. AddTriangle(v1, v2, v3, color.ToUInt(), depthTest);
  83. }
  84. void DebugRenderer::AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest)
  85. {
  86. if (triangles_.Size() + noDepthTriangles_.Size() >= MAX_TRIANGLES)
  87. return;
  88. if (depthTest)
  89. triangles_.Push(DebugTriangle(v1, v2, v3, color));
  90. else
  91. noDepthTriangles_.Push(DebugTriangle(v1, v2, v3, color));
  92. }
  93. void DebugRenderer::AddNode(Node* node, float scale, bool depthTest)
  94. {
  95. if (!node)
  96. return;
  97. Vector3 start = node->GetWorldPosition();
  98. Quaternion rotation = node->GetWorldRotation();
  99. AddLine(start, start + rotation * (scale * Vector3::RIGHT), Color::RED.ToUInt(), depthTest);
  100. AddLine(start, start + rotation * (scale * Vector3::UP), Color::GREEN.ToUInt(), depthTest);
  101. AddLine(start, start + rotation * (scale * Vector3::FORWARD), Color::BLUE.ToUInt(), depthTest);
  102. }
  103. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest)
  104. {
  105. const Vector3& min = box.min_;
  106. const Vector3& max = box.max_;
  107. Vector3 v1(max.x_, min.y_, min.z_);
  108. Vector3 v2(max.x_, max.y_, min.z_);
  109. Vector3 v3(min.x_, max.y_, min.z_);
  110. Vector3 v4(min.x_, min.y_, max.z_);
  111. Vector3 v5(max.x_, min.y_, max.z_);
  112. Vector3 v6(min.x_, max.y_, max.z_);
  113. unsigned uintColor = color.ToUInt();
  114. AddLine(min, v1, uintColor, depthTest);
  115. AddLine(v1, v2, uintColor, depthTest);
  116. AddLine(v2, v3, uintColor, depthTest);
  117. AddLine(v3, min, uintColor, depthTest);
  118. AddLine(v4, v5, uintColor, depthTest);
  119. AddLine(v5, max, uintColor, depthTest);
  120. AddLine(max, v6, uintColor, depthTest);
  121. AddLine(v6, v4, uintColor, depthTest);
  122. AddLine(min, v4, uintColor, depthTest);
  123. AddLine(v1, v5, uintColor, depthTest);
  124. AddLine(v2, max, uintColor, depthTest);
  125. AddLine(v3, v6, uintColor, depthTest);
  126. }
  127. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest)
  128. {
  129. const Vector3& min = box.min_;
  130. const Vector3& max = box.max_;
  131. Vector3 v0(transform * min);
  132. Vector3 v1(transform * Vector3(max.x_, min.y_, min.z_));
  133. Vector3 v2(transform * Vector3(max.x_, max.y_, min.z_));
  134. Vector3 v3(transform * Vector3(min.x_, max.y_, min.z_));
  135. Vector3 v4(transform * Vector3(min.x_, min.y_, max.z_));
  136. Vector3 v5(transform * Vector3(max.x_, min.y_, max.z_));
  137. Vector3 v6(transform * Vector3(min.x_, max.y_, max.z_));
  138. Vector3 v7(transform * max);
  139. unsigned uintColor = color.ToUInt();
  140. AddLine(v0, v1, uintColor, depthTest);
  141. AddLine(v1, v2, uintColor, depthTest);
  142. AddLine(v2, v3, uintColor, depthTest);
  143. AddLine(v3, v0, uintColor, depthTest);
  144. AddLine(v4, v5, uintColor, depthTest);
  145. AddLine(v5, v7, uintColor, depthTest);
  146. AddLine(v7, v6, uintColor, depthTest);
  147. AddLine(v6, v4, uintColor, depthTest);
  148. AddLine(v0, v4, uintColor, depthTest);
  149. AddLine(v1, v5, uintColor, depthTest);
  150. AddLine(v2, v7, uintColor, depthTest);
  151. AddLine(v3, v6, uintColor, depthTest);
  152. }
  153. void DebugRenderer::AddFrustum(const Frustum& frustum, const Color& color, bool depthTest)
  154. {
  155. const Vector3* vertices = frustum.vertices_;
  156. unsigned uintColor = color.ToUInt();
  157. AddLine(vertices[0], vertices[1], uintColor, depthTest);
  158. AddLine(vertices[1], vertices[2], uintColor, depthTest);
  159. AddLine(vertices[2], vertices[3], uintColor, depthTest);
  160. AddLine(vertices[3], vertices[0], uintColor, depthTest);
  161. AddLine(vertices[4], vertices[5], uintColor, depthTest);
  162. AddLine(vertices[5], vertices[6], uintColor, depthTest);
  163. AddLine(vertices[6], vertices[7], uintColor, depthTest);
  164. AddLine(vertices[7], vertices[4], uintColor, depthTest);
  165. AddLine(vertices[0], vertices[4], uintColor, depthTest);
  166. AddLine(vertices[1], vertices[5], uintColor, depthTest);
  167. AddLine(vertices[2], vertices[6], uintColor, depthTest);
  168. AddLine(vertices[3], vertices[7], uintColor, depthTest);
  169. }
  170. void DebugRenderer::AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest)
  171. {
  172. unsigned uintColor = color.ToUInt();
  173. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  174. {
  175. const PODVector<Vector3>& face = poly.faces_[i];
  176. if (face.Size() >= 3)
  177. {
  178. for (unsigned j = 0; j < face.Size(); ++j)
  179. AddLine(face[j], face[(j + 1) % face.Size()], uintColor, depthTest);
  180. }
  181. }
  182. }
  183. static Vector3 PointOnSphere(const Sphere& sphere, unsigned theta, unsigned phi)
  184. {
  185. return Vector3(
  186. sphere.center_.x_ + sphere.radius_ * Sin((float)theta) * Sin((float)phi),
  187. sphere.center_.y_ + sphere.radius_ * Cos((float)phi),
  188. sphere.center_.z_ + sphere.radius_ * Cos((float)theta) * Sin((float)phi)
  189. );
  190. }
  191. void DebugRenderer::AddSphere(const Sphere& sphere, const Color& color, bool depthTest)
  192. {
  193. unsigned uintColor = color.ToUInt();
  194. for (unsigned j = 0; j < 180; j += 45)
  195. {
  196. for (unsigned i = 0; i < 360; i += 45)
  197. {
  198. Vector3 p1 = PointOnSphere(sphere, i, j);
  199. Vector3 p2 = PointOnSphere(sphere, i + 45, j);
  200. Vector3 p3 = PointOnSphere(sphere, i, j + 45);
  201. Vector3 p4 = PointOnSphere(sphere, i + 45, j + 45);
  202. AddLine(p1, p2, uintColor, depthTest);
  203. AddLine(p3, p4, uintColor, depthTest);
  204. AddLine(p1, p3, uintColor, depthTest);
  205. AddLine(p2, p4, uintColor, depthTest);
  206. }
  207. }
  208. }
  209. #ifdef ATOMIC_3D
  210. void DebugRenderer::AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest)
  211. {
  212. const Vector<Bone>& bones = skeleton.GetBones();
  213. if (!bones.Size())
  214. return;
  215. unsigned uintColor = color.ToUInt();
  216. for (unsigned i = 0; i < bones.Size(); ++i)
  217. {
  218. // Skip if bone contains no skinned geometry
  219. if (bones[i].radius_ < M_EPSILON && bones[i].boundingBox_.Size().LengthSquared() < M_EPSILON)
  220. continue;
  221. Node* boneNode = bones[i].node_;
  222. if (!boneNode)
  223. continue;
  224. Vector3 start = boneNode->GetWorldPosition();
  225. Vector3 end;
  226. unsigned j = bones[i].parentIndex_;
  227. Node* parentNode = boneNode->GetParent();
  228. // If bone has a parent defined, and it also skins geometry, draw a line to it. Else draw the bone as a point
  229. if (parentNode && (bones[j].radius_ >= M_EPSILON || bones[j].boundingBox_.Size().LengthSquared() >= M_EPSILON))
  230. end = parentNode->GetWorldPosition();
  231. else
  232. end = start;
  233. AddLine(start, end, uintColor, depthTest);
  234. }
  235. }
  236. #endif
  237. void DebugRenderer::AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize,
  238. unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest)
  239. {
  240. unsigned uintColor = color.ToUInt();
  241. const unsigned char* srcData = (const unsigned char*)vertexData;
  242. // 16-bit indices
  243. if (indexSize == sizeof(unsigned short))
  244. {
  245. const unsigned short* indices = ((const unsigned short*)indexData) + indexStart;
  246. const unsigned short* indicesEnd = indices + indexCount;
  247. while (indices < indicesEnd)
  248. {
  249. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  250. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  251. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  252. AddLine(v0, v1, uintColor, depthTest);
  253. AddLine(v1, v2, uintColor, depthTest);
  254. AddLine(v2, v0, uintColor, depthTest);
  255. indices += 3;
  256. }
  257. }
  258. else
  259. {
  260. const unsigned* indices = ((const unsigned*)indexData) + indexStart;
  261. const unsigned* indicesEnd = indices + indexCount;
  262. while (indices < indicesEnd)
  263. {
  264. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  265. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  266. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  267. AddLine(v0, v1, uintColor, depthTest);
  268. AddLine(v1, v2, uintColor, depthTest);
  269. AddLine(v2, v0, uintColor, depthTest);
  270. indices += 3;
  271. }
  272. }
  273. }
  274. void DebugRenderer::Render()
  275. {
  276. if (lines_.Empty() && noDepthLines_.Empty() && triangles_.Empty() && noDepthTriangles_.Empty())
  277. return;
  278. Graphics* graphics = GetSubsystem<Graphics>();
  279. // Engine does not render when window is closed or device is lost
  280. assert(graphics && graphics->IsInitialized() && !graphics->IsDeviceLost());
  281. PROFILE(RenderDebugGeometry);
  282. ShaderVariation* vs = graphics->GetShader(VS, "Basic", "VERTEXCOLOR");
  283. ShaderVariation* ps = graphics->GetShader(PS, "Basic", "VERTEXCOLOR");
  284. unsigned numVertices = (lines_.Size() + noDepthLines_.Size()) * 2 + (triangles_.Size() + noDepthTriangles_.Size()) * 3;
  285. // Resize the vertex buffer if too small or much too large
  286. if (vertexBuffer_->GetVertexCount() < numVertices || vertexBuffer_->GetVertexCount() > numVertices * 2)
  287. vertexBuffer_->SetSize(numVertices, MASK_POSITION | MASK_COLOR, true);
  288. float* dest = (float*)vertexBuffer_->Lock(0, numVertices, true);
  289. if (!dest)
  290. return;
  291. for (unsigned i = 0; i < lines_.Size(); ++i)
  292. {
  293. const DebugLine& line = lines_[i];
  294. dest[0] = line.start_.x_; dest[1] = line.start_.y_; dest[2] = line.start_.z_;
  295. ((unsigned&)dest[3]) = line.color_;
  296. dest[4] = line.end_.x_; dest[5] = line.end_.y_; dest[6] = line.end_.z_;
  297. ((unsigned&)dest[7]) = line.color_;
  298. dest += 8;
  299. }
  300. for (unsigned i = 0; i < noDepthLines_.Size(); ++i)
  301. {
  302. const DebugLine& line = noDepthLines_[i];
  303. dest[0] = line.start_.x_; dest[1] = line.start_.y_; dest[2] = line.start_.z_;
  304. ((unsigned&)dest[3]) = line.color_;
  305. dest[4] = line.end_.x_; dest[5] = line.end_.y_; dest[6] = line.end_.z_;
  306. ((unsigned&)dest[7]) = line.color_;
  307. dest += 8;
  308. }
  309. for (unsigned i = 0; i < triangles_.Size(); ++i)
  310. {
  311. const DebugTriangle& triangle = triangles_[i];
  312. dest[0] = triangle.v1_.x_; dest[1] = triangle.v1_.y_; dest[2] = triangle.v1_.z_;
  313. ((unsigned&)dest[3]) = triangle.color_;
  314. dest[4] = triangle.v2_.x_; dest[5] = triangle.v2_.y_; dest[6] = triangle.v2_.z_;
  315. ((unsigned&)dest[7]) = triangle.color_;
  316. dest[8] = triangle.v3_.x_; dest[9] = triangle.v3_.y_; dest[10] = triangle.v3_.z_;
  317. ((unsigned&)dest[11]) = triangle.color_;
  318. dest += 12;
  319. }
  320. for (unsigned i = 0; i < noDepthTriangles_.Size(); ++i)
  321. {
  322. const DebugTriangle& triangle = noDepthTriangles_[i];
  323. dest[0] = triangle.v1_.x_; dest[1] = triangle.v1_.y_; dest[2] = triangle.v1_.z_;
  324. ((unsigned&)dest[3]) = triangle.color_;
  325. dest[4] = triangle.v2_.x_; dest[5] = triangle.v2_.y_; dest[6] = triangle.v2_.z_;
  326. ((unsigned&)dest[7]) = triangle.color_;
  327. dest[8] = triangle.v3_.x_; dest[9] = triangle.v3_.y_; dest[10] = triangle.v3_.z_;
  328. ((unsigned&)dest[11]) = triangle.color_;
  329. dest += 12;
  330. }
  331. vertexBuffer_->Unlock();
  332. graphics->SetBlendMode(BLEND_REPLACE);
  333. graphics->SetColorWrite(true);
  334. graphics->SetCullMode(CULL_NONE);
  335. graphics->SetDepthWrite(true);
  336. graphics->SetDrawAntialiased(true);
  337. graphics->SetScissorTest(false);
  338. graphics->SetStencilTest(false);
  339. graphics->SetShaders(vs, ps);
  340. graphics->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  341. graphics->SetShaderParameter(VSP_VIEWPROJ, projection_ * view_);
  342. graphics->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  343. graphics->SetVertexBuffer(vertexBuffer_);
  344. unsigned start = 0;
  345. unsigned count = 0;
  346. if (lines_.Size())
  347. {
  348. count = lines_.Size() * 2;
  349. graphics->SetDepthTest(CMP_LESSEQUAL);
  350. graphics->Draw(LINE_LIST, start, count);
  351. start += count;
  352. }
  353. if (noDepthLines_.Size())
  354. {
  355. count = noDepthLines_.Size() * 2;
  356. graphics->SetDepthTest(CMP_ALWAYS);
  357. graphics->Draw(LINE_LIST, start, count);
  358. start += count;
  359. }
  360. graphics->SetBlendMode(BLEND_ALPHA);
  361. if (triangles_.Size())
  362. {
  363. count = triangles_.Size() * 3;
  364. graphics->SetDepthTest(CMP_LESSEQUAL);
  365. graphics->Draw(TRIANGLE_LIST, start, count);
  366. start += count;
  367. }
  368. if (noDepthTriangles_.Size())
  369. {
  370. count = noDepthTriangles_.Size() * 3;
  371. graphics->SetDepthTest(CMP_ALWAYS);
  372. graphics->Draw(TRIANGLE_LIST, start, count);
  373. }
  374. }
  375. bool DebugRenderer::IsInside(const BoundingBox& box) const
  376. {
  377. return frustum_.IsInsideFast(box) == INSIDE;
  378. }
  379. void DebugRenderer::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  380. {
  381. // When the amount of debug geometry is reduced, release memory
  382. unsigned linesSize = lines_.Size();
  383. unsigned noDepthLinesSize = noDepthLines_.Size();
  384. unsigned trianglesSize = triangles_.Size();
  385. unsigned noDepthTrianglesSize = noDepthTriangles_.Size();
  386. lines_.Clear();
  387. noDepthLines_.Clear();
  388. triangles_.Clear();
  389. noDepthTriangles_.Clear();
  390. if (lines_.Capacity() > linesSize * 2)
  391. lines_.Reserve(linesSize);
  392. if (noDepthLines_.Capacity() > noDepthLinesSize * 2)
  393. noDepthLines_.Reserve(noDepthLinesSize);
  394. if (triangles_.Capacity() > trianglesSize * 2)
  395. triangles_.Reserve(trianglesSize);
  396. if (noDepthTriangles_.Capacity() > noDepthTrianglesSize * 2)
  397. noDepthTriangles_.Reserve(noDepthTrianglesSize);
  398. }
  399. }