1 #ifndef DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H
\r
2 #define DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H
\r
5 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
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
25 using namespace Dali;
\r
26 using namespace Dali::Toolkit;
\r
44 METALLICROUGHNESS_SHADER,
\r
45 BASECOLOR_METALLICROUGHNESS_SHADER,
\r
47 BASECOLOR_NORMAL_SHADER,
\r
48 METALLICROUGHNESS_NORMAL_SHADER,
\r
49 BASECOLOR_METALLICROUGHNESS_NORMAL_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
59 BASECOLOR_EMIT_SHADER,
\r
60 METALLICROUGHNESS_EMIT_SHADER,
\r
61 BASECOLOR_METALLICROUGHNESS_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
75 IBL_BASECOLOR_SHADER,
\r
76 IBL_METALLICROUGHNESS_SHADER,
\r
77 IBL_BASECOLOR_METALLICROUGHNESS_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
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
112 : byteLength( -1 ),
\r
127 struct BufferViewInfo
\r
166 struct PbrTextureInfo
\r
183 struct 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
201 Vector4 baseColorFactor;
\r
202 float metallicFactor;
\r
203 float roughnessFactor;
\r
204 Vector3 emissiveFactor;
\r
205 std::string alphaMode;
\r
209 PbrTextureInfo baseColorTexture;
\r
210 PbrTextureInfo metallicRoughnessTexture;
\r
211 PbrTextureInfo normalTexture;
\r
212 PbrTextureInfo occlusionTexture;
\r
213 PbrTextureInfo emissiveTexture;
\r
216 //need to add max, min
\r
219 struct AccessorInfo
\r
222 : bufferView( -1 ),
\r
224 componentType( -1 ),
\r
225 normalized( false ),
\r
247 //need to add max, min
\r
267 std::vector<int> TEXCOORD;
\r
268 std::vector<int> COLOR;
\r
274 : indicesIdx( -1 ),
\r
275 materialsIdx( -1 ),
\r
293 Attribute attribute;
\r
294 //need to add max, min
\r
297 struct AnimationChannelInfo
\r
299 AnimationChannelInfo()
\r
306 ~AnimationChannelInfo()
\r
316 struct AnimationSamplerInfo
\r
318 AnimationSamplerInfo()
\r
321 interpolation( "" )
\r
325 ~AnimationSamplerInfo()
\r
331 std::string interpolation;
\r
334 struct AnimationInfo
\r
346 std::vector<AnimationChannelInfo> channelArray;
\r
347 std::vector<AnimationSamplerInfo> samplerArray;
\r
350 struct OrthographicInfo
\r
360 ~OrthographicInfo()
\r
370 struct PerspectiveInfo
\r
373 : aspectRatio( 0.0f ),
\r
404 OrthographicInfo orthographic;
\r
405 PerspectiveInfo perspective;
\r
408 } // namespace GLTF
\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
415 * @remarks glTF loader didn't support such features.
\r
416 * - Sparse accessor
\r
418 * - Skeletal Animation
\r
419 * These features will be supported soon.
\r
427 * @brief Create an uninitialized GltfLoader.
\r
432 * @brief Destructor
\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
442 bool LoadScene( const std::string& filePath, Internal::Scene& scene );
\r
445 bool ParseGltf( const std::string& filePath );
\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
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
459 bool LoadMaterialSetArray( const TreeNode* root );
\r
460 bool LoadPbrMetallicRoughness( const TreeNode& material, GLTF::MaterialInfo& materialInfo );
\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
468 bool CreateScene( Internal::Scene& scene );
\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
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
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
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
505 void SetIndexBuffersData( GLTF::MeshInfo& meshInfo, int indexIdx );
\r
506 void SetVertexBufferData( GLTF::MeshInfo& meshInfo, int accessorIdx, std::string map, int type );
\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
520 Dali::Toolkit::JsonParser mParser;
\r
521 const TreeNode* mNodes;
\r
522 const TreeNode* mRoot;
\r
526 std::vector<Actor> mActorCache;
\r
527 Shader mShaderCache[GLTF::ShaderType::SHADER_TYPE_MAX + 1];
\r
529 std::vector<GLTF::BufferInfo> mBufferArray;
\r
530 std::vector<GLTF::BufferViewInfo> mBufferViewArray;
\r
531 std::vector<GLTF::AccessorInfo> mAccessorArray;
\r
533 std::vector<GLTF::MeshInfo> mMeshArray;
\r
534 std::vector<GLTF::MaterialInfo> mMaterialArray;
\r
535 std::vector<GLTF::TextureInfo> mTextureArray;
\r
537 std::vector<Texture> mSourceArray;
\r
538 std::vector<Sampler> mSamplerArray;
\r
541 }//namespace Internal
\r
543 }//namespace Toolkit
\r
547 #endif // DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H
\r