aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2018-10-22 01:41:32 +0300
committerTomasz Kramkowski <tk@the-tk.com>2018-10-22 01:41:32 +0300
commit0cd7f50e11d89311762ae062bb1cfa876a317353 (patch)
tree2d4fbf2ff330c5d150f660d1aefd145d3135ddd0
parent3c7790062aa05653efa0f8ee230c07cb7ead91dd (diff)
downloadfaqe-0cd7f50e11d89311762ae062bb1cfa876a317353.tar.gz
faqe-0cd7f50e11d89311762ae062bb1cfa876a317353.tar.xz
faqe-0cd7f50e11d89311762ae062bb1cfa876a317353.zip
use shaders/data.h for the definition of vertices
Now that shaders/data.h is in place, it can be taken to the next extreme. The information stored in shaders/data.h is plentiful enough that vertex attribute location information can be standardised, the definition of vertices can be specified in only one place and the bindings for vertex information can be generated. It's around this point in time that I wish there was a better pre-processor for all this. It might make things a lot cleaner.
-rw-r--r--faqe.c4
-rw-r--r--glfunc.h1
-rw-r--r--glprog.c6
-rw-r--r--glprog.h4
-rw-r--r--model.c12
-rw-r--r--vertex.h5
6 files changed, 20 insertions, 12 deletions
diff --git a/faqe.c b/faqe.c
index f381273..ebc7542 100644
--- a/faqe.c
+++ b/faqe.c
@@ -81,10 +81,10 @@ int main(int argc, char **argv)
gl_load((gl_loadfunc *)SDL_GL_GetProcAddress);
- loadmodel(&mdl, "assets/utah.fmd");
-
glprog_init();
+ loadmodel(&mdl, "assets/utah.fmd");
+
gl_prog_use(prog.main.prog);
viewport(prog.main.uni.proj, WIDTH, HEIGHT);
gl_uni_set1i(prog.main.tex.tdiff, 0);
diff --git a/glfunc.h b/glfunc.h
index a5c575e..cec32b3 100644
--- a/glfunc.h
+++ b/glfunc.h
@@ -52,3 +52,4 @@ GL_FUNC(glBindTexture, void, gl_tex_bind, GLenum target, GLuint texture)
GL_FUNC(glTexImage2D, void, gl_tex_img2d, GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *data)
GL_FUNC(glTexParameteri, void, gl_tex_parami, GLenum target, GLenum pname, GLint param)
GL_FUNC(glGenerateMipmap, void, gl_tex_genmip, GLenum target)
+GL_FUNC(glBindAttribLocation, void, gl_attr_bindloc, GLuint program, GLuint index, const char *name)
diff --git a/glprog.c b/glprog.c
index 2996889..10f01a4 100644
--- a/glprog.c
+++ b/glprog.c
@@ -107,7 +107,7 @@ static void detach_shaders(GLuint prog)
static void load_shader(struct shader *s, const char *path)
{
GLuint prog;
- GLint success;
+ GLint success, pos = 0;
char log[LOGSIZE], *full, *src = NULL;
size_t srclen = 0, fullsz;
@@ -133,6 +133,8 @@ static void load_shader(struct shader *s, const char *path)
}
free(src);
free(full);
+#define SH_IN(_, name) gl_attr_bindloc(prog, pos++, #name);
+#include "shaders/data.h"
gl_prog_link(prog);
gl_prog_param(prog, GL_LINK_STATUS, &success);
if (!success) {
@@ -150,6 +152,8 @@ static void load_shader(struct shader *s, const char *path)
void glprog_init(void)
{
+ GLint pos = 0;
#define SH_PROG(name) load_shader(&prog.name, "shaders/" #name);
+#define SH_IN(_, name) prog.attr.name = pos++;
#include "shaders/data.h"
}
diff --git a/glprog.h b/glprog.h
index 74987b3..783e574 100644
--- a/glprog.h
+++ b/glprog.h
@@ -22,6 +22,10 @@ struct shader {
struct glprog_progs {
#define SH_PROG(name) struct shader name;
#include "shaders/data.h"
+ struct {
+#define SH_IN(_0, name) GLint name;
+#include "shaders/data.h"
+ } attr;
};
extern struct glprog_progs prog;
diff --git a/model.c b/model.c
index 88931c0..1f07201 100644
--- a/model.c
+++ b/model.c
@@ -5,6 +5,7 @@
#include <assert.h>
#include "eprintf.h"
+#include "glprog.h"
#include "linmath.h"
#include "model.h"
#include "nelem.h"
@@ -30,12 +31,11 @@ void model_load(struct model *mdl, const struct fmd *fmd)
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);
+
+#define SH_IN(_0, name) \
+ gl_va_define(prog.attr.name, NELEM(v->name), GL_FLOAT, GL_FALSE, sizeof *v, (void *)offsetof(struct vertex, name)); \
+ gl_va_enable(prog.attr.name);
+#include "shaders/data.h"
// TODO: materials, use a shared material set
diff --git a/vertex.h b/vertex.h
index 7f58ee9..93521ac 100644
--- a/vertex.h
+++ b/vertex.h
@@ -8,9 +8,8 @@
#include "linmath.h"
struct vertex {
- vec3 pos;
- vec3 norm;
- vec2 uv;
+#define SH_IN(type, name) type name;
+#include "shaders/data.h"
};
#endif // VERTEX_H