[Tizen] Add glb-loader to load gltf2-binary
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / gltf2-util.h
1 #ifndef DALI_SCENE3D_LOADER_GLTF2_UTIL_H
2 #define DALI_SCENE3D_LOADER_GLTF2_UTIL_H
3 /*
4  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // INTERNAL INCLUDES
21 #include <dali-scene3d/internal/loader/gltf2-asset.h>
22 #include <dali-scene3d/public-api/loader/load-result.h>
23 #include <dali-scene3d/public-api/loader/resource-bundle.h>
24 #include <dali-scene3d/public-api/loader/scene-definition.h>
25 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
26
27 // EXTERNAL INCLUDES
28 #include <dali/devel-api/threading/mutex.h>
29 #include <dali/integration-api/debug.h>
30
31 namespace gt = gltf2;
32 namespace js = json;
33
34 namespace Dali
35 {
36 namespace Scene3D
37 {
38 namespace Loader
39 {
40 namespace Internal
41 {
42 namespace Gltf2Util
43 {
44
45 struct NodeMapping
46 {
47   Index gltfIdx;
48   Index runtimeIdx;
49
50   bool operator<(Index gltfIdx) const
51   {
52     return this->gltfIdx < gltfIdx;
53   }
54 };
55
56 class NodeIndexMapper
57 {
58 public:
59   NodeIndexMapper()                                  = default;
60   NodeIndexMapper(const NodeIndexMapper&)            = delete;
61   NodeIndexMapper& operator=(const NodeIndexMapper&) = delete;
62
63   ///@brief Registers a mapping of the @a gltfIdx of a node to its @a runtimeIdx .
64   ///@note If the indices are the same, the registration is omitted, in order to
65   /// save growing a vector.
66   void RegisterMapping(Index gltfIdx, Index runtimeIdx)
67   {
68     if(gltfIdx != runtimeIdx)
69     {
70       auto iInsert = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx);
71       DALI_ASSERT_DEBUG(iInsert == mNodes.end() || iInsert->gltfIdx != gltfIdx);
72       mNodes.insert(iInsert, NodeMapping{gltfIdx, runtimeIdx});
73     }
74   }
75
76   ///@brief Retrieves the runtime index of a Node, mapped to the given @a gltfIdx.
77   Index GetRuntimeId(Index gltfIdx) const
78   {
79     auto iFind = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx); // using custom operator<
80     return (iFind != mNodes.end() && iFind->gltfIdx == gltfIdx) ? iFind->runtimeIdx : gltfIdx;
81   }
82
83 private:
84   std::vector<NodeMapping> mNodes;
85 };
86
87 struct ConversionContext
88 {
89   LoadResult& mOutput;
90
91   std::string mPath;
92   Index       mDefaultMaterial;
93
94   std::vector<Index> mMeshIds;
95   NodeIndexMapper    mNodeIndices;
96 };
97
98 void ConvertBuffers(const gt::Document& doc, ConversionContext& context);
99
100 void ConvertMaterials(const gt::Document& doc, ConversionContext& context);
101
102 void ConvertMeshes(const gt::Document& doc, ConversionContext& context);
103
104 void ConvertCamera(const gt::Camera& camera, CameraParameters& camParams);
105
106 void ConvertNodes(const gt::Document& doc, ConversionContext& context, bool isMRendererModel);
107
108 void ConvertAnimations(const gt::Document& doc, ConversionContext& context);
109
110 void ProcessSkins(const gt::Document& doc, ConversionContext& context);
111
112 void ProduceShaders(ShaderDefinitionFactory& shaderFactory, SceneDefinition& scene);
113
114 void SetDefaultEnvironmentMap(const gt::Document& doc, ConversionContext& context);
115
116 const std::string_view GetRendererModelIdentification();
117
118 void ReadDocument(const json_object_s& jsonObject, gt::Document& document);
119
120 void InitializeGltfLoader();
121
122 void ReadDocumentFromParsedData(const json_object_s& jsonObject, gltf2::Document& document);
123
124 bool GenerateDocument(json::unique_ptr& root, gt::Document& document, bool& isMRendererModel);
125
126 void ConvertGltfToContext(gt::Document& document, Gltf2Util::ConversionContext& context, bool isMRendererModel);
127
128 } // namespace Gltf2Util
129
130 } // namespace Internal
131 } // namespace Loader
132 } // namespace Scene3D
133 } // namespace Dali
134
135 #endif // DALI_SCENE3D_LOADER_GLTF2_UTIL_H