metasurf

diff examples/metaballs/src/sdr.c @ 0:7aa4627e492b

first commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Oct 2011 07:34:31 +0300
parents
children 9ab057fba0c5
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/metaballs/src/sdr.c	Tue Oct 25 07:34:31 2011 +0300
     1.3 @@ -0,0 +1,329 @@
     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 +
   1.136 +	if(!(sdr = load_shader(fname, sdr_type))) {
   1.137 +		return 0;
   1.138 +	}
   1.139 +	return sdr;
   1.140 +}
   1.141 +
   1.142 +
   1.143 +/* ---- gpu programs ---- */
   1.144 +
   1.145 +unsigned int create_program(void)
   1.146 +{
   1.147 +	unsigned int prog = glCreateProgram();
   1.148 +	assert(glGetError() == GL_NO_ERROR);
   1.149 +	return prog;
   1.150 +}
   1.151 +
   1.152 +unsigned int create_program_link(unsigned int vs, unsigned int ps)
   1.153 +{
   1.154 +	unsigned int prog;
   1.155 +
   1.156 +	if(!(prog = create_program())) {
   1.157 +		return 0;
   1.158 +	}
   1.159 +
   1.160 +	if(vs) {
   1.161 +		attach_shader(prog, vs);
   1.162 +		assert(glGetError() == GL_NO_ERROR);
   1.163 +	}
   1.164 +	if(ps) {
   1.165 +		attach_shader(prog, ps);
   1.166 +		assert(glGetError() == GL_NO_ERROR);
   1.167 +	}
   1.168 +
   1.169 +	if(link_program(prog) == -1) {
   1.170 +		free_program(prog);
   1.171 +		return 0;
   1.172 +	}
   1.173 +	return prog;
   1.174 +}
   1.175 +
   1.176 +unsigned int create_program_load(const char *vfile, const char *pfile)
   1.177 +{
   1.178 +	unsigned int vs = 0, ps = 0;
   1.179 +
   1.180 +	if(vfile && !(vs = get_vertex_shader(vfile))) {
   1.181 +		return 0;
   1.182 +	}
   1.183 +	if(pfile && !(ps = get_pixel_shader(pfile))) {
   1.184 +		return 0;
   1.185 +	}
   1.186 +	return create_program_link(vs, ps);
   1.187 +}
   1.188 +
   1.189 +void free_program(unsigned int sdr)
   1.190 +{
   1.191 +	glDeleteProgram(sdr);
   1.192 +}
   1.193 +
   1.194 +void attach_shader(unsigned int prog, unsigned int sdr)
   1.195 +{
   1.196 +	glAttachShader(prog, sdr);
   1.197 +	assert(glGetError() == GL_NO_ERROR);
   1.198 +}
   1.199 +
   1.200 +int link_program(unsigned int prog)
   1.201 +{
   1.202 +	int linked, info_len, retval = 0;
   1.203 +	char *info_str = 0;
   1.204 +
   1.205 +	glLinkProgram(prog);
   1.206 +	assert(glGetError() == GL_NO_ERROR);
   1.207 +	glGetProgramiv(prog, GL_LINK_STATUS, &linked);
   1.208 +	assert(glGetError() == GL_NO_ERROR);
   1.209 +	glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
   1.210 +	assert(glGetError() == GL_NO_ERROR);
   1.211 +
   1.212 +	if(info_len) {
   1.213 +		if((info_str = malloc(info_len + 1))) {
   1.214 +			glGetProgramInfoLog(prog, info_len, 0, info_str);
   1.215 +			assert(glGetError() == GL_NO_ERROR);
   1.216 +		}
   1.217 +	}
   1.218 +
   1.219 +	if(linked) {
   1.220 +		fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
   1.221 +	} else {
   1.222 +		fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
   1.223 +		retval = -1;
   1.224 +	}
   1.225 +
   1.226 +	free(info_str);
   1.227 +	return retval;
   1.228 +}
   1.229 +
   1.230 +int bind_program(unsigned int prog)
   1.231 +{
   1.232 +	GLenum err;
   1.233 +
   1.234 +	glUseProgram(prog);
   1.235 +	if(prog && (err = glGetError()) != GL_NO_ERROR) {
   1.236 +		/* maybe the program is not linked, try linking first */
   1.237 +		if(err == GL_INVALID_OPERATION) {
   1.238 +			if(link_program(prog) == -1) {
   1.239 +				return -1;
   1.240 +			}
   1.241 +			glUseProgram(prog);
   1.242 +			return glGetError() == GL_NO_ERROR ? 0 : -1;
   1.243 +		}
   1.244 +		return -1;
   1.245 +	}
   1.246 +	return 0;
   1.247 +}
   1.248 +
   1.249 +/* ugly but I'm not going to write the same bloody code over and over */
   1.250 +#define BEGIN_UNIFORM_CODE \
   1.251 +	int loc, curr_prog; \
   1.252 +	glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
   1.253 +	if(curr_prog != prog && bind_program(prog) == -1) { \
   1.254 +		return -1; \
   1.255 +	} \
   1.256 +	if((loc = glGetUniformLocation(prog, name)) != -1)
   1.257 +
   1.258 +#define END_UNIFORM_CODE \
   1.259 +	if(curr_prog != prog) { \
   1.260 +		bind_program(curr_prog); \
   1.261 +	} \
   1.262 +	return loc == -1 ? -1 : 0
   1.263 +
   1.264 +int set_uniform_int(unsigned int prog, const char *name, int val)
   1.265 +{
   1.266 +	BEGIN_UNIFORM_CODE {
   1.267 +		glUniform1i(loc, val);
   1.268 +	}
   1.269 +	END_UNIFORM_CODE;
   1.270 +}
   1.271 +
   1.272 +int set_uniform_float(unsigned int prog, const char *name, float val)
   1.273 +{
   1.274 +	BEGIN_UNIFORM_CODE {
   1.275 +		glUniform1f(loc, val);
   1.276 +	}
   1.277 +	END_UNIFORM_CODE;
   1.278 +}
   1.279 +
   1.280 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
   1.281 +{
   1.282 +	BEGIN_UNIFORM_CODE {
   1.283 +		glUniform3f(loc, x, y, z);
   1.284 +	}
   1.285 +	END_UNIFORM_CODE;
   1.286 +}
   1.287 +
   1.288 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
   1.289 +{
   1.290 +	BEGIN_UNIFORM_CODE {
   1.291 +		glUniform4f(loc, x, y, z, w);
   1.292 +	}
   1.293 +	END_UNIFORM_CODE;
   1.294 +}
   1.295 +
   1.296 +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
   1.297 +{
   1.298 +	BEGIN_UNIFORM_CODE {
   1.299 +		glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
   1.300 +	}
   1.301 +	END_UNIFORM_CODE;
   1.302 +}
   1.303 +
   1.304 +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
   1.305 +{
   1.306 +	BEGIN_UNIFORM_CODE {
   1.307 +		glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
   1.308 +	}
   1.309 +	END_UNIFORM_CODE;
   1.310 +}
   1.311 +
   1.312 +int get_attrib_loc(unsigned int prog, const char *name)
   1.313 +{
   1.314 +	int loc, curr_prog;
   1.315 +
   1.316 +	glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
   1.317 +	if(curr_prog != prog && bind_program(prog) == -1) {
   1.318 +		return -1;
   1.319 +	}
   1.320 +
   1.321 +	loc = glGetAttribLocation(prog, (char*)name);
   1.322 +
   1.323 +	if(curr_prog != prog) {
   1.324 +		bind_program(curr_prog);
   1.325 +	}
   1.326 +	return loc;
   1.327 +}
   1.328 +
   1.329 +void set_attrib_float3(int attr_loc, float x, float y, float z)
   1.330 +{
   1.331 +	glVertexAttrib3f(attr_loc, x, y, z);
   1.332 +}