1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef CORE__GLTF__GLTF2_UTIL_H
17 #define CORE__GLTF__GLTF2_UTIL_H
18
19 #include <base/containers/string.h>
20 #include <base/containers/string_view.h>
21 #include <base/containers/vector.h>
22 #include <core/namespace.h>
23
24 #include "gltf/data.h"
25
CORE3D_BEGIN_NAMESPACE()26 CORE3D_BEGIN_NAMESPACE()
27 namespace GLTF2 {
28 // Helper functions to access GLTF2 data.
29 bool GetAttributeType(BASE_NS::string_view dataType, AttributeBase& out);
30 bool GetMimeType(BASE_NS::string_view type, MimeType& out);
31
32 bool GetDataType(BASE_NS::string_view dataType, DataType& out);
33 bool GetCameraType(BASE_NS::string_view type, CameraType& out);
34
35 bool GetAlphaMode(BASE_NS::string_view dataType, AlphaMode& out);
36
37 bool GetAnimationInterpolation(BASE_NS::string_view interpolation, AnimationInterpolation& out);
38 bool GetAnimationPath(BASE_NS::string_view path, AnimationPath& out);
39
40 BASE_NS::string_view GetAttributeType(AttributeBase type);
41 BASE_NS::string_view GetMimeType(MimeType type);
42 BASE_NS::string_view GetDataType(DataType type);
43 BASE_NS::string_view GetCameraType(CameraType type);
44
45 #if defined(GLTF2_EXTENSION_KHR_LIGHTS) || defined(GLTF2_EXTENSION_KHR_LIGHTS_PBR)
46 bool GetLightType(BASE_NS::string_view type, LightType& out);
47 BASE_NS::string_view GetLightType(LightType type);
48 #endif
49
50 BASE_NS::string_view GetAlphaMode(AlphaMode mode);
51 BASE_NS::string_view GetAnimationInterpolation(AnimationInterpolation interpolation);
52 BASE_NS::string_view GetAnimationPath(AnimationPath path);
53
54 uint32_t GetComponentByteSize(ComponentType component);
55 uint32_t GetComponentsCount(DataType type);
56 ComponentType GetAlternativeType(ComponentType component, size_t newByteCount);
57
58 #if defined(GLTF2_EXTENSION_KHR_MESH_QUANTIZATION)
59 BASE_NS::string_view ValidatePrimitiveAttributeQuatization(
60 AttributeType attribute, DataType accessorType, ComponentType accessorComponentType);
61 BASE_NS::string_view ValidateMorphTargetAttributeQuantization(
62 AttributeType attribute, DataType accessorType, ComponentType accessorComponentType);
63 #endif
64 BASE_NS::string_view ValidatePrimitiveAttribute(
65 AttributeType attribute, DataType accessorType, ComponentType accessorComponentType);
66 BASE_NS::string_view ValidateMorphTargetAttribute(
67 AttributeType attribute, DataType accessorType, ComponentType accessorComponentType);
68
69 void SplitFilename(BASE_NS::string_view source, BASE_NS::string_view& base, BASE_NS::string_view& path);
70 void SplitBaseFilename(BASE_NS::string_view source, BASE_NS::string_view& name, BASE_NS::string_view& extension);
71
72 BASE_NS::string_view ParseDataUri(const BASE_NS::string_view in, size_t& offsetToData);
73 bool DecodeDataURI(BASE_NS::vector<uint8_t>& out, BASE_NS::string_view in, size_t reqBytes, bool checkSize);
74 bool IsDataURI(BASE_NS::string_view in);
75
76 // Buffer / data helpers
77 struct GLTFLoadDataResult {
78 GLTFLoadDataResult() = default;
79 GLTFLoadDataResult(const GLTFLoadDataResult& other) = delete;
80 GLTFLoadDataResult(GLTFLoadDataResult&& other) noexcept;
81
82 GLTFLoadDataResult& operator=(GLTFLoadDataResult&& other) noexcept;
83
84 bool success { false };
85 bool normalized { false };
86 BASE_NS::string error;
87
88 ComponentType componentType { ComponentType::INVALID };
89 size_t componentByteSize { 0 };
90 size_t componentCount { 0 };
91
92 size_t elementSize { 0 };
93 size_t elementCount { 0 };
94
95 BASE_NS::vector<float> min;
96 BASE_NS::vector<float> max;
97
98 BASE_NS::vector<uint8_t> data;
99 };
100
101 struct BufferLoadResult {
102 bool success { true };
103 BASE_NS::string error;
104 };
105
106 // Populate GLTF buffers with data.
107 BufferLoadResult LoadBuffers(const Data* data, CORE_NS::IFileManager& fileManager);
108
109 enum UriLoadResult {
110 URI_LOAD_SUCCESS,
111 URI_LOAD_FAILED_INVALID_MIME_TYPE,
112 URI_LOAD_FAILED_TO_DECODE_BASE64,
113 URI_LOAD_FAILED_TO_READ_FILE
114 };
115
116 /** Load URI to data buffer.
117 * @param aUri Uri to data, this can point either to data uri or external file.
118 * @param mimeType Requested mime type, such as "image", can be empty to allow all mime types.
119 * @param aFilePath Root path that can be used for relative uri/file lookup.
120 * @param aFileManager File manager to access external files.
121 * @param aOutExtension File extension of the actual file.
122 * @param aOutData Output data.
123 * @return Loading result, URI_LOAD_SUCCESS if data was successfully loaded.
124 */
125 UriLoadResult LoadUri(BASE_NS::string_view uri, BASE_NS::string_view mimeType, BASE_NS::string_view filepath,
126 CORE_NS::IFileManager& fileManager, BASE_NS::string_view& outExtension, BASE_NS::vector<uint8_t>& outData);
127
128 // Load accessor data.
129 GLTFLoadDataResult LoadData(Accessor const& accessor);
130 } // namespace GLTF2
131 CORE3D_END_NAMESPACE()
132
133 #endif // CORE__GLTF__GLTF2_UTIL_H
134