Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move GLTFAccessorType into GLTFAccessor #93920

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions modules/gltf/doc_classes/GLTFAccessor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<members>
<member name="accessor_type" type="int" setter="set_accessor_type" getter="get_accessor_type" default="0">
<member name="accessor_type" type="int" setter="set_accessor_type" getter="get_accessor_type" enum="GLTFAccessor.GLTFAccessorType" default="0">
The GLTF accessor type as an enum. Possible values are 0 for "SCALAR", 1 for "VEC2", 2 for "VEC3", 3 for "VEC4", 4 for "MAT2", 5 for "MAT3", and 6 for "MAT4".
</member>
<member name="buffer_view" type="int" setter="set_buffer_view" getter="get_buffer_view" default="-1">
Expand Down Expand Up @@ -54,8 +54,31 @@
<member name="sparse_values_byte_offset" type="int" setter="set_sparse_values_byte_offset" getter="get_sparse_values_byte_offset" default="0">
The offset relative to the start of the bufferView in bytes.
</member>
<member name="type" type="int" setter="set_type" getter="get_type" default="0" deprecated="Use [member accessor_type] instead.">
<member name="type" type="int" setter="set_type" getter="get_type" deprecated="Use [member accessor_type] instead.">
The GLTF accessor type as an enum. Use [member accessor_type] instead.
</member>
</members>
<constants>
<constant name="TYPE_SCALAR" value="0" enum="GLTFAccessorType">
Accessor type "SCALAR". For the glTF object model, this can be used to map to a single float, int, or bool value, or a float array.
</constant>
<constant name="TYPE_VEC2" value="1" enum="GLTFAccessorType">
Accessor type "VEC2". For the glTF object model, this maps to "float2", represented in the glTF JSON as an array of two floats.
</constant>
<constant name="TYPE_VEC3" value="2" enum="GLTFAccessorType">
Accessor type "VEC3". For the glTF object model, this maps to "float3", represented in the glTF JSON as an array of three floats.
</constant>
<constant name="TYPE_VEC4" value="3" enum="GLTFAccessorType">
Accessor type "VEC4". For the glTF object model, this maps to "float4", represented in the glTF JSON as an array of four floats.
</constant>
<constant name="TYPE_MAT2" value="4" enum="GLTFAccessorType">
Accessor type "MAT2". For the glTF object model, this maps to "float2x2", represented in the glTF JSON as an array of four floats.
</constant>
<constant name="TYPE_MAT3" value="5" enum="GLTFAccessorType">
Accessor type "MAT3". For the glTF object model, this maps to "float3x3", represented in the glTF JSON as an array of nine floats.
</constant>
<constant name="TYPE_MAT4" value="6" enum="GLTFAccessorType">
Accessor type "MAT4". For the glTF object model, this maps to "float4x4", represented in the glTF JSON as an array of sixteen floats.
</constant>
</constants>
</class>
78 changes: 39 additions & 39 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,58 +934,58 @@ Error GLTFDocument::_encode_accessors(Ref<GLTFState> p_state) {
return OK;
}

String GLTFDocument::_get_accessor_type_name(const GLTFAccessorType p_accessor_type) {
if (p_accessor_type == GLTFAccessorType::TYPE_SCALAR) {
String GLTFDocument::_get_accessor_type_name(const GLTFAccessor::GLTFAccessorType p_accessor_type) {
if (p_accessor_type == GLTFAccessor::TYPE_SCALAR) {
return "SCALAR";
}
if (p_accessor_type == GLTFAccessorType::TYPE_VEC2) {
if (p_accessor_type == GLTFAccessor::TYPE_VEC2) {
return "VEC2";
}
if (p_accessor_type == GLTFAccessorType::TYPE_VEC3) {
if (p_accessor_type == GLTFAccessor::TYPE_VEC3) {
return "VEC3";
}
if (p_accessor_type == GLTFAccessorType::TYPE_VEC4) {
if (p_accessor_type == GLTFAccessor::TYPE_VEC4) {
return "VEC4";
}

if (p_accessor_type == GLTFAccessorType::TYPE_MAT2) {
if (p_accessor_type == GLTFAccessor::TYPE_MAT2) {
return "MAT2";
}
if (p_accessor_type == GLTFAccessorType::TYPE_MAT3) {
if (p_accessor_type == GLTFAccessor::TYPE_MAT3) {
return "MAT3";
}
if (p_accessor_type == GLTFAccessorType::TYPE_MAT4) {
if (p_accessor_type == GLTFAccessor::TYPE_MAT4) {
return "MAT4";
}
ERR_FAIL_V("SCALAR");
}

GLTFAccessorType GLTFDocument::_get_accessor_type_from_str(const String &p_string) {
GLTFAccessor::GLTFAccessorType GLTFDocument::_get_accessor_type_from_str(const String &p_string) {
if (p_string == "SCALAR") {
return GLTFAccessorType::TYPE_SCALAR;
return GLTFAccessor::TYPE_SCALAR;
}

if (p_string == "VEC2") {
return GLTFAccessorType::TYPE_VEC2;
return GLTFAccessor::TYPE_VEC2;
}
if (p_string == "VEC3") {
return GLTFAccessorType::TYPE_VEC3;
return GLTFAccessor::TYPE_VEC3;
}
if (p_string == "VEC4") {
return GLTFAccessorType::TYPE_VEC4;
return GLTFAccessor::TYPE_VEC4;
}

if (p_string == "MAT2") {
return GLTFAccessorType::TYPE_MAT2;
return GLTFAccessor::TYPE_MAT2;
}
if (p_string == "MAT3") {
return GLTFAccessorType::TYPE_MAT3;
return GLTFAccessor::TYPE_MAT3;
}
if (p_string == "MAT4") {
return GLTFAccessorType::TYPE_MAT4;
return GLTFAccessor::TYPE_MAT4;
}

ERR_FAIL_V(GLTFAccessorType::TYPE_SCALAR);
ERR_FAIL_V(GLTFAccessor::TYPE_SCALAR);
}

Error GLTFDocument::_parse_accessors(Ref<GLTFState> p_state) {
Expand Down Expand Up @@ -1088,7 +1088,7 @@ String GLTFDocument::_get_component_type_name(const uint32_t p_component) {
return "<Error>";
}

Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_src, const int p_count, const GLTFAccessorType p_accessor_type, const int p_component_type, const bool p_normalized, const int p_byte_offset, const bool p_for_vertex, GLTFBufferViewIndex &r_accessor, const bool p_for_vertex_indices) {
Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_src, const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type, const int p_component_type, const bool p_normalized, const int p_byte_offset, const bool p_for_vertex, GLTFBufferViewIndex &r_accessor, const bool p_for_vertex_indices) {
const int component_count_for_type[7] = {
1, 2, 3, 4, 4, 9, 16
};
Expand All @@ -1103,18 +1103,18 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
switch (p_component_type) {
case COMPONENT_TYPE_BYTE:
case COMPONENT_TYPE_UNSIGNED_BYTE: {
if (p_accessor_type == TYPE_MAT2) {
if (p_accessor_type == GLTFAccessor::TYPE_MAT2) {
skip_every = 2;
skip_bytes = 2;
}
if (p_accessor_type == TYPE_MAT3) {
if (p_accessor_type == GLTFAccessor::TYPE_MAT3) {
skip_every = 3;
skip_bytes = 1;
}
} break;
case COMPONENT_TYPE_SHORT:
case COMPONENT_TYPE_UNSIGNED_SHORT: {
if (p_accessor_type == TYPE_MAT3) {
if (p_accessor_type == GLTFAccessor::TYPE_MAT3) {
skip_every = 6;
skip_bytes = 4;
}
Expand Down Expand Up @@ -1296,7 +1296,7 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
return OK;
}

Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, const GLTFBufferViewIndex p_buffer_view, const int p_skip_every, const int p_skip_bytes, const int p_element_size, const int p_count, const GLTFAccessorType p_accessor_type, const int p_component_count, const int p_component_type, const int p_component_size, const bool p_normalized, const int p_byte_offset, const bool p_for_vertex) {
Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, const GLTFBufferViewIndex p_buffer_view, const int p_skip_every, const int p_skip_bytes, const int p_element_size, const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type, const int p_component_count, const int p_component_type, const int p_component_size, const bool p_normalized, const int p_byte_offset, const bool p_for_vertex) {
const Ref<GLTFBufferView> bv = p_state->buffer_views[p_buffer_view];

int stride = p_element_size;
Expand Down Expand Up @@ -1427,20 +1427,20 @@ Vector<double> GLTFDocument::_decode_accessor(Ref<GLTFState> p_state, const GLTF
switch (a->component_type) {
case COMPONENT_TYPE_BYTE:
case COMPONENT_TYPE_UNSIGNED_BYTE: {
if (a->accessor_type == TYPE_MAT2) {
if (a->accessor_type == GLTFAccessor::TYPE_MAT2) {
skip_every = 2;
skip_bytes = 2;
element_size = 8; //override for this case
}
if (a->accessor_type == TYPE_MAT3) {
if (a->accessor_type == GLTFAccessor::TYPE_MAT3) {
skip_every = 3;
skip_bytes = 1;
element_size = 12; //override for this case
}
} break;
case COMPONENT_TYPE_SHORT:
case COMPONENT_TYPE_UNSIGNED_SHORT: {
if (a->accessor_type == TYPE_MAT3) {
if (a->accessor_type == GLTFAccessor::TYPE_MAT3) {
skip_every = 6;
skip_bytes = 4;
element_size = 16; //override for this case
Expand Down Expand Up @@ -1474,7 +1474,7 @@ Vector<double> GLTFDocument::_decode_accessor(Ref<GLTFState> p_state, const GLTF
indices.resize(a->sparse_count);
const int indices_component_size = _get_component_type_size(a->sparse_indices_component_type);

Error err = _decode_buffer_view(p_state, indices.ptrw(), a->sparse_indices_buffer_view, 0, 0, indices_component_size, a->sparse_count, TYPE_SCALAR, 1, a->sparse_indices_component_type, indices_component_size, false, a->sparse_indices_byte_offset, false);
Error err = _decode_buffer_view(p_state, indices.ptrw(), a->sparse_indices_buffer_view, 0, 0, indices_component_size, a->sparse_count, GLTFAccessor::TYPE_SCALAR, 1, a->sparse_indices_component_type, indices_component_size, false, a->sparse_indices_byte_offset, false);
if (err != OK) {
return Vector<double>();
}
Expand Down Expand Up @@ -1536,7 +1536,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_ints(Ref<GLTFState> p_state,
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_SCALAR;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_SCALAR;
int component_type;
if (max_index > 65535 || p_for_vertex) {
component_type = GLTFDocument::COMPONENT_TYPE_INT;
Expand Down Expand Up @@ -1650,7 +1650,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_vec2(Ref<GLTFState> p_state,
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_VEC2;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC2;
const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;

accessor->max = type_max;
Expand Down Expand Up @@ -1703,7 +1703,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_color(Ref<GLTFState> p_state
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_VEC4;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;

accessor->max = type_max;
Expand Down Expand Up @@ -1770,7 +1770,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_weights(Ref<GLTFState> p_sta
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_VEC4;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;

accessor->max = type_max;
Expand Down Expand Up @@ -1821,7 +1821,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_joints(Ref<GLTFState> p_stat
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_VEC4;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
const int component_type = GLTFDocument::COMPONENT_TYPE_UNSIGNED_SHORT;

accessor->max = type_max;
Expand Down Expand Up @@ -1874,7 +1874,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_quaternions(Ref<GLTFState> p
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_VEC4;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;

accessor->max = type_max;
Expand Down Expand Up @@ -1949,7 +1949,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_floats(Ref<GLTFState> p_stat
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_SCALAR;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_SCALAR;
const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;

accessor->max = type_max;
Expand Down Expand Up @@ -1999,7 +1999,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_vec3(Ref<GLTFState> p_state,
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_VEC3;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC3;
const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;

accessor->max = type_max;
Expand Down Expand Up @@ -2075,7 +2075,7 @@ GLTFAccessorIndex GLTFDocument::_encode_sparse_accessor_as_vec3(Ref<GLTFState> p
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_VEC3;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC3;
const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;

sparse_accessor->normalized = false;
Expand Down Expand Up @@ -2103,7 +2103,7 @@ GLTFAccessorIndex GLTFDocument::_encode_sparse_accessor_as_vec3(Ref<GLTFState> p
} else {
sparse_accessor->sparse_indices_component_type = GLTFDocument::COMPONENT_TYPE_UNSIGNED_SHORT;
}
if (_encode_buffer_view(p_state, changed_indices.ptr(), changed_indices.size(), GLTFAccessorType::TYPE_SCALAR, sparse_accessor->sparse_indices_component_type, sparse_accessor->normalized, sparse_accessor->sparse_indices_byte_offset, false, buffer_view_i_indices) != OK) {
if (_encode_buffer_view(p_state, changed_indices.ptr(), changed_indices.size(), GLTFAccessor::TYPE_SCALAR, sparse_accessor->sparse_indices_component_type, sparse_accessor->normalized, sparse_accessor->sparse_indices_byte_offset, false, buffer_view_i_indices) != OK) {
return -1;
}
// We use changed_indices.size() here, because we must pass the number of vec3 values rather than the number of components.
Expand Down Expand Up @@ -2180,7 +2180,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_xform(Ref<GLTFState> p_state
p_state->buffers.push_back(Vector<uint8_t>());
}
int64_t size = p_state->buffers[0].size();
const GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_MAT4;
const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_MAT4;
const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;

accessor->max = type_max;
Expand Down Expand Up @@ -2234,9 +2234,9 @@ Vector<Color> GLTFDocument::_decode_accessor_as_color(Ref<GLTFState> p_state, co
}

const int accessor_type = p_state->accessors[p_accessor]->accessor_type;
ERR_FAIL_COND_V(!(accessor_type == TYPE_VEC3 || accessor_type == TYPE_VEC4), ret);
ERR_FAIL_COND_V(!(accessor_type == GLTFAccessor::TYPE_VEC3 || accessor_type == GLTFAccessor::TYPE_VEC4), ret);
int vec_len = 3;
if (accessor_type == TYPE_VEC4) {
if (accessor_type == GLTFAccessor::TYPE_VEC4) {
vec_len = 4;
}

Expand Down
8 changes: 4 additions & 4 deletions modules/gltf/gltf_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class GLTFDocument : public Resource {
int _get_component_type_size(const int p_component_type);
Error _parse_scenes(Ref<GLTFState> p_state);
Error _parse_nodes(Ref<GLTFState> p_state);
String _get_accessor_type_name(const GLTFAccessorType p_accessor_type);
String _get_accessor_type_name(const GLTFAccessor::GLTFAccessorType p_accessor_type);
String _sanitize_animation_name(const String &p_name);
String _gen_unique_animation_name(Ref<GLTFState> p_state, const String &p_name);
String _sanitize_bone_name(const String &p_name);
Expand All @@ -131,13 +131,13 @@ class GLTFDocument : public Resource {
void _compute_node_heights(Ref<GLTFState> p_state);
Error _parse_buffers(Ref<GLTFState> p_state, const String &p_base_path);
Error _parse_buffer_views(Ref<GLTFState> p_state);
GLTFAccessorType _get_accessor_type_from_str(const String &p_string);
GLTFAccessor::GLTFAccessorType _get_accessor_type_from_str(const String &p_string);
Error _parse_accessors(Ref<GLTFState> p_state);
Error _decode_buffer_view(Ref<GLTFState> p_state, double *p_dst,
const GLTFBufferViewIndex p_buffer_view,
const int p_skip_every, const int p_skip_bytes,
const int p_element_size, const int p_count,
const GLTFAccessorType p_accessor_type, const int p_component_count,
const GLTFAccessor::GLTFAccessorType p_accessor_type, const int p_component_count,
const int p_component_type, const int p_component_size,
const bool p_normalized, const int p_byte_offset,
const bool p_for_vertex);
Expand Down Expand Up @@ -266,7 +266,7 @@ class GLTFDocument : public Resource {
const Vector<Transform3D> p_attribs,
const bool p_for_vertex);
Error _encode_buffer_view(Ref<GLTFState> p_state, const double *p_src,
const int p_count, const GLTFAccessorType p_accessor_type,
const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type,
const int p_component_type, const bool p_normalized,
const int p_byte_offset, const bool p_for_vertex,
GLTFBufferViewIndex &r_accessor, const bool p_for_indices = false);
Expand Down
Loading
Loading