intravenous

annotate src/sdr.c @ 9:90af225f469a

- merged collision detection code - reduced inertia
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 28 Apr 2012 17:10:29 +0300
parents
children
rev   line source
nuclear@3 1 #include <stdio.h>
nuclear@3 2 #include <stdlib.h>
nuclear@3 3 #include <string.h>
nuclear@3 4 #include <errno.h>
nuclear@3 5 #include <assert.h>
nuclear@3 6 #include <GL/glew.h>
nuclear@3 7
nuclear@3 8 #if defined(unix) || defined(__unix__)
nuclear@3 9 #include <unistd.h>
nuclear@3 10 #include <sys/stat.h>
nuclear@3 11 #endif /* unix */
nuclear@3 12
nuclear@3 13 #include "sdr.h"
nuclear@3 14
nuclear@3 15 unsigned int create_vertex_shader(const char *src)
nuclear@3 16 {
nuclear@3 17 return create_shader(src, GL_VERTEX_SHADER);
nuclear@3 18 }
nuclear@3 19
nuclear@3 20 unsigned int create_pixel_shader(const char *src)
nuclear@3 21 {
nuclear@3 22 return create_shader(src, GL_FRAGMENT_SHADER);
nuclear@3 23 }
nuclear@3 24
nuclear@3 25 unsigned int create_shader(const char *src, unsigned int sdr_type)
nuclear@3 26 {
nuclear@3 27 unsigned int sdr;
nuclear@3 28 int success, info_len;
nuclear@3 29 char *info_str = 0;
nuclear@3 30 GLenum err;
nuclear@3 31
nuclear@3 32 sdr = glCreateShader(sdr_type);
nuclear@3 33 assert(glGetError() == GL_NO_ERROR);
nuclear@3 34 glShaderSource(sdr, 1, &src, 0);
nuclear@3 35 err = glGetError();
nuclear@3 36 assert(err == GL_NO_ERROR);
nuclear@3 37 glCompileShader(sdr);
nuclear@3 38 assert(glGetError() == GL_NO_ERROR);
nuclear@3 39
nuclear@3 40 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
nuclear@3 41 assert(glGetError() == GL_NO_ERROR);
nuclear@3 42 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
nuclear@3 43 assert(glGetError() == GL_NO_ERROR);
nuclear@3 44
nuclear@3 45 if(info_len) {
nuclear@3 46 if((info_str = malloc(info_len + 1))) {
nuclear@3 47 glGetShaderInfoLog(sdr, info_len, 0, info_str);
nuclear@3 48 assert(glGetError() == GL_NO_ERROR);
nuclear@3 49 }
nuclear@3 50 }
nuclear@3 51
nuclear@3 52 if(success) {
nuclear@3 53 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
nuclear@3 54 } else {
nuclear@3 55 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
nuclear@3 56 glDeleteShader(sdr);
nuclear@3 57 sdr = 0;
nuclear@3 58 }
nuclear@3 59
nuclear@3 60 free(info_str);
nuclear@3 61 return sdr;
nuclear@3 62 }
nuclear@3 63
nuclear@3 64 void free_shader(unsigned int sdr)
nuclear@3 65 {
nuclear@3 66 glDeleteShader(sdr);
nuclear@3 67 }
nuclear@3 68
nuclear@3 69 unsigned int load_vertex_shader(const char *fname)
nuclear@3 70 {
nuclear@3 71 return load_shader(fname, GL_VERTEX_SHADER);
nuclear@3 72 }
nuclear@3 73
nuclear@3 74 unsigned int load_pixel_shader(const char *fname)
nuclear@3 75 {
nuclear@3 76 return load_shader(fname, GL_FRAGMENT_SHADER);
nuclear@3 77 }
nuclear@3 78
nuclear@3 79 unsigned int load_shader(const char *fname, unsigned int sdr_type)
nuclear@3 80 {
nuclear@3 81 #if defined(unix) || defined(__unix__)
nuclear@3 82 struct stat st;
nuclear@3 83 #endif
nuclear@3 84 unsigned int sdr;
nuclear@3 85 size_t filesize;
nuclear@3 86 FILE *fp;
nuclear@3 87 char *src;
nuclear@3 88
nuclear@3 89 if(!(fp = fopen(fname, "r"))) {
nuclear@3 90 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
nuclear@3 91 return 0;
nuclear@3 92 }
nuclear@3 93
nuclear@3 94 #if defined(unix) || defined(__unix__)
nuclear@3 95 fstat(fileno(fp), &st);
nuclear@3 96 filesize = st.st_size;
nuclear@3 97 #else
nuclear@3 98 fseek(fp, 0, SEEK_END);
nuclear@3 99 filesize = ftell(fp);
nuclear@3 100 fseek(fp, 0, SEEK_SET);
nuclear@3 101 #endif /* unix */
nuclear@3 102
nuclear@3 103 if(!(src = malloc(filesize + 1))) {
nuclear@3 104 fclose(fp);
nuclear@3 105 return 0;
nuclear@3 106 }
nuclear@3 107 fread(src, 1, filesize, fp);
nuclear@3 108 src[filesize] = 0;
nuclear@3 109 fclose(fp);
nuclear@3 110
nuclear@3 111 fprintf(stderr, "compiling %s shader: %s... ", (sdr_type == GL_VERTEX_SHADER ? "vertex" : "pixel"), fname);
nuclear@3 112 sdr = create_shader(src, sdr_type);
nuclear@3 113
nuclear@3 114 free(src);
nuclear@3 115 return sdr;
nuclear@3 116 }
nuclear@3 117
nuclear@3 118
nuclear@3 119 unsigned int get_vertex_shader(const char *fname)
nuclear@3 120 {
nuclear@3 121 return get_shader(fname, GL_VERTEX_SHADER);
nuclear@3 122 }
nuclear@3 123
nuclear@3 124 unsigned int get_pixel_shader(const char *fname)
nuclear@3 125 {
nuclear@3 126 return get_shader(fname, GL_FRAGMENT_SHADER);
nuclear@3 127 }
nuclear@3 128
nuclear@3 129 unsigned int get_shader(const char *fname, unsigned int sdr_type)
nuclear@3 130 {
nuclear@3 131 unsigned int sdr;
nuclear@3 132 if(!(sdr = load_shader(fname, sdr_type))) {
nuclear@3 133 return 0;
nuclear@3 134 }
nuclear@3 135 return sdr;
nuclear@3 136 }
nuclear@3 137
nuclear@3 138
nuclear@3 139 /* ---- gpu programs ---- */
nuclear@3 140
nuclear@3 141 unsigned int create_program(void)
nuclear@3 142 {
nuclear@3 143 unsigned int prog = glCreateProgram();
nuclear@3 144 assert(glGetError() == GL_NO_ERROR);
nuclear@3 145 return prog;
nuclear@3 146 }
nuclear@3 147
nuclear@3 148 unsigned int create_program_link(unsigned int vs, unsigned int ps)
nuclear@3 149 {
nuclear@3 150 unsigned int prog;
nuclear@3 151
nuclear@3 152 if(!(prog = create_program())) {
nuclear@3 153 return 0;
nuclear@3 154 }
nuclear@3 155
nuclear@3 156 if(vs) {
nuclear@3 157 attach_shader(prog, vs);
nuclear@3 158 assert(glGetError() == GL_NO_ERROR);
nuclear@3 159 }
nuclear@3 160 if(ps) {
nuclear@3 161 attach_shader(prog, ps);
nuclear@3 162 assert(glGetError() == GL_NO_ERROR);
nuclear@3 163 }
nuclear@3 164
nuclear@3 165 if(link_program(prog) == -1) {
nuclear@3 166 free_program(prog);
nuclear@3 167 return 0;
nuclear@3 168 }
nuclear@3 169 return prog;
nuclear@3 170 }
nuclear@3 171
nuclear@3 172 unsigned int create_program_load(const char *vfile, const char *pfile)
nuclear@3 173 {
nuclear@3 174 unsigned int vs = 0, ps = 0;
nuclear@3 175
nuclear@3 176 if(vfile && !(vs = get_vertex_shader(vfile))) {
nuclear@3 177 return 0;
nuclear@3 178 }
nuclear@3 179 if(pfile && !(ps = get_pixel_shader(pfile))) {
nuclear@3 180 return 0;
nuclear@3 181 }
nuclear@3 182 return create_program_link(vs, ps);
nuclear@3 183 }
nuclear@3 184
nuclear@3 185 void free_program(unsigned int sdr)
nuclear@3 186 {
nuclear@3 187 glDeleteProgram(sdr);
nuclear@3 188 }
nuclear@3 189
nuclear@3 190 void attach_shader(unsigned int prog, unsigned int sdr)
nuclear@3 191 {
nuclear@3 192 glAttachShader(prog, sdr);
nuclear@3 193 assert(glGetError() == GL_NO_ERROR);
nuclear@3 194 }
nuclear@3 195
nuclear@3 196 int link_program(unsigned int prog)
nuclear@3 197 {
nuclear@3 198 int linked, info_len, retval = 0;
nuclear@3 199 char *info_str = 0;
nuclear@3 200
nuclear@3 201 glLinkProgram(prog);
nuclear@3 202 assert(glGetError() == GL_NO_ERROR);
nuclear@3 203 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
nuclear@3 204 assert(glGetError() == GL_NO_ERROR);
nuclear@3 205 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
nuclear@3 206 assert(glGetError() == GL_NO_ERROR);
nuclear@3 207
nuclear@3 208 if(info_len) {
nuclear@3 209 if((info_str = malloc(info_len + 1))) {
nuclear@3 210 glGetProgramInfoLog(prog, info_len, 0, info_str);
nuclear@3 211 assert(glGetError() == GL_NO_ERROR);
nuclear@3 212 }
nuclear@3 213 }
nuclear@3 214
nuclear@3 215 if(linked) {
nuclear@3 216 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
nuclear@3 217 } else {
nuclear@3 218 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
nuclear@3 219 retval = -1;
nuclear@3 220 }
nuclear@3 221
nuclear@3 222 free(info_str);
nuclear@3 223 return retval;
nuclear@3 224 }
nuclear@3 225
nuclear@3 226 int bind_program(unsigned int prog)
nuclear@3 227 {
nuclear@3 228 GLenum err;
nuclear@3 229
nuclear@3 230 glUseProgram(prog);
nuclear@3 231 if(prog && (err = glGetError()) != GL_NO_ERROR) {
nuclear@3 232 /* maybe the program is not linked, try linking first */
nuclear@3 233 if(err == GL_INVALID_OPERATION) {
nuclear@3 234 if(link_program(prog) == -1) {
nuclear@3 235 return -1;
nuclear@3 236 }
nuclear@3 237 glUseProgram(prog);
nuclear@3 238 return glGetError() == GL_NO_ERROR ? 0 : -1;
nuclear@3 239 }
nuclear@3 240 return -1;
nuclear@3 241 }
nuclear@3 242 return 0;
nuclear@3 243 }
nuclear@3 244
nuclear@3 245 /* ugly but I'm not going to write the same bloody code over and over */
nuclear@3 246 #define BEGIN_UNIFORM_CODE \
nuclear@3 247 int loc, curr_prog; \
nuclear@3 248 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
nuclear@3 249 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
nuclear@3 250 return -1; \
nuclear@3 251 } \
nuclear@3 252 if((loc = glGetUniformLocation(prog, name)) != -1)
nuclear@3 253
nuclear@3 254 #define END_UNIFORM_CODE \
nuclear@3 255 if((unsigned int)curr_prog != prog) { \
nuclear@3 256 bind_program(curr_prog); \
nuclear@3 257 } \
nuclear@3 258 return loc == -1 ? -1 : 0
nuclear@3 259
nuclear@3 260 int set_uniform_int(unsigned int prog, const char *name, int val)
nuclear@3 261 {
nuclear@3 262 BEGIN_UNIFORM_CODE {
nuclear@3 263 glUniform1i(loc, val);
nuclear@3 264 }
nuclear@3 265 END_UNIFORM_CODE;
nuclear@3 266 }
nuclear@3 267
nuclear@3 268 int set_uniform_float(unsigned int prog, const char *name, float val)
nuclear@3 269 {
nuclear@3 270 BEGIN_UNIFORM_CODE {
nuclear@3 271 glUniform1f(loc, val);
nuclear@3 272 }
nuclear@3 273 END_UNIFORM_CODE;
nuclear@3 274 }
nuclear@3 275
nuclear@3 276 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
nuclear@3 277 {
nuclear@3 278 BEGIN_UNIFORM_CODE {
nuclear@3 279 glUniform3f(loc, x, y, z);
nuclear@3 280 }
nuclear@3 281 END_UNIFORM_CODE;
nuclear@3 282 }
nuclear@3 283
nuclear@3 284 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
nuclear@3 285 {
nuclear@3 286 BEGIN_UNIFORM_CODE {
nuclear@3 287 glUniform4f(loc, x, y, z, w);
nuclear@3 288 }
nuclear@3 289 END_UNIFORM_CODE;
nuclear@3 290 }
nuclear@3 291
nuclear@3 292 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
nuclear@3 293 {
nuclear@3 294 BEGIN_UNIFORM_CODE {
nuclear@3 295 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
nuclear@3 296 }
nuclear@3 297 END_UNIFORM_CODE;
nuclear@3 298 }
nuclear@3 299
nuclear@3 300 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
nuclear@3 301 {
nuclear@3 302 BEGIN_UNIFORM_CODE {
nuclear@3 303 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
nuclear@3 304 }
nuclear@3 305 END_UNIFORM_CODE;
nuclear@3 306 }
nuclear@3 307
nuclear@3 308 int get_attrib_loc(unsigned int prog, const char *name)
nuclear@3 309 {
nuclear@3 310 int loc, curr_prog;
nuclear@3 311
nuclear@3 312 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
nuclear@3 313 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
nuclear@3 314 return -1;
nuclear@3 315 }
nuclear@3 316
nuclear@3 317 loc = glGetAttribLocation(prog, (char*)name);
nuclear@3 318
nuclear@3 319 if((unsigned int)curr_prog != prog) {
nuclear@3 320 bind_program(curr_prog);
nuclear@3 321 }
nuclear@3 322 return loc;
nuclear@3 323 }
nuclear@3 324
nuclear@3 325 void set_attrib_float3(int attr_loc, float x, float y, float z)
nuclear@3 326 {
nuclear@3 327 glVertexAttrib3f(attr_loc, x, y, z);
nuclear@3 328 }