DebugRenderer.cpp 11 KB

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