summaryrefslogtreecommitdiff
path: root/convmodel.c
diff options
context:
space:
mode:
Diffstat (limited to 'convmodel.c')
-rw-r--r--convmodel.c31
1 files changed, 30 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);
}