DebugRenderer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. OBJECTTYPESTATIC(DebugRenderer);
  39. DebugRenderer::DebugRenderer(Context* context) :
  40. Component(context)
  41. {
  42. vertexBuffer_ = new VertexBuffer(context_);
  43. SubscribeToEvent(E_ENDFRAME, HANDLER(DebugRenderer, HandleEndFrame));
  44. }
  45. DebugRenderer::~DebugRenderer()
  46. {
  47. }
  48. void DebugRenderer::RegisterObject(Context* context)
  49. {
  50. context->RegisterFactory<DebugRenderer>();
  51. }
  52. void DebugRenderer::SetView(Camera* camera)
  53. {
  54. if (!camera)
  55. return;
  56. view_ = camera->GetInverseWorldTransform();
  57. projection_ = camera->GetProjection();
  58. frustum_ = camera->GetFrustum();
  59. }
  60. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest)
  61. {
  62. if (depthTest)
  63. lines_.Push(DebugLine(start, end, color.ToUInt()));
  64. else
  65. noDepthLines_.Push(DebugLine(start, end, color.ToUInt()));
  66. }
  67. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest)
  68. {
  69. if (depthTest)
  70. lines_.Push(DebugLine(start, end, color));
  71. else
  72. noDepthLines_.Push(DebugLine(start, end, color));
  73. }
  74. void DebugRenderer::AddNode(Node* node, bool depthTest)
  75. {
  76. if (!node)
  77. return;
  78. Vector3 start = node->GetWorldPosition();
  79. Quaternion rotation = node->GetWorldRotation();
  80. if (depthTest)
  81. {
  82. lines_.Push(DebugLine(start, start + rotation * Vector3::RIGHT, Color::RED.ToUInt()));
  83. lines_.Push(DebugLine(start, start + rotation * Vector3::UP, Color::GREEN.ToUInt()));
  84. lines_.Push(DebugLine(start, start + rotation * Vector3::FORWARD, Color::BLUE.ToUInt()));
  85. }
  86. else
  87. {
  88. noDepthLines_.Push(DebugLine(start, start + rotation * Vector3::RIGHT, Color::RED.ToUInt()));
  89. noDepthLines_.Push(DebugLine(start, start + rotation * Vector3::UP, Color::GREEN.ToUInt()));
  90. noDepthLines_.Push(DebugLine(start, start + rotation * Vector3::FORWARD, Color::BLUE.ToUInt()));
  91. }
  92. }
  93. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest)
  94. {
  95. const Vector3& min = box.min_;
  96. const Vector3& max = box.max_;
  97. Vector3 v1(max.x_, min.y_, min.z_);
  98. Vector3 v2(max.x_, max.y_, min.z_);
  99. Vector3 v3(min.x_, max.y_, min.z_);
  100. Vector3 v4(min.x_, min.y_, max.z_);
  101. Vector3 v5(max.x_, min.y_, max.z_);
  102. Vector3 v6(min.x_, max.y_, max.z_);
  103. unsigned uintColor = color.ToUInt();
  104. PODVector<DebugLine>* dest = &lines_;
  105. if (!depthTest)
  106. dest = &noDepthLines_;
  107. dest->Push(DebugLine(min, v1, uintColor));
  108. dest->Push(DebugLine(v1, v2, uintColor));
  109. dest->Push(DebugLine(v2, v3, uintColor));
  110. dest->Push(DebugLine(v3, min, uintColor));
  111. dest->Push(DebugLine(v4, v5, uintColor));
  112. dest->Push(DebugLine(v5, max, uintColor));
  113. dest->Push(DebugLine(max, v6, uintColor));
  114. dest->Push(DebugLine(v6, v4, uintColor));
  115. dest->Push(DebugLine(min, v4, uintColor));
  116. dest->Push(DebugLine(v1, v5, uintColor));
  117. dest->Push(DebugLine(v2, max, uintColor));
  118. dest->Push(DebugLine(v3, v6, uintColor));
  119. }
  120. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest)
  121. {
  122. const Vector3& min = box.min_;
  123. const Vector3& max = box.max_;
  124. Vector3 v0(transform * min);
  125. Vector3 v1(transform * Vector3(max.x_, min.y_, min.z_));
  126. Vector3 v2(transform * Vector3(max.x_, max.y_, min.z_));
  127. Vector3 v3(transform * Vector3(min.x_, max.y_, min.z_));
  128. Vector3 v4(transform * Vector3(min.x_, min.y_, max.z_));
  129. Vector3 v5(transform * Vector3(max.x_, min.y_, max.z_));
  130. Vector3 v6(transform * Vector3(min.x_, max.y_, max.z_));
  131. Vector3 v7(transform * max);
  132. unsigned uintColor = color.ToUInt();
  133. PODVector<DebugLine>* dest = &lines_;
  134. if (!depthTest)
  135. dest = &noDepthLines_;
  136. dest->Push(DebugLine(v0, v1, uintColor));
  137. dest->Push(DebugLine(v1, v2, uintColor));
  138. dest->Push(DebugLine(v2, v3, uintColor));
  139. dest->Push(DebugLine(v3, v0, uintColor));
  140. dest->Push(DebugLine(v4, v5, uintColor));
  141. dest->Push(DebugLine(v5, v7, uintColor));
  142. dest->Push(DebugLine(v7, v6, uintColor));
  143. dest->Push(DebugLine(v6, v4, uintColor));
  144. dest->Push(DebugLine(v0, v4, uintColor));
  145. dest->Push(DebugLine(v1, v5, uintColor));
  146. dest->Push(DebugLine(v2, v7, uintColor));
  147. dest->Push(DebugLine(v3, v6, uintColor));
  148. }
  149. void DebugRenderer::AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest)
  150. {
  151. unsigned uintColor = color.ToUInt();
  152. PODVector<DebugLine>* dest = &lines_;
  153. if (!depthTest)
  154. dest = &noDepthLines_;
  155. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  156. {
  157. const Vector<Vector3>& face = poly.faces_[i];
  158. if (face.Size() >= 3)
  159. {
  160. for (unsigned j = 0; j < face.Size(); ++j)
  161. dest->Push(DebugLine(face[j], face[(j + 1) % face.Size()], uintColor));
  162. }
  163. }
  164. }
  165. void DebugRenderer::AddFrustum(const Frustum& frustum, const Color& color, bool depthTest)
  166. {
  167. const Vector3* vertices = frustum.vertices_;
  168. unsigned uintColor = color.ToUInt();
  169. PODVector<DebugLine>* dest = &lines_;
  170. if (!depthTest)
  171. dest = &noDepthLines_;
  172. dest->Push(DebugLine(vertices[0], vertices[1], uintColor));
  173. dest->Push(DebugLine(vertices[1], vertices[2], uintColor));
  174. dest->Push(DebugLine(vertices[2], vertices[3], uintColor));
  175. dest->Push(DebugLine(vertices[3], vertices[0], uintColor));
  176. dest->Push(DebugLine(vertices[4], vertices[5], uintColor));
  177. dest->Push(DebugLine(vertices[5], vertices[6], uintColor));
  178. dest->Push(DebugLine(vertices[6], vertices[7], uintColor));
  179. dest->Push(DebugLine(vertices[7], vertices[4], uintColor));
  180. dest->Push(DebugLine(vertices[0], vertices[4], uintColor));
  181. dest->Push(DebugLine(vertices[1], vertices[5], uintColor));
  182. dest->Push(DebugLine(vertices[2], vertices[6], uintColor));
  183. dest->Push(DebugLine(vertices[3], vertices[7], uintColor));
  184. }
  185. void DebugRenderer::AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest)
  186. {
  187. const Vector<Bone>& bones = skeleton.GetBones();
  188. if (!bones.Size())
  189. return;
  190. DebugLine newLine;
  191. newLine.color_ = color.ToUInt();
  192. PODVector<DebugLine>* dest = &lines_;
  193. if (!depthTest)
  194. dest = &noDepthLines_;
  195. for (unsigned i = 0; i < bones.Size(); ++i)
  196. {
  197. Node* boneNode = bones[i].node_;
  198. if (!boneNode)
  199. continue;
  200. Node* parentNode = boneNode->GetParent();
  201. newLine.start_ = boneNode->GetWorldPosition();
  202. // If bone has a parent defined, draw a line to it. Else draw the bone as a point
  203. if (parentNode)
  204. newLine.end_ = parentNode->GetWorldPosition();
  205. else
  206. newLine.end_ = newLine.start_;
  207. dest->Push(newLine);
  208. }
  209. }
  210. void DebugRenderer::Render()
  211. {
  212. if (lines_.Empty() && noDepthLines_.Empty())
  213. return;
  214. Graphics* graphics = GetSubsystem<Graphics>();
  215. Renderer* renderer = GetSubsystem<Renderer>();
  216. if (!graphics || graphics->IsDeviceLost())
  217. return;
  218. PROFILE(RenderDebugGeometry);
  219. unsigned numVertices = (lines_.Size() + noDepthLines_.Size()) * 2;
  220. if (vertexBuffer_->GetVertexCount() < numVertices)
  221. vertexBuffer_->SetSize(numVertices, MASK_POSITION | MASK_COLOR, true);
  222. void* lockedData = vertexBuffer_->Lock(0, numVertices, LOCK_DISCARD);
  223. if (!lockedData)
  224. return;
  225. float* dest = (float*)lockedData;
  226. for (unsigned i = 0; i < lines_.Size(); ++i)
  227. {
  228. const DebugLine& line = lines_[i];
  229. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  230. *((unsigned*)dest) = line.color_; dest++;
  231. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  232. *((unsigned*)dest) = line.color_; dest++;
  233. }
  234. for (unsigned i = 0; i < noDepthLines_.Size(); ++i)
  235. {
  236. const DebugLine& line = noDepthLines_[i];
  237. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  238. *((unsigned*)dest) = line.color_; dest++;
  239. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  240. *((unsigned*)dest) = line.color_; dest++;
  241. }
  242. vertexBuffer_->Unlock();
  243. graphics->SetAlphaTest(false);
  244. graphics->SetBlendMode(BLEND_REPLACE);
  245. graphics->SetColorWrite(true);
  246. graphics->SetCullMode(CULL_NONE);
  247. graphics->SetDepthWrite(true);
  248. graphics->SetFillMode(FILL_SOLID);
  249. graphics->SetScissorTest(false);
  250. graphics->SetStencilTest(false);
  251. graphics->SetShaders(renderer->GetVertexShader("Basic_VCol"), renderer->GetPixelShader("Basic_VCol"));
  252. graphics->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  253. graphics->SetShaderParameter(VSP_VIEWPROJ, projection_ * view_);
  254. graphics->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  255. graphics->SetVertexBuffer(vertexBuffer_);
  256. if (lines_.Size())
  257. {
  258. graphics->SetDepthTest(CMP_LESSEQUAL);
  259. graphics->Draw(LINE_LIST, 0, lines_.Size() * 2);
  260. }
  261. if (noDepthLines_.Size())
  262. {
  263. graphics->SetDepthTest(CMP_ALWAYS);
  264. graphics->Draw(LINE_LIST, lines_.Size() * 2, noDepthLines_.Size() * 2);
  265. }
  266. }
  267. bool DebugRenderer::IsInside(const BoundingBox& box) const
  268. {
  269. return frustum_.IsInsideFast(box) == INSIDE;
  270. }
  271. void DebugRenderer::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  272. {
  273. lines_.Clear();
  274. noDepthLines_.Clear();
  275. }