Radium Engine  1.5.20
Loading...
Searching...
No Matches
AssimpLightDataLoader.cpp
1#include <IO/AssimpLoader/AssimpLightDataLoader.hpp>
2
3#include <assimp/scene.h>
4
5#include <Core/Utils/Log.hpp>
6
7#include <IO/AssimpLoader/AssimpWrapper.hpp>
8
9namespace Ra {
10namespace IO {
11
12using namespace Core::Utils; // log
13using namespace Core::Asset;
14
15AssimpLightDataLoader::AssimpLightDataLoader( const std::string& filepath,
16 const bool VERBOSE_MODE ) :
17 DataLoader<LightData>( VERBOSE_MODE ), m_filepath( filepath ) {}
18
19AssimpLightDataLoader::~AssimpLightDataLoader() = default;
20
22void AssimpLightDataLoader::loadData( const aiScene* scene,
24 data.clear();
25
26 if ( scene == nullptr ) {
27 LOG( logDEBUG ) << "AssimpLightDataLoader : scene is nullptr.";
28 return;
29 }
30
31 if ( !sceneHasLight( scene ) ) {
32 LOG( logDEBUG ) << "AssimpLightDataLoader : scene has no lights.";
33 return;
34 }
35
36 if ( m_verbose ) {
37 LOG( logINFO ) << "File contains light.";
38 LOG( logINFO ) << "Light Loading begin...";
39 }
40
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] ) ) );
45 }
46
47 if ( m_verbose ) { LOG( logINFO ) << "Light Loading end.\n"; }
48}
49
50bool AssimpLightDataLoader::sceneHasLight( const aiScene* scene ) const {
51 return ( scene->HasLights() );
52}
53
54uint AssimpLightDataLoader::sceneLightSize( const aiScene* scene ) const {
55 return scene->mNumLights;
56}
57
58// LightData * AssimpLightDataLoader::loadLightData(const aiScene *scene, const aiLight
59// &light)
60std::unique_ptr<LightData> AssimpLightDataLoader::loadLightData( const aiScene* scene,
61 const aiLight& light ) {
62 // auto builtLight = new LightData(fetchName( light ), fetchType( 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() );
67 setFrame( frame );
68 auto color = assimpToCore( light.mColorDiffuse );
69
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>() );
74 } break;
75
76 case LightData::POINT_LIGHT: {
77 builtLight->setLight(
78 color,
79 ( frame *
80 Core::Vector4 { light.mPosition[0], light.mPosition[1], light.mPosition[2], 1.0 } )
81 .hnormalized(),
82 LightData::LightAttenuation( light.mAttenuationConstant,
83 light.mAttenuationLinear,
84 light.mAttenuationQuadratic ) );
85 } break;
86
87 case LightData::SPOT_LIGHT: {
88 Core::Vector4 dir( light.mDirection[0], light.mDirection[1], light.mDirection[2], 0.0 );
89
90 builtLight->setLight(
91 color,
92 ( frame *
93 Core::Vector4 { light.mPosition[0], light.mPosition[1], light.mPosition[2], 1.0 } )
94 .hnormalized(),
95 -( frame.transpose().inverse() * dir ).head<3>(),
96 light.mAngleInnerCone,
97 light.mAngleOuterCone,
98 LightData::LightAttenuation( light.mAttenuationConstant,
99 light.mAttenuationLinear,
100 light.mAttenuationQuadratic ) );
101 } break;
102
103 case LightData::AREA_LIGHT: {
104 LOG( logWARNING ) << "Light " << builtLight->getName()
105 << " : AREA light are not yet supported.";
106 } break;
107 default: {
108 LOG( logWARNING ) << "Light " << builtLight->getName() << " : unknown type.";
109 } break;
110 }
111 return builtLight;
112}
113
114Core::Matrix4 AssimpLightDataLoader::loadLightFrame( const aiScene* scene,
115 const Core::Matrix4& parentFrame,
116 const std::string& lightName ) const {
117 const aiNode* lightNode = scene->mRootNode->FindNode( lightName.c_str() );
118
119 if ( lightNode != nullptr ) {
120
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]; } );
125
126 return parentFrame * t0 * t1;
127 }
128 return parentFrame;
129}
130
131std::string AssimpLightDataLoader::fetchName( const aiLight& light ) const {
132 return assimpToCore( light.mName );
133}
134
135LightData::LightType AssimpLightDataLoader::fetchType( const aiLight& light ) const {
136 switch ( light.mType ) {
137 case aiLightSource_DIRECTIONAL: {
138 return LightData::DIRECTIONAL_LIGHT;
139 }
140
141 case aiLightSource_POINT: {
142 return LightData::POINT_LIGHT;
143 }
144
145 case aiLightSource_SPOT: {
146 return LightData::SPOT_LIGHT;
147 }
148
149 case aiLightSource_AREA: {
150 return LightData::AREA_LIGHT;
151 }
152
153 case aiLightSource_UNDEFINED:
154 default: {
155 // LOG(ERROR) << "Light " << name.C_Str() << " has undefined type.";
156 return LightData::UNKNOWN;
157 }
158 }
159}
160
161} // namespace IO
162} // namespace Ra
T c_str(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3