fixed bugs, added debug shaders, drawing a dungeon wall segment for testing
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 7 Sep 2021 02:05:43 +0000 (05:05 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 7 Sep 2021 02:05:43 +0000 (05:05 +0300)
sdr/foo.p.glsl [new file with mode: 0644]
sdr/foo.v.glsl [new file with mode: 0644]
src/game.c
src/mesh.c
src/opengl.c
src/opengl.h
src/scenefile.c
src/sdr.c [new file with mode: 0644]
src/sdr.h [new file with mode: 0644]

diff --git a/sdr/foo.p.glsl b/sdr/foo.p.glsl
new file mode 100644 (file)
index 0000000..73a4a15
--- /dev/null
@@ -0,0 +1,6 @@
+varying vec3 vnorm;
+
+void main()
+{
+       gl_FragColor.rgb = vnorm * 0.5 + 0.5;
+}
diff --git a/sdr/foo.v.glsl b/sdr/foo.v.glsl
new file mode 100644 (file)
index 0000000..70292b3
--- /dev/null
@@ -0,0 +1,10 @@
+attribute vec3 apos;
+attribute vec3 anorm;
+
+varying vec3 vnorm;
+
+void main()
+{
+       gl_Position = gl_ModelViewProjectionMatrix * vec4(apos, 1.0);
+       vnorm = gl_NormalMatrix * anorm;
+}
index 5a846ed..e159ec2 100644 (file)
@@ -1,9 +1,23 @@
 #include <assert.h>
+#include "cgmath/cgmath.h"
 #include "game.h"
 #include "opengl.h"
 #include "level.h"
+#include "scenefile.h"
+#include "sdr.h"
 
 struct level lvl;
+struct scenefile scn;
+
+int win_width, win_height;
+float win_aspect;
+int mouse_x, mouse_y;
+int bnstate[8];
+
+float cam_theta, cam_phi, cam_dist = 10;
+float view_matrix[16], proj_matrix[16];
+
+unsigned int sdr_foo;
 
 int game_init(void)
 {
@@ -11,23 +25,65 @@ int game_init(void)
                return -1;
        }
 
+       glEnable(GL_DEPTH_TEST);
+       glEnable(GL_CULL_FACE);
+
+       if(!(sdr_foo = create_program_load("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) {
+               return -1;
+       }
+       glBindAttribLocation(sdr_foo, MESH_ATTR_VERTEX, "apos");
+       glBindAttribLocation(sdr_foo, MESH_ATTR_NORMAL, "anorm");
+       glBindAttribLocation(sdr_foo, MESH_ATTR_TANGENT, "atang");
+       glBindAttribLocation(sdr_foo, MESH_ATTR_TEXCOORD, "atex");
+       link_program(sdr_foo);
+
        if(load_level(&lvl, "data/test.lvl") == -1) {
                return -1;
        }
 
+       /* DBG */
+       if(load_scenefile(&scn, "data/dwall1.obj") == -1) {
+               return -1;
+       }
+
        return 0;
 }
 
 void game_shutdown(void)
 {
        destroy_level(&lvl);
+       free_program(sdr_foo);
 }
 
 void game_display(void)
 {
-       glClearColor(1, 0, 0, 1);
+       struct mesh *mesh;
+
+       glClearColor(0.1, 0.1, 0.1, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
+       cgm_midentity(proj_matrix);
+       cgm_mperspective(proj_matrix, cgm_deg_to_rad(50), win_aspect, 0.5, 500.0);
+       glMatrixMode(GL_PROJECTION);
+       glLoadMatrixf(proj_matrix);
+
+       cgm_midentity(view_matrix);
+       cgm_mpretranslate(view_matrix, 0, 0, -cam_dist);
+       cgm_mprerotate(view_matrix, cam_phi, 1, 0, 0);
+       cgm_mprerotate(view_matrix, cam_theta, 0, 1, 0);
+       glMatrixMode(GL_MODELVIEW);
+       glLoadMatrixf(view_matrix);
+
+       glUseProgram(sdr_foo);
+
+       mesh = scn.meshlist;
+       while(mesh) {
+               draw_mesh(mesh);
+               mesh = mesh->next;
+       }
+
+       glUseProgram(0);
+
        game_swap_buffers();
        assert(glGetError() == GL_NO_ERROR);
 }
@@ -35,6 +91,9 @@ void game_display(void)
 void game_reshape(int x, int y)
 {
        glViewport(0, 0, x, y);
+       win_width = x;
+       win_height = y;
+       win_aspect = (float)x / (float)y;
 }
 
 void game_keyboard(int key, int press)
@@ -47,8 +106,28 @@ void game_keyboard(int key, int press)
 
 void game_mbutton(int bn, int press, int x, int y)
 {
+       bnstate[bn] = press;
+       mouse_x = x;
+       mouse_y = y;
 }
 
 void game_mmotion(int x, int y)
 {
+       int dx = x - mouse_x;
+       int dy = y - mouse_y;
+       mouse_x = x;
+       mouse_y = y;
+
+       if(!(dx | dy)) return;
+
+       if(bnstate[0]) {
+               cam_theta += cgm_deg_to_rad(dx * 0.5f);
+               cam_phi += cgm_deg_to_rad(dy * 0.5f);
+               if(cam_phi < -M_PI/2) cam_phi = -M_PI/2;
+               if(cam_phi > M_PI/2) cam_phi = M_PI/2;
+       }
+       if(bnstate[2]) {
+               cam_dist += dy * 0.1;
+               if(cam_dist < 0) cam_dist = 0;
+       }
 }
index 149fbe7..538f7c9 100644 (file)
@@ -3,9 +3,7 @@
 #include <stddef.h>
 #include <string.h>
 #include <float.h>
-#define GL_GLEXT_PROTOTYPES 1
-#include <GL/gl.h>
-#include <GL/glext.h>
+#include "opengl.h"
 #include "mesh.h"
 
 static int update_mesh_vbo(struct mesh *m);
index aa17885..8eadb97 100644 (file)
@@ -28,11 +28,21 @@ int init_opengl(void)
                return -1;
        }
 
+       if(glcaps.ver_major >= 2 || glcaps.ver_minor >= 5 || (strstr(glext, "GL_ARB_vertex_buffer_object"))) {
+               glcaps.vbo = 1;
+       }
+
        if(glcaps.ver_major >= 2 || (strstr(glext, "GL_ARB_vertex_shader") && strstr(glext, "GL_ARB_fragment_shader"))) {
                glcaps.sdr = 1;
        }
 
-#ifndef LOADEXT_SDR
+       if(glcaps.vbo) {
+               LOADPROC(PFNGLGENBUFFERSPROC, glGenBuffers);
+               LOADPROC(PFNGLDELETEBUFFERSPROC, glDeleteBuffers);
+               LOADPROC(PFNGLBINDBUFFERPROC, glBindBuffer);
+               LOADPROC(PFNGLBUFFERDATAPROC, glBufferData);
+       }
+
        if(glcaps.sdr) {
                LOADPROC(PFNGLCREATEPROGRAMPROC, glCreateProgram);
                LOADPROC(PFNGLDELETEPROGRAMPROC, glDeleteProgram);
@@ -56,8 +66,11 @@ int init_opengl(void)
                LOADPROC(PFNGLCOMPILESHADERPROC, glCompileShader);
                LOADPROC(PFNGLGETSHADERIVPROC, glGetShaderiv);
                LOADPROC(PFNGLGETSHADERINFOLOGPROC, glGetShaderInfoLog);
+               LOADPROC(PFNGLBINDATTRIBLOCATIONPROC, glBindAttribLocation);
+               LOADPROC(PFNGLENABLEVERTEXATTRIBARRAYPROC, glEnableVertexAttribArray);
+               LOADPROC(PFNGLDISABLEVERTEXATTRIBARRAYPROC, glDisableVertexAttribArray);
+               LOADPROC(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer);
        }
-#endif
 
        return 0;
 }
index 9388ec2..aa32d9c 100644 (file)
@@ -2,19 +2,18 @@
 #define OPENGL_H_
 
 #include <GL/gl.h>
-
-#ifndef GL_VERSION_2_0
-#define LOADEXT_SDR
-#endif
-
 #include <GL/glext.h>
 
 struct glcaps {
        int ver_major, ver_minor;
-       int sdr;
+       int sdr, vbo;
 } glcaps;
 
-#ifndef LOADEXT_SDR
+PFNGLGENBUFFERSPROC glGenBuffers;
+PFNGLDELETEBUFFERSPROC glDeleteBuffers;
+PFNGLBINDBUFFERPROC glBindBuffer;
+PFNGLBUFFERDATAPROC glBufferData;
+
 PFNGLCREATEPROGRAMPROC glCreateProgram;
 PFNGLDELETEPROGRAMPROC glDeleteProgram;
 PFNGLATTACHSHADERPROC glAttachShader;
@@ -37,7 +36,10 @@ PFNGLSHADERSOURCEPROC glShaderSource;
 PFNGLCOMPILESHADERPROC glCompileShader;
 PFNGLGETSHADERIVPROC glGetShaderiv;
 PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
-#endif
+PFNGLBINDATTRIBLOCATIONPROC glBindAttribLocation;
+PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
+PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
+PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
 
 int init_opengl(void);
 
index 39d9b92..398ce96 100644 (file)
@@ -339,14 +339,18 @@ static int load_mtllib(struct scenefile *scn, const char *path_prefix, const cha
 
                if(memcmp(line, "newmtl", 6) == 0) {
                        if(mtl) {
+                               conv_mtl(mtl, &om, path_prefix);
                                mtl->next = scn->mtllist;
                                scn->mtllist = mtl;
                        }
-                       if((mtl = calloc(1, sizeof *mtl))) {
-                               if((line = cleanline(line + 6))) {
-                                       mtl->name = strdup(line);
-                               }
+                       mtl = calloc(1, sizeof *mtl);
+
+                       memset(&om, 0, sizeof om);
+
+                       if((line = cleanline(line + 6))) {
+                               om.name = strdup(line);
                        }
+
                } else if(memcmp(line, "Kd", 2) == 0) {
                        sscanf(line + 3, "%f %f %f", &om.kd.x, &om.kd.y, &om.kd.z);
                } else if(memcmp(line, "Ks", 2) == 0) {
@@ -370,10 +374,10 @@ static int load_mtllib(struct scenefile *scn, const char *path_prefix, const cha
                                om.map_alpha = strdup(line);
                        }
                }
-               conv_mtl(mtl, &om, path_prefix);
        }
 
        if(mtl) {
+               conv_mtl(mtl, &om, path_prefix);
                mtl->next = scn->mtllist;
                scn->mtllist = mtl;
        }
@@ -397,7 +401,7 @@ static void conv_mtl(struct material *mm, struct objmtl *om, const char *path_pr
        int len, prefix_len, maxlen = 0;
 
        memset(mm, 0, sizeof *mm);
-       mm->name = strdup(om->name);
+       mm->name = om->name;
        mm->color = om->kd;
        mm->spec = om->ks;
        mm->shininess = om->shin;
diff --git a/src/sdr.c b/src/sdr.c
new file mode 100644 (file)
index 0000000..0152a6e
--- /dev/null
+++ b/src/sdr.c
@@ -0,0 +1,562 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <assert.h>
+#include "opengl.h"
+
+#if defined(unix) || defined(__unix__)
+#include <unistd.h>
+#include <sys/stat.h>
+#endif /* unix */
+
+#include "sdr.h"
+
+static const char *sdrtypestr(unsigned int sdrtype);
+static int sdrtypeidx(unsigned int sdrtype);
+
+
+unsigned int create_vertex_shader(const char *src)
+{
+       return create_shader(src, GL_VERTEX_SHADER);
+}
+
+unsigned int create_pixel_shader(const char *src)
+{
+       return create_shader(src, GL_FRAGMENT_SHADER);
+}
+
+unsigned int create_tessctl_shader(const char *src)
+{
+#ifdef GL_TESS_CONTROL_SHADER
+       return create_shader(src, GL_TESS_CONTROL_SHADER);
+#else
+       return 0;
+#endif
+}
+
+unsigned int create_tesseval_shader(const char *src)
+{
+#ifdef GL_TESS_EVALUATION_SHADER
+       return create_shader(src, GL_TESS_EVALUATION_SHADER);
+#else
+       return 0;
+#endif
+}
+
+unsigned int create_geometry_shader(const char *src)
+{
+#ifdef GL_GEOMETRY_SHADER
+       return create_shader(src, GL_GEOMETRY_SHADER);
+#else
+       return 0;
+#endif
+}
+
+unsigned int create_shader(const char *src, unsigned int sdr_type)
+{
+       unsigned int sdr;
+       int success, info_len;
+       char *info_str = 0;
+       const char *src_str[3], *header, *footer;
+       int src_str_count = 0;
+       GLenum err;
+
+       if((header = get_shader_header(sdr_type))) {
+               src_str[src_str_count++] = header;
+       }
+       src_str[src_str_count++] = src;
+       if((footer = get_shader_footer(sdr_type))) {
+               src_str[src_str_count++] = footer;
+       }
+
+       sdr = glCreateShader(sdr_type);
+       assert(glGetError() == GL_NO_ERROR);
+       glShaderSource(sdr, src_str_count, src_str, 0);
+       err = glGetError();
+       assert(err == GL_NO_ERROR);
+       glCompileShader(sdr);
+       assert(glGetError() == GL_NO_ERROR);
+
+       glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
+       assert(glGetError() == GL_NO_ERROR);
+       glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
+       assert(glGetError() == GL_NO_ERROR);
+
+       if(info_len) {
+               if((info_str = malloc(info_len + 1))) {
+                       glGetShaderInfoLog(sdr, info_len, 0, info_str);
+                       assert(glGetError() == GL_NO_ERROR);
+                       info_str[info_len] = 0;
+               }
+       }
+
+       if(success) {
+               fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
+       } else {
+               fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
+               glDeleteShader(sdr);
+               sdr = 0;
+       }
+
+       free(info_str);
+       return sdr;
+}
+
+void free_shader(unsigned int sdr)
+{
+       glDeleteShader(sdr);
+}
+
+unsigned int load_vertex_shader(const char *fname)
+{
+       return load_shader(fname, GL_VERTEX_SHADER);
+}
+
+unsigned int load_pixel_shader(const char *fname)
+{
+       return load_shader(fname, GL_FRAGMENT_SHADER);
+}
+
+unsigned int load_tessctl_shader(const char *fname)
+{
+#ifdef GL_TESS_CONTROL_SHADER
+       return load_shader(fname, GL_TESS_CONTROL_SHADER);
+#else
+       return 0;
+#endif
+}
+
+unsigned int load_tesseval_shader(const char *fname)
+{
+#ifdef GL_TESS_EVALUATION_SHADER
+       return load_shader(fname, GL_TESS_EVALUATION_SHADER);
+#else
+       return 0;
+#endif
+}
+
+unsigned int load_geometry_shader(const char *fname)
+{
+#ifdef GL_GEOMETRY_SHADER
+       return load_shader(fname, GL_GEOMETRY_SHADER);
+#else
+       return 0;
+#endif
+}
+
+unsigned int load_shader(const char *fname, unsigned int sdr_type)
+{
+       unsigned int sdr;
+       size_t filesize;
+       FILE *fp;
+       char *src;
+
+       if(!(fp = fopen(fname, "rb"))) {
+               fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
+               return 0;
+       }
+
+       fseek(fp, 0, SEEK_END);
+       filesize = ftell(fp);
+       fseek(fp, 0, SEEK_SET);
+
+       if(!(src = malloc(filesize + 1))) {
+               fclose(fp);
+               return 0;
+       }
+       fread(src, 1, filesize, fp);
+       src[filesize] = 0;
+       fclose(fp);
+
+       fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
+       sdr = create_shader(src, sdr_type);
+
+       free(src);
+       return sdr;
+}
+
+
+/* ---- gpu programs ---- */
+
+unsigned int create_program(void)
+{
+       unsigned int prog = glCreateProgram();
+       assert(glGetError() == GL_NO_ERROR);
+       return prog;
+}
+
+unsigned int create_program_link(unsigned int sdr0, ...)
+{
+       unsigned int prog, sdr;
+       va_list ap;
+
+       if(!(prog = create_program())) {
+               return 0;
+       }
+
+       attach_shader(prog, sdr0);
+       if(glGetError()) {
+               return 0;
+       }
+
+       va_start(ap, sdr0);
+       while((sdr = va_arg(ap, unsigned int))) {
+               attach_shader(prog, sdr);
+               if(glGetError()) {
+                       return 0;
+               }
+       }
+       va_end(ap);
+
+       if(link_program(prog) == -1) {
+               free_program(prog);
+               return 0;
+       }
+       return prog;
+}
+
+unsigned int create_program_load(const char *vfile, const char *pfile)
+{
+       unsigned int vs = 0, ps = 0;
+
+       if(vfile && *vfile && !(vs = load_vertex_shader(vfile))) {
+               return 0;
+       }
+       if(pfile && *pfile && !(ps = load_pixel_shader(pfile))) {
+               return 0;
+       }
+       return create_program_link(vs, ps, 0);
+}
+
+void free_program(unsigned int sdr)
+{
+       glDeleteProgram(sdr);
+}
+
+void attach_shader(unsigned int prog, unsigned int sdr)
+{
+       int err;
+
+       if(prog && sdr) {
+               assert(glGetError() == GL_NO_ERROR);
+               glAttachShader(prog, sdr);
+               if((err = glGetError()) != GL_NO_ERROR) {
+                       fprintf(stderr, "failed to attach shader %u to program %u (err: 0x%x)\n", sdr, prog, err);
+                       abort();
+               }
+       }
+}
+
+int link_program(unsigned int prog)
+{
+       int linked, info_len, retval = 0;
+       char *info_str = 0;
+
+       glLinkProgram(prog);
+       assert(glGetError() == GL_NO_ERROR);
+       glGetProgramiv(prog, GL_LINK_STATUS, &linked);
+       assert(glGetError() == GL_NO_ERROR);
+       glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
+       assert(glGetError() == GL_NO_ERROR);
+
+       if(info_len) {
+               if((info_str = malloc(info_len + 1))) {
+                       glGetProgramInfoLog(prog, info_len, 0, info_str);
+                       assert(glGetError() == GL_NO_ERROR);
+                       info_str[info_len] = 0;
+               }
+       }
+
+       if(linked) {
+               fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
+       } else {
+               fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
+               retval = -1;
+       }
+
+       free(info_str);
+       return retval;
+}
+
+int bind_program(unsigned int prog)
+{
+       GLenum err;
+
+       glUseProgram(prog);
+       if(prog && (err = glGetError()) != GL_NO_ERROR) {
+               /* maybe the program is not linked, try linking first */
+               if(err == GL_INVALID_OPERATION) {
+                       if(link_program(prog) == -1) {
+                               return -1;
+                       }
+                       glUseProgram(prog);
+                       return glGetError() == GL_NO_ERROR ? 0 : -1;
+               }
+               return -1;
+       }
+       return 0;
+}
+
+/* ugly but I'm not going to write the same bloody code over and over */
+#define BEGIN_UNIFORM_CODE \
+       int loc, curr_prog; \
+       glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
+       if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
+               return -1; \
+       } \
+       if((loc = glGetUniformLocation(prog, name)) != -1)
+
+#define END_UNIFORM_CODE \
+       if((unsigned int)curr_prog != prog) { \
+               bind_program(curr_prog); \
+       } \
+       return loc == -1 ? -1 : 0
+
+int get_uniform_loc(unsigned int prog, const char *name)
+{
+       int loc, curr_prog;
+       glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
+       if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
+               return -1;
+       }
+       loc = glGetUniformLocation(prog, name);
+       if((unsigned int)curr_prog != prog) {
+               bind_program(curr_prog);
+       }
+       return loc;
+}
+
+int set_uniform_int(unsigned int prog, const char *name, int val)
+{
+       BEGIN_UNIFORM_CODE {
+               glUniform1i(loc, val);
+       }
+       END_UNIFORM_CODE;
+}
+
+int set_uniform_float(unsigned int prog, const char *name, float val)
+{
+       BEGIN_UNIFORM_CODE {
+               glUniform1f(loc, val);
+       }
+       END_UNIFORM_CODE;
+}
+
+int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
+{
+       BEGIN_UNIFORM_CODE {
+               glUniform2f(loc, x, y);
+       }
+       END_UNIFORM_CODE;
+}
+
+int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
+{
+       BEGIN_UNIFORM_CODE {
+               glUniform3f(loc, x, y, z);
+       }
+       END_UNIFORM_CODE;
+}
+
+int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
+{
+       BEGIN_UNIFORM_CODE {
+               glUniform4f(loc, x, y, z, w);
+       }
+       END_UNIFORM_CODE;
+}
+
+int set_uniform_matrix4(unsigned int prog, const char *name, const float *mat)
+{
+       BEGIN_UNIFORM_CODE {
+               glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
+       }
+       END_UNIFORM_CODE;
+}
+
+int set_uniform_matrix4_transposed(unsigned int prog, const char *name, const float *mat)
+{
+       BEGIN_UNIFORM_CODE {
+               glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
+       }
+       END_UNIFORM_CODE;
+}
+
+int get_attrib_loc(unsigned int prog, const char *name)
+{
+       int loc, curr_prog;
+
+       glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
+       if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
+               return -1;
+       }
+
+       loc = glGetAttribLocation(prog, (char*)name);
+
+       if((unsigned int)curr_prog != prog) {
+               bind_program(curr_prog);
+       }
+       return loc;
+}
+
+void set_attrib_float3(int attr_loc, float x, float y, float z)
+{
+       glVertexAttrib3f(attr_loc, x, y, z);
+}
+
+/* ---- shader composition ---- */
+struct string {
+       char *text;
+       int len;
+};
+
+#define NUM_SHADER_TYPES       5
+static struct string header[NUM_SHADER_TYPES];
+static struct string footer[NUM_SHADER_TYPES];
+
+static void clear_string(struct string *str)
+{
+       free(str->text);
+       str->text = 0;
+       str->len = 0;
+}
+
+static void append_string(struct string *str, const char *s)
+{
+       int len, newlen;
+       char *newstr;
+
+       if(!s || !*s) return;
+
+       len = strlen(s);
+       newlen = str->len + len;
+       if(!(newstr = malloc(newlen + 2))) {    /* leave space for a possible newline */
+               fprintf(stderr, "shader composition: failed to append string of size %d\n", len);
+               abort();
+       }
+
+       if(str->text) {
+               memcpy(newstr, str->text, str->len);
+       }
+       memcpy(newstr + str->len, s, len + 1);
+
+       if(s[len - 1] != '\n') {
+               newstr[newlen] = '\n';
+               newstr[newlen + 1] = 0;
+       }
+
+       free(str->text);
+       str->text = newstr;
+       str->len = newlen;
+}
+
+void clear_shader_header(unsigned int type)
+{
+       if(type) {
+               int idx = sdrtypeidx(type);
+               clear_string(&header[idx]);
+       } else {
+               int i;
+               for(i=0; i<NUM_SHADER_TYPES; i++) {
+                       clear_string(&header[i]);
+               }
+       }
+}
+
+void clear_shader_footer(unsigned int type)
+{
+       if(type) {
+               int idx = sdrtypeidx(type);
+               clear_string(&footer[idx]);
+       } else {
+               int i;
+               for(i=0; i<NUM_SHADER_TYPES; i++) {
+                       clear_string(&footer[i]);
+               }
+       }
+}
+
+void add_shader_header(unsigned int type, const char *s)
+{
+       if(type) {
+               int idx = sdrtypeidx(type);
+               append_string(&header[idx], s);
+       } else {
+               int i;
+               for(i=0; i<NUM_SHADER_TYPES; i++) {
+                       append_string(&header[i], s);
+               }
+       }
+}
+
+void add_shader_footer(unsigned int type, const char *s)
+{
+       if(type) {
+               int idx = sdrtypeidx(type);
+               append_string(&footer[idx], s);
+       } else {
+               int i;
+               for(i=0; i<NUM_SHADER_TYPES; i++) {
+                       append_string(&footer[i], s);
+               }
+       }
+}
+
+const char *get_shader_header(unsigned int type)
+{
+       int idx = sdrtypeidx(type);
+       return header[idx].text;
+}
+
+const char *get_shader_footer(unsigned int type)
+{
+       int idx = sdrtypeidx(type);
+       return footer[idx].text;
+}
+
+static const char *sdrtypestr(unsigned int sdrtype)
+{
+       switch(sdrtype) {
+       case GL_VERTEX_SHADER:
+               return "vertex";
+       case GL_FRAGMENT_SHADER:
+               return "pixel";
+#ifdef GL_TESS_CONTROL_SHADER
+       case GL_TESS_CONTROL_SHADER:
+               return "tessellation control";
+#endif
+#ifdef GL_TESS_EVALUATION_SHADER
+       case GL_TESS_EVALUATION_SHADER:
+               return "tessellation evaluation";
+#endif
+#ifdef GL_GEOMETRY_SHADER
+       case GL_GEOMETRY_SHADER:
+               return "geometry";
+#endif
+
+       default:
+               break;
+       }
+       return "<unknown>";
+}
+
+static int sdrtypeidx(unsigned int sdrtype)
+{
+       switch(sdrtype) {
+       case GL_VERTEX_SHADER:
+               return 0;
+       case GL_FRAGMENT_SHADER:
+               return 1;
+       case GL_TESS_CONTROL_SHADER:
+               return 2;
+       case GL_TESS_EVALUATION_SHADER:
+               return 3;
+       case GL_GEOMETRY_SHADER:
+               return 4;
+       default:
+               break;
+       }
+       return 0;
+}
diff --git a/src/sdr.h b/src/sdr.h
new file mode 100644 (file)
index 0000000..7bf2389
--- /dev/null
+++ b/src/sdr.h
@@ -0,0 +1,68 @@
+#ifndef SDR_H_
+#define SDR_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* ---- shaders ---- */
+unsigned int create_vertex_shader(const char *src);
+unsigned int create_pixel_shader(const char *src);
+unsigned int create_tessctl_shader(const char *src);
+unsigned int create_tesseval_shader(const char *src);
+unsigned int create_geometry_shader(const char *src);
+unsigned int create_shader(const char *src, unsigned int sdr_type);
+void free_shader(unsigned int sdr);
+
+unsigned int load_vertex_shader(const char *fname);
+unsigned int load_pixel_shader(const char *fname);
+unsigned int load_tessctl_shader(const char *fname);
+unsigned int load_tesseval_shader(const char *fname);
+unsigned int load_geometry_shader(const char *fname);
+unsigned int load_shader(const char *src, unsigned int sdr_type);
+
+int add_shader(const char *fname, unsigned int sdr);
+int remove_shader(const char *fname);
+
+/* ---- gpu programs ---- */
+unsigned int create_program(void);
+unsigned int create_program_link(unsigned int sdr0, ...);
+unsigned int create_program_load(const char *vfile, const char *pfile);
+void free_program(unsigned int sdr);
+
+void attach_shader(unsigned int prog, unsigned int sdr);
+int link_program(unsigned int prog);
+int bind_program(unsigned int prog);
+
+int get_uniform_loc(unsigned int prog, const char *name);
+
+int set_uniform_int(unsigned int prog, const char *name, int val);
+int set_uniform_float(unsigned int prog, const char *name, float val);
+int set_uniform_float2(unsigned int prog, const char *name, float x, float y);
+int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z);
+int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w);
+int set_uniform_matrix4(unsigned int prog, const char *name, const float *mat);
+int set_uniform_matrix4_transposed(unsigned int prog, const char *name, const float *mat);
+
+int get_attrib_loc(unsigned int prog, const char *name);
+void set_attrib_float3(int attr_loc, float x, float y, float z);
+
+/* ---- shader composition ---- */
+
+/* clear shader header/footer text.
+ * pass the shader type to clear, or 0 to clear all types */
+void clear_shader_header(unsigned int type);
+void clear_shader_footer(unsigned int type);
+/* append text to the header/footer of a specific shader type
+ * or use type 0 to add it to all shade types */
+void add_shader_header(unsigned int type, const char *s);
+void add_shader_footer(unsigned int type, const char *s);
+/* get the current header/footer text for a specific shader type */
+const char *get_shader_header(unsigned int type);
+const char *get_shader_footer(unsigned int type);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* SDR_H_ */