DebugRenderer.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "Profiler.h"
  32. #include "Renderer.h"
  33. #include "ResourceCache.h"
  34. #include "ShaderVariation.h"
  35. #include "DebugNew.h"
  36. OBJECTTYPESTATIC(DebugRenderer);
  37. DebugRenderer::DebugRenderer(Context* context) :
  38. Component(context)
  39. {
  40. SubscribeToEvent(E_ENDFRAME, HANDLER(DebugRenderer, HandleEndFrame));
  41. }
  42. DebugRenderer::~DebugRenderer()
  43. {
  44. }
  45. void DebugRenderer::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<DebugRenderer>();
  48. }
  49. void DebugRenderer::SetView(Camera* camera)
  50. {
  51. if (!camera)
  52. return;
  53. view_ = camera->GetInverseWorldTransform();
  54. projection_ = camera->GetProjection(false);
  55. frustum_ = camera->GetFrustum();
  56. }
  57. void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest)
  58. {
  59. if (depthTest)
  60. lines_.Push(DebugLine(start, end, color.ToUInt()));
  61. else
  62. noDepthLines_.Push(DebugLine(start, end, color.ToUInt()));
  63. }
  64. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest)
  65. {
  66. const Vector3& min = box.min_;
  67. const Vector3& max = box.max_;
  68. Vector3 v0(max.x_, min.y_, min.z_);
  69. Vector3 v1(max.x_, max.y_, min.z_);
  70. Vector3 v2(min.x_, max.y_, min.z_);
  71. Vector3 v3(min.x_, min.y_, max.z_);
  72. Vector3 v4(max.x_, min.y_, max.z_);
  73. Vector3 v5(min.x_, max.y_, max.z_);
  74. unsigned d3dColor = color.ToUInt();
  75. PODVector<DebugLine>* dest = &lines_;
  76. if (!depthTest)
  77. dest = &noDepthLines_;
  78. dest->Push(DebugLine(min, v0, d3dColor));
  79. dest->Push(DebugLine(v0, v1, d3dColor));
  80. dest->Push(DebugLine(v1, v2, d3dColor));
  81. dest->Push(DebugLine(v2, min, d3dColor));
  82. dest->Push(DebugLine(v3, v4, d3dColor));
  83. dest->Push(DebugLine(v4, max, d3dColor));
  84. dest->Push(DebugLine(max, v5, d3dColor));
  85. dest->Push(DebugLine(v5, v3, d3dColor));
  86. dest->Push(DebugLine(min, v3, d3dColor));
  87. dest->Push(DebugLine(v0, v4, d3dColor));
  88. dest->Push(DebugLine(v1, max, d3dColor));
  89. dest->Push(DebugLine(v2, v5, d3dColor));
  90. }
  91. void DebugRenderer::AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest)
  92. {
  93. const Vector3& min = box.min_;
  94. const Vector3& max = box.max_;
  95. Vector3 v0(transform * Vector3(min.x_, min.y_, min.z_));
  96. Vector3 v1(transform * Vector3(max.x_, min.y_, min.z_));
  97. Vector3 v2(transform * Vector3(max.x_, max.y_, min.z_));
  98. Vector3 v3(transform * Vector3(min.x_, max.y_, min.z_));
  99. Vector3 v4(transform * Vector3(min.x_, min.y_, max.z_));
  100. Vector3 v5(transform * Vector3(max.x_, min.y_, max.z_));
  101. Vector3 v6(transform * Vector3(max.x_, max.y_, max.z_));
  102. Vector3 v7(transform * Vector3(min.x_, max.y_, max.z_));
  103. unsigned d3dColor = color.ToUInt();
  104. PODVector<DebugLine>* dest = &lines_;
  105. if (!depthTest)
  106. dest = &noDepthLines_;
  107. dest->Push(DebugLine(v0, v1, d3dColor));
  108. dest->Push(DebugLine(v1, v2, d3dColor));
  109. dest->Push(DebugLine(v2, v3, d3dColor));
  110. dest->Push(DebugLine(v3, v0, d3dColor));
  111. dest->Push(DebugLine(v4, v5, d3dColor));
  112. dest->Push(DebugLine(v5, v6, d3dColor));
  113. dest->Push(DebugLine(v6, v7, d3dColor));
  114. dest->Push(DebugLine(v7, v4, d3dColor));
  115. dest->Push(DebugLine(v0, v4, d3dColor));
  116. dest->Push(DebugLine(v1, v5, d3dColor));
  117. dest->Push(DebugLine(v2, v6, d3dColor));
  118. dest->Push(DebugLine(v3, v7, d3dColor));
  119. }
  120. void DebugRenderer::AddFrustum(const Frustum& frustum, const Color& color, bool depthTest)
  121. {
  122. const Vector3* vertices = frustum.vertices_;
  123. unsigned d3dColor = color.ToUInt();
  124. PODVector<DebugLine>* dest = &lines_;
  125. if (!depthTest)
  126. dest = &noDepthLines_;
  127. dest->Push(DebugLine(vertices[0], vertices[1], d3dColor));
  128. dest->Push(DebugLine(vertices[1], vertices[2], d3dColor));
  129. dest->Push(DebugLine(vertices[2], vertices[3], d3dColor));
  130. dest->Push(DebugLine(vertices[3], vertices[0], d3dColor));
  131. dest->Push(DebugLine(vertices[4], vertices[5], d3dColor));
  132. dest->Push(DebugLine(vertices[5], vertices[6], d3dColor));
  133. dest->Push(DebugLine(vertices[6], vertices[7], d3dColor));
  134. dest->Push(DebugLine(vertices[7], vertices[4], d3dColor));
  135. dest->Push(DebugLine(vertices[0], vertices[4], d3dColor));
  136. dest->Push(DebugLine(vertices[1], vertices[5], d3dColor));
  137. dest->Push(DebugLine(vertices[2], vertices[6], d3dColor));
  138. dest->Push(DebugLine(vertices[3], vertices[7], d3dColor));
  139. }
  140. void DebugRenderer::AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest)
  141. {
  142. const Vector<Bone>& bones = skeleton.GetBones();
  143. if (!bones.Size())
  144. return;
  145. DebugLine newLine;
  146. newLine.color_ = color.ToUInt();
  147. PODVector<DebugLine>* dest = &lines_;
  148. if (!depthTest)
  149. dest = &noDepthLines_;
  150. for (unsigned i = 0; i < bones.Size(); ++i)
  151. {
  152. Node* boneNode = bones[i].node_;
  153. if (!boneNode)
  154. continue;
  155. Node* parentNode = boneNode->GetParent();
  156. newLine.start_ = boneNode->GetWorldPosition();
  157. // If bone has a parent defined, draw a line to it. Else draw the bone as a point
  158. if (parentNode)
  159. newLine.end_ = parentNode->GetWorldPosition();
  160. else
  161. newLine.end_ = newLine.start_;
  162. dest->Push(newLine);
  163. }
  164. }
  165. void DebugRenderer::Render()
  166. {
  167. PROFILE(RenderDebugGeometry);
  168. if (lines_.Empty() && noDepthLines_.Empty())
  169. return;
  170. Graphics* graphics = GetSubsystem<Graphics>();
  171. Renderer* renderer = GetSubsystem<Renderer>();
  172. graphics->SetAlphaTest(false);
  173. graphics->SetBlendMode(BLEND_REPLACE);
  174. graphics->SetColorWrite(true);
  175. graphics->SetCullMode(CULL_NONE);
  176. graphics->SetDepthWrite(true);
  177. graphics->SetDepthTest(CMP_LESSEQUAL);
  178. graphics->SetFillMode(FILL_SOLID);
  179. graphics->SetScissorTest(false);
  180. graphics->SetStencilTest(false);
  181. graphics->SetShaders(renderer->GetVertexShader("Basic_VCol"), renderer->GetPixelShader("Basic_VCol"));
  182. graphics->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  183. graphics->SetShaderParameter(VSP_VIEWPROJ, projection_ * view_);
  184. graphics->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  185. // Draw all line geometry with depth testing
  186. if (lines_.Size())
  187. {
  188. graphics->BeginImmediate(LINE_LIST, lines_.Size() * 2, MASK_POSITION | MASK_COLOR);
  189. float* dest = (float*)graphics->GetImmediateDataPtr();
  190. for (unsigned i = 0; i < lines_.Size(); ++i)
  191. {
  192. const DebugLine& line = lines_[i];
  193. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  194. *((unsigned*)dest) = line.color_; dest++;
  195. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  196. *((unsigned*)dest) = line.color_; dest++;
  197. }
  198. graphics->EndImmediate();
  199. }
  200. // Draw all line geometry without depth testing
  201. graphics->SetDepthTest(CMP_ALWAYS);
  202. if (noDepthLines_.Size())
  203. {
  204. graphics->BeginImmediate(LINE_LIST, noDepthLines_.Size() * 2, MASK_POSITION | MASK_COLOR);
  205. float* dest = (float*)graphics->GetImmediateDataPtr();
  206. for (unsigned i = 0; i < noDepthLines_.Size(); ++i)
  207. {
  208. const DebugLine& line = noDepthLines_[i];
  209. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  210. *((unsigned*)dest) = line.color_; dest++;
  211. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  212. *((unsigned*)dest) = line.color_; dest++;
  213. }
  214. graphics->EndImmediate();
  215. }
  216. }
  217. bool DebugRenderer::IsInside(const BoundingBox& box) const
  218. {
  219. return frustum_.IsInsideFast(box) == INSIDE;
  220. }
  221. void DebugRenderer::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  222. {
  223. lines_.Clear();
  224. noDepthLines_.Clear();
  225. }