1 #include <IO/AssimpLoader/AssimpCameraDataLoader.hpp>
3 #include <assimp/scene.h>
5 #include <Core/Asset/Camera.hpp>
6 #include <Core/Utils/Log.hpp>
8 #include <IO/AssimpLoader/AssimpWrapper.hpp>
13 using namespace Core::Utils;
14 using namespace Core::Asset;
16 AssimpCameraDataLoader::AssimpCameraDataLoader(
const bool VERBOSE_MODE ) :
17 DataLoader<Camera>( VERBOSE_MODE ) {}
19 AssimpCameraDataLoader::~AssimpCameraDataLoader() =
default;
21 void AssimpCameraDataLoader::loadData(
const aiScene* scene,
22 std::vector<std::unique_ptr<Camera>>& data ) {
25 if ( scene ==
nullptr ) {
26 LOG( logDEBUG ) <<
"AssimpCameraDataLoader : scene is nullptr.";
30 if ( !sceneHasCamera( scene ) ) {
31 LOG( logDEBUG ) <<
"AssimpCameraDataLoader : scene has no Cameras.";
36 LOG( logINFO ) <<
"File contains Camera.";
37 LOG( logINFO ) <<
"Camera Loading begin...";
40 uint CameraSize = sceneCameraSize( scene );
41 data.reserve( CameraSize );
42 for ( uint CameraId = 0; CameraId < CameraSize; ++CameraId ) {
44 loadCameraData( scene, *( scene->mCameras[CameraId] ), *camera );
46 data.push_back( std::unique_ptr<Camera>( camera ) );
49 if ( m_verbose ) { LOG( logINFO ) <<
"Camera Loading end.\n"; }
52 bool AssimpCameraDataLoader::sceneHasCamera(
const aiScene* scene )
const {
53 return ( scene->HasCameras() );
56 uint AssimpCameraDataLoader::sceneCameraSize(
const aiScene* scene )
const {
57 return scene->mNumCameras;
60 void AssimpCameraDataLoader::loadCameraData(
const aiScene* scene,
61 const aiCamera& camera,
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 );
73 frame.block( 0, 0, 3, 1 ).normalize();
74 frame.block( 0, 1, 3, 1 ).normalize();
75 frame.block( 0, 2, 3, 1 ).normalize();
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 } );
84 data.
setType( Camera::ProjType::PERSPECTIVE );
88 data.
setFOV( camera.mHorizontalFOV );
89 data.
setZNear( camera.mClipPlaneNear );
90 data.
setZFar( camera.mClipPlaneFar );
95 Core::Matrix4 AssimpCameraDataLoader::loadCameraFrame(
const aiScene* scene,
97 const aiCamera& cameraNode )
const {
98 aiNode* node = scene->mRootNode->FindNode( cameraNode.mName );
101 while ( node !=
nullptr ) {
102 frame = Core::Matrix4::NullaryExpr(
103 [&node](
int i,
int j ) {
return node->mTransformation[i][j]; } ) *
105 node = node->mParent;
Camera class storing the Camera frame and the projection properties The view direction is -z in camer...
void setViewport(Scalar width, Scalar height)
Change the viewport size. Also compute aspectRatio.
void setZNear(Scalar zNear)
Set the Z Near plane distance to zNear.
void setZFar(Scalar zFar)
Set the Z Far plane distance to zFar.
void setZoomFactor(const Scalar &zoomFactor)
Set the zoom factor to zoomFactor.
void setType(const ProjType &projectionType)
Set the projection type to projectionType.
void setFrame(const Core::Transform &frame)
Set the frame of the camera to frame.