1 #include <Core/Asset/Camera.hpp>
2 #include <Engine/Scene/CameraComponent.hpp>
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>
14 using Core::Math::PiDiv2;
15 using Core::Math::PiDiv4;
20 CameraComponent::CameraComponent( Entity* entity,
21 const std::string& name,
24 Component( name, entity ) {
25 m_camera = std::make_unique<Core::Asset::Camera>( height, width );
28 CameraComponent::~CameraComponent() =
default;
30 void CameraComponent::initialize() {
34 if ( !m_renderObjects.empty() )
return;
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 },
57 auto m = std::make_shared<Data::LineMesh>( m_name +
"_mesh" );
58 m->loadGeometry( std::move( triMesh ) );
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",
66 Rendering::RenderObjectType::Geometry,
70 m_RO->setLocalTransform( m_camera->getFrame() );
72 m_RO->setMaterial( mat );
74 m_RO->setPickable(
true );
75 addRenderObject( m_RO );
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 );
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() *
87 1_ra / m_camera->getAspect(),
88 .5_ra / std::tan( m_camera->getFOV() * .5_ra ) ) );
92 const std::string& cloneName )
const {
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() );
Mesh, own a Core::Geometry::TriangleMesh.
An entity is an scene element. It ties together components with a transform.