Revert "[Tizen] Modify codes for Dali Windows backend"
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scene / gltf-loader.h
1 #ifndef DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H\r
2 #define DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H\r
3 \r
4 /*\r
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  */\r
20 \r
21 #include <dali/dali.h>\r
22 #include <dali-toolkit/devel-api/builder/json-parser.h>\r
23 #include <dali-toolkit/internal/controls/scene/scene-impl.h>\r
24 \r
25 using namespace Dali;\r
26 using namespace Dali::Toolkit;\r
27 \r
28 namespace Dali\r
29 {\r
30 \r
31 namespace Toolkit\r
32 {\r
33 \r
34 namespace Internal\r
35 {\r
36 \r
37 namespace GLTF\r
38 {\r
39 \r
40 enum ShaderType\r
41 {\r
42   NO_TEXTURE_SHADER,\r
43   BASECOLOR_SHADER,\r
44   METALLICROUGHNESS_SHADER,\r
45   BASECOLOR_METALLICROUGHNESS_SHADER,\r
46   NORMAL_SHADER,\r
47   BASECOLOR_NORMAL_SHADER,\r
48   METALLICROUGHNESS_NORMAL_SHADER,\r
49   BASECOLOR_METALLICROUGHNESS_NORMAL_SHADER,\r
50   OCCLUSION_SHADER,\r
51   BASECOLOR_OCCLUSION_SHADER,\r
52   METALLICROUGHNESS_OCCLUSION_SHADER,\r
53   BASECOLOR_METALLICROUGHNESS_OCCLUSION_SHADER,\r
54   NORMAL_OCCLUSION_SHADER,\r
55   BASECOLOR_NORMAL_OCCLUSION_SHADER,\r
56   METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
57   BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
58   EMIT_SHADER,\r
59   BASECOLOR_EMIT_SHADER,\r
60   METALLICROUGHNESS_EMIT_SHADER,\r
61   BASECOLOR_METALLICROUGHNESS_EMIT_SHADER,\r
62   NORMAL_EMIT_SHADER,\r
63   BASECOLOR_NORMAL_EMIT_SHADER,\r
64   METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
65   BASECOLOR_METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
66   OCCLUSION_EMIT_SHADER,\r
67   BASECOLOR_OCCLUSION_EMIT_SHADER,\r
68   METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
69   BASECOLOR_METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
70   NORMAL_OCCLUSION_EMIT_SHADER,\r
71   BASECOLOR_NORMAL_OCCLUSION_EMIT_SHADER,\r
72   METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
73   BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
74   IBL_SHADER,\r
75   IBL_BASECOLOR_SHADER,\r
76   IBL_METALLICROUGHNESS_SHADER,\r
77   IBL_BASECOLOR_METALLICROUGHNESS_SHADER,\r
78   IBL_NORMAL_SHADER,\r
79   IBL_BASECOLOR_NORMAL_SHADER,\r
80   IBL_METALLICROUGHNESS_NORMAL_SHADER,\r
81   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_SHADER,\r
82   IBL_OCCLUSION_SHADER,\r
83   IBL_BASECOLOR_OCCLUSION_SHADER,\r
84   IBL_METALLICROUGHNESS_OCCLUSION_SHADER,\r
85   IBL_BASECOLOR_METALLICROUGHNESS_OCCLUSION_SHADER,\r
86   IBL_NORMAL_OCCLUSION_SHADER,\r
87   IBL_BASECOLOR_NORMAL_OCCLUSION_SHADER,\r
88   IBL_METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
89   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
90   IBL_EMIT_SHADER,\r
91   IBL_BASECOLOR_EMIT_SHADER,\r
92   IBL_METALLICROUGHNESS_EMIT_SHADER,\r
93   IBL_BASECOLOR_METALLICROUGHNESS_EMIT_SHADER,\r
94   IBL_NORMAL_EMIT_SHADER,\r
95   IBL_BASECOLOR_NORMAL_EMIT_SHADER,\r
96   IBL_METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
97   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
98   IBL_OCCLUSION_EMIT_SHADER,\r
99   IBL_BASECOLOR_OCCLUSION_EMIT_SHADER,\r
100   IBL_METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
101   IBL_BASECOLOR_METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
102   IBL_NORMAL_OCCLUSION_EMIT_SHADER,\r
103   IBL_BASECOLOR_NORMAL_OCCLUSION_EMIT_SHADER,\r
104   IBL_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
105   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
106   SHADER_TYPE_MAX = IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER\r
107 };\r
108 \r
109 struct BufferInfo\r
110 {\r
111   BufferInfo()\r
112     : byteLength( -1 ),\r
113     uri( "" ),\r
114     name( "" )\r
115   {\r
116   }\r
117 \r
118   ~BufferInfo()\r
119   {\r
120   }\r
121 \r
122   int byteLength;\r
123   std::string uri;\r
124   std::string name;\r
125 };\r
126 \r
127 struct BufferViewInfo\r
128 {\r
129   BufferViewInfo()\r
130     : buffer( -1 ),\r
131     byteOffset( 0 ),\r
132     byteLength( 0 ),\r
133     byteStride( 0 ),\r
134     target( 0 ),\r
135     name( "" )\r
136   {\r
137   }\r
138 \r
139   ~BufferViewInfo()\r
140   {\r
141   }\r
142 \r
143   int buffer;\r
144   int byteOffset;\r
145   int byteLength;\r
146   int byteStride;\r
147   int target;\r
148   std::string name;\r
149 };\r
150 \r
151 struct TextureInfo\r
152 {\r
153   TextureInfo()\r
154     : sourceIdx( -1 ),\r
155     samplerIdx( -1 )\r
156   {\r
157   }\r
158 \r
159   ~TextureInfo()\r
160   {\r
161   }\r
162   int sourceIdx;\r
163   int samplerIdx;\r
164 };\r
165 \r
166 struct PbrTextureInfo\r
167 {\r
168   PbrTextureInfo()\r
169     : index( -1 ),\r
170     texCoord( 0 ),\r
171     value( 0.0 )\r
172   {\r
173   }\r
174   ~PbrTextureInfo()\r
175   {\r
176   }\r
177 \r
178   int index;\r
179   int texCoord;\r
180   float value;\r
181 };\r
182 \r
183 struct MaterialInfo\r
184 {\r
185   MaterialInfo()\r
186     : baseColorFactor( 1, 1, 1, 1 ),\r
187     metallicFactor( 1.0 ),\r
188     roughnessFactor( 1.0 ),\r
189     emissiveFactor( 0.0, 0.0, 0.0 ),\r
190     alphaMode( "OPAQUE" ),\r
191     alphaCutoff( 0.5 ),\r
192     doubleSided( false ),\r
193     name( "" )\r
194   {\r
195   }\r
196 \r
197   ~MaterialInfo()\r
198   {\r
199   }\r
200 \r
201   Vector4 baseColorFactor;\r
202   float metallicFactor;\r
203   float roughnessFactor;\r
204   Vector3 emissiveFactor;\r
205   std::string alphaMode;\r
206   float alphaCutoff;\r
207   bool doubleSided;\r
208 \r
209   PbrTextureInfo baseColorTexture;\r
210   PbrTextureInfo metallicRoughnessTexture;\r
211   PbrTextureInfo normalTexture;\r
212   PbrTextureInfo occlusionTexture;\r
213   PbrTextureInfo emissiveTexture;\r
214 \r
215   std::string name;\r
216   //need to add max, min\r
217 };\r
218 \r
219 struct AccessorInfo\r
220 {\r
221   AccessorInfo()\r
222     : bufferView( -1 ),\r
223     byteOffset( 0 ),\r
224     componentType( -1 ),\r
225     normalized( false ),\r
226     count( 0 ),\r
227     type( "" ),\r
228     max( 0 ),\r
229     min( 0 ),\r
230     name( "" )\r
231   {\r
232   }\r
233 \r
234   ~AccessorInfo()\r
235   {\r
236   }\r
237 \r
238   int bufferView;\r
239   int byteOffset;\r
240   int componentType;\r
241   bool normalized;\r
242   int count;\r
243   std::string type;\r
244   int max;\r
245   int min;\r
246   std::string name;\r
247   //need to add max, min\r
248 };\r
249 \r
250 struct Attribute\r
251 {\r
252   Attribute()\r
253     : POSITION( -1 ),\r
254     NORMAL( -1 ),\r
255     TANGENT( -1 )\r
256   {\r
257   }\r
258 \r
259   ~Attribute()\r
260   {\r
261   }\r
262 \r
263   int POSITION;\r
264   int NORMAL;\r
265   int TANGENT;\r
266 \r
267   std::vector<int> TEXCOORD;\r
268   std::vector<int> COLOR;\r
269 };\r
270 \r
271 struct MeshInfo\r
272 {\r
273   MeshInfo()\r
274     : indicesIdx( -1 ),\r
275     materialsIdx( -1 ),\r
276     mode( 4 )\r
277   {\r
278   }\r
279 \r
280   ~MeshInfo()\r
281   {\r
282   }\r
283   Geometry geometry;\r
284   std::string name;\r
285 \r
286   int indicesIdx;\r
287   int materialsIdx;\r
288   int mode;\r
289 \r
290   Vector3 size;\r
291   Vector3 pivot;\r
292 \r
293   Attribute attribute;\r
294   //need to add max, min\r
295 };\r
296 \r
297 struct AnimationChannelInfo\r
298 {\r
299   AnimationChannelInfo()\r
300     : sampler( -1 ),\r
301     targetNode( -1 ),\r
302     path( "" )\r
303   {\r
304   }\r
305 \r
306   ~AnimationChannelInfo()\r
307   {\r
308   }\r
309 \r
310   int sampler;\r
311   int targetNode;\r
312   std::string path;\r
313 \r
314 };\r
315 \r
316 struct AnimationSamplerInfo\r
317 {\r
318   AnimationSamplerInfo()\r
319     : input( -1 ),\r
320     output( -1 ),\r
321     interpolation( "" )\r
322   {\r
323   }\r
324 \r
325   ~AnimationSamplerInfo()\r
326   {\r
327   }\r
328 \r
329   int input;\r
330   int output;\r
331   std::string interpolation;\r
332 };\r
333 \r
334 struct AnimationInfo\r
335 {\r
336   AnimationInfo()\r
337     : name( "" )\r
338   {\r
339   }\r
340 \r
341   ~AnimationInfo()\r
342   {\r
343   }\r
344 \r
345   std::string name;\r
346   std::vector<AnimationChannelInfo> channelArray;\r
347   std::vector<AnimationSamplerInfo> samplerArray;\r
348 };\r
349 \r
350 struct OrthographicInfo\r
351 {\r
352   OrthographicInfo()\r
353     : xmag( 0.0f ),\r
354     ymag( 0.0f ),\r
355     zfar( 0.0f ),\r
356     znear( 0.0f )\r
357   {\r
358   }\r
359 \r
360   ~OrthographicInfo()\r
361   {\r
362   }\r
363 \r
364   float xmag;\r
365   float ymag;\r
366   float zfar;\r
367   float znear;\r
368 };\r
369 \r
370 struct PerspectiveInfo\r
371 {\r
372   PerspectiveInfo()\r
373     : aspectRatio( 0.0f ),\r
374     yfov( 0.0f ),\r
375     zfar( 0.0f ),\r
376     znear( 0.0f )\r
377   {\r
378   }\r
379 \r
380   ~PerspectiveInfo()\r
381   {\r
382   }\r
383 \r
384   float aspectRatio;\r
385   float yfov;\r
386   float zfar;\r
387   float znear;\r
388 };\r
389 \r
390 struct CameraInfo\r
391 {\r
392   CameraInfo()\r
393     : name( "" ),\r
394     type( "" )\r
395   {\r
396   }\r
397 \r
398   ~CameraInfo()\r
399   {\r
400   }\r
401 \r
402   std::string name;\r
403   std::string type;\r
404   OrthographicInfo orthographic;\r
405   PerspectiveInfo perspective;\r
406 };\r
407 \r
408 } // namespace GLTF\r
409 \r
410 /**\r
411  *\r
412  * GltfLoader is a class to parse glTf, to load data from file, and to generate Scene.\r
413  * This glTF loader supports glTF 2.0 features.\r
414  *\r
415  * @remarks glTF loader didn't support such features.\r
416  *  - Sparse accessor\r
417  *  - Morphing\r
418  *  - Skeletal Animation\r
419  * These features will be supported soon.\r
420  *\r
421  */\r
422 class GltfLoader\r
423 {\r
424 public:\r
425 \r
426   /**\r
427    * @brief Create an uninitialized GltfLoader.\r
428    */\r
429   GltfLoader();\r
430 \r
431   /**\r
432    * @brief Destructor\r
433    */\r
434   ~GltfLoader();\r
435 \r
436   /**\r
437    * @brief Load Scene from scene format file(e.g., glTF).\r
438    * @param[in] filePath Path of scene format file.\r
439    * @param[in] scene Scene data loaded from file.\r
440    * @return true if scene is successfully loaded\r
441    */\r
442   bool LoadScene( const std::string& filePath, Internal::Scene& scene );\r
443 \r
444 private:\r
445   bool ParseGltf( const std::string& filePath );\r
446   bool LoadAssets();\r
447 \r
448   bool LoadBinaryData( const TreeNode* root );\r
449   bool LoadBuffer( const TreeNode& buffer );\r
450   bool LoadBufferView( const TreeNode& buffer );\r
451   bool LoadAccessor( const TreeNode& buffer );\r
452 \r
453   bool LoadTextureArray( const TreeNode* root );\r
454   Texture LoadTexture( const char* imageUrl, bool generateMipmaps );\r
455   Sampler LoadSampler( const TreeNode& samplerNode );\r
456   FilterMode::Type GetFilterMode( int mode );\r
457   WrapMode::Type GetWrapMode( int mode );\r
458 \r
459   bool LoadMaterialSetArray( const TreeNode* root );\r
460   bool LoadPbrMetallicRoughness( const TreeNode& material, GLTF::MaterialInfo& materialInfo );\r
461 \r
462   bool LoadMeshArray( const TreeNode* root );\r
463   bool LoadPrimitive( const TreeNode& mesh, GLTF::MeshInfo& meshInfo );\r
464   bool LoadAttribute( const TreeNode* attribute, GLTF::MeshInfo& meshInfo );\r
465   bool SetGeometry( GLTF::MeshInfo& meshInfo );\r
466   void SetMeshInfoAndCanonize( GLTF::MeshInfo& meshInfo, Dali::Vector<Dali::Vector3> &vertexBufferData );\r
467 \r
468   bool CreateScene( Internal::Scene& scene );\r
469 \r
470   void LoadCamera( Scene& scene );\r
471   bool LoadOrthoGraphic( const TreeNode& camera, GLTF::CameraInfo& cameraInfo );\r
472   bool LoadPerspective( const TreeNode& camera, GLTF::CameraInfo& cameraInfo );\r
473 \r
474   bool LoadSceneNodes( Scene& scene );\r
475   Actor AddNode( Scene& scene, int index );\r
476   void SetActorCache( Actor& actor, const int index );\r
477   bool SetTextureAndSampler( TextureSet& textureSet, int textureIdx, std::string& toShader, std::string shader, int& addIdx );\r
478 \r
479   bool LoadAnimation( Scene& scene );\r
480   bool LoadAnimationChannels( const TreeNode& animation, GLTF::AnimationInfo& animationInfo );\r
481   bool LoadAnimationSamplers( const TreeNode& animation, GLTF::AnimationInfo& animationInfo );\r
482 \r
483   // template functions\r
484   template <typename T>\r
485   bool ReadBinFile( Vector<T> &dataBuffer, std::string url, int offset, int count );\r
486   template <typename T>\r
487   float IntToFloat( T element, bool normalize );\r
488   template <typename Td, typename Ts>\r
489   void FitBuffer( Dali::Vector<Td>& bufferDestiny, Dali::Vector<Ts>& bufferSource, int bufferSize, int elementNumOfByteStride, bool normalize );\r
490   template <typename Ts>\r
491   void FitBuffer( Dali::Vector<Vector2>& bufferDestiny, Dali::Vector<Ts>& bufferSource, int bufferSize, int elementNumOfByteStride, bool normalize );\r
492   template <typename Ts>\r
493   void FitBuffer( Dali::Vector<Vector3>& bufferDestiny, Dali::Vector<Ts>& bufferSource, int bufferSize, int elementNumOfByteStride, bool normalize );\r
494   template <typename Ts>\r
495   void FitBuffer( Dali::Vector<Vector4>& bufferDestiny, Dali::Vector<Ts>& bufferSource, int bufferSize, int elementNumOfByteStride, bool normalize );\r
496   template <typename T>\r
497   void LoadDataFromAccessor( int accessorIdx, Dali::Vector<T>& bufferData );\r
498   template <typename T>\r
499   PropertyBuffer CreatePropertyBuffer( Vector<T> bufferData, std::string map, int type );\r
500   template <typename T>\r
501   void SetAttributeBufferData( GLTF::MeshInfo& meshInfo, int accessorIdx, std::string map, int type );\r
502   template<typename T>\r
503   float LoadKeyFrames( const GLTF::AnimationSamplerInfo& currentSampler, const Property::Index propIndex, KeyFrames& keyframes );\r
504 \r
505   void SetIndexBuffersData( GLTF::MeshInfo& meshInfo, int indexIdx );\r
506   void SetVertexBufferData( GLTF::MeshInfo& meshInfo, int accessorIdx, std::string map, int type );\r
507 \r
508   // utility functions\r
509   const TreeNode* Tidx( const TreeNode *node, int index );\r
510   bool ReadBool( const TreeNode* node, bool& num );\r
511   bool ReadInt( const TreeNode* node, int& num );\r
512   bool ReadFloat( const TreeNode* node, float& num );\r
513   bool ReadVector( const TreeNode* node, float* num, unsigned int size );\r
514   bool ReadString( const TreeNode* node, std::string& strValue );\r
515 \r
516 public:\r
517   void InitGltf();\r
518 \r
519 private:\r
520   Dali::Toolkit::JsonParser mParser;\r
521   const TreeNode* mNodes;\r
522   const TreeNode* mRoot;\r
523 \r
524   std::string mPath;\r
525 \r
526   std::vector<Actor> mActorCache;\r
527   Shader mShaderCache[GLTF::ShaderType::SHADER_TYPE_MAX + 1];\r
528 \r
529   std::vector<GLTF::BufferInfo> mBufferArray;\r
530   std::vector<GLTF::BufferViewInfo> mBufferViewArray;\r
531   std::vector<GLTF::AccessorInfo> mAccessorArray;\r
532 \r
533   std::vector<GLTF::MeshInfo> mMeshArray;\r
534   std::vector<GLTF::MaterialInfo> mMaterialArray;\r
535   std::vector<GLTF::TextureInfo> mTextureArray;\r
536 \r
537   std::vector<Texture> mSourceArray;\r
538   std::vector<Sampler> mSamplerArray;\r
539 };\r
540 \r
541 }//namespace Internal\r
542 \r
543 }//namespace Toolkit\r
544 \r
545 }//namespace Dali\r
546 \r
547 #endif // DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H\r