diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2018-04-27 20:56:00 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2018-04-27 20:56:00 +0100 |
commit | bab0824608498d1079d4e9522f3014d3d538aabe (patch) | |
tree | 932f082b58fd48f85fbbdfa3f7419d2b3b03eedb /model.c | |
parent | 3d73622dbd5a9ddc687fe6f42268c760695d7226 (diff) | |
download | faqe-bab0824608498d1079d4e9522f3014d3d538aabe.tar.gz faqe-bab0824608498d1079d4e9522f3014d3d538aabe.tar.xz faqe-bab0824608498d1079d4e9522f3014d3d538aabe.zip |
Implement basic model loading of FMD format.
The FMD (Faqe Model Data) format is a format designed for faqe. It
stores vertex, element and material information and mesh information.
This patch provides the basic implementation and use of this format.
This patch also implements perspective projection, depth testing and
view and model matrices.
An example fmd file is provided.
Diffstat (limited to 'model.c')
-rw-r--r-- | model.c | 60 |
1 files changed, 60 insertions, 0 deletions
@@ -0,0 +1,60 @@ +/* + * Copyright (C) 2018 Tomasz Kramkowski <tk@the-tk.com> + * SPDX-License-Identifier: MIT + */ +#include <assert.h> + +#include "eprintf.h" +#include "linmath.h" +#include "model.h" +#include "nelem.h" +#include "vertex.h" + +void model_load(struct model *mdl, const struct fmd *fmd) +{ + GLuint vbo, ebo; + struct vertex *v; + + assert(mdl != NULL); + assert(fmd != NULL); + + gl_va_gen(1, &mdl->vao); + gl_va_bind(mdl->vao); + + gl_buf_gen(1, &vbo); + gl_buf_bind(GL_ARRAY_BUFFER, vbo); + gl_buf_data(GL_ARRAY_BUFFER, fmd->nverts * sizeof *fmd->verts, fmd->verts, GL_STATIC_DRAW); + + gl_buf_gen(1, &ebo); + gl_buf_bind(GL_ELEMENT_ARRAY_BUFFER, ebo); + gl_buf_data(GL_ELEMENT_ARRAY_BUFFER, fmd->ntris * sizeof *fmd->tris, fmd->tris, GL_STATIC_DRAW); + + v = &fmd->verts[0]; + gl_va_define(0, NELEM(v->pos), GL_FLOAT, GL_FALSE, sizeof *v, (void *)offsetof(struct vertex, pos)); + gl_va_enable(0); + gl_va_define(1, NELEM(v->norm), GL_FLOAT, GL_FALSE, sizeof *v, (void *)offsetof(struct vertex, norm)); + gl_va_enable(1); + gl_va_define(2, NELEM(v->uv), GL_FLOAT, GL_FALSE, sizeof *v, (void *)offsetof(struct vertex, uv)); + gl_va_enable(2); + + // TODO: materials, use a shared material set + + mdl->nmeshes = fmd->nmeshes; + mdl->meshes = emalloc(mdl->nmeshes * sizeof *mdl->meshes); + for (int i = 0; i < fmd->nmeshes; i++) { + struct mesh *m = &mdl->meshes[i]; + struct fmd_mesh *fm = &fmd->meshes[i]; + m->mtl = NULL; + m->elems.idx = fm->tidx * NELEM(*fmd->tris); + m->elems.cnt = fm->tcnt * NELEM(*fmd->tris); + } +} + +void model_render(struct model *mdl) +{ + gl_va_bind(mdl->vao); + for (int i = 0; i < mdl->nmeshes; i++) { + struct mesh *m = &mdl->meshes[i]; + gl_draw_elems(GL_TRIANGLES, m->elems.cnt, GL_UNSIGNED_INT, &((GLuint *)NULL)[m->elems.idx]); + } +} |