summaryrefslogtreecommitdiff
path: root/convmodel.c
diff options
context:
space:
mode:
Diffstat (limited to 'convmodel.c')
-rw-r--r--convmodel.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/convmodel.c b/convmodel.c
index 5e57fc9..1a09f99 100644
--- a/convmodel.c
+++ b/convmodel.c
@@ -75,6 +75,13 @@ void parse_node_cfg(const cgltf_node* n, Node_Config* cfg) {
const jsmntok_t* t = &toks[i];
if (tcmp(json, "shader", t)) {
cfg->shader = read_str(json, &t[1]);
+ if (string_len(cfg->shader) > 27) {
+ print_err(
+ "Shader name %s too long (max 27 chars).\n",
+ cfg->shader
+ );
+ pbreak(3478);
+ }
i++;
}
}
@@ -216,20 +223,30 @@ void parse_prim(
}
void parse_node_mesh(
+ int parent_index,
const cgltf_node* n,
+ const Node_Config* cfg,
Shader_Attrib* desired,
FILE* outfile
) {
const cgltf_mesh* m = n->mesh;
int i, c = m->primitives_count;
+ char buf[28];
+ float matrix[16];
+ zero(buf, sizeof buf);
+ string_copy(buf, cfg->shader);
vertex_count = 0;
index_count = 0;
for (i = 0; i < c; i++)
parse_prim(&m->primitives[i], desired);
+ cgltf_node_transform_local(n, matrix);
fwrite("MESH", 4, 1, outfile);
+ fwrite(buf, 1, sizeof buf, outfile);
fwrite(&vertex_size, 4, 1, outfile);
fwrite(&index_count, 4, 1, outfile);
fwrite(&vertex_count, 4, 1, outfile);
+ fwrite(&parent_index, 4, 1, outfile);
+ fwrite(matrix, sizeof matrix, 1, outfile);
fwrite(vertex_buffer, 1, vertex_count * vertex_size, outfile);
fwrite(index_buffer, 1, index_count * sizeof *index_buffer, outfile);
}
@@ -336,7 +353,11 @@ int calc_vertex_size(Shader_Attrib* attribs) {
return s;
}
-void parse_node(const cgltf_node* n, FILE* outfile) {
+void parse_node(
+ int parent_index,
+ const cgltf_node* n,
+ FILE* outfile
+) {
Node_Config cfg = { 0 };
if (n->extras.data)
parse_node_cfg(n, &cfg);
@@ -353,7 +374,7 @@ void parse_node(const cgltf_node* n, FILE* outfile) {
vertex_size = calc_vertex_size(desired);
current_vertex = arena_alloc(&arena, vertex_size);
assert(desired != 0);
- parse_node_mesh(n, desired, outfile);
+ parse_node_mesh(parent_index, n, &cfg, desired, outfile);
}
}
@@ -376,8 +397,12 @@ int parse(const char* fname, FILE* outfile) {
c = (int)d->nodes_count;
fwrite(&c, 4, 1, outfile);
for (i = 0; i < c; i++) {
+ const cgltf_node* n = &d->nodes[i];
+ int parent_index = 0;
+ if (n->parent)
+ parent_index = n->parent - d->nodes;
clear_arena(&arena);
- parse_node(&d->nodes[i], outfile);
+ parse_node(parent_index, &d->nodes[i], outfile);
}
cgltf_free(d);
return 0;