Radium Engine  1.5.0
CameraComponent.cpp
1 #include <Core/Asset/Camera.hpp>
2 #include <Engine/Scene/CameraComponent.hpp>
3 
4 #include <Core/Containers/MakeShared.hpp>
5 #include <Core/Math/Math.hpp>
6 #include <Engine/Data/Mesh.hpp>
7 #include <Engine/Data/PlainMaterial.hpp>
8 #include <Engine/Data/ShaderConfigFactory.hpp>
9 #include <Engine/Rendering/RenderObject.hpp>
10 
11 namespace Ra {
12 
13 using Core::Math::Pi;
14 using Core::Math::PiDiv2;
15 using Core::Math::PiDiv4;
16 
17 namespace Engine {
18 namespace Scene {
19 
20 CameraComponent::CameraComponent( Entity* entity,
21  const std::string& name,
22  Scalar height,
23  Scalar width ) :
24  Component( name, entity ) {
25  m_camera = std::make_unique<Core::Asset::Camera>( height, width );
26 }
27 
28 CameraComponent::~CameraComponent() = default;
29 
30 void CameraComponent::initialize() {
31  using Data::Material;
32  using Data::Mesh;
33  using Data::PlainMaterial;
34  if ( !m_renderObjects.empty() ) return;
35  // Create the render mesh for the camera
36 
37  Ra::Core::Geometry::LineMesh triMesh;
38  triMesh.setVertices( { { 0_ra, 0_ra, 0_ra },
39  { -.5_ra, -.5_ra, -1_ra },
40  { -.5_ra, .5_ra, -1_ra },
41  { .5_ra, .5_ra, -1_ra },
42  { .5_ra, -.5_ra, -1_ra },
43  { -.3_ra, .5_ra, -1_ra },
44  { 0_ra, .7_ra, -1_ra },
45  { .3_ra, .5_ra, -1_ra } } );
46  triMesh.setIndices( { { 0, 1 },
47  { 0, 2 },
48  { 0, 3 },
49  { 0, 4 },
50  { 1, 2 },
51  { 1, 4 },
52  { 2, 3 },
53  { 3, 4 },
54  { 5, 6 },
55  { 6, 7 } } );
56 
57  auto m = std::make_shared<Data::LineMesh>( m_name + "_mesh" );
58  m->loadGeometry( std::move( triMesh ) );
59 
60  // Create the RO
61  auto mat = Core::make_shared<PlainMaterial>( m_name + "_Material" );
62  mat->m_color = { 1_ra, .5_ra, 0_ra, 1_ra };
63  mat->m_perVertexColor = false;
64  m_RO = Rendering::RenderObject::createRenderObject( m_name + "_RO",
65  this,
66  Rendering::RenderObjectType::Geometry,
67  m,
69 
70  m_RO->setLocalTransform( m_camera->getFrame() );
71 
72  m_RO->setMaterial( mat );
73  show( false );
74  m_RO->setPickable( true );
75  addRenderObject( m_RO );
76 }
77 
78 void CameraComponent::show( bool on ) {
79  CORE_ASSERT( m_RO, "Camera's render object must be initialize with Camera::intialize()" );
80  m_RO->setVisible( on );
81 }
82 
83 void CameraComponent::updateTransform() {
84  CORE_ASSERT( m_RO, "Camera's render object must be initialize with Camera::intialize()" );
85  m_RO->setLocalTransform( m_camera->getFrame() *
86  Eigen::Scaling( 1_ra,
87  1_ra / m_camera->getAspect(),
88  .5_ra / std::tan( m_camera->getFOV() * .5_ra ) ) );
89 }
90 
91 CameraComponent* CameraComponent::duplicate( Entity* cloneEntity,
92  const std::string& cloneName ) const {
93  auto cam =
94  new CameraComponent { cloneEntity, cloneName, m_camera->getWidth(), m_camera->getHeight() };
95  cam->getCamera()->setFrame( m_camera->getFrame() );
96  cam->getCamera()->setProjMatrix( m_camera->getProjMatrix() );
97  cam->getCamera()->setType( m_camera->getType() );
98  cam->getCamera()->setZoomFactor( m_camera->getZoomFactor() );
99  cam->getCamera()->setFOV( m_camera->getFOV() );
100  cam->getCamera()->setZNear( m_camera->getZNear() );
101  cam->getCamera()->setZFar( m_camera->getZFar() );
102  cam->initialize();
103  return cam;
104 }
105 
106 } // namespace Scene
107 } // namespace Engine
108 } // namespace Ra
Mesh, own a Core::Geometry::TriangleMesh.
Definition: Mesh.hpp:414
An entity is an scene element. It ties together components with a transform.
Definition: Entity.hpp:23
Definition: Cage.cpp:3