26c57e37a2411526719171bb1d739492632a88e9
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / slp / resource-loader / assimp-model-builder.h
1 #ifndef __DALI_SLP_PLATFORM_ASSIMP_MODEL_BUILDER_H__
2 #define __DALI_SLP_PLATFORM_ASSIMP_MODEL_BUILDER_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //     http://floralicense.org/license/
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19
20 #include "model-builder.h"
21
22 #include <string>
23 #include <assimp/assimp.hpp>
24 #include <assimp/aiScene.h>
25
26 #include <dali/public-api/modeling/model-data.h>
27
28 namespace Dali
29 {
30 class Bone;
31 class Mesh;
32 class Entity;
33 class ModelAnimationMap;
34 class EntityAnimatorMap;
35
36 class MeshData;
37
38 namespace SlpPlatform
39 {
40
41 class AssimpProxy;
42
43 class AssimpModelBuilder : public ModelBuilder
44 {
45 public:
46   /**
47    * Construct a Builder object with the filename of the Model to import
48    * @param[in] name - the filename of the model
49    */
50   AssimpModelBuilder(AssimpProxy* importer, const std::string& name);
51
52   /**
53    * Destructor
54    */
55   virtual ~AssimpModelBuilder();
56
57 public: // ModelBuilder Interface
58
59   /**
60    * Build a model structure from the Assimp scene
61    * @param[inout] Model - the model to build
62    */
63   virtual bool Build(ModelData model);
64
65   virtual const std::string& GetModelName();
66
67 private:
68   /**
69    * Build this mesh from the assimp model.
70    * @param[out]   meshData  - object to which the data will be written into
71    * @param[in]    ai_scene  - the Assimp scene with the data
72    * @param[in]    meshIndex - the index within the ai_scene of the mesh data
73    * @param[in]    model     - the current model
74    */
75   void BuildMeshData(MeshData& meshData, const aiScene* ai_scene, unsigned int meshIndex, ModelData& model);
76
77   /**
78    * Build this entity from the assimp model
79    * @pre The internal model should already contain the assimp mesh data
80    * @param[in]    ai_scene  - the assimp scene with the data
81    * @param[in]    ai_node   - the assimp node to which this entity corresponds
82    * @param[in]    model     - the current model
83    * @param[in]    transform - the current transform matrix to apply
84    * @return An Entity object.
85    */
86   Entity BuildEntity(const aiScene* ai_scene, aiNode* ai_node, ModelData* model, const Matrix& transform);
87
88   /**
89    * Create a key frame animator from the given node.
90    * @param[in] node - assimp animation node
91    * @param[in] duration - length of animation in seconds
92    * @return the animator data for the node
93    */
94   EntityAnimatorMap CreateAnimator(aiNodeAnim* aiNode, float duration, const std::string& name, Entity animatedEntity);
95
96   /**
97    * Build this animation from the assimp model
98    * @pre The internal model should already contain all the entities of the scene, and they
99    * should have an entity id set (so that the animation can be referenced back to them)
100    * @param[in]    ai_anim   - the assimp animation
101    * @param[in]    model     - the current internal model
102    * @return The animation
103    */
104   ModelAnimationMap BuildAnimation(const aiAnimation *ai_anim, ModelData *model);
105
106   /**
107    * Build this light from the assimp model
108    * @param[in]    ai_scene    - The assimp scene with the data
109    * @param[in]    lightIndex - The index of the light within the assimp scene
110    * @return A valid Dali::Light handle or false on failure
111    */
112   Light BuildLight(const aiScene* ai_scene, const unsigned int lightIndex);
113
114   /**
115    * Build this material from the assimp model, and load all appropriate textures
116    * @param[in] ai_material A pointer to an <a href="http://assimp.sourceforge.net/lib_html/structai_material.html">Assimp Material</a>.
117    * @param[in] sBasePath   The base path for any texture filenames referenced by the material
118    * @return                A pointer to a Material or false.
119    */
120   Material BuildMaterial(const aiMaterial* ai_material, const std::string& sBasePath);
121
122   /**
123    * Match up the bone names to entities. Goes through each mesh, and assigns the correct
124    * entity onto each bone.
125    * @model - the model to update
126    */
127   void MatchBonesToEntities(ModelData* model);
128
129
130 private: // Attributes
131   const std::string mFilename;
132   std::string       mModelName;
133   char*             mTmpFilename;
134   AssimpProxy*      mModelImporter;
135 };
136
137 }//SlpPlatform
138 }//Dali
139
140 #endif // __DALI_SLP_PLATFORM_ASSIMP_MODEL_BUILDER_H__