DebugRenderer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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::AddFrustum(const Frustum& frustum, const Color& color, bool depthTest)
  150. {
  151. const Vector3* vertices = frustum.vertices_;
  152. unsigned uintColor = color.ToUInt();
  153. PODVector<DebugLine>* dest = &lines_;
  154. if (!depthTest)
  155. dest = &noDepthLines_;
  156. dest->Push(DebugLine(vertices[0], vertices[1], uintColor));
  157. dest->Push(DebugLine(vertices[1], vertices[2], uintColor));
  158. dest->Push(DebugLine(vertices[2], vertices[3], uintColor));
  159. dest->Push(DebugLine(vertices[3], vertices[0], uintColor));
  160. dest->Push(DebugLine(vertices[4], vertices[5], uintColor));
  161. dest->Push(DebugLine(vertices[5], vertices[6], uintColor));
  162. dest->Push(DebugLine(vertices[6], vertices[7], uintColor));
  163. dest->Push(DebugLine(vertices[7], vertices[4], uintColor));
  164. dest->Push(DebugLine(vertices[0], vertices[4], uintColor));
  165. dest->Push(DebugLine(vertices[1], vertices[5], uintColor));
  166. dest->Push(DebugLine(vertices[2], vertices[6], uintColor));
  167. dest->Push(DebugLine(vertices[3], vertices[7], uintColor));
  168. }
  169. void DebugRenderer::AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest)
  170. {
  171. unsigned uintColor = color.ToUInt();
  172. PODVector<DebugLine>* dest = &lines_;
  173. if (!depthTest)
  174. dest = &noDepthLines_;
  175. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  176. {
  177. const Vector<Vector3>& face = poly.faces_[i];
  178. if (face.Size() >= 3)
  179. {
  180. for (unsigned j = 0; j < face.Size(); ++j)
  181. dest->Push(DebugLine(face[j], face[(j + 1) % face.Size()], uintColor));
  182. }
  183. }
  184. }
  185. void DebugRenderer::AddSphere(const Sphere& sphere, const Color& color, bool depthTest)
  186. {
  187. const Vector3& center = sphere.center_;
  188. float radius = sphere.radius_;
  189. unsigned uintColor = color.ToUInt();
  190. for (unsigned i = 0; i < 360; i += 45)
  191. {
  192. unsigned j = i + 45;
  193. float a = radius * sinf(i * M_DEGTORAD);
  194. float b = radius * cosf(i * M_DEGTORAD);
  195. float c = radius * sinf(j * M_DEGTORAD);
  196. float d = radius * cosf(j * M_DEGTORAD);
  197. Vector3 start, end;
  198. start = center + Vector3(a, b, 0.0f);
  199. end = center + Vector3(c, d, 0.0f);
  200. AddLine(start, end, uintColor, depthTest);
  201. start = center + Vector3(a, 0.0f, b);
  202. end = center + Vector3(c, 0.0f, d);
  203. AddLine(start, end, uintColor, depthTest);
  204. start = center + Vector3(0.0f, a, b);
  205. end = center + Vector3(0.0f, c, d);
  206. AddLine(start, end, uintColor, depthTest);
  207. }
  208. }
  209. void DebugRenderer::AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest)
  210. {
  211. const Vector<Bone>& bones = skeleton.GetBones();
  212. if (!bones.Size())
  213. return;
  214. DebugLine newLine;
  215. newLine.color_ = color.ToUInt();
  216. PODVector<DebugLine>* dest = &lines_;
  217. if (!depthTest)
  218. dest = &noDepthLines_;
  219. for (unsigned i = 0; i < bones.Size(); ++i)
  220. {
  221. Node* boneNode = bones[i].node_;
  222. if (!boneNode)
  223. continue;
  224. Node* parentNode = boneNode->GetParent();
  225. newLine.start_ = boneNode->GetWorldPosition();
  226. // If bone has a parent defined, draw a line to it. Else draw the bone as a point
  227. if (parentNode)
  228. newLine.end_ = parentNode->GetWorldPosition();
  229. else
  230. newLine.end_ = newLine.start_;
  231. dest->Push(newLine);
  232. }
  233. }
  234. void DebugRenderer::AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize,
  235. unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest)
  236. {
  237. unsigned uintColor = color.ToUInt();
  238. PODVector<DebugLine>* dest = &lines_;
  239. if (!depthTest)
  240. dest = &noDepthLines_;
  241. const unsigned char* vertexDataChar = (const unsigned char*)vertexData;
  242. unsigned oldSize = dest->Size();
  243. dest->Resize(oldSize + indexCount);
  244. DebugLine* destLines = &dest->At(oldSize);
  245. // 16-bit indices
  246. if (indexSize == sizeof(unsigned short))
  247. {
  248. const unsigned short* indices = ((const unsigned short*)indexData) + indexStart;
  249. const unsigned short* indicesEnd = indices + indexCount;
  250. while (indices < indicesEnd)
  251. {
  252. const Vector3& v0 = *((const Vector3*)(&vertexDataChar[indices[0] * vertexSize]));
  253. const Vector3& v1 = *((const Vector3*)(&vertexDataChar[indices[1] * vertexSize]));
  254. const Vector3& v2 = *((const Vector3*)(&vertexDataChar[indices[2] * vertexSize]));
  255. destLines[0].start_ = transform * v0;
  256. destLines[0].end_ = transform * v1;
  257. destLines[0].color_ = uintColor;
  258. destLines[1].start_ = destLines[0].end_;
  259. destLines[1].end_ = transform * v2;
  260. destLines[1].color_ = uintColor;
  261. destLines[2].start_ = destLines[1].end_;
  262. destLines[2].end_ = destLines[0].start_;
  263. destLines[2].color_ = uintColor;
  264. indices += 3;
  265. destLines += 3;
  266. }
  267. }
  268. else
  269. {
  270. const unsigned* indices = ((const unsigned*)indexData) + indexStart;
  271. const unsigned* indicesEnd = indices + indexCount;
  272. while (indices < indicesEnd)
  273. {
  274. const Vector3& v0 = *((const Vector3*)(&vertexDataChar[indices[0] * vertexSize]));
  275. const Vector3& v1 = *((const Vector3*)(&vertexDataChar[indices[1] * vertexSize]));
  276. const Vector3& v2 = *((const Vector3*)(&vertexDataChar[indices[2] * vertexSize]));
  277. destLines[0].start_ = transform * v0;
  278. destLines[0].end_ = transform * v1;
  279. destLines[0].color_ = uintColor;
  280. destLines[1].start_ = destLines[0].end_;
  281. destLines[1].end_ = transform * v2;
  282. destLines[1].color_ = uintColor;
  283. destLines[2].start_ = destLines[1].end_;
  284. destLines[2].end_ = destLines[0].start_;
  285. destLines[2].color_ = uintColor;
  286. indices += 3;
  287. destLines += 3;
  288. }
  289. }
  290. }
  291. void DebugRenderer::Render()
  292. {
  293. if (lines_.Empty() && noDepthLines_.Empty())
  294. return;
  295. Graphics* graphics = GetSubsystem<Graphics>();
  296. Renderer* renderer = GetSubsystem<Renderer>();
  297. if (!graphics || graphics->IsDeviceLost())
  298. return;
  299. PROFILE(RenderDebugGeometry);
  300. unsigned numVertices = (lines_.Size() + noDepthLines_.Size()) * 2;
  301. // Resize the vertex buffer if too small or much too large
  302. if (vertexBuffer_->GetVertexCount() < numVertices || vertexBuffer_->GetVertexCount() > numVertices * 2)
  303. vertexBuffer_->SetSize(numVertices, MASK_POSITION | MASK_COLOR, true);
  304. void* lockedData = vertexBuffer_->Lock(0, numVertices, LOCK_DISCARD);
  305. if (!lockedData)
  306. return;
  307. float* dest = (float*)lockedData;
  308. for (unsigned i = 0; i < lines_.Size(); ++i)
  309. {
  310. const DebugLine& line = lines_[i];
  311. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  312. *((unsigned*)dest) = line.color_; dest++;
  313. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  314. *((unsigned*)dest) = line.color_; dest++;
  315. }
  316. for (unsigned i = 0; i < noDepthLines_.Size(); ++i)
  317. {
  318. const DebugLine& line = noDepthLines_[i];
  319. *dest++ = line.start_.x_; *dest++ = line.start_.y_; *dest++ = line.start_.z_;
  320. *((unsigned*)dest) = line.color_; dest++;
  321. *dest++ = line.end_.x_; *dest++ = line.end_.y_; *dest++ = line.end_.z_;
  322. *((unsigned*)dest) = line.color_; dest++;
  323. }
  324. vertexBuffer_->Unlock();
  325. graphics->SetAlphaTest(false);
  326. graphics->SetBlendMode(BLEND_REPLACE);
  327. graphics->SetColorWrite(true);
  328. graphics->SetCullMode(CULL_NONE);
  329. graphics->SetDepthWrite(true);
  330. graphics->SetFillMode(FILL_SOLID);
  331. graphics->SetScissorTest(false);
  332. graphics->SetStencilTest(false);
  333. graphics->SetShaders(renderer->GetVertexShader("Basic_VCol"), renderer->GetPixelShader("Basic_VCol"));
  334. graphics->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  335. graphics->SetShaderParameter(VSP_VIEWPROJ, projection_ * view_);
  336. graphics->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  337. graphics->SetVertexBuffer(vertexBuffer_);
  338. if (lines_.Size())
  339. {
  340. graphics->SetDepthTest(CMP_LESSEQUAL);
  341. graphics->Draw(LINE_LIST, 0, lines_.Size() * 2);
  342. }
  343. if (noDepthLines_.Size())
  344. {
  345. graphics->SetDepthTest(CMP_ALWAYS);
  346. graphics->Draw(LINE_LIST, lines_.Size() * 2, noDepthLines_.Size() * 2);
  347. }
  348. }
  349. bool DebugRenderer::IsInside(const BoundingBox& box) const
  350. {
  351. return frustum_.IsInsideFast(box) == INSIDE;
  352. }
  353. void DebugRenderer::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  354. {
  355. lines_.Clear();
  356. noDepthLines_.Clear();
  357. }