glstuff

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

commit 60da1bb73eded7f9dd5b24c043c7574b263dd061
parent e7f163b3ced3d5db960a8b8326c6b99d019c63ae
Author: Brian Swetland <swetland@frotz.net>
Date:   Fri, 18 Jan 2013 15:59:52 -0800

better error reporting and more useful names for shader related things

Diffstat:
Msdlglue.c | 59++++++++++++++++++++++++++++++++++++-----------------------
Mtest1.c | 28++++++++++++++--------------
Dtest1.fragment.glsl | 6------
Atest1.fs | 6++++++
Dtest1.vertex.glsl | 10----------
Atest1.vs | 10++++++++++
Mtest2.c | 28++++++++++++++--------------
Mtest3.c | 28++++++++++++++--------------
Mtest4.c | 30++++++++++++++----------------
Dtest4.fragment.glsl | 11-----------
Atest4.fs | 11+++++++++++
Dtest4.vertex.glsl | 10----------
Atest4.vs | 10++++++++++
13 files changed, 129 insertions(+), 118 deletions(-)

diff --git a/sdlglue.c b/sdlglue.c @@ -29,9 +29,34 @@ void die(const char *fmt, ...) { exit(-1); } +int check_compile_error(GLuint obj, const char *fn) { + GLint r, len; + char *buf; + + if (fn) glGetShaderiv(obj, GL_COMPILE_STATUS, &r); + else glGetProgramiv(obj, GL_LINK_STATUS, &r); + + if (r) return 0; + + if (fn) glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &len); + else glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &len); + + buf = malloc(len + 1); + memset(buf, 0, len); + if (buf != 0) { + if (fn) glGetShaderInfoLog(obj, len, &len, buf); + else glGetProgramInfoLog(obj, len, &len, buf); + buf[len] = 0; + } + fprintf(stderr,"Shader %s Error:\n%s\n", + fn ? "Compile" : "Link", buf ? buf : "???"); + + free(buf); + return -1; +} + int shader_compile(const char *vshader, const char *fshader, GLuint *_pgm, GLuint *_vshd, GLuint *_fshd) { - GLint r; GLuint pgm, vshd, fshd; pgm = glCreateProgram(); @@ -40,33 +65,21 @@ int shader_compile(const char *vshader, const char *fshader, glShaderSource(vshd, 1, &vshader, NULL); glShaderSource(fshd, 1, &fshader, NULL); glCompileShader(vshd); + check_compile_error(vshd, vshader); + glCompileShader(fshd); + check_compile_error(fshd, fshader); + glAttachShader(pgm, vshd); glAttachShader(pgm, fshd); -// glBindAttribLocation(pgm, 0, "vPosition"); glLinkProgram(pgm); - - glGetProgramiv(pgm, GL_LINK_STATUS, &r); - - if (!r) { - GLint len; - char *buf; - glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &len); - buf = malloc(len + 1); - memset(buf, 0, len); - if (buf != 0) { - glGetProgramInfoLog(pgm, len, &len, buf); - buf[len] = 0; - fprintf(stderr,"<< Shader Compiler Error >>\n%s\n",buf); - free(buf); - } + if (check_compile_error(pgm, 0)) return -1; - } else { - *_pgm = pgm; - *_vshd = vshd; - *_fshd = fshd; - return 0; - } + + *_pgm = pgm; + *_vshd = vshd; + *_fshd = fshd; + return 0; } void handle_events(void) { diff --git a/test1.c b/test1.c @@ -26,8 +26,8 @@ unsigned texw, texh; const char *vert_src, *frag_src; GLuint pgm, vshd, fshd, tex0; -GLuint _vPosition, _vUV; -GLuint _uMVP, _uSampler; +GLuint aVertex, aTexCoord; +GLuint uMVP, uTexture; mat4 MVP; @@ -48,9 +48,9 @@ GLfloat texcoords[] = { int scene_init(struct ctxt *c) { if (!(texdata = load_png_rgba("texture1.png", &texw, &texh, 1))) return -1; - if (!(vert_src = load_file("test1.vertex.glsl", 0))) + if (!(vert_src = load_file("test1.vs", 0))) return -1; - if (!(frag_src = load_file("test1.fragment.glsl", 0))) + if (!(frag_src = load_file("test1.fs", 0))) return -1; mtx_identity(MVP); @@ -63,10 +63,10 @@ int scene_init(struct ctxt *c) { if (shader_compile(vert_src, frag_src, &pgm, &vshd, &fshd)) return -1; - _vPosition = glGetAttribLocation(pgm, "vPosition"); - _vUV = glGetAttribLocation(pgm, "vUV"); - _uMVP = glGetUniformLocation(pgm, "uMVP"); - _uSampler = glGetUniformLocation(pgm, "uSampler"); + aVertex = glGetAttribLocation(pgm, "aVertex"); + aTexCoord = glGetAttribLocation(pgm, "aTexCoord"); + uMVP = glGetUniformLocation(pgm, "uMVP"); + uTexture = glGetUniformLocation(pgm, "uTexture"); if(glGetError() != GL_NO_ERROR) fprintf(stderr,"OOPS!\n"); @@ -93,14 +93,14 @@ int scene_draw(struct ctxt *c) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex0); - glUniformMatrix4fv(_uMVP, 1, GL_FALSE, (void*) MVP); - glUniform1i(_uSampler, 0); + glUniformMatrix4fv(uMVP, 1, GL_FALSE, (void*) MVP); + glUniform1i(uTexture, 0); - glVertexAttribPointer(_vPosition, 3, GL_FLOAT, GL_FALSE, 0, verts); - glEnableVertexAttribArray(_vPosition); + glVertexAttribPointer(aVertex, 3, GL_FLOAT, GL_FALSE, 0, verts); + glEnableVertexAttribArray(aVertex); - glVertexAttribPointer(_vUV, 2, GL_FLOAT, GL_FALSE, 0, texcoords); - glEnableVertexAttribArray(_vUV); + glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 0, texcoords); + glEnableVertexAttribArray(aTexCoord); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); diff --git a/test1.fragment.glsl b/test1.fragment.glsl @@ -1,6 +0,0 @@ -uniform sampler2D uSampler; -varying vec2 fUV; - -void main() { - gl_FragColor = texture2D(uSampler, fUV); -} diff --git a/test1.fs b/test1.fs @@ -0,0 +1,6 @@ +uniform sampler2D uTexture; +varying vec2 vTexCoord; + +void main() { + gl_FragColor = texture2D(uTexture, vTexCoord); +} diff --git a/test1.vertex.glsl b/test1.vertex.glsl @@ -1,10 +0,0 @@ -uniform mat4 uMVP; -attribute vec4 vPosition; -attribute vec2 vUV; -varying vec2 fUV; - -void main() { - gl_Position = uMVP * vPosition; - fUV = vUV; -} - diff --git a/test1.vs b/test1.vs @@ -0,0 +1,10 @@ +uniform mat4 uMVP; +attribute vec4 aVertex; +attribute vec2 aTexCoord; +varying vec2 vTexCoord; + +void main() { + gl_Position = uMVP * aVertex; + vTexCoord = aTexCoord; +} + diff --git a/test2.c b/test2.c @@ -29,8 +29,8 @@ unsigned texw, texh; const char *vert_src, *frag_src; GLuint pgm, vshd, fshd, tex0; -GLuint _vPosition, _vUV; -GLuint _uMVP, _uSampler; +GLuint aVertex, aTexCoord; +GLuint uMVP, uTexture; mat4 perspective; mat4 MVP; @@ -83,9 +83,9 @@ int scene_init(struct ctxt *c) { if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1))) return -1; - if (!(vert_src = load_file("test1.vertex.glsl", 0))) + if (!(vert_src = load_file("test1.vs", 0))) return -1; - if (!(frag_src = load_file("test1.fragment.glsl", 0))) + if (!(frag_src = load_file("test1.fs", 0))) return -1; mtx_identity(MVP); @@ -98,10 +98,10 @@ int scene_init(struct ctxt *c) { if (shader_compile(vert_src, frag_src, &pgm, &vshd, &fshd)) return -1; - _vPosition = glGetAttribLocation(pgm, "vPosition"); - _vUV = glGetAttribLocation(pgm, "vUV"); - _uMVP = glGetUniformLocation(pgm, "uMVP"); - _uSampler = glGetUniformLocation(pgm, "uSampler"); + aVertex = glGetAttribLocation(pgm, "aVertex"); + aTexCoord = glGetAttribLocation(pgm, "aTexCoord"); + uMVP = glGetUniformLocation(pgm, "uMVP"); + uTexture = glGetUniformLocation(pgm, "uTexture"); if(glGetError() != GL_NO_ERROR) fprintf(stderr,"OOPS!\n"); @@ -142,14 +142,14 @@ int scene_draw(struct ctxt *c) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex0); - glUniformMatrix4fv(_uMVP, 1, GL_FALSE, (void*) MVP); - glUniform1i(_uSampler, 0); + glUniformMatrix4fv(uMVP, 1, GL_FALSE, (void*) MVP); + glUniform1i(uTexture, 0); - glVertexAttribPointer(_vPosition, 3, GL_FLOAT, GL_FALSE, 5*4, data); - glEnableVertexAttribArray(_vPosition); + glVertexAttribPointer(aVertex, 3, GL_FLOAT, GL_FALSE, 5*4, data); + glEnableVertexAttribArray(aVertex); - glVertexAttribPointer(_vUV, 2, GL_FLOAT, GL_FALSE, 5*4, data + 3); - glEnableVertexAttribArray(_vUV); + glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 5*4, data + 3); + glEnableVertexAttribArray(aTexCoord); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices); diff --git a/test3.c b/test3.c @@ -29,8 +29,8 @@ unsigned texw, texh; const char *vert_src, *frag_src; GLuint pgm, vshd, fshd, tex0; -GLuint _vPosition, _vUV; -GLuint _uMVP, _uSampler; +GLuint aVertex, aTexCoord; +GLuint uMVP, uTexture; mat4 perspective; mat4 MVP; @@ -44,9 +44,9 @@ int scene_init(struct ctxt *c) { if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1))) return -1; - if (!(vert_src = load_file("test1.vertex.glsl", 0))) + if (!(vert_src = load_file("test1.vs", 0))) return -1; - if (!(frag_src = load_file("test1.fragment.glsl", 0))) + if (!(frag_src = load_file("test1.fs", 0))) return -1; if (!(m = load_wavefront_obj("cube.obj"))) @@ -62,10 +62,10 @@ int scene_init(struct ctxt *c) { if (shader_compile(vert_src, frag_src, &pgm, &vshd, &fshd)) return -1; - _vPosition = glGetAttribLocation(pgm, "vPosition"); - _vUV = glGetAttribLocation(pgm, "vUV"); - _uMVP = glGetUniformLocation(pgm, "uMVP"); - _uSampler = glGetUniformLocation(pgm, "uSampler"); + aVertex = glGetAttribLocation(pgm, "aVertex"); + aTexCoord = glGetAttribLocation(pgm, "aTexCoord"); + uMVP = glGetUniformLocation(pgm, "uMVP"); + uTexture = glGetUniformLocation(pgm, "uTexture"); if(glGetError() != GL_NO_ERROR) fprintf(stderr,"OOPS!\n"); @@ -106,14 +106,14 @@ int scene_draw(struct ctxt *c) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex0); - glUniformMatrix4fv(_uMVP, 1, GL_FALSE, (void*) MVP); - glUniform1i(_uSampler, 0); + glUniformMatrix4fv(uMVP, 1, GL_FALSE, (void*) MVP); + glUniform1i(uTexture, 0); - glVertexAttribPointer(_vPosition, 3, GL_FLOAT, GL_FALSE, 8*4, m->vdata); - glEnableVertexAttribArray(_vPosition); + glVertexAttribPointer(aVertex, 3, GL_FLOAT, GL_FALSE, 8*4, m->vdata); + glEnableVertexAttribArray(aVertex); - glVertexAttribPointer(_vUV, 2, GL_FLOAT, GL_FALSE, 8*4, m->vdata + 6); - glEnableVertexAttribArray(_vUV); + glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 8*4, m->vdata + 6); + glEnableVertexAttribArray(aTexCoord); glDrawElements(GL_TRIANGLES, m->icount, GL_UNSIGNED_SHORT, m->idx); diff --git a/test4.c b/test4.c @@ -26,8 +26,8 @@ unsigned texw, texh; const char *vert_src, *frag_src; GLuint pgm, vshd, fshd, tex0; -GLuint _vPosition, _vUV; -GLuint _uMVP, _uSampler; +GLuint aVertex, aTexCoord; +GLuint uMVP, uTexture; mat4 MVP; @@ -52,9 +52,9 @@ GLfloat texcoords[] = { int scene_init(struct ctxt *c) { if (!(texdata = load_png_gray("texture.sdf.png", &texw, &texh, 1))) return -1; - if (!(vert_src = load_file("test4.vertex.glsl", 0))) + if (!(vert_src = load_file("test4.vs", 0))) return -1; - if (!(frag_src = load_file("test4.fragment.glsl", 0))) + if (!(frag_src = load_file("test4.fs", 0))) return -1; mtx_identity(MVP); @@ -67,12 +67,10 @@ int scene_init(struct ctxt *c) { if (shader_compile(vert_src, frag_src, &pgm, &vshd, &fshd)) return -1; - _vPosition = glGetAttribLocation(pgm, "vPosition"); - _vUV = glGetAttribLocation(pgm, "vUV"); - _uMVP = glGetUniformLocation(pgm, "uMVP"); - _uSampler = glGetUniformLocation(pgm, "uSampler"); - - if(glGetError() != GL_NO_ERROR) fprintf(stderr,"OOPS!\n"); + aVertex = glGetAttribLocation(pgm, "aVertex"); + aTexCoord = glGetAttribLocation(pgm, "aTexCoord"); + uMVP = glGetUniformLocation(pgm, "uMVP"); + uTexture = glGetUniformLocation(pgm, "uTexture"); glEnable(GL_TEXTURE_2D); // glEnable(GL_BLEND); @@ -97,14 +95,14 @@ int scene_draw(struct ctxt *c) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex0); - glUniformMatrix4fv(_uMVP, 1, GL_FALSE, (void*) MVP); - glUniform1i(_uSampler, 0); + glUniformMatrix4fv(uMVP, 1, GL_FALSE, (void*) MVP); + glUniform1i(uTexture, 0); - glVertexAttribPointer(_vPosition, 3, GL_FLOAT, GL_FALSE, 0, verts); - glEnableVertexAttribArray(_vPosition); + glVertexAttribPointer(aVertex, 3, GL_FLOAT, GL_FALSE, 0, verts); + glEnableVertexAttribArray(aVertex); - glVertexAttribPointer(_vUV, 2, GL_FLOAT, GL_FALSE, 0, texcoords); - glEnableVertexAttribArray(_vUV); + glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 0, texcoords); + glEnableVertexAttribArray(aTexCoord); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); diff --git a/test4.fragment.glsl b/test4.fragment.glsl @@ -1,11 +0,0 @@ -uniform sampler2D uSampler; -varying vec2 fUV; - -void main() { - float mask = texture2D(uSampler, fUV).a; - - if (mask < 0.5) - discard; - - gl_FragColor = vec4(1, 1, 0, 1); -} diff --git a/test4.fs b/test4.fs @@ -0,0 +1,11 @@ +uniform sampler2D uTexture; +varying vec2 vTexCoord; + +void main() { + float mask = texture2D(uTexture, vTexCoord).a; + + if (mask < 0.5) + discard; + + gl_FragColor = vec4(1, 1, 0, 1); +} diff --git a/test4.vertex.glsl b/test4.vertex.glsl @@ -1,10 +0,0 @@ -uniform mat4 uMVP; -attribute vec4 vPosition; -attribute vec2 vUV; -varying vec2 fUV; - -void main() { - gl_Position = uMVP * vPosition; - fUV = vUV; -} - diff --git a/test4.vs b/test4.vs @@ -0,0 +1,10 @@ +uniform mat4 uMVP; +attribute vec4 aVertex; +attribute vec2 aTexCoord; +varying vec2 vTexCoord; + +void main() { + gl_Position = uMVP * aVertex; + vTexCoord = aTexCoord; +} +