glstuff

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit b5ed7965ca22c482dd6833830969a0082fe3cd8e
parent b5edff414afb811521ed70a23abb524809b07248
Author: Brian Swetland <swetland@frotz.net>
Date:   Sat, 19 Jan 2013 09:42:00 -0800

test4: automatically reload shaders if they've changed on disk

Diffstat:
Mtest4.c | 75++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Mtest4.fs | 6++++--
2 files changed, 62 insertions(+), 19 deletions(-)

diff --git a/test4.c b/test4.c @@ -20,12 +20,15 @@ #include "util.h" #include "glue.h" +#include <sys/time.h> +#include <sys/stat.h> + void *texdata; unsigned texw, texh; -const char *vert_src, *frag_src; +char *vert_src, *frag_src; -GLuint pgm, vshd, fshd, tex0; +GLuint pgm = -1, vshd = -1, fshd = -1, tex0; GLuint aVertex, aTexCoord; GLuint uMVP, uTexture; @@ -49,15 +52,62 @@ GLfloat texcoords[] = { 1.0, 1.0, }; +time_t vstime = 0, fstime = 0; + +void update_shaders(void) { + struct stat vsstat, fsstat; + GLuint newpgm, newvs, newfs; + + stat("test4.vs", &vsstat); + stat("test4.fs", &fsstat); + + if ((vsstat.st_mtime == vstime) && (fsstat.st_mtime == fstime)) + return; + + vstime = vsstat.st_mtime; + fstime = fsstat.st_mtime; + + if (!(vert_src = load_file("test4.vs", 0))) + goto exit; + if (!(frag_src = load_file("test4.fs", 0))) + goto free_vsrc_and_exit; + + if (shader_compile(vert_src, frag_src, &newpgm, &newvs, &newfs)) + goto free_both_and_exit; + + glDeleteShader(vshd); + glDeleteShader(fshd); + glDeleteProgram(pgm); + + pgm = newpgm; + vshd = newvs; + fshd = newfs; + + aVertex = glGetAttribLocation(pgm, "aVertex"); + aTexCoord = glGetAttribLocation(pgm, "aTexCoord"); + uMVP = glGetUniformLocation(pgm, "uMVP"); + uTexture = glGetUniformLocation(pgm, "uTexture"); + + free(frag_src); + free(vert_src); + + fprintf(stderr,"shaders updated\n"); + return; + +free_both_and_exit: + free(frag_src); +free_vsrc_and_exit: + free(vert_src); +exit: + fprintf(stderr,"failed to update shaders\n"); + return; +} + int scene_init(struct ctxt *c) { float aspect; if (!(texdata = load_png_gray("texture.sdf.png", &texw, &texh, 1))) return -1; - if (!(vert_src = load_file("test4.vs", 0))) - return -1; - if (!(frag_src = load_file("test4.fs", 0))) - return -1; aspect = ((float)c->width) / ((float)c->height); mtx_ortho(MVP, -aspect, aspect, -1, 1, 1, -1); @@ -66,20 +116,9 @@ int scene_init(struct ctxt *c) { glClearColor(0, 0, 0, 0); glClearDepth(1.0f); - if (shader_compile(vert_src, frag_src, &pgm, &vshd, &fshd)) - return -1; - - aVertex = glGetAttribLocation(pgm, "aVertex"); - aTexCoord = glGetAttribLocation(pgm, "aTexCoord"); - uMVP = glGetUniformLocation(pgm, "uMVP"); - uTexture = glGetUniformLocation(pgm, "uTexture"); - glEnable(GL_TEXTURE_2D); -// glEnable(GL_BLEND); -// glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glGenTextures(1, &tex0); - glBindTexture(GL_TEXTURE_2D, tex0); glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texw, texh, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texdata); @@ -90,6 +129,8 @@ int scene_init(struct ctxt *c) { } int scene_draw(struct ctxt *c) { + update_shaders(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glUseProgram(pgm); diff --git a/test4.fs b/test4.fs @@ -4,8 +4,10 @@ varying vec2 vTexCoord; void main() { float mask = texture2D(uTexture, vTexCoord).a; - if (mask < 0.5) + if (mask < 0.471) discard; - gl_FragColor = vec4(1, 1, 0, 1); + float c = smoothstep(mask, 0.47, 0.49); + + gl_FragColor = vec4(c, c, c, 1); }