DebugRenderer.cpp 9.4 KB

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