DebugRenderer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "AnimatedModel.h"
  25. #include "Camera.h"
  26. #include "Context.h"
  27. #include "CoreEvents.h"
  28. #include "DebugRenderer.h"
  29. #include "Graphics.h"
  30. #include "Light.h"
  31. #include "Polyhedron.h"
  32. #include "Profiler.h"
  33. #include "Renderer.h"
  34. #include "ResourceCache.h"
  35. #include "ShaderVariation.h"
  36. #include "VertexBuffer.h"
  37. #include "DebugNew.h"
  38. // Cap the amount of lines to prevent crash when eg. debug rendering large heightfields
  39. static const unsigned MAX_LINES = 1000000;
  40. OBJECTTYPESTATIC(DebugRenderer);
  41. DebugRenderer::DebugRenderer(Context* context) :
  42. Component(context)
  43. {
  44. vertexBuffer_ = new VertexBuffer(context_);
  45. SubscribeToEvent(E_ENDFRAME, HANDLER(DebugRenderer, HandleEndFrame));
  46. }
  47. DebugRenderer::~DebugRenderer()
  48. {
  49. }
  50. void DebugRenderer::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<DebugRenderer>();
  53. }
  54. void DebugRenderer::SetView(Camera* camera)
  55. {
  56. if (!camera)
  57. return;
  58. view_ = camera->GetInverseWorldTransform();
  59. projection_ = camera->GetProjection();
  60. frustum_ = camera->GetFrustum();
  61. }
  62. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest)
  63. {
  64. if (lines_.Size() + noDepthLines_.Size() >= MAX_LINES)
  65. return;
  66. // Perform sphere culling to reject lines outside the view
  67. /// \todo This is slow to do on a per line basis
  68. Vector3 center = (start + end) * 0.5f;
  69. Vector3 extent = end - center;
  70. Sphere sphere(center, extent.Length());
  71. if (frustum_.IsInsideFast(sphere) == OUTSIDE)
  72. return;
  73. if (depthTest)
  74. lines_.Push(DebugLine(start, end, color.ToUInt()));
  75. else
  76. noDepthLines_.Push(DebugLine(start, end, color.ToUInt()));
  77. }
  78. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest)
  79. {
  80. if (lines_.Size() + noDepthLines_.Size() >= MAX_LINES)
  81. return;
  82. Vector3 center = (start + end) * 0.5f;
  83. Vector3 extent = end - center;
  84. Sphere sphere(center, extent.Length());
  85. if (frustum_.IsInsideFast(sphere) == OUTSIDE)
  86. return;
  87. if (depthTest)
  88. lines_.Push(DebugLine(start, end, color));
  89. else
  90. noDepthLines_.Push(DebugLine(start, end, 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. void DebugRenderer::AddSphere(const Sphere& sphere, const Color& color, bool depthTest)
  183. {
  184. const Vector3& center = sphere.center_;
  185. float radius = sphere.radius_;
  186. unsigned uintColor = color.ToUInt();
  187. for (unsigned i = 0; i < 360; i += 45)
  188. {
  189. unsigned j = i + 45;
  190. float a = radius * sinf(i * M_DEGTORAD);
  191. float b = radius * cosf(i * M_DEGTORAD);
  192. float c = radius * sinf(j * M_DEGTORAD);
  193. float d = radius * cosf(j * M_DEGTORAD);
  194. Vector3 start, end;
  195. start = center + Vector3(a, b, 0.0f);
  196. end = center + Vector3(c, d, 0.0f);
  197. AddLine(start, end, uintColor, depthTest);
  198. start = center + Vector3(a, 0.0f, b);
  199. end = center + Vector3(c, 0.0f, d);
  200. AddLine(start, end, uintColor, depthTest);
  201. start = center + Vector3(0.0f, a, b);
  202. end = center + Vector3(0.0f, c, d);
  203. AddLine(start, end, uintColor, depthTest);
  204. }
  205. }
  206. void DebugRenderer::AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest)
  207. {
  208. const Vector<Bone>& bones = skeleton.GetBones();
  209. if (!bones.Size())
  210. return;
  211. unsigned uintColor = color.ToUInt();
  212. for (unsigned i = 0; i < bones.Size(); ++i)
  213. {
  214. // Skip if bone contains no skinned geometry
  215. if (bones[i].radius_ < M_EPSILON && bones[i].boundingBox_.Size().LengthSquared() < M_EPSILON)
  216. continue;
  217. Node* boneNode = bones[i].node_;
  218. if (!boneNode)
  219. continue;
  220. Vector3 start = boneNode->GetWorldPosition();
  221. Vector3 end;
  222. unsigned j = bones[i].parentIndex_;
  223. Node* parentNode = boneNode->GetParent();
  224. // If bone has a parent defined, and it also skins geometry, draw a line to it. Else draw the bone as a point
  225. if (parentNode && (bones[j].radius_ >= M_EPSILON || bones[j].boundingBox_.Size().LengthSquared() >= M_EPSILON))
  226. end = parentNode->GetWorldPosition();
  227. else
  228. end = start;
  229. AddLine(start, end, uintColor, depthTest);
  230. }
  231. }
  232. void DebugRenderer::AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize,
  233. unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest)
  234. {
  235. unsigned uintColor = color.ToUInt();
  236. const unsigned char* srcData = (const unsigned char*)vertexData;
  237. // 16-bit indices
  238. if (indexSize == sizeof(unsigned short))
  239. {
  240. const unsigned short* indices = ((const unsigned short*)indexData) + indexStart;
  241. const unsigned short* indicesEnd = indices + indexCount;
  242. while (indices < indicesEnd)
  243. {
  244. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  245. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  246. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  247. AddLine(v0, v1, uintColor, depthTest);
  248. AddLine(v1, v2, uintColor, depthTest);
  249. AddLine(v2, v0, uintColor, depthTest);
  250. indices += 3;
  251. }
  252. }
  253. else
  254. {
  255. const unsigned* indices = ((const unsigned*)indexData) + indexStart;
  256. const unsigned* indicesEnd = indices + indexCount;
  257. while (indices < indicesEnd)
  258. {
  259. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  260. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  261. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  262. AddLine(v0, v1, uintColor, depthTest);
  263. AddLine(v1, v2, uintColor, depthTest);
  264. AddLine(v2, v0, uintColor, depthTest);
  265. indices += 3;
  266. }
  267. }
  268. }
  269. void DebugRenderer::Render()
  270. {
  271. if (lines_.Empty() && noDepthLines_.Empty())
  272. return;
  273. Graphics* graphics = GetSubsystem<Graphics>();
  274. Renderer* renderer = GetSubsystem<Renderer>();
  275. if (!graphics || graphics->IsDeviceLost())
  276. return;
  277. PROFILE(RenderDebugGeometry);
  278. unsigned numVertices = (lines_.Size() + noDepthLines_.Size()) * 2;
  279. // Resize the vertex buffer if too small or much too large
  280. if (vertexBuffer_->GetVertexCount() < numVertices || vertexBuffer_->GetVertexCount() > numVertices * 2)
  281. vertexBuffer_->SetSize(numVertices, MASK_POSITION | MASK_COLOR, true);
  282. float* dest = (float*)vertexBuffer_->Lock(0, numVertices, true);
  283. if (!dest)
  284. return;
  285. for (unsigned i = 0; i < lines_.Size(); ++i)
  286. {
  287. const DebugLine& line = lines_[i];
  288. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  289. *((unsigned*)dest) = line.color_; dest++;
  290. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  291. *((unsigned*)dest) = line.color_; dest++;
  292. }
  293. for (unsigned i = 0; i < noDepthLines_.Size(); ++i)
  294. {
  295. const DebugLine& line = noDepthLines_[i];
  296. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  297. *((unsigned*)dest) = line.color_; dest++;
  298. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  299. *((unsigned*)dest) = line.color_; dest++;
  300. }
  301. vertexBuffer_->Unlock();
  302. graphics->SetBlendMode(BLEND_REPLACE);
  303. graphics->SetColorWrite(true);
  304. graphics->SetCullMode(CULL_NONE);
  305. graphics->SetDepthWrite(true);
  306. graphics->SetScissorTest(false);
  307. graphics->SetStencilTest(false);
  308. graphics->SetShaders(renderer->GetVertexShader("Basic_VCol"), renderer->GetPixelShader("Basic_VCol"));
  309. graphics->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  310. graphics->SetShaderParameter(VSP_VIEWPROJ, projection_ * view_);
  311. graphics->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  312. graphics->SetVertexBuffer(vertexBuffer_);
  313. if (lines_.Size())
  314. {
  315. graphics->SetDepthTest(CMP_LESSEQUAL);
  316. graphics->Draw(LINE_LIST, 0, lines_.Size() * 2);
  317. }
  318. if (noDepthLines_.Size())
  319. {
  320. graphics->SetDepthTest(CMP_ALWAYS);
  321. graphics->Draw(LINE_LIST, lines_.Size() * 2, noDepthLines_.Size() * 2);
  322. }
  323. }
  324. bool DebugRenderer::IsInside(const BoundingBox& box) const
  325. {
  326. return frustum_.IsInsideFast(box) == INSIDE;
  327. }
  328. void DebugRenderer::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  329. {
  330. // When the amount of debug geometry is reduced, release memory
  331. unsigned linesSize = lines_.Size();
  332. unsigned noDepthLinesSize = noDepthLines_.Size();
  333. lines_.Clear();
  334. noDepthLines_.Clear();
  335. if (lines_.Capacity() > linesSize * 2)
  336. lines_.Reserve(linesSize);
  337. if (noDepthLines_.Capacity() > noDepthLinesSize * 2)
  338. noDepthLines_.Reserve(noDepthLinesSize);
  339. }