Make sure that global variables are initialized lazily in scene3d.
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / gltf2-asset.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 // EXTERNAL INCLUDES
19 #include <dali/public-api/math/matrix.h>
20 #include <algorithm>
21 #include <map>
22
23 // INTERNAL INCLUDES
24 #include <dali-scene3d/internal/loader/gltf2-asset.h>
25
26 using namespace Dali;
27
28 namespace gltf2
29 {
30 namespace
31 {
32 constexpr uint32_t ACCESSOR_TYPE_ELEMENT_COUNT[]{
33   1,
34   2,
35   3,
36   4,
37   4,
38   9,
39   16,
40   static_cast<uint32_t>(-1)};
41
42 const std::map<std::string_view, AccessorType::Type>& GetAccessorTypes()
43 {
44   static const std::map<std::string_view, AccessorType::Type> ACCESSOR_TYPES{
45     ENUM_STRING_MAPPING(AccessorType, SCALAR),
46     ENUM_STRING_MAPPING(AccessorType, VEC2),
47     ENUM_STRING_MAPPING(AccessorType, VEC3),
48     ENUM_STRING_MAPPING(AccessorType, VEC4),
49     ENUM_STRING_MAPPING(AccessorType, MAT2),
50     ENUM_STRING_MAPPING(AccessorType, MAT3),
51     ENUM_STRING_MAPPING(AccessorType, MAT4),
52   };
53   return ACCESSOR_TYPES;
54 }
55
56 const std::map<std::string_view, AlphaMode::Type>& GetAlphaModeTypes()
57 {
58   static const std::map<std::string_view, AlphaMode::Type> ALPHA_MODE_TYPES{
59     ENUM_STRING_MAPPING(AlphaMode::Type, OPAQUE),
60     ENUM_STRING_MAPPING(AlphaMode::Type, MASK),
61     ENUM_STRING_MAPPING(AlphaMode::Type, BLEND),
62   };
63   return ALPHA_MODE_TYPES;
64 }
65
66 const std::map<std::string_view, Attribute::Type>& GetAttributeTypes()
67 {
68   static const std::map<std::string_view, Attribute::Type> ATTRIBUTE_TYPES{
69     ENUM_STRING_MAPPING(Attribute::Type, POSITION),
70     ENUM_STRING_MAPPING(Attribute::Type, NORMAL),
71     ENUM_STRING_MAPPING(Attribute::Type, TANGENT),
72     ENUM_STRING_MAPPING(Attribute::Type, TEXCOORD_0),
73     ENUM_STRING_MAPPING(Attribute::Type, TEXCOORD_1),
74     ENUM_STRING_MAPPING(Attribute::Type, COLOR_0),
75     ENUM_STRING_MAPPING(Attribute::Type, JOINTS_0),
76     ENUM_STRING_MAPPING(Attribute::Type, WEIGHTS_0),
77   };
78   return ATTRIBUTE_TYPES;
79 }
80
81 const std::map<std::string_view, Animation::Sampler::Interpolation::Type>& GetAnimationSamplerInterpolation()
82 {
83   static const std::map<std::string_view, Animation::Sampler::Interpolation::Type> ANIMATION_SAMPLER_INTERPOLATION{
84     ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, STEP),
85     ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, LINEAR),
86     ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, CUBICSPLINE),
87   };
88   return ANIMATION_SAMPLER_INTERPOLATION;
89 }
90
91 const std::map<std::string_view, Animation::Channel::Target::Type>& GetAnimationChannelTargetPathTypes()
92 {
93   static const std::map<std::string_view, Animation::Channel::Target::Type> ANIMATION_CHANNEL_TARGET_PATH_TYPES{
94     ENUM_STRING_MAPPING(Animation::Channel::Target::Type, TRANSLATION),
95     ENUM_STRING_MAPPING(Animation::Channel::Target::Type, ROTATION),
96     ENUM_STRING_MAPPING(Animation::Channel::Target::Type, SCALE),
97     ENUM_STRING_MAPPING(Animation::Channel::Target::Type, WEIGHTS),
98   };
99   return ANIMATION_CHANNEL_TARGET_PATH_TYPES;
100 }
101
102 } // namespace
103
104 ENUM_TYPE_FROM_STRING(AccessorType, GetAccessorTypes())
105 ENUM_TYPE_FROM_STRING(AlphaMode, GetAlphaModeTypes())
106 ENUM_TYPE_FROM_STRING(Attribute, GetAttributeTypes())
107 ENUM_TYPE_FROM_STRING(Animation::Sampler::Interpolation, GetAnimationSamplerInterpolation())
108 ENUM_TYPE_FROM_STRING(Animation::Channel::Target, GetAnimationChannelTargetPathTypes())
109
110 bool Component::IsUnsigned(Type t)
111 {
112   return t == UNSIGNED_BYTE || t == UNSIGNED_SHORT || t == UNSIGNED_INT;
113 }
114
115 uint32_t Component::Size(Type t)
116 {
117   switch(t)
118   {
119     case BYTE:
120     case UNSIGNED_BYTE:
121       return 1;
122     case SHORT:
123     case UNSIGNED_SHORT:
124       return 2;
125     case UNSIGNED_INT:
126     case FLOAT:
127       return 4;
128     default:
129       return -1;
130   }
131 }
132
133 uint32_t AccessorType::ElementCount(Type t)
134 {
135   return ACCESSOR_TYPE_ELEMENT_COUNT[t];
136 }
137
138 uint32_t ComponentTypedBufferViewClient::GetBytesPerComponent() const
139 {
140   return Component::Size(mComponentType);
141 }
142
143 void Node::SetMatrix(const Matrix& m)
144 {
145   m.GetTransformComponents(mTranslation, mRotation, mScale);
146 }
147
148 Quaternion ReadQuaternion(const json_value_s& j)
149 {
150   return Quaternion(ReadDaliVector<Vector4>(j));
151 }
152
153 } // namespace gltf2