/* * Copyright (C) 2018 Tomasz Kramkowski * SPDX-License-Identifier: MIT */ #include #include #include #include "ensize.h" #include "eprintf.h" #include "glprog.h" #include "mtl.h" #include "tex.h" struct mtl { char name[50]; #define SH_TEX(_, name) GLuint name; #include "shaders/data.h" }; static struct mtl *mtls; static int lmtls; static size_t nmtls; // tex_file: Load texture asset from file GLuint tex_file(const char *name, const char *type) { 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; } // mtl_load: Load a set of textures as a material int mtl_load(char *name) { struct mtl *m; 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); #define SH_TEX(_, n) m->n = tex_file(name, #n); #include "shaders/data.h" return lmtls++; } // mtl_use: Set OpenGL state to use a certain material index void mtl_use(int mtl) { assert(mtl < lmtls); GLenum texture = GL_TEXTURE0; // TODO: Blank textures or something if (mtl < 0) return; #define SH_TEX(type, name) \ gl_tex_active(texture++); \ gl_tex_bind(GL_TEXTURE_##type, mtls[mtl].name); #include "shaders/data.h" }