Radium Engine  1.5.20
Loading...
Searching...
No Matches
AssimpCameraDataLoader.cpp
1#include <IO/AssimpLoader/AssimpCameraDataLoader.hpp>
2
3#include <assimp/scene.h>
4
5#include <Core/Asset/Camera.hpp>
6#include <Core/Utils/Log.hpp>
7
8#include <IO/AssimpLoader/AssimpWrapper.hpp>
9
10namespace Ra {
11namespace IO {
12
13using namespace Core::Utils; // log
14using namespace Core::Asset; // log
15
16AssimpCameraDataLoader::AssimpCameraDataLoader( const bool VERBOSE_MODE ) :
17 DataLoader<Camera>( VERBOSE_MODE ) {}
18
19AssimpCameraDataLoader::~AssimpCameraDataLoader() = default;
20
21void AssimpCameraDataLoader::loadData( const aiScene* scene,
23 data.clear();
24
25 if ( scene == nullptr ) {
26 LOG( logDEBUG ) << "AssimpCameraDataLoader : scene is nullptr.";
27 return;
28 }
29
30 if ( !sceneHasCamera( scene ) ) {
31 LOG( logDEBUG ) << "AssimpCameraDataLoader : scene has no Cameras.";
32 return;
33 }
34
35 if ( m_verbose ) {
36 LOG( logINFO ) << "File contains Camera.";
37 LOG( logINFO ) << "Camera Loading begin...";
38 }
39
40 uint CameraSize = sceneCameraSize( scene );
41 data.reserve( CameraSize );
42 for ( uint CameraId = 0; CameraId < CameraSize; ++CameraId ) {
43 Camera* camera = new Camera();
44 loadCameraData( scene, *( scene->mCameras[CameraId] ), *camera );
46 data.push_back( std::unique_ptr<Camera>( camera ) );
47 }
48
49 if ( m_verbose ) { LOG( logINFO ) << "Camera Loading end.\n"; }
50}
51
52bool AssimpCameraDataLoader::sceneHasCamera( const aiScene* scene ) const {
53 return ( scene->HasCameras() );
54}
55
56uint AssimpCameraDataLoader::sceneCameraSize( const aiScene* scene ) const {
57 return scene->mNumCameras;
58}
59
60void AssimpCameraDataLoader::loadCameraData( const aiScene* scene,
61 const aiCamera& camera,
62 Camera& data ) {
63 Core::Matrix4 rootMatrix;
64 rootMatrix = Core::Matrix4::Identity();
65 Core::Matrix4 frame = loadCameraFrame( scene, rootMatrix, camera );
66 Core::Vector3 pos = assimpToCore( camera.mPosition );
67 Core::Vector3 lookAt = -assimpToCore( camera.mLookAt ).normalized();
68 Core::Vector3 up = assimpToCore( camera.mUp ).normalized();
69 Core::Vector3 right = lookAt.cross( up );
70 Core::Matrix4 view;
71
72 // make frame a normal frame change (consider it's already ortho ...)
73 frame.block( 0, 0, 3, 1 ).normalize();
74 frame.block( 0, 1, 3, 1 ).normalize();
75 frame.block( 0, 2, 3, 1 ).normalize();
76
77 view.block<3, 1>( 0, 0 ) = right;
78 view.block<3, 1>( 0, 1 ) = up;
79 view.block<3, 1>( 0, 2 ) = lookAt;
80 view.block<3, 1>( 0, 3 ) = pos;
81 view.block<1, 3>( 0, 0 ) *= -1_ra;
82 data.setFrame( Core::Transform { view * frame } );
83
84 data.setType( Camera::ProjType::PERSPECTIVE ); // default value since not in aiCamera
85 // assimp doc (the version we used, updated in assimp master) state mHorizontalFOV is half
86 // angle, but it is not, at least for collada, see https://github.com/assimp/assimp/issues/2256
87 // https://github.com/assimp/assimp/pull/3912
88 data.setFOV( camera.mHorizontalFOV );
89 data.setZNear( camera.mClipPlaneNear );
90 data.setZFar( camera.mClipPlaneFar );
91 data.setZoomFactor( 1.0 ); // default value since not in aiCamera
92 data.setViewport( camera.mAspect, 1_ra );
93}
94
95Core::Matrix4 AssimpCameraDataLoader::loadCameraFrame( const aiScene* scene,
96 const Core::Matrix4&,
97 const aiCamera& cameraNode ) const {
98 aiNode* node = scene->mRootNode->FindNode( cameraNode.mName );
99 Core::Matrix4 frame;
100 frame.setIdentity();
101 while ( node != nullptr ) {
102 frame = Core::Matrix4::NullaryExpr(
103 [&node]( int i, int j ) { return node->mTransformation[i][j]; } ) *
104 frame;
105 node = node->mParent;
106 }
107 return frame;
108}
109
110} // namespace IO
111} // namespace Ra
Camera class storing the Camera frame and the projection properties The view direction is -z in camer...
Definition Camera.hpp:16
void setViewport(Scalar width, Scalar height)
Change the viewport size. Also compute aspectRatio.
Definition Camera.cpp:50
void setZNear(Scalar zNear)
Set the Z Near plane distance to zNear.
Definition Camera.hpp:288
void setZFar(Scalar zFar)
Set the Z Far plane distance to zFar.
Definition Camera.hpp:297
void setZoomFactor(const Scalar &zoomFactor)
Set the zoom factor to zoomFactor.
Definition Camera.hpp:306
void setType(const ProjType &projectionType)
Set the projection type to projectionType.
Definition Camera.hpp:327
void setFrame(const Core::Transform &frame)
Set the frame of the camera to frame.
Definition Camera.hpp:240
void setFOV(Scalar fov)
Definition Camera.hpp:276
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3