PhysicsDebugNode.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/scene/PhysicsDebugNode.h>
  6. #include <anki/resource/ResourceManager.h>
  7. #include <anki/scene/components/RenderComponent.h>
  8. #include <anki/scene/components/SpatialComponent.h>
  9. #include <anki/scene/SceneGraph.h>
  10. #include <anki/scene/DebugDrawer.h>
  11. namespace anki
  12. {
  13. /// Render component implementation.
  14. class PhysicsDebugNode::MyRenderComponent : public RenderComponent
  15. {
  16. public:
  17. SceneNode* m_node;
  18. DebugDrawer m_dbgDrawer;
  19. PhysicsDebugDrawer m_physDbgDrawer;
  20. MyRenderComponent(SceneNode* node)
  21. : m_node(node)
  22. , m_physDbgDrawer(&m_dbgDrawer)
  23. {
  24. ANKI_ASSERT(node);
  25. setFlags(RenderComponentFlag::NONE);
  26. initRaster([](RenderQueueDrawContext& ctx,
  27. ConstWeakArray<void*> userData) { static_cast<MyRenderComponent*>(userData[0])->draw(ctx); },
  28. this, 0);
  29. }
  30. ANKI_USE_RESULT Error init()
  31. {
  32. return m_dbgDrawer.init(&m_node->getSceneGraph().getResourceManager());
  33. }
  34. /// Draw the world.
  35. void draw(RenderQueueDrawContext& ctx)
  36. {
  37. if(ctx.m_debugDraw)
  38. {
  39. m_dbgDrawer.prepareFrame(&ctx);
  40. m_dbgDrawer.setViewProjectionMatrix(ctx.m_viewProjectionMatrix);
  41. m_dbgDrawer.setModelMatrix(Mat4::getIdentity());
  42. m_physDbgDrawer.drawWorld(m_node->getSceneGraph().getPhysicsWorld());
  43. m_dbgDrawer.finishFrame();
  44. }
  45. }
  46. };
  47. PhysicsDebugNode::~PhysicsDebugNode()
  48. {
  49. }
  50. Error PhysicsDebugNode::init()
  51. {
  52. MyRenderComponent* rcomp = newComponent<MyRenderComponent>(this);
  53. ANKI_CHECK(rcomp->init());
  54. ObbSpatialComponent* scomp = newComponent<ObbSpatialComponent>(this);
  55. const Vec3 center = (getSceneGraph().getSceneMax() + getSceneGraph().getSceneMin()) / 2.0f;
  56. scomp->m_obb.setCenter(center.xyz0());
  57. scomp->m_obb.setExtend((getSceneGraph().getSceneMax() - center).xyz0());
  58. scomp->m_obb.setRotation(Mat3x4::getIdentity());
  59. scomp->setSpatialOrigin(Vec4(0.0f));
  60. scomp->setUpdateOctreeBounds(false); // Don't mess with the bounds
  61. return Error::NONE;
  62. }
  63. } // end namespace anki