DebugRenderer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. if (depthTest)
  67. lines_.Push(DebugLine(start, end, color.ToUInt()));
  68. else
  69. noDepthLines_.Push(DebugLine(start, end, color.ToUInt()));
  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::AddNode(Node* node, float scale, bool depthTest)
  81. {
  82. if (!node)
  83. return;
  84. Vector3 start = node->GetWorldPosition();
  85. Quaternion rotation = node->GetWorldRotation();
  86. AddLine(start, start + rotation * (scale * Vector3::RIGHT), Color::RED.ToUInt(), depthTest);
  87. AddLine(start, start + rotation * (scale * Vector3::UP), Color::GREEN.ToUInt(), depthTest);
  88. AddLine(start, start + rotation * (scale * Vector3::FORWARD), Color::BLUE.ToUInt(), depthTest);
  89. }
  90. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest)
  91. {
  92. const Vector3& min = box.min_;
  93. const Vector3& max = box.max_;
  94. Vector3 v1(max.x_, min.y_, min.z_);
  95. Vector3 v2(max.x_, max.y_, min.z_);
  96. Vector3 v3(min.x_, max.y_, min.z_);
  97. Vector3 v4(min.x_, min.y_, max.z_);
  98. Vector3 v5(max.x_, min.y_, max.z_);
  99. Vector3 v6(min.x_, max.y_, max.z_);
  100. unsigned uintColor = color.ToUInt();
  101. AddLine(min, v1, uintColor, depthTest);
  102. AddLine(v1, v2, uintColor, depthTest);
  103. AddLine(v2, v3, uintColor, depthTest);
  104. AddLine(v3, min, uintColor, depthTest);
  105. AddLine(v4, v5, uintColor, depthTest);
  106. AddLine(v5, max, uintColor, depthTest);
  107. AddLine(max, v6, uintColor, depthTest);
  108. AddLine(v6, v4, uintColor, depthTest);
  109. AddLine(min, v4, uintColor, depthTest);
  110. AddLine(v1, v5, uintColor, depthTest);
  111. AddLine(v2, max, uintColor, depthTest);
  112. AddLine(v3, v6, uintColor, depthTest);
  113. }
  114. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest)
  115. {
  116. const Vector3& min = box.min_;
  117. const Vector3& max = box.max_;
  118. Vector3 v0(transform * min);
  119. Vector3 v1(transform * Vector3(max.x_, min.y_, min.z_));
  120. Vector3 v2(transform * Vector3(max.x_, max.y_, min.z_));
  121. Vector3 v3(transform * Vector3(min.x_, max.y_, min.z_));
  122. Vector3 v4(transform * Vector3(min.x_, min.y_, max.z_));
  123. Vector3 v5(transform * Vector3(max.x_, min.y_, max.z_));
  124. Vector3 v6(transform * Vector3(min.x_, max.y_, max.z_));
  125. Vector3 v7(transform * max);
  126. unsigned uintColor = color.ToUInt();
  127. AddLine(v0, v1, uintColor, depthTest);
  128. AddLine(v1, v2, uintColor, depthTest);
  129. AddLine(v2, v3, uintColor, depthTest);
  130. AddLine(v3, v0, uintColor, depthTest);
  131. AddLine(v4, v5, uintColor, depthTest);
  132. AddLine(v5, v7, uintColor, depthTest);
  133. AddLine(v7, v6, uintColor, depthTest);
  134. AddLine(v6, v4, uintColor, depthTest);
  135. AddLine(v0, v4, uintColor, depthTest);
  136. AddLine(v1, v5, uintColor, depthTest);
  137. AddLine(v2, v7, uintColor, depthTest);
  138. AddLine(v3, v6, uintColor, depthTest);
  139. }
  140. void DebugRenderer::AddFrustum(const Frustum& frustum, const Color& color, bool depthTest)
  141. {
  142. const Vector3* vertices = frustum.vertices_;
  143. unsigned uintColor = color.ToUInt();
  144. AddLine(vertices[0], vertices[1], uintColor, depthTest);
  145. AddLine(vertices[1], vertices[2], uintColor, depthTest);
  146. AddLine(vertices[2], vertices[3], uintColor, depthTest);
  147. AddLine(vertices[3], vertices[0], uintColor, depthTest);
  148. AddLine(vertices[4], vertices[5], uintColor, depthTest);
  149. AddLine(vertices[5], vertices[6], uintColor, depthTest);
  150. AddLine(vertices[6], vertices[7], uintColor, depthTest);
  151. AddLine(vertices[7], vertices[4], uintColor, depthTest);
  152. AddLine(vertices[0], vertices[4], uintColor, depthTest);
  153. AddLine(vertices[1], vertices[5], uintColor, depthTest);
  154. AddLine(vertices[2], vertices[6], uintColor, depthTest);
  155. AddLine(vertices[3], vertices[7], uintColor, depthTest);
  156. }
  157. void DebugRenderer::AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest)
  158. {
  159. unsigned uintColor = color.ToUInt();
  160. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  161. {
  162. const PODVector<Vector3>& face = poly.faces_[i];
  163. if (face.Size() >= 3)
  164. {
  165. for (unsigned j = 0; j < face.Size(); ++j)
  166. AddLine(face[j], face[(j + 1) % face.Size()], uintColor, depthTest);
  167. }
  168. }
  169. }
  170. void DebugRenderer::AddSphere(const Sphere& sphere, const Color& color, bool depthTest)
  171. {
  172. const Vector3& center = sphere.center_;
  173. float radius = sphere.radius_;
  174. unsigned uintColor = color.ToUInt();
  175. for (unsigned i = 0; i < 360; i += 45)
  176. {
  177. unsigned j = i + 45;
  178. float a = radius * sinf(i * M_DEGTORAD);
  179. float b = radius * cosf(i * M_DEGTORAD);
  180. float c = radius * sinf(j * M_DEGTORAD);
  181. float d = radius * cosf(j * M_DEGTORAD);
  182. Vector3 start, end;
  183. start = center + Vector3(a, b, 0.0f);
  184. end = center + Vector3(c, d, 0.0f);
  185. AddLine(start, end, uintColor, depthTest);
  186. start = center + Vector3(a, 0.0f, b);
  187. end = center + Vector3(c, 0.0f, d);
  188. AddLine(start, end, uintColor, depthTest);
  189. start = center + Vector3(0.0f, a, b);
  190. end = center + Vector3(0.0f, c, d);
  191. AddLine(start, end, uintColor, depthTest);
  192. }
  193. }
  194. void DebugRenderer::AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest)
  195. {
  196. const Vector<Bone>& bones = skeleton.GetBones();
  197. if (!bones.Size())
  198. return;
  199. unsigned uintColor = color.ToUInt();
  200. for (unsigned i = 0; i < bones.Size(); ++i)
  201. {
  202. // Skip if bone contains no skinned geometry
  203. if (bones[i].radius_ < M_EPSILON && bones[i].boundingBox_.Size().LengthSquared() < M_EPSILON)
  204. continue;
  205. Node* boneNode = bones[i].node_;
  206. if (!boneNode)
  207. continue;
  208. Vector3 start = boneNode->GetWorldPosition();
  209. Vector3 end;
  210. unsigned j = bones[i].parentIndex_;
  211. Node* parentNode = boneNode->GetParent();
  212. // If bone has a parent defined, and it also skins geometry, draw a line to it. Else draw the bone as a point
  213. if (parentNode && (bones[j].radius_ >= M_EPSILON || bones[j].boundingBox_.Size().LengthSquared() >= M_EPSILON))
  214. end = parentNode->GetWorldPosition();
  215. else
  216. end = start;
  217. AddLine(start, end, uintColor, depthTest);
  218. }
  219. }
  220. void DebugRenderer::AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize,
  221. unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest)
  222. {
  223. unsigned uintColor = color.ToUInt();
  224. const unsigned char* srcData = (const unsigned char*)vertexData;
  225. // 16-bit indices
  226. if (indexSize == sizeof(unsigned short))
  227. {
  228. const unsigned short* indices = ((const unsigned short*)indexData) + indexStart;
  229. const unsigned short* indicesEnd = indices + indexCount;
  230. while (indices < indicesEnd)
  231. {
  232. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  233. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  234. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  235. AddLine(v0, v1, uintColor, depthTest);
  236. AddLine(v1, v2, uintColor, depthTest);
  237. AddLine(v2, v0, uintColor, depthTest);
  238. indices += 3;
  239. }
  240. }
  241. else
  242. {
  243. const unsigned* indices = ((const unsigned*)indexData) + indexStart;
  244. const unsigned* indicesEnd = indices + indexCount;
  245. while (indices < indicesEnd)
  246. {
  247. Vector3 v0 = transform * *((const Vector3*)(&srcData[indices[0] * vertexSize]));
  248. Vector3 v1 = transform * *((const Vector3*)(&srcData[indices[1] * vertexSize]));
  249. Vector3 v2 = transform * *((const Vector3*)(&srcData[indices[2] * vertexSize]));
  250. AddLine(v0, v1, uintColor, depthTest);
  251. AddLine(v1, v2, uintColor, depthTest);
  252. AddLine(v2, v0, uintColor, depthTest);
  253. indices += 3;
  254. }
  255. }
  256. }
  257. void DebugRenderer::Render()
  258. {
  259. if (lines_.Empty() && noDepthLines_.Empty())
  260. return;
  261. Graphics* graphics = GetSubsystem<Graphics>();
  262. Renderer* renderer = GetSubsystem<Renderer>();
  263. if (!graphics || graphics->IsDeviceLost())
  264. return;
  265. PROFILE(RenderDebugGeometry);
  266. unsigned numVertices = (lines_.Size() + noDepthLines_.Size()) * 2;
  267. // Resize the vertex buffer if too small or much too large
  268. if (vertexBuffer_->GetVertexCount() < numVertices || vertexBuffer_->GetVertexCount() > numVertices * 2)
  269. vertexBuffer_->SetSize(numVertices, MASK_POSITION | MASK_COLOR, true);
  270. float* dest = (float*)vertexBuffer_->Lock(0, numVertices, true);
  271. if (!dest)
  272. return;
  273. for (unsigned i = 0; i < lines_.Size(); ++i)
  274. {
  275. const DebugLine& line = lines_[i];
  276. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  277. *((unsigned*)dest) = line.color_; dest++;
  278. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  279. *((unsigned*)dest) = line.color_; dest++;
  280. }
  281. for (unsigned i = 0; i < noDepthLines_.Size(); ++i)
  282. {
  283. const DebugLine& line = noDepthLines_[i];
  284. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  285. *((unsigned*)dest) = line.color_; dest++;
  286. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  287. *((unsigned*)dest) = line.color_; dest++;
  288. }
  289. vertexBuffer_->Unlock();
  290. graphics->SetBlendMode(BLEND_REPLACE);
  291. graphics->SetColorWrite(true);
  292. graphics->SetCullMode(CULL_NONE);
  293. graphics->SetDepthWrite(true);
  294. graphics->SetScissorTest(false);
  295. graphics->SetStencilTest(false);
  296. graphics->SetShaders(renderer->GetVertexShader("Basic_VCol"), renderer->GetPixelShader("Basic_VCol"));
  297. graphics->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  298. graphics->SetShaderParameter(VSP_VIEWPROJ, projection_ * view_);
  299. graphics->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  300. graphics->SetVertexBuffer(vertexBuffer_);
  301. if (lines_.Size())
  302. {
  303. graphics->SetDepthTest(CMP_LESSEQUAL);
  304. graphics->Draw(LINE_LIST, 0, lines_.Size() * 2);
  305. }
  306. if (noDepthLines_.Size())
  307. {
  308. graphics->SetDepthTest(CMP_ALWAYS);
  309. graphics->Draw(LINE_LIST, lines_.Size() * 2, noDepthLines_.Size() * 2);
  310. }
  311. }
  312. bool DebugRenderer::IsInside(const BoundingBox& box) const
  313. {
  314. return frustum_.IsInsideFast(box) == INSIDE;
  315. }
  316. void DebugRenderer::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  317. {
  318. // When the amount of debug geometry is reduced, release memory
  319. unsigned linesSize = lines_.Size();
  320. unsigned noDepthLinesSize = noDepthLines_.Size();
  321. lines_.Clear();
  322. noDepthLines_.Clear();
  323. if (lines_.Capacity() > linesSize * 2)
  324. lines_.Reserve(linesSize);
  325. if (noDepthLines_.Capacity() > noDepthLinesSize * 2)
  326. noDepthLines_.Reserve(noDepthLinesSize);
  327. }