diff options
author | quou <quou@disroot.org> | 2025-01-12 00:14:16 +1100 |
---|---|---|
committer | quou <quou@disroot.org> | 2025-01-12 00:14:16 +1100 |
commit | db7e94765c972ef940bf431e541dda4d893efc40 (patch) | |
tree | 1d374bdc42cb4d90c3121cbf878d324936f271ad | |
parent | 306c6318a32a9af351daaa60d2cc082e45e30dd4 (diff) |
-rw-r--r-- | convmodel.c | 31 | ||||
-rw-r--r-- | model.cpp | 2 | ||||
-rw-r--r-- | model.hpp | 1 |
3 files changed, 33 insertions, 1 deletions
diff --git a/convmodel.c b/convmodel.c index 7963881..8646ace 100644 --- a/convmodel.c +++ b/convmodel.c @@ -1,3 +1,4 @@ +#include <math.h> #include <stdio.h> #include <stdlib.h> @@ -39,6 +40,7 @@ int full_ind_size = 0; float* current_vertex; const cgltf_accessor* accessors[cgltf_attribute_type_max_enum]; Shader_Attrib* target_attribs[cgltf_attribute_type_max_enum]; +float min_bound[3], max_bound[3]; int tcmp(const char* src, const char* s, const jsmntok_t* t) { int l = t->end - t->start; @@ -217,7 +219,7 @@ void parse_prim( Shader_Attrib* desired ) { int i, c; - const cgltf_accessor* a; + const cgltf_accessor* a, * pa; const cgltf_buffer_view* v; uint16_t* indices; if (p->type != cgltf_primitive_type_triangles) { @@ -240,6 +242,25 @@ void parse_prim( c = a->count; indices = (uint16_t*)&((char*)v->buffer->data)[v->offset]; build_attrib_accessors(p, desired); + pa = accessors[cgltf_attribute_type_position]; + if ( + pa && + pa->type == cgltf_type_vec3 && + pa->component_type == cgltf_component_type_r_32f + ) { + char* data = pa->buffer_view->buffer->data; + int s = pa->stride; + data += pa->buffer_view->offset; + for (i = 0; i < c; i++) { + float* pos = (float*)&data[indices[i] * s]; + if (pos[0] < min_bound[0]) min_bound[0] = pos[0]; + if (pos[1] < min_bound[1]) min_bound[1] = pos[1]; + if (pos[2] < min_bound[2]) min_bound[2] = pos[2]; + if (pos[0] > max_bound[0]) max_bound[0] = pos[0]; + if (pos[1] > max_bound[1]) max_bound[1] = pos[1]; + if (pos[2] > max_bound[2]) max_bound[2] = pos[2]; + } + } for (i = 0; i < c; i++) { read_vertex(current_vertex, indices[i]); add_vertex(current_vertex); @@ -259,6 +280,12 @@ void parse_node_mesh( float matrix[16]; vertex_count = 0; index_count = 0; + min_bound[0] = INFINITY; + min_bound[1] = INFINITY; + min_bound[2] = INFINITY; + max_bound[0] = -INFINITY; + max_bound[1] = -INFINITY; + max_bound[2] = -INFINITY; for (i = 0; i < c; i++) parse_prim(&m->primitives[i], desired); cgltf_node_transform_local(n, matrix); @@ -277,6 +304,8 @@ void parse_node_mesh( fwrite(&vertex_count, 4, 1, outfile); fwrite(&parent_index, 4, 1, outfile); fwrite(matrix, sizeof matrix, 1, outfile); + fwrite(min_bound, sizeof min_bound, 1, outfile); + fwrite(max_bound, sizeof max_bound, 1, outfile); fwrite(vertex_buffer, 1, vertex_count * vertex_size, outfile); fwrite(index_buffer, 1, index_count * sizeof *index_buffer, outfile); } @@ -139,6 +139,8 @@ Asset* Model_Loader::load( pack_read(f, &vertex_count, 4); pack_read(f, &mesh.parent, 4); pack_read(f, &mesh.local, 64); + pack_read(f, &mesh.bound.min, 12); + pack_read(f, &mesh.bound.max, 12); pack_read(f, &verts[coff], vertex_count * vertex_size); pack_read(f, &indices[icoff], mesh.count * sizeof *indices); mesh.vbo_offset = vcoff; @@ -30,6 +30,7 @@ struct Mesh { int mvp_binding_depth; bool world_dirty; m4f world, local; + AABB bound; Shader_Id shader, depth_shader; Material* material; const m4f& get_world(Model& m); |