dungeon_crawler
diff prototype/src/sdr.c @ 15:3a3236a4833c
adding shaders and renderer abstraction
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 19 Aug 2012 23:09:30 +0300 |
parents | |
children | d98240a13793 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/prototype/src/sdr.c Sun Aug 19 23:09:30 2012 +0300 1.3 @@ -0,0 +1,328 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include <assert.h> 1.9 +#include <GL/glew.h> 1.10 + 1.11 +#if defined(unix) || defined(__unix__) 1.12 +#include <unistd.h> 1.13 +#include <sys/stat.h> 1.14 +#endif /* unix */ 1.15 + 1.16 +#include "sdr.h" 1.17 + 1.18 +unsigned int create_vertex_shader(const char *src) 1.19 +{ 1.20 + return create_shader(src, GL_VERTEX_SHADER); 1.21 +} 1.22 + 1.23 +unsigned int create_pixel_shader(const char *src) 1.24 +{ 1.25 + return create_shader(src, GL_FRAGMENT_SHADER); 1.26 +} 1.27 + 1.28 +unsigned int create_shader(const char *src, unsigned int sdr_type) 1.29 +{ 1.30 + unsigned int sdr; 1.31 + int success, info_len; 1.32 + char *info_str = 0; 1.33 + GLenum err; 1.34 + 1.35 + sdr = glCreateShader(sdr_type); 1.36 + assert(glGetError() == GL_NO_ERROR); 1.37 + glShaderSource(sdr, 1, &src, 0); 1.38 + err = glGetError(); 1.39 + assert(err == GL_NO_ERROR); 1.40 + glCompileShader(sdr); 1.41 + assert(glGetError() == GL_NO_ERROR); 1.42 + 1.43 + glGetShaderiv(sdr, GL_COMPILE_STATUS, &success); 1.44 + assert(glGetError() == GL_NO_ERROR); 1.45 + glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len); 1.46 + assert(glGetError() == GL_NO_ERROR); 1.47 + 1.48 + if(info_len) { 1.49 + if((info_str = malloc(info_len + 1))) { 1.50 + glGetShaderInfoLog(sdr, info_len, 0, info_str); 1.51 + assert(glGetError() == GL_NO_ERROR); 1.52 + } 1.53 + } 1.54 + 1.55 + if(success) { 1.56 + fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str); 1.57 + } else { 1.58 + fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str); 1.59 + glDeleteShader(sdr); 1.60 + sdr = 0; 1.61 + } 1.62 + 1.63 + free(info_str); 1.64 + return sdr; 1.65 +} 1.66 + 1.67 +void free_shader(unsigned int sdr) 1.68 +{ 1.69 + glDeleteShader(sdr); 1.70 +} 1.71 + 1.72 +unsigned int load_vertex_shader(const char *fname) 1.73 +{ 1.74 + return load_shader(fname, GL_VERTEX_SHADER); 1.75 +} 1.76 + 1.77 +unsigned int load_pixel_shader(const char *fname) 1.78 +{ 1.79 + return load_shader(fname, GL_FRAGMENT_SHADER); 1.80 +} 1.81 + 1.82 +unsigned int load_shader(const char *fname, unsigned int sdr_type) 1.83 +{ 1.84 +#if defined(unix) || defined(__unix__) 1.85 + struct stat st; 1.86 +#endif 1.87 + unsigned int sdr; 1.88 + size_t filesize; 1.89 + FILE *fp; 1.90 + char *src; 1.91 + 1.92 + if(!(fp = fopen(fname, "r"))) { 1.93 + fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno)); 1.94 + return 0; 1.95 + } 1.96 + 1.97 +#if defined(unix) || defined(__unix__) 1.98 + fstat(fileno(fp), &st); 1.99 + filesize = st.st_size; 1.100 +#else 1.101 + fseek(fp, 0, SEEK_END); 1.102 + filesize = ftell(fp); 1.103 + fseek(fp, 0, SEEK_SET); 1.104 +#endif /* unix */ 1.105 + 1.106 + if(!(src = malloc(filesize + 1))) { 1.107 + fclose(fp); 1.108 + return 0; 1.109 + } 1.110 + fread(src, 1, filesize, fp); 1.111 + src[filesize] = 0; 1.112 + fclose(fp); 1.113 + 1.114 + fprintf(stderr, "compiling %s shader: %s... ", (sdr_type == GL_VERTEX_SHADER ? "vertex" : "pixel"), fname); 1.115 + sdr = create_shader(src, sdr_type); 1.116 + 1.117 + free(src); 1.118 + return sdr; 1.119 +} 1.120 + 1.121 + 1.122 +unsigned int get_vertex_shader(const char *fname) 1.123 +{ 1.124 + return get_shader(fname, GL_VERTEX_SHADER); 1.125 +} 1.126 + 1.127 +unsigned int get_pixel_shader(const char *fname) 1.128 +{ 1.129 + return get_shader(fname, GL_FRAGMENT_SHADER); 1.130 +} 1.131 + 1.132 +unsigned int get_shader(const char *fname, unsigned int sdr_type) 1.133 +{ 1.134 + unsigned int sdr; 1.135 + if(!(sdr = load_shader(fname, sdr_type))) { 1.136 + return 0; 1.137 + } 1.138 + return sdr; 1.139 +} 1.140 + 1.141 + 1.142 +/* ---- gpu programs ---- */ 1.143 + 1.144 +unsigned int create_program(void) 1.145 +{ 1.146 + unsigned int prog = glCreateProgram(); 1.147 + assert(glGetError() == GL_NO_ERROR); 1.148 + return prog; 1.149 +} 1.150 + 1.151 +unsigned int create_program_link(unsigned int vs, unsigned int ps) 1.152 +{ 1.153 + unsigned int prog; 1.154 + 1.155 + if(!(prog = create_program())) { 1.156 + return 0; 1.157 + } 1.158 + 1.159 + if(vs) { 1.160 + attach_shader(prog, vs); 1.161 + assert(glGetError() == GL_NO_ERROR); 1.162 + } 1.163 + if(ps) { 1.164 + attach_shader(prog, ps); 1.165 + assert(glGetError() == GL_NO_ERROR); 1.166 + } 1.167 + 1.168 + if(link_program(prog) == -1) { 1.169 + free_program(prog); 1.170 + return 0; 1.171 + } 1.172 + return prog; 1.173 +} 1.174 + 1.175 +unsigned int create_program_load(const char *vfile, const char *pfile) 1.176 +{ 1.177 + unsigned int vs = 0, ps = 0; 1.178 + 1.179 + if(vfile && !(vs = get_vertex_shader(vfile))) { 1.180 + return 0; 1.181 + } 1.182 + if(pfile && !(ps = get_pixel_shader(pfile))) { 1.183 + return 0; 1.184 + } 1.185 + return create_program_link(vs, ps); 1.186 +} 1.187 + 1.188 +void free_program(unsigned int sdr) 1.189 +{ 1.190 + glDeleteProgram(sdr); 1.191 +} 1.192 + 1.193 +void attach_shader(unsigned int prog, unsigned int sdr) 1.194 +{ 1.195 + glAttachShader(prog, sdr); 1.196 + assert(glGetError() == GL_NO_ERROR); 1.197 +} 1.198 + 1.199 +int link_program(unsigned int prog) 1.200 +{ 1.201 + int linked, info_len, retval = 0; 1.202 + char *info_str = 0; 1.203 + 1.204 + glLinkProgram(prog); 1.205 + assert(glGetError() == GL_NO_ERROR); 1.206 + glGetProgramiv(prog, GL_LINK_STATUS, &linked); 1.207 + assert(glGetError() == GL_NO_ERROR); 1.208 + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len); 1.209 + assert(glGetError() == GL_NO_ERROR); 1.210 + 1.211 + if(info_len) { 1.212 + if((info_str = malloc(info_len + 1))) { 1.213 + glGetProgramInfoLog(prog, info_len, 0, info_str); 1.214 + assert(glGetError() == GL_NO_ERROR); 1.215 + } 1.216 + } 1.217 + 1.218 + if(linked) { 1.219 + fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str); 1.220 + } else { 1.221 + fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str); 1.222 + retval = -1; 1.223 + } 1.224 + 1.225 + free(info_str); 1.226 + return retval; 1.227 +} 1.228 + 1.229 +int bind_program(unsigned int prog) 1.230 +{ 1.231 + GLenum err; 1.232 + 1.233 + glUseProgram(prog); 1.234 + if(prog && (err = glGetError()) != GL_NO_ERROR) { 1.235 + /* maybe the program is not linked, try linking first */ 1.236 + if(err == GL_INVALID_OPERATION) { 1.237 + if(link_program(prog) == -1) { 1.238 + return -1; 1.239 + } 1.240 + glUseProgram(prog); 1.241 + return glGetError() == GL_NO_ERROR ? 0 : -1; 1.242 + } 1.243 + return -1; 1.244 + } 1.245 + return 0; 1.246 +} 1.247 + 1.248 +/* ugly but I'm not going to write the same bloody code over and over */ 1.249 +#define BEGIN_UNIFORM_CODE \ 1.250 + int loc, curr_prog; \ 1.251 + glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \ 1.252 + if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \ 1.253 + return -1; \ 1.254 + } \ 1.255 + if((loc = glGetUniformLocation(prog, name)) != -1) 1.256 + 1.257 +#define END_UNIFORM_CODE \ 1.258 + if((unsigned int)curr_prog != prog) { \ 1.259 + bind_program(curr_prog); \ 1.260 + } \ 1.261 + return loc == -1 ? -1 : 0 1.262 + 1.263 +int set_uniform_int(unsigned int prog, const char *name, int val) 1.264 +{ 1.265 + BEGIN_UNIFORM_CODE { 1.266 + glUniform1i(loc, val); 1.267 + } 1.268 + END_UNIFORM_CODE; 1.269 +} 1.270 + 1.271 +int set_uniform_float(unsigned int prog, const char *name, float val) 1.272 +{ 1.273 + BEGIN_UNIFORM_CODE { 1.274 + glUniform1f(loc, val); 1.275 + } 1.276 + END_UNIFORM_CODE; 1.277 +} 1.278 + 1.279 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z) 1.280 +{ 1.281 + BEGIN_UNIFORM_CODE { 1.282 + glUniform3f(loc, x, y, z); 1.283 + } 1.284 + END_UNIFORM_CODE; 1.285 +} 1.286 + 1.287 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w) 1.288 +{ 1.289 + BEGIN_UNIFORM_CODE { 1.290 + glUniform4f(loc, x, y, z, w); 1.291 + } 1.292 + END_UNIFORM_CODE; 1.293 +} 1.294 + 1.295 +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat) 1.296 +{ 1.297 + BEGIN_UNIFORM_CODE { 1.298 + glUniformMatrix4fv(loc, 1, GL_FALSE, mat); 1.299 + } 1.300 + END_UNIFORM_CODE; 1.301 +} 1.302 + 1.303 +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat) 1.304 +{ 1.305 + BEGIN_UNIFORM_CODE { 1.306 + glUniformMatrix4fv(loc, 1, GL_TRUE, mat); 1.307 + } 1.308 + END_UNIFORM_CODE; 1.309 +} 1.310 + 1.311 +int get_attrib_loc(unsigned int prog, const char *name) 1.312 +{ 1.313 + int loc, curr_prog; 1.314 + 1.315 + glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); 1.316 + if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { 1.317 + return -1; 1.318 + } 1.319 + 1.320 + loc = glGetAttribLocation(prog, (char*)name); 1.321 + 1.322 + if((unsigned int)curr_prog != prog) { 1.323 + bind_program(curr_prog); 1.324 + } 1.325 + return loc; 1.326 +} 1.327 + 1.328 +void set_attrib_float3(int attr_loc, float x, float y, float z) 1.329 +{ 1.330 + glVertexAttrib3f(attr_loc, x, y, z); 1.331 +}