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