From a5262a9504d0ad264d93e1761f5a4123e524cc87 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 19 Oct 2018 23:42:59 +0300 Subject: Implement specular mapping --- mtl.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'mtl.c') diff --git a/mtl.c b/mtl.c index 3b46ce7..dc74e01 100644 --- a/mtl.c +++ b/mtl.c @@ -13,19 +13,34 @@ struct mtl { char name[50]; - GLuint diffuse; + GLuint diff; + GLuint spec; }; static struct mtl *mtls; static int lmtls; static size_t nmtls; -int mtl_load(char *name) +GLuint tex_file(const char *name, const char *type) { - struct mtl *m; char loc[100]; + GLuint ret; FILE *f; + snprintf(loc, sizeof loc, "assets/%s.%s.png", name, type); + f = fopen(loc, "rb"); + if (f == NULL) + eprintf("Could not open '%s':", loc); + ret = png2tex(f); + fclose(f); + + return ret; +} + +int mtl_load(char *name) +{ + struct mtl *m; + for (int i = 0; i < lmtls; i++) if (strcmp(mtls[i].name, name) == 0) return i; @@ -35,12 +50,8 @@ int mtl_load(char *name) snprintf(m->name, sizeof m->name, "%s", name); - snprintf(loc, sizeof loc, "assets/%s.diff.png", name); - f = fopen(loc, "rb"); - if (f == NULL) - eprintf("Could not open '%s':", loc); - m->diffuse = png2tex(f); - fclose(f); + m->diff = tex_file(name, "diff"); + m->spec = tex_file(name, "spec"); return lmtls++; } @@ -54,5 +65,7 @@ void mtl_use(int mtl) return; gl_tex_active(GL_TEXTURE0); - gl_tex_bind(GL_TEXTURE_2D, mtls[mtl].diffuse); + gl_tex_bind(GL_TEXTURE_2D, mtls[mtl].diff); + gl_tex_active(GL_TEXTURE1); + gl_tex_bind(GL_TEXTURE_2D, mtls[mtl].spec); } -- cgit v1.2.3-54-g00ecf