1#include <IO/AssimpLoader/AssimpLightDataLoader.hpp>
3#include <assimp/scene.h>
5#include <Core/Utils/Log.hpp>
7#include <IO/AssimpLoader/AssimpWrapper.hpp>
12using namespace Core::Utils;
13using namespace Core::Asset;
15AssimpLightDataLoader::AssimpLightDataLoader(
const std::string& filepath,
16 const bool VERBOSE_MODE ) :
17 DataLoader<LightData>( VERBOSE_MODE ), m_filepath( filepath ) {}
19AssimpLightDataLoader::~AssimpLightDataLoader() =
default;
22void AssimpLightDataLoader::loadData(
const aiScene* scene,
26 if ( scene ==
nullptr ) {
27 LOG( logDEBUG ) <<
"AssimpLightDataLoader : scene is nullptr.";
31 if ( !sceneHasLight( scene ) ) {
32 LOG( logDEBUG ) <<
"AssimpLightDataLoader : scene has no lights.";
37 LOG( logINFO ) <<
"File contains light.";
38 LOG( logINFO ) <<
"Light Loading begin...";
41 uint lightSize = sceneLightSize( scene );
42 data.reserve( lightSize );
43 for ( uint lightId = 0; lightId < lightSize; ++lightId ) {
44 data.emplace_back( loadLightData( scene, *( scene->mLights[lightId] ) ) );
47 if ( m_verbose ) { LOG( logINFO ) <<
"Light Loading end.\n"; }
50bool AssimpLightDataLoader::sceneHasLight(
const aiScene* scene )
const {
51 return ( scene->HasLights() );
54uint AssimpLightDataLoader::sceneLightSize(
const aiScene* scene )
const {
55 return scene->mNumLights;
61 const aiLight& light ) {
63 auto builtLight = std::make_unique<LightData>( fetchName( light ), fetchType( light ) );
64 Core::Matrix4 rootMatrix;
65 rootMatrix = Core::Matrix4::Identity();
66 Core::Matrix4 frame = loadLightFrame( scene, rootMatrix, builtLight->getName() );
68 auto color = assimpToCore( light.mColorDiffuse );
70 switch ( builtLight->getType() ) {
71 case LightData::DIRECTIONAL_LIGHT: {
72 Core::Vector4 dir( light.mDirection[0], light.mDirection[1], light.mDirection[2], 0.0 );
73 builtLight->setLight( color, -( frame.transpose().inverse() * dir ).head<3>() );
76 case LightData::POINT_LIGHT: {
80 Core::Vector4 { light.mPosition[0], light.mPosition[1], light.mPosition[2], 1.0 } )
82 LightData::LightAttenuation( light.mAttenuationConstant,
83 light.mAttenuationLinear,
84 light.mAttenuationQuadratic ) );
87 case LightData::SPOT_LIGHT: {
88 Core::Vector4 dir( light.mDirection[0], light.mDirection[1], light.mDirection[2], 0.0 );
93 Core::Vector4 { light.mPosition[0], light.mPosition[1], light.mPosition[2], 1.0 } )
95 -( frame.transpose().inverse() * dir ).head<3>(),
96 light.mAngleInnerCone,
97 light.mAngleOuterCone,
98 LightData::LightAttenuation( light.mAttenuationConstant,
99 light.mAttenuationLinear,
100 light.mAttenuationQuadratic ) );
103 case LightData::AREA_LIGHT: {
104 LOG( logWARNING ) <<
"Light " << builtLight->getName()
105 <<
" : AREA light are not yet supported.";
108 LOG( logWARNING ) <<
"Light " << builtLight->getName() <<
" : unknown type.";
114Core::Matrix4 AssimpLightDataLoader::loadLightFrame(
const aiScene* scene,
115 const Core::Matrix4& parentFrame,
117 const aiNode* lightNode = scene->mRootNode->FindNode( lightName.
c_str() );
119 if ( lightNode !=
nullptr ) {
121 auto t0 = Core::Matrix4::NullaryExpr(
122 [&scene](
int i,
int j ) {
return scene->mRootNode->mTransformation[i][j]; } );
123 auto t1 = Core::Matrix4::NullaryExpr(
124 [&lightNode](
int i,
int j ) {
return lightNode->mTransformation[i][j]; } );
126 return parentFrame * t0 * t1;
131std::string AssimpLightDataLoader::fetchName(
const aiLight& light )
const {
132 return assimpToCore( light.mName );
135LightData::LightType AssimpLightDataLoader::fetchType(
const aiLight& light )
const {
136 switch ( light.mType ) {
137 case aiLightSource_DIRECTIONAL: {
138 return LightData::DIRECTIONAL_LIGHT;
141 case aiLightSource_POINT: {
142 return LightData::POINT_LIGHT;
145 case aiLightSource_SPOT: {
146 return LightData::SPOT_LIGHT;
149 case aiLightSource_AREA: {
150 return LightData::AREA_LIGHT;
153 case aiLightSource_UNDEFINED:
156 return LightData::UNKNOWN;
hepler function to manage enum as underlying types in VariableSet