SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAssimpImporter.h
Go to the documentation of this file.
1 /**
2  * \file SLAssimpImporter.h
3  * \date July 2014
4  * \authors Marc Wacker, Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLASSIMPIMPORTER_H
11 #define SLASSIMPIMPORTER_H
12 
13 #ifdef SL_BUILD_WITH_ASSIMP
14 
15 # include <SLGLTexture.h>
16 # include <SLImporter.h>
17 
18 // forward declarations of assimp types
19 struct aiScene;
20 struct aiNode;
21 struct aiMaterial;
22 struct aiAnimation;
23 struct aiMesh;
24 
25 class SLAssetManager;
26 class SLAnimManager;
27 class SLProgressHandler;
28 class SLSkybox;
29 
30 //-----------------------------------------------------------------------------
31 typedef std::map<int, SLMesh*> SLMeshMap;
32 //-----------------------------------------------------------------------------
33 //! Small class interface into the AssImp library for importing 3D assets.
34 /*! See AssImp library (http://assimp.sourceforge.net/) documentation for
35 supported file formats and the import processing options.
36 */
37 class SLAssimpImporter : public SLImporter
38 {
39 public:
40  explicit SLAssimpImporter(SLLogVerbosity consoleVerb = LV_quiet)
41  : SLImporter(consoleVerb) {}
42  explicit SLAssimpImporter(SLstring& logFile,
43  SLLogVerbosity logConsoleVerb = LV_normal,
44  SLLogVerbosity logFileVerb = LV_diagnostic)
45  : SLImporter(logFile, logConsoleVerb, logFileVerb) {}
46 
47  SLNode* load(SLAnimManager& aniMan,
48  SLAssetManager* assetMgr,
49  SLstring pathAndFile,
50  SLstring texturePath,
51  SLSkybox* skybox = nullptr,
52  SLbool deleteTexImgAfterBuild = false,
53  SLbool loadMeshesOnly = true,
54  SLMaterial* overrideMat = nullptr,
55  float ambientFactor = 0.0f,
56  SLbool forceCookTorranceRM = false,
57  SLProgressHandler* progressHandler = nullptr,
58  SLuint flags =
65  //|SLProcess_SortByPType
66  //|SLProcess_OptimizeMeshes
67  //|SLProcess_OptimizeGraph
68  //|SLProcess_CalcTangentSpace
69  //|SLProcess_MakeLeftHanded
70  //|SLProcess_RemoveComponent
71  //|SLProcess_GenNormals
72  //|SLProcess_GenSmoothNormals
73  //|SLProcess_PreTransformVertices
74  //|SLProcess_LimitJointWeights
75  //|SLProcess_ValidateDataStructure
76  //|SLProcess_ImproveCacheLocality
77  //|SLProcess_FixInfacingNormals
78  //|SLProcess_GenUVCoords
79  //|SLProcess_TransformUVCoords
80  //|SLProcess_FindInstances
81  //|SLProcess_FlipUVs
82  //|SLProcess_FlipWindingOrder
83  //|SLProcess_SplitByJointCount
84  //|SLProcess_Dejoint
85  );
86 
87 protected:
88  // intermediate containers
89  typedef std::map<SLstring, aiNode*> SLNodeMap;
90  typedef std::map<SLstring, SLMat4f> SLJointOffsetMap;
91  typedef vector<aiNode*> SLVaiNode;
92 
93  SLNodeMap _nodeMap; //!< map containing name to aiNode releationships
94  SLJointOffsetMap _jointOffsets; //!< map containing name to joint offset matrices
95  aiNode* _skeletonRoot{}; //!< the common aiNode root for the skeleton of this file
96 
97  // SL type containers
98  typedef vector<SLMesh*> MeshList;
99 
100  SLuint _jointIndex{}; //!< index counter used when iterating over joints
101  MeshList _skinnedMeshes; //!< list containing all of the skinned meshes, used to assign the skinned materials
102 
103  // loading helper
104  aiNode* getNodeByName(const SLstring& name); // return an aiNode ptr if name exists, or null if it doesn't
105  SLMat4f getOffsetMat(const SLstring& name); // return an aiJoint ptr if name exists, or null if it doesn't
106 
107  void performInitialScan(const aiScene* scene); // populates nameToNode, nameToJoint, jointGroups, skinnedMeshes,
108  void findNodes(aiNode* node,
109  SLstring padding,
110  SLbool lastChild); // scans the assimp scene graph structure and populates nameToNode
111  void findJoints(const aiScene* scene); // scans all meshes in the assimp scene and populates nameToJoint and jointGroups
112  void findSkeletonRoot(); // finds the common ancestor for each remaining group in jointGroups, these are our final skeleton roots
113 
114  void loadSkeleton(SLAnimManager& animManager,
115  SLJoint* parent,
116  aiNode* node);
117  static SLMaterial* loadMaterial(SLAssetManager* am,
118  SLint index,
119  aiMaterial* aiMat,
120  const SLstring& modelPath,
121  const SLstring& texturePath,
122  SLSkybox* skybox = nullptr,
123  float ambientFactor = 0.0f,
124  SLbool forceCookTorranceLM = false,
125  SLbool deleteTexImgAfterBuild = false);
126  static SLGLTexture* loadTexture(SLAssetManager* assetMgr,
127  SLstring& path,
128  SLTextureType texType,
129  SLuint uvIndex,
130  SLbool deleteTexImgAfterBuild = false);
131  SLMesh* loadMesh(SLAssetManager* am, aiMesh* mesh);
132  SLNode* loadNodesRec(SLNode* curNode,
133  aiNode* aiNode,
134  SLMeshMap& meshes,
135  SLbool loadMeshesOnly = true);
136  SLAnimation* loadAnimation(SLAnimManager& animManager,
137  aiAnimation* anim);
138  static SLstring checkFilePath(const SLstring& modelPath,
139  const SLstring& texturePath,
140  SLstring texFile,
141  bool showWarning = true);
142  SLbool aiNodeHasMesh(aiNode* node);
143 
144  // misc helper
145  void clear();
146 };
147 
148 //-----------------------------------------------------------------------------
149 #endif // SL_BUILD_WITH_ASSIMP
150 #endif // SLASSIMPIMPORTER_H
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
SLLogVerbosity
Definition: SLEnums.h:244
@ LV_diagnostic
Definition: SLEnums.h:249
@ LV_normal
Definition: SLEnums.h:247
@ LV_quiet
Definition: SLEnums.h:245
SLTextureType
Texture type enumeration & their filename appendix for auto type detection.
Definition: SLGLTexture.h:76
@ SLProcess_SplitLargeMeshes
Definition: SLImporter.h:40
@ SLProcess_Triangulate
Definition: SLImporter.h:36
@ SLProcess_FindDegenerates
Definition: SLImporter.h:48
@ SLProcess_FindInvalidData
Definition: SLImporter.h:49
@ SLProcess_RemoveRedundantMaterials
Definition: SLImporter.h:45
@ SLProcess_JoinIdenticalVertices
Definition: SLImporter.h:34
std::map< int, SLMesh * > SLMeshMap
Definition: SLImporter.h:62
SLAnimManager is the central class for all animation handling.
Definition: SLAnimManager.h:27
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
Toplevel holder of the assets meshes, materials, textures and shaders.
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
Interface for 3D file format importer implementations.
Definition: SLImporter.h:66
virtual SLNode * load(SLAnimManager &aniMan, SLAssetManager *assetMgr, SLstring pathFilename, SLstring texturePath, SLSkybox *skybox=nullptr, SLbool deleteTexImgAfterBuild=false, SLbool loadMeshesOnly=true, SLMaterial *overrideMat=nullptr, float ambientFactor=0.0f, SLbool forceCookTorranceRM=false, SLProgressHandler *progressHandler=nullptr, SLuint flags=SLProcess_Triangulate|SLProcess_JoinIdenticalVertices|SLProcess_SplitLargeMeshes|SLProcess_RemoveRedundantMaterials|SLProcess_SortByPType|SLProcess_FindDegenerates|SLProcess_FindInvalidData)=0
Specialized SLNode that represents a single joint (or bone) in a skeleton.
Definition: SLJoint.h:27
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
An SLMesh object is a triangulated mesh, drawn with one draw call.
Definition: SLMesh.h:134
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
Skybox node class with a SLBox mesh.
Definition: SLSkybox.h:29
void clear(std::string path)
Definition: SLIOMemory.cpp:34