diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2018-05-02 21:22:03 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2018-05-02 21:22:03 +0100 |
commit | b659c412263f6dd7df962f7d42f4f51b7c3d5609 (patch) | |
tree | e2495e0b1446527b909a9c9fa2776797c7c28221 /mtl.c | |
parent | ec93a8eddf70adef2b1439a511d990021ad25d99 (diff) | |
download | faqe-b659c412263f6dd7df962f7d42f4f51b7c3d5609.tar.gz faqe-b659c412263f6dd7df962f7d42f4f51b7c3d5609.tar.xz faqe-b659c412263f6dd7df962f7d42f4f51b7c3d5609.zip |
Implement diffuse texture loading and basic material support
Diffstat (limited to 'mtl.c')
-rw-r--r-- | mtl.c | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -0,0 +1,54 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +#include "ensize.h" +#include "eprintf.h" +#include "mtl.h" +#include "tex.h" + +struct mtl { + char name[50]; + GLuint diffuse; +}; + +static struct mtl *mtls; +static int lmtls; +static size_t nmtls; + +int mtl_load(char *name) +{ + struct mtl *m; + char loc[100]; + FILE *f; + + for (int i = 0; i < lmtls; i++) + if (strcmp(mtls[i].name, name) == 0) + return i; + + mtls = ENSIZE(mtls, lmtls + 1, &nmtls, 16); + m = &mtls[lmtls]; + + snprintf(m->name, sizeof m->name, "%s", name); + + snprintf(loc, sizeof loc, "./tex/%s.png", name); + f = fopen(loc, "rb"); + if (f == NULL) + eprintf("Could not open '%s':", loc); + m->diffuse = png2tex(f); + fclose(f); + + return lmtls++; +} + +void mtl_use(int mtl) +{ + assert(mtl < lmtls); + + // TODO: Blank textures or something + if (mtl < 0) + return; + + gl_tex_active(GL_TEXTURE0); + gl_tex_bind(GL_TEXTURE_2D, mtls[mtl].diffuse); +} |