Radium Engine  1.5.0
Gizmo.cpp
1 #include <Gui/Viewer/Gizmo/Gizmo.hpp>
2 
3 #include <Core/Asset/Camera.hpp>
4 #include <Core/Geometry/RayCast.hpp>
5 #include <Engine/Data/Mesh.hpp>
6 #include <Engine/Rendering/RenderObject.hpp>
7 #include <Engine/Rendering/RenderObjectManager.hpp>
8 #include <Engine/Scene/CameraComponent.hpp>
9 
10 #include <Core/Containers/MakeShared.hpp>
11 #include <Engine/Data/PlainMaterial.hpp>
12 #include <Engine/Data/ShaderConfigFactory.hpp>
13 #include <Engine/Rendering/RenderTechnique.hpp>
14 
15 namespace Ra {
16 namespace Gui {
17 
18 std::array<std::shared_ptr<Ra::Engine::Data::PlainMaterial>, 3> Gizmo::s_material;
19 
20 Gizmo::Gizmo( Engine::Scene::Component* c,
21  const Core::Transform& worldTo,
22  const Core::Transform& t,
23  Mode mode ) :
24  m_worldTo( worldTo ), m_transform( t ), m_comp( c ), m_mode( mode ) {
25  using namespace Core::Utils;
26  if ( !s_material[0] ) {
27  auto mat = Core::make_shared<Engine::Data::PlainMaterial>( "GizmoRedMaterial" );
28  mat->m_color = Color::Red();
29  s_material[0] = mat;
30  mat = Core::make_shared<Engine::Data::PlainMaterial>( "GizmoGreenMaterial" );
31  mat->m_color = Color::Green();
32  s_material[1] = mat;
33  mat = Core::make_shared<Engine::Data::PlainMaterial>( "GizmoBlueMaterial" );
34  mat->m_color = Color::Blue();
35  s_material[2] = mat;
36  }
37 }
38 
39 Gizmo::~Gizmo() {
40  for ( auto ro : m_ros ) {
41  m_comp->removeRenderObject( ro->getIndex() );
42  }
43 }
44 
45 void Gizmo::show( bool on ) {
46  for ( auto ro : m_ros ) {
47  ro->setVisible( on );
48  }
49 }
50 
51 bool Gizmo::findPointOnAxis( const Core::Asset::Camera& cam,
52  const Core::Vector3& origin,
53  const Core::Vector3& axis,
54  const Core::Vector2& pix,
55  Core::Vector3& pointOut,
56  std::vector<Scalar>& hits ) {
57  // Taken from Rodolphe's View engine gizmos -- see slide_axis().
58  // Find a plane containing axis and as parallel as possible to
59  // the camera image plane
60  const Core::Vector3 ortho = cam.getDirection().cross( axis );
61  const Core::Vector3 normal =
62  ( ortho.squaredNorm() > 0 ) ? axis.cross( ortho ) : axis.cross( cam.getUpVector() );
63 
64  const auto ray = cam.getRayFromScreen( pix );
65  bool hasHit = Core::Geometry::RayCastPlane( ray, origin, normal, hits );
66  if ( hasHit ) { pointOut = origin + ( axis.dot( ray.pointAt( hits[0] ) - origin ) ) * axis; }
67  return hasHit;
68 }
69 
70 bool Gizmo::findPointOnPlane( const Core::Asset::Camera& cam,
71  const Core::Vector3& origin,
72  const Core::Vector3& axis,
73  const Core::Vector2& pix,
74  Core::Vector3& pointOut,
75  std::vector<Scalar>& hits ) {
76  // Taken from Rodolphe's View engine gizmos -- see slide_plane().
77  const auto ray = cam.getRayFromScreen( pix );
78  bool hasHit = Core::Geometry::RayCastPlane( ray, origin, axis, hits );
79  if ( hasHit ) { pointOut = ray.pointAt( hits[0] ); }
80  return hasHit;
81 }
82 
83 void Gizmo::addRenderObject( Engine::Rendering::RenderObject* ro ) {
84  m_comp->addRenderObject( ro );
85  m_ros.push_back( ro );
86 }
87 
88 std::shared_ptr<Engine::Rendering::RenderTechnique> Gizmo::makeRenderTechnique( int color ) {
89  auto rt = Core::make_shared<Engine::Rendering::RenderTechnique>();
91  auto provider = new Gizmo::UiSelectionControler( s_material[color] );
92  rt->setConfiguration( *plaincfg );
93  rt->setParametersProvider( std::shared_ptr<Engine::Data::ShaderParameterProvider>( provider ) );
94  return rt;
95 }
96 
97 Gizmo::UiSelectionControler* Gizmo::getControler( int ro ) {
98  // TODO : can these casts be replaced by a improved design ?
99  auto c = dynamic_cast<const Gizmo::UiSelectionControler*>(
100  m_ros[ro]->getRenderTechnique()->getParametersProvider() );
101  CORE_ASSERT( c != nullptr, "Gizmo not set (or with wrong state controler?" );
102  return const_cast<Gizmo::UiSelectionControler*>( c );
103 }
104 
105 Gizmo::UiSelectionControler::UiSelectionControler(
106  std::shared_ptr<Ra::Engine::Data::PlainMaterial>& material,
107  const Core::Utils::Color& selectedColor ) :
108  ShaderParameterProvider(), m_associatedMaterial( material ), m_selectedColor( selectedColor ) {}
109 
111  m_associatedMaterial->updateGL();
112  auto& renderParameters = getParameters();
113  renderParameters.mergeReplaceParameters( m_associatedMaterial->getParameters() );
114  if ( m_selected ) { renderParameters.addParameter( "material.color", m_selectedColor ); }
115 }
116 
118  m_selected = !m_selected;
119 }
120 
122  m_selected = true;
123 }
124 
126  m_selected = false;
127 }
128 
129 } // namespace Gui
130 } // namespace Ra
Camera class storing the Camera frame and the projection properties The view direction is -z in camer...
Definition: Camera.hpp:16
Core::Ray getRayFromScreen(const Core::Vector2 &pix) const
Definition: Camera.cpp:178
Core::Vector3 getUpVector() const
Return the up vector.
Definition: Camera.hpp:258
Core::Vector3 getDirection() const
Return the direction the camera is looking at.
Definition: Camera.hpp:254
void setState()
Set the state of the controler to true.
Definition: Gizmo.cpp:121
void toggleState()
Swap the state of the controler.
Definition: Gizmo.cpp:117
void clearState()
Set the state of the controler to false.
Definition: Gizmo.cpp:125
void updateGL() override
Inherited.
Definition: Gizmo.cpp:110
static std::array< std::shared_ptr< Ra::Engine::Data::PlainMaterial >, 3 > s_material
Definition: Gizmo.hpp:118
Core::Utils::optional< ShaderConfiguration > getConfiguration(const std::string &name)
Definition: Cage.cpp:3