DebugRenderer.cpp 14 KB

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