Merge "Legacy Size negotiation mapper" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scene3d-view / 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 // EXTERNAL INCLUDES\r
22 #include <dali/public-api/actors/layer.h>\r
23 #include <dali/public-api/rendering/renderer.h>\r
24 #include <dali/public-api/rendering/shader.h>\r
25 #include <dali/public-api/animation/animation.h>\r
26 \r
27 // INTERNAL INCLUDES\r
28 #include <dali-toolkit/devel-api/builder/json-parser.h>\r
29 #include <dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.h>\r
30 \r
31 using namespace Dali;\r
32 \r
33 namespace Dali\r
34 {\r
35 \r
36 namespace Toolkit\r
37 {\r
38 \r
39 namespace Internal\r
40 {\r
41 \r
42 namespace Gltf\r
43 {\r
44 \r
45 enum ShaderType\r
46 {\r
47   NO_TEXTURE_SHADER,\r
48   BASECOLOR_SHADER,\r
49   METALLICROUGHNESS_SHADER,\r
50   BASECOLOR_METALLICROUGHNESS_SHADER,\r
51   NORMAL_SHADER,\r
52   BASECOLOR_NORMAL_SHADER,\r
53   METALLICROUGHNESS_NORMAL_SHADER,\r
54   BASECOLOR_METALLICROUGHNESS_NORMAL_SHADER,\r
55   OCCLUSION_SHADER,\r
56   BASECOLOR_OCCLUSION_SHADER,\r
57   METALLICROUGHNESS_OCCLUSION_SHADER,\r
58   BASECOLOR_METALLICROUGHNESS_OCCLUSION_SHADER,\r
59   NORMAL_OCCLUSION_SHADER,\r
60   BASECOLOR_NORMAL_OCCLUSION_SHADER,\r
61   METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
62   BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
63   EMIT_SHADER,\r
64   BASECOLOR_EMIT_SHADER,\r
65   METALLICROUGHNESS_EMIT_SHADER,\r
66   BASECOLOR_METALLICROUGHNESS_EMIT_SHADER,\r
67   NORMAL_EMIT_SHADER,\r
68   BASECOLOR_NORMAL_EMIT_SHADER,\r
69   METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
70   BASECOLOR_METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
71   OCCLUSION_EMIT_SHADER,\r
72   BASECOLOR_OCCLUSION_EMIT_SHADER,\r
73   METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
74   BASECOLOR_METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
75   NORMAL_OCCLUSION_EMIT_SHADER,\r
76   BASECOLOR_NORMAL_OCCLUSION_EMIT_SHADER,\r
77   METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
78   BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
79   IBL_SHADER,\r
80   IBL_BASECOLOR_SHADER,\r
81   IBL_METALLICROUGHNESS_SHADER,\r
82   IBL_BASECOLOR_METALLICROUGHNESS_SHADER,\r
83   IBL_NORMAL_SHADER,\r
84   IBL_BASECOLOR_NORMAL_SHADER,\r
85   IBL_METALLICROUGHNESS_NORMAL_SHADER,\r
86   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_SHADER,\r
87   IBL_OCCLUSION_SHADER,\r
88   IBL_BASECOLOR_OCCLUSION_SHADER,\r
89   IBL_METALLICROUGHNESS_OCCLUSION_SHADER,\r
90   IBL_BASECOLOR_METALLICROUGHNESS_OCCLUSION_SHADER,\r
91   IBL_NORMAL_OCCLUSION_SHADER,\r
92   IBL_BASECOLOR_NORMAL_OCCLUSION_SHADER,\r
93   IBL_METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
94   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
95   IBL_EMIT_SHADER,\r
96   IBL_BASECOLOR_EMIT_SHADER,\r
97   IBL_METALLICROUGHNESS_EMIT_SHADER,\r
98   IBL_BASECOLOR_METALLICROUGHNESS_EMIT_SHADER,\r
99   IBL_NORMAL_EMIT_SHADER,\r
100   IBL_BASECOLOR_NORMAL_EMIT_SHADER,\r
101   IBL_METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
102   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
103   IBL_OCCLUSION_EMIT_SHADER,\r
104   IBL_BASECOLOR_OCCLUSION_EMIT_SHADER,\r
105   IBL_METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
106   IBL_BASECOLOR_METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
107   IBL_NORMAL_OCCLUSION_EMIT_SHADER,\r
108   IBL_BASECOLOR_NORMAL_OCCLUSION_EMIT_SHADER,\r
109   IBL_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
110   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
111   SHADER_TYPE_MAX = IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER\r
112 };\r
113 \r
114 struct BufferInfo\r
115 {\r
116   BufferInfo()\r
117     : byteLength( -1 ),\r
118     uri( "" ),\r
119     name( "" )\r
120   {\r
121   }\r
122 \r
123   ~BufferInfo()\r
124   {\r
125   }\r
126 \r
127   int32_t byteLength;\r
128   std::string uri;\r
129   std::string name;\r
130 };\r
131 \r
132 struct BufferViewInfo\r
133 {\r
134   BufferViewInfo()\r
135     : buffer( -1 ),\r
136     byteOffset( 0 ),\r
137     byteLength( 0 ),\r
138     byteStride( 0 ),\r
139     target( 0 ),\r
140     name( "" )\r
141   {\r
142   }\r
143 \r
144   ~BufferViewInfo()\r
145   {\r
146   }\r
147 \r
148   int32_t buffer;\r
149   int32_t byteOffset;\r
150   int32_t byteLength;\r
151   int32_t byteStride;\r
152   int32_t target;\r
153   std::string name;\r
154 };\r
155 \r
156 struct TextureInfo\r
157 {\r
158   TextureInfo()\r
159     : sourceIdx( -1 ),\r
160     samplerIdx( -1 )\r
161   {\r
162   }\r
163 \r
164   ~TextureInfo()\r
165   {\r
166   }\r
167   int32_t sourceIdx;\r
168   int32_t samplerIdx;\r
169 };\r
170 \r
171 struct PbrTextureInfo\r
172 {\r
173   PbrTextureInfo()\r
174     : index( -1 ),\r
175     texCoord( 0 ),\r
176     value( 0.0 )\r
177   {\r
178   }\r
179   ~PbrTextureInfo()\r
180   {\r
181   }\r
182 \r
183   int32_t index;\r
184   int32_t texCoord;\r
185   float value;\r
186 };\r
187 \r
188 struct MaterialInfo\r
189 {\r
190   MaterialInfo()\r
191     : baseColorFactor( 1, 1, 1, 1 ),\r
192     metallicFactor( 1.0 ),\r
193     roughnessFactor( 1.0 ),\r
194     emissiveFactor( 0.0, 0.0, 0.0 ),\r
195     alphaMode( "OPAQUE" ),\r
196     alphaCutoff( 0.5 ),\r
197     doubleSided( false ),\r
198     name( "" )\r
199   {\r
200   }\r
201 \r
202   ~MaterialInfo()\r
203   {\r
204   }\r
205 \r
206   Vector4 baseColorFactor;\r
207   float metallicFactor;\r
208   float roughnessFactor;\r
209   Vector3 emissiveFactor;\r
210   std::string alphaMode;\r
211   float alphaCutoff;\r
212   bool doubleSided;\r
213 \r
214   PbrTextureInfo baseColorTexture;\r
215   PbrTextureInfo metallicRoughnessTexture;\r
216   PbrTextureInfo normalTexture;\r
217   PbrTextureInfo occlusionTexture;\r
218   PbrTextureInfo emissiveTexture;\r
219 \r
220   std::string name;\r
221   //need to add max, min\r
222 };\r
223 \r
224 struct AccessorInfo\r
225 {\r
226   AccessorInfo()\r
227     : bufferView( -1 ),\r
228     byteOffset( 0 ),\r
229     componentType( -1 ),\r
230     normalized( false ),\r
231     count( 0 ),\r
232     type( "" ),\r
233     max( 0 ),\r
234     min( 0 ),\r
235     name( "" )\r
236   {\r
237   }\r
238 \r
239   ~AccessorInfo()\r
240   {\r
241   }\r
242 \r
243   int32_t bufferView;\r
244   int32_t byteOffset;\r
245   int32_t componentType;\r
246   bool normalized;\r
247   int32_t count;\r
248   std::string type;\r
249   int32_t max;\r
250   int32_t min;\r
251   std::string name;\r
252   //need to add max, min\r
253 };\r
254 \r
255 struct Attribute\r
256 {\r
257   Attribute()\r
258     : POSITION( -1 ),\r
259     NORMAL( -1 ),\r
260     TANGENT( -1 )\r
261   {\r
262   }\r
263 \r
264   ~Attribute()\r
265   {\r
266   }\r
267 \r
268   int32_t POSITION;\r
269   int32_t NORMAL;\r
270   int32_t TANGENT;\r
271 \r
272   std::vector<int32_t> TEXCOORD;\r
273   std::vector<int32_t> COLOR;\r
274 };\r
275 \r
276 struct MeshInfo\r
277 {\r
278   MeshInfo()\r
279     : indicesIdx( -1 ),\r
280     materialsIdx( -1 ),\r
281     mode( 4 )\r
282   {\r
283   }\r
284 \r
285   ~MeshInfo()\r
286   {\r
287   }\r
288   Geometry geometry;\r
289   std::string name;\r
290 \r
291   int32_t indicesIdx;\r
292   int32_t materialsIdx;\r
293   int32_t mode;\r
294 \r
295   Vector3 size;\r
296   Vector3 pivot;\r
297 \r
298   Attribute attribute;\r
299   //need to add max, min\r
300 };\r
301 \r
302 struct AnimationChannelInfo\r
303 {\r
304   AnimationChannelInfo()\r
305     : sampler( -1 ),\r
306     targetNode( -1 ),\r
307     path( "" )\r
308   {\r
309   }\r
310 \r
311   ~AnimationChannelInfo()\r
312   {\r
313   }\r
314 \r
315   int32_t sampler;\r
316   int32_t targetNode;\r
317   std::string path;\r
318 \r
319 };\r
320 \r
321 struct AnimationSamplerInfo\r
322 {\r
323   AnimationSamplerInfo()\r
324     : input( -1 ),\r
325     output( -1 ),\r
326     interpolation( "" )\r
327   {\r
328   }\r
329 \r
330   ~AnimationSamplerInfo()\r
331   {\r
332   }\r
333 \r
334   int32_t input;\r
335   int32_t output;\r
336   std::string interpolation;\r
337 };\r
338 \r
339 struct AnimationInfo\r
340 {\r
341   AnimationInfo()\r
342     : name( "" )\r
343   {\r
344   }\r
345 \r
346   ~AnimationInfo()\r
347   {\r
348   }\r
349 \r
350   std::string name;\r
351   std::vector<AnimationChannelInfo> channelArray;\r
352   std::vector<AnimationSamplerInfo> samplerArray;\r
353 };\r
354 \r
355 struct OrthographicInfo\r
356 {\r
357   OrthographicInfo()\r
358     : xmag( 0.0f ),\r
359     ymag( 0.0f ),\r
360     zfar( 0.0f ),\r
361     znear( 0.0f )\r
362   {\r
363   }\r
364 \r
365   ~OrthographicInfo()\r
366   {\r
367   }\r
368 \r
369   float xmag;\r
370   float ymag;\r
371   float zfar;\r
372   float znear;\r
373 };\r
374 \r
375 struct PerspectiveInfo\r
376 {\r
377   PerspectiveInfo()\r
378     : aspectRatio( 0.0f ),\r
379     yfov( 0.0f ),\r
380     zfar( 0.0f ),\r
381     znear( 0.0f )\r
382   {\r
383   }\r
384 \r
385   ~PerspectiveInfo()\r
386   {\r
387   }\r
388 \r
389   float aspectRatio;\r
390   float yfov;\r
391   float zfar;\r
392   float znear;\r
393 };\r
394 \r
395 struct CameraInfo\r
396 {\r
397   CameraInfo()\r
398     : name( "" ),\r
399     type( "" )\r
400   {\r
401   }\r
402 \r
403   ~CameraInfo()\r
404   {\r
405   }\r
406 \r
407   std::string name;\r
408   std::string type;\r
409   OrthographicInfo orthographic;\r
410   PerspectiveInfo perspective;\r
411 };\r
412 \r
413 /**\r
414  *\r
415  * Loader is a class to parse glTf, to load data from file, and to generate Scene.\r
416  * This glTF loader supports glTF 2.0 features.\r
417  *\r
418  * @remarks glTF loader didn't support such features.\r
419  *  - Sparse accessor\r
420  *  - Morphing\r
421  *  - Skeletal Animation\r
422  * These features will be supported soon.\r
423  *\r
424  */\r
425 class Loader\r
426 {\r
427 public:\r
428 \r
429   /**\r
430    * @brief Create an uninitialized Loader.\r
431    */\r
432   Loader();\r
433 \r
434   /**\r
435    * @brief Destructor\r
436    */\r
437   ~Loader();\r
438 \r
439   /**\r
440    * @brief Load Scene3dView from scene format file(e.g., glTF).\r
441    * @param[in] filePath Path of scene format file.\r
442    * @param[in] scene3dView Scene3dView data loaded from file.\r
443    * @return true if scene is successfully loaded\r
444    */\r
445   bool LoadScene( const std::string& filePath, Internal::Scene3dView& scene3dView );\r
446 \r
447 private:\r
448   bool ParseGltf( const std::string& filePath );\r
449   bool LoadAssets();\r
450 \r
451   bool CreateScene( Internal::Scene3dView& scene3dView );\r
452 \r
453   void LoadCamera( Scene3dView& scene3dView );\r
454   bool LoadOrthoGraphic( const TreeNode& camera, CameraInfo& cameraInfo );\r
455   bool LoadPerspective( const TreeNode& camera, CameraInfo& cameraInfo );\r
456 \r
457   bool LoadSceneNodes( Scene3dView& scene3dView );\r
458   Actor AddNode( Scene3dView& scene3dView, uint32_t index );\r
459   void SetActorCache( Actor& actor, uint32_t index );\r
460   bool SetTextureAndSampler( TextureSet& textureSet, int32_t textureIdx, std::string& toShader, std::string shader, int32_t& addIdx );\r
461 \r
462   bool LoadAnimation( Scene3dView& scene3dView );\r
463   bool LoadAnimationChannels( const TreeNode& animation, AnimationInfo& animationInfo );\r
464   bool LoadAnimationSamplers( const TreeNode& animation, AnimationInfo& animationInfo );\r
465 \r
466 private:\r
467   Dali::Toolkit::JsonParser mParser;\r
468   const TreeNode* mNodes;\r
469   const TreeNode* mRoot;\r
470 \r
471   std::string mPath;\r
472 \r
473   std::vector<Actor> mActorCache;\r
474   Shader mShaderCache[ShaderType::SHADER_TYPE_MAX + 1];\r
475 \r
476   std::vector<BufferInfo> mBufferArray;\r
477   std::vector<BufferViewInfo> mBufferViewArray;\r
478   std::vector<AccessorInfo> mAccessorArray;\r
479 \r
480   std::vector<MeshInfo> mMeshArray;\r
481   std::vector<MaterialInfo> mMaterialArray;\r
482   std::vector<TextureInfo> mTextureArray;\r
483 \r
484   std::vector<Texture> mSourceArray;\r
485   std::vector<Sampler> mSamplerArray;\r
486 };\r
487 \r
488 }//namespace Gltf\r
489 \r
490 }//namespace Internal\r
491 \r
492 }//namespace Toolkit\r
493 \r
494 }//namespace Dali\r
495 \r
496 #endif // DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H\r