[Tizen] Add glb-loader to load gltf2-binary
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / model-loader.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // FILE HEADER
19 #include <dali-scene3d/public-api/loader/model-loader.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <filesystem>
24 #include <memory>
25
26 // INTERNAL INCLUDES
27 #include <dali-scene3d/internal/loader/dli-loader-impl.h>
28 #include <dali-scene3d/internal/loader/gltf2-loader-impl.h>
29 #include <dali-scene3d/internal/loader/glb-loader-impl.h>
30 #include <dali-scene3d/internal/loader/model-loader-impl.h>
31
32 namespace Dali
33 {
34 namespace Scene3D
35 {
36 namespace Loader
37 {
38 namespace
39 {
40 static constexpr std::string_view OBJ_EXTENSION      = ".obj";
41 static constexpr std::string_view GLTF_EXTENSION     = ".gltf";
42 static constexpr std::string_view GLB_EXTENSION      = ".glb";
43 static constexpr std::string_view DLI_EXTENSION      = ".dli";
44 static constexpr std::string_view METADATA_EXTENSION = "metadata";
45 } // namespace
46
47 ModelLoader::ModelLoader(const std::string& modelUrl, const std::string& resourceDirectoryUrl, Dali::Scene3D::Loader::LoadResult& loadResult)
48 : mModelUrl(modelUrl),
49   mResourceDirectoryUrl(resourceDirectoryUrl),
50   mLoadResult(loadResult)
51 {
52   CreateModelLoader();
53 }
54
55 bool ModelLoader::LoadModel(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
56 {
57   if(!mImpl)
58   {
59     return false;
60   }
61
62   bool loadSucceeded = false;
63
64   mLoadResult.mAnimationDefinitions.clear();
65   std::filesystem::path metaDataUrl(mModelUrl);
66   metaDataUrl.replace_extension(METADATA_EXTENSION.data());
67
68   Dali::Scene3D::Loader::LoadSceneMetadata(metaDataUrl.c_str(), mLoadResult.mSceneMetadata);
69   loadSucceeded = mImpl->LoadModel(mModelUrl, mLoadResult);
70   LoadResource(pathProvider, loadOnlyRawResource);
71
72   return loadSucceeded;
73 }
74
75 void ModelLoader::SetInputParameter(InputParameter& inputParameter)
76 {
77   mImpl->SetInputParameter(inputParameter);
78 }
79
80 Dali::Scene3D::Loader::SceneDefinition& ModelLoader::GetScene()
81 {
82   return mLoadResult.mScene;
83 }
84
85 Dali::Scene3D::Loader::ResourceBundle& ModelLoader::GetResources()
86 {
87   return mLoadResult.mResources;
88 }
89
90 std::vector<Dali::Scene3D::Loader::AnimationDefinition>& ModelLoader::GetAnimations()
91 {
92   return mLoadResult.mAnimationDefinitions;
93 }
94
95 std::vector<Dali::Scene3D::Loader::CameraParameters>& ModelLoader::GetCameras()
96 {
97   return mLoadResult.mCameraParameters;
98 }
99
100 Dali::Scene3D::Loader::Customization::Choices& ModelLoader::GetResourceChoices()
101 {
102   return mResourceChoices;
103 }
104
105 void ModelLoader::CreateModelLoader()
106 {
107   std::filesystem::path modelPath(mModelUrl);
108   if(mResourceDirectoryUrl.empty())
109   {
110     mResourceDirectoryUrl = std::string(modelPath.parent_path()) + "/";
111   }
112   std::string extension = modelPath.extension();
113   std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
114
115   if(extension == DLI_EXTENSION)
116   {
117     mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::DliLoaderImpl>();
118   }
119   else if(extension == GLTF_EXTENSION)
120   {
121     mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::Gltf2LoaderImpl>();
122   }
123   else if(extension == GLB_EXTENSION)
124   {
125     mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::GlbLoaderImpl>();
126   }
127   else
128   {
129     DALI_LOG_ERROR("Not supported model format : %s\n", extension.c_str());
130   }
131 }
132
133 void ModelLoader::LoadResource(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
134 {
135   if(GetResources().mRawResourcesLoaded && loadOnlyRawResource)
136   {
137     return;
138   }
139
140   Dali::Scene3D::Loader::ResourceRefCounts resourceRefCount = std::move(mLoadResult.mResources.CreateRefCounter());
141   for(auto iRoot : GetScene().GetRoots())
142   {
143     GetScene().CountResourceRefs(iRoot, mResourceChoices, resourceRefCount);
144   }
145
146   GetResources().mReferenceCounts = std::move(resourceRefCount);
147   GetResources().CountEnvironmentReferences();
148
149   if(loadOnlyRawResource)
150   {
151     GetResources().LoadRawResources(pathProvider);
152   }
153   else
154   {
155     GetResources().LoadResources(pathProvider);
156   }
157 }
158
159 } // namespace Loader
160 } // namespace Scene3D
161 } // namespace Dali