Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gltf/structures/gltf_accessor.h
21344 views
1
/**************************************************************************/
2
/* gltf_accessor.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "../gltf_defines.h"
34
35
#include "gltf_buffer_view.h"
36
37
class GLTFAccessor : public Resource {
38
GDCLASS(GLTFAccessor, Resource);
39
friend class GLTFDocument;
40
41
public:
42
enum GLTFAccessorType {
43
TYPE_SCALAR,
44
TYPE_VEC2,
45
TYPE_VEC3,
46
TYPE_VEC4,
47
TYPE_MAT2,
48
TYPE_MAT3,
49
TYPE_MAT4,
50
};
51
52
enum GLTFComponentType {
53
COMPONENT_TYPE_NONE = 0,
54
COMPONENT_TYPE_SIGNED_BYTE = 5120,
55
COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
56
COMPONENT_TYPE_SIGNED_SHORT = 5122,
57
COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
58
COMPONENT_TYPE_SIGNED_INT = 5124,
59
COMPONENT_TYPE_UNSIGNED_INT = 5125,
60
COMPONENT_TYPE_SINGLE_FLOAT = 5126,
61
COMPONENT_TYPE_DOUBLE_FLOAT = 5130,
62
COMPONENT_TYPE_HALF_FLOAT = 5131,
63
COMPONENT_TYPE_SIGNED_LONG = 5134,
64
COMPONENT_TYPE_UNSIGNED_LONG = 5135,
65
};
66
67
private:
68
GLTFBufferViewIndex buffer_view = -1;
69
int64_t byte_offset = 0;
70
GLTFComponentType component_type = COMPONENT_TYPE_NONE;
71
bool normalized = false;
72
int64_t count = 0;
73
GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_SCALAR;
74
Vector<double> min;
75
Vector<double> max;
76
int64_t sparse_count = 0;
77
GLTFBufferViewIndex sparse_indices_buffer_view = 0;
78
int64_t sparse_indices_byte_offset = 0;
79
GLTFComponentType sparse_indices_component_type = COMPONENT_TYPE_NONE;
80
GLTFBufferViewIndex sparse_values_buffer_view = 0;
81
int64_t sparse_values_byte_offset = 0;
82
83
// Trivial helper functions.
84
void _calculate_min_and_max(const PackedFloat64Array &p_numbers);
85
void _determine_pad_skip(int64_t &r_skip_every, int64_t &r_skip_bytes) const;
86
int64_t _determine_padded_byte_count(int64_t p_raw_byte_size) const;
87
PackedFloat64Array _filter_numbers(const PackedFloat64Array &p_numbers) const;
88
static String _get_component_type_name(const GLTFComponentType p_component);
89
static GLTFComponentType _get_indices_component_type_for_size(const int64_t p_size);
90
static GLTFAccessorType _get_accessor_type_from_str(const String &p_string);
91
String _get_accessor_type_name() const;
92
int64_t _get_vector_size() const;
93
static int64_t _get_numbers_per_variant_for_gltf(Variant::Type p_variant_type);
94
static int64_t _get_bytes_per_component(const GLTFComponentType p_component_type);
95
int64_t _get_bytes_per_vector() const;
96
97
// Private decode functions.
98
PackedInt64Array _decode_sparse_indices(const Ref<GLTFState> &p_gltf_state, const Vector<Ref<GLTFBufferView>> &p_buffer_views) const;
99
template <typename T>
100
Vector<T> _decode_raw_numbers(const Ref<GLTFState> &p_gltf_state, const Vector<Ref<GLTFBufferView>> &p_buffer_views, bool p_sparse_values) const;
101
template <typename T>
102
Vector<T> _decode_as_numbers(const Ref<GLTFState> &p_gltf_state) const;
103
104
// Private encode functions.
105
PackedFloat64Array _encode_variants_as_floats(const Array &p_input_data, Variant::Type p_variant_type) const;
106
void _store_sparse_indices_into_state(const Ref<GLTFState> &p_gltf_state, const PackedInt64Array &p_sparse_indices, const bool p_deduplicate = true);
107
108
protected:
109
static void _bind_methods();
110
111
#ifndef DISABLE_DEPRECATED
112
// 32-bit and non-const versions for compatibility.
113
GLTFBufferViewIndex _get_buffer_view_bind_compat_106220();
114
int _get_byte_offset_bind_compat_106220();
115
int _get_component_type_bind_compat_106220();
116
void _set_component_type_bind_compat_106220(int p_component_type);
117
bool _get_normalized_bind_compat_106220();
118
int _get_count_bind_compat_106220();
119
GLTFAccessorType _get_accessor_type_bind_compat_106220();
120
int _get_type_bind_compat_106220();
121
Vector<double> _get_min_bind_compat_106220();
122
Vector<double> _get_max_bind_compat_106220();
123
int _get_sparse_count_bind_compat_106220();
124
int _get_sparse_indices_buffer_view_bind_compat_106220();
125
int _get_sparse_indices_byte_offset_bind_compat_106220();
126
int _get_sparse_indices_component_type_bind_compat_106220();
127
void _set_sparse_indices_component_type_bind_compat_106220(int p_sparse_indices_component_type);
128
int _get_sparse_values_buffer_view_bind_compat_106220();
129
int _get_sparse_values_byte_offset_bind_compat_106220();
130
static void _bind_compatibility_methods();
131
#endif // DISABLE_DEPRECATED
132
133
public:
134
// Property getters and setters.
135
GLTFBufferViewIndex get_buffer_view() const;
136
void set_buffer_view(GLTFBufferViewIndex p_buffer_view);
137
138
int64_t get_byte_offset() const;
139
void set_byte_offset(int64_t p_byte_offset);
140
141
GLTFComponentType get_component_type() const;
142
void set_component_type(GLTFComponentType p_component_type);
143
144
bool get_normalized() const;
145
void set_normalized(bool p_normalized);
146
147
int64_t get_count() const;
148
void set_count(int64_t p_count);
149
150
GLTFAccessorType get_accessor_type() const;
151
void set_accessor_type(GLTFAccessorType p_accessor_type);
152
153
int get_type() const;
154
void set_type(int p_accessor_type);
155
156
Vector<double> get_min() const;
157
void set_min(const Vector<double> &p_min);
158
159
Vector<double> get_max() const;
160
void set_max(const Vector<double> &p_max);
161
162
int64_t get_sparse_count() const;
163
void set_sparse_count(int64_t p_sparse_count);
164
165
GLTFBufferViewIndex get_sparse_indices_buffer_view() const;
166
void set_sparse_indices_buffer_view(GLTFBufferViewIndex p_sparse_indices_buffer_view);
167
168
int64_t get_sparse_indices_byte_offset() const;
169
void set_sparse_indices_byte_offset(int64_t p_sparse_indices_byte_offset);
170
171
GLTFComponentType get_sparse_indices_component_type() const;
172
void set_sparse_indices_component_type(GLTFComponentType p_sparse_indices_component_type);
173
174
GLTFBufferViewIndex get_sparse_values_buffer_view() const;
175
void set_sparse_values_buffer_view(GLTFBufferViewIndex p_sparse_values_buffer_view);
176
177
int64_t get_sparse_values_byte_offset() const;
178
void set_sparse_values_byte_offset(int64_t p_sparse_values_byte_offset);
179
180
bool is_equal_exact(const Ref<GLTFAccessor> &p_other) const;
181
182
// High-level decode functions.
183
PackedColorArray decode_as_colors(const Ref<GLTFState> &p_gltf_state) const;
184
PackedFloat32Array decode_as_float32s(const Ref<GLTFState> &p_gltf_state) const;
185
PackedFloat64Array decode_as_float64s(const Ref<GLTFState> &p_gltf_state) const;
186
PackedInt32Array decode_as_int32s(const Ref<GLTFState> &p_gltf_state) const;
187
PackedInt64Array decode_as_int64s(const Ref<GLTFState> &p_gltf_state) const;
188
Vector<Quaternion> decode_as_quaternions(const Ref<GLTFState> &p_gltf_state) const;
189
PackedVector2Array decode_as_vector2s(const Ref<GLTFState> &p_gltf_state) const;
190
PackedVector3Array decode_as_vector3s(const Ref<GLTFState> &p_gltf_state) const;
191
PackedVector4Array decode_as_vector4s(const Ref<GLTFState> &p_gltf_state) const;
192
Array decode_as_variants(const Ref<GLTFState> &p_gltf_state, Variant::Type p_variant_type) const;
193
194
// Low-level encode functions.
195
static GLTFComponentType get_minimal_integer_component_type_from_ints(const PackedInt64Array &p_numbers);
196
PackedByteArray encode_floats_as_bytes(const PackedFloat64Array &p_input_numbers);
197
PackedByteArray encode_ints_as_bytes(const PackedInt64Array &p_input_numbers);
198
PackedByteArray encode_variants_as_bytes(const Array &p_input_data, Variant::Type p_variant_type);
199
200
GLTFAccessorIndex store_accessor_data_into_state(const Ref<GLTFState> &p_gltf_state, const PackedByteArray &p_data_bytes, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const GLTFBufferIndex p_buffer_index = 0, const bool p_deduplicate = true);
201
static Ref<GLTFAccessor> make_new_accessor_without_data(GLTFAccessorType p_accessor_type = TYPE_SCALAR, GLTFComponentType p_component_type = COMPONENT_TYPE_SINGLE_FLOAT);
202
203
// High-level encode functions.
204
static GLTFAccessorIndex encode_new_accessor_from_colors(const Ref<GLTFState> &p_gltf_state, const PackedColorArray &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
205
static GLTFAccessorIndex encode_new_accessor_from_float64s(const Ref<GLTFState> &p_gltf_state, const PackedFloat64Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
206
static GLTFAccessorIndex encode_new_accessor_from_int32s(const Ref<GLTFState> &p_gltf_state, const PackedInt32Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
207
static GLTFAccessorIndex encode_new_accessor_from_int64s(const Ref<GLTFState> &p_gltf_state, const PackedInt64Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
208
static GLTFAccessorIndex encode_new_accessor_from_quaternions(const Ref<GLTFState> &p_gltf_state, const Vector<Quaternion> &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
209
static GLTFAccessorIndex encode_new_accessor_from_variants(const Ref<GLTFState> &p_gltf_state, const Array &p_input_data, Variant::Type p_variant_type, GLTFAccessorType p_accessor_type = TYPE_SCALAR, GLTFComponentType p_component_type = COMPONENT_TYPE_SINGLE_FLOAT, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
210
static GLTFAccessorIndex encode_new_accessor_from_vector2s(const Ref<GLTFState> &p_gltf_state, const PackedVector2Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
211
static GLTFAccessorIndex encode_new_accessor_from_vector3s(const Ref<GLTFState> &p_gltf_state, const PackedVector3Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
212
static GLTFAccessorIndex encode_new_accessor_from_vector4s(const Ref<GLTFState> &p_gltf_state, const PackedVector4Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
213
static GLTFAccessorIndex encode_new_accessor_from_vector4is(const Ref<GLTFState> &p_gltf_state, const Vector<Vector4i> &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
214
static GLTFAccessorIndex encode_new_sparse_accessor_from_vec3s(const Ref<GLTFState> &p_gltf_state, const PackedVector3Array &p_input_data, const PackedVector3Array &p_base_reference_data, const double p_tolerance_multiplier = 1.0, const GLTFBufferView::ArrayBufferTarget p_main_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);
215
216
// Dictionary conversion.
217
static Ref<GLTFAccessor> from_dictionary(const Dictionary &p_dict);
218
Dictionary to_dictionary() const;
219
};
220
221
VARIANT_ENUM_CAST(GLTFAccessor::GLTFAccessorType);
222
VARIANT_ENUM_CAST(GLTFAccessor::GLTFComponentType);
223
224