Change namings of gltf-util
[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 // EXTERNAL INCLUDES
21 #include <dali/devel-api/threading/mutex.h>
22 #include <dali/public-api/common/dali-common.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-scene3d/internal/loader/gltf2-asset.h>
26 #include <dali-scene3d/public-api/loader/load-result.h>
27 #include <dali-scene3d/public-api/loader/resource-bundle.h>
28 #include <dali-scene3d/public-api/loader/scene-definition.h>
29 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
30
31 namespace gt = gltf2;
32 namespace js = json;
33
34 namespace Dali::Scene3D::Loader::Internal
35 {
36 namespace Gltf2Util
37 {
38 struct NodeMapping
39 {
40   Index gltfIndex;
41   Index runtimeIndex;
42
43   bool operator<(Index gltfIndex) const
44   {
45     return this->gltfIndex < gltfIndex;
46   }
47 };
48
49 class NodeIndexMapper
50 {
51 public:
52   NodeIndexMapper()                       = default;
53   NodeIndexMapper(const NodeIndexMapper&) = delete;
54   NodeIndexMapper& operator=(const NodeIndexMapper&) = delete;
55
56   ///@brief Registers a mapping of the @a gltfIndex of a node to its @a runtimeIndex .
57   ///@note If the indices are the same, the registration is omitted, in order to
58   /// save growing a vector.
59   void RegisterMapping(Index gltfIndex, Index runtimeIndex)
60   {
61     if(gltfIndex != runtimeIndex)
62     {
63       auto iInsert = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIndex);
64       DALI_ASSERT_DEBUG(iInsert == mNodes.end() || iInsert->gltfIndex != gltfIndex);
65       mNodes.insert(iInsert, NodeMapping{gltfIndex, runtimeIndex});
66     }
67   }
68
69   ///@brief Retrieves the runtime index of a Node, mapped to the given @a gltfIndex.
70   Index GetRuntimeId(Index gltfIndex) const
71   {
72     auto iFind = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIndex); // using custom operator<
73     return (iFind != mNodes.end() && iFind->gltfIndex == gltfIndex) ? iFind->runtimeIndex : gltfIndex;
74   }
75
76 private:
77   std::vector<NodeMapping> mNodes;
78 };
79
80 struct ConversionContext
81 {
82   LoadResult& mOutput;
83
84   std::string mPath;
85   Index       mDefaultMaterial;
86
87   std::vector<Index> mMeshIds;
88   NodeIndexMapper    mNodeIndices;
89 };
90
91 void ConvertBuffers(const gt::Document& document, ConversionContext& context);
92
93 void ConvertMaterials(const gt::Document& document, ConversionContext& context);
94
95 void ConvertMeshes(const gt::Document& document, ConversionContext& context);
96
97 void ConvertCamera(const gt::Camera& camera, CameraParameters& cameraParameters);
98
99 void ConvertNodes(const gt::Document& document, ConversionContext& context, bool isMRendererModel);
100
101 void ConvertAnimations(const gt::Document& document, ConversionContext& context);
102
103 void ProcessSkins(const gt::Document& document, ConversionContext& context);
104
105 void ProduceShaders(ShaderDefinitionFactory& shaderFactory, SceneDefinition& scene);
106
107 void SetDefaultEnvironmentMap(const gt::Document& document, ConversionContext& context);
108
109 const std::string_view GetRendererModelIdentification();
110
111 void ReadDocument(const json_object_s& jsonObject, gt::Document& document);
112
113 void InitializeGltfLoader();
114
115 void ReadDocumentFromParsedData(const json_object_s& jsonObject, gltf2::Document& document);
116
117 bool GenerateDocument(json::unique_ptr& root, gt::Document& document, bool& isMRendererModel);
118
119 void ConvertGltfToContext(gt::Document& document, Gltf2Util::ConversionContext& context, bool isMRendererModel);
120
121 } // namespace Gltf2Util
122
123 } // namespace Dali::Scene3D::Loader::Internal
124
125 #endif // DALI_SCENE3D_LOADER_GLTF2_UTIL_H