[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / gltf2-loader-impl.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/internal/loader/gltf2-loader-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-scene3d/internal/loader/gltf2-util.h>
26 #include <dali-scene3d/public-api/loader/load-result.h>
27 #include <dali-scene3d/public-api/loader/utils.h>
28
29 namespace gt = gltf2;
30 namespace js = json;
31
32 namespace Dali::Scene3D::Loader::Internal
33 {
34 bool Gltf2LoaderImpl::LoadModel(const std::string& url, Dali::Scene3D::Loader::LoadResult& result)
35 {
36   bool failed   = false;
37   auto gltfText = LoadTextFile(url.c_str(), &failed);
38   if(failed)
39   {
40     DALI_LOG_ERROR("Failed to load %s\n", url.c_str());
41     return false;
42   }
43
44   json::unique_ptr root(json_parse(gltfText.c_str(), gltfText.size()));
45   if(!root)
46   {
47     DALI_LOG_ERROR("Failed to parse %s\n", url.c_str());
48     return false;
49   }
50
51   gt::Document document;
52
53   bool isMRendererModel(false);
54   if(!Gltf2Util::GenerateDocument(root, document, isMRendererModel))
55   {
56     return false;
57   }
58
59   auto                         path = url.substr(0, url.rfind('/') + 1);
60   Gltf2Util::ConversionContext context{result, path, INVALID_INDEX};
61
62   Gltf2Util::ConvertGltfToContext(document, context, isMRendererModel);
63
64   return true;
65 }
66
67 } // namespace Dali::Scene3D::Loader::Internal