DebugRenderer.cpp 14 KB

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