foo
authorJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 19 May 2023 01:13:51 +0000 (04:13 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 19 May 2023 01:13:51 +0000 (04:13 +0300)
Signed-off-by: John Tsiombikas <nuclear@member.fsf.org>

sdr/raydungeon.p.glsl [new file with mode: 0644]
sdr/raydungeon.v.glsl [new file with mode: 0644]
src/main.c
src/opengl.c [new file with mode: 0644]
src/opengl.h [new file with mode: 0644]
src/scr_game.c
src/sdr.c [new file with mode: 0644]
src/sdr.h [new file with mode: 0644]

diff --git a/sdr/raydungeon.p.glsl b/sdr/raydungeon.p.glsl
new file mode 100644 (file)
index 0000000..8a66df7
--- /dev/null
@@ -0,0 +1,5 @@
+void main()
+{
+       vec3 col = vec3(gl_TexCoord[0].st, 1.0);
+       gl_FragColor = vec4(col, 1.0);
+}
diff --git a/sdr/raydungeon.v.glsl b/sdr/raydungeon.v.glsl
new file mode 100644 (file)
index 0000000..a8fb0cf
--- /dev/null
@@ -0,0 +1,5 @@
+void main()
+{
+       gl_Position = gl_Vertex;
+       gl_TexCoord[0] = gl_MultiTexCoord0;
+}
index e96732f..beb3cf2 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
+#include "opengl.h"
 #include "miniglut.h"
 #include "game.h"
 
@@ -49,6 +50,10 @@ int main(int argc, char **argv)
        glutMotionFunc(motion);
        glutPassiveMotionFunc(motion);
 
+       if(init_opengl() == -1) {
+               return 1;
+       }
+
 #if defined(__unix__) || defined(unix)
        xdpy = glXGetCurrentDisplay();
        xwin = glXGetCurrentDrawable();
diff --git a/src/opengl.c b/src/opengl.c
new file mode 100644 (file)
index 0000000..beba99d
--- /dev/null
@@ -0,0 +1,7 @@
+#include "opengl.h"
+
+int init_opengl(void)
+{
+       glewInit();
+       return 0;
+}
diff --git a/src/opengl.h b/src/opengl.h
new file mode 100644 (file)
index 0000000..a834f93
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef OPENGL_H_
+#define OPENGL_H_
+
+#include "GL/glew.h"
+#include <GL/gl.h>
+
+int init_opengl(void);
+
+#endif /* OPENGL_H_ */
index bcc6c34..2f1f31b 100644 (file)
@@ -1,9 +1,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
-#include <GL/gl.h>
+#include "opengl.h"
 #include "cgmath/cgmath.h"
 #include "game.h"
+#include "sdr.h"
 #include "level.h"
 #include "util.h"
 
@@ -32,14 +33,20 @@ static float proj_mat[16], view_mat[16];
 static float cam_theta, cam_phi = 20, cam_dist = 10;
 static cgm_vec3 cam_pan;
 
+static unsigned int sdr;
+
 
 static int ginit(void)
 {
+       if(!(sdr = create_program_load("sdr/raydungeon.v.glsl", "sdr/raydungeon.p.glsl"))) {
+               return -1;
+       }
        return 0;
 }
 
 static void gdestroy(void)
 {
+       free_program(sdr);
 }
 
 static int gstart(void)
@@ -76,6 +83,19 @@ static void gdisplay(void)
        glMatrixMode(GL_MODELVIEW);
        glLoadMatrixf(view_mat);
 
+       glUseProgram(sdr);
+
+       glDepthMask(0);
+       glBegin(GL_TRIANGLES);
+       glTexCoord2f(0, 0); glVertex2f(-1, -1);
+       glTexCoord2f(2, 0); glVertex2f(4, -1);
+       glTexCoord2f(0, 2); glVertex2f(-1, 4);
+       glEnd();
+       glDepthMask(1);
+
+       glUseProgram(0);
+
+
        cell = lvl->cells;
        glBegin(GL_QUADS);
        glColor3f(1, 1, 1);
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_ */