DebugRenderer.cpp 9.7 KB

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