dbf-udg

changeset 0:2d27bfd21fc5

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 08 Jan 2013 03:16:48 +0200
parents
children 1d5dc834d403
files .hgignore Makefile sdr/dither.p.glsl sdr/dither.v.glsl src/dither_matrix.h src/sdr.c src/sdr.h src/udg.cc
diffstat 8 files changed, 882 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Tue Jan 08 03:16:48 2013 +0200
     1.3 @@ -0,0 +1,4 @@
     1.4 +\.o$
     1.5 +\.d$
     1.6 +\.swp$
     1.7 +^udg$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Tue Jan 08 03:16:48 2013 +0200
     2.3 @@ -0,0 +1,30 @@
     2.4 +csrc = $(wildcard src/*.c)
     2.5 +ccsrc = $(wildcard src/*.cc)
     2.6 +obj = $(csrc:.c=.o) $(ccsrc:.cc=.o)
     2.7 +dep = $(obj:.o=.d)
     2.8 +bin = udg
     2.9 +
    2.10 +CFLAGS = -pedantic -Wall -g
    2.11 +CXXFLAGS = -std=c++11 $(CFLAGS)
    2.12 +LDFLAGS = $(libgl) -limago
    2.13 +
    2.14 +ifeq ($(shell uname -s), Darwin)
    2.15 +	libgl = -framework OpenGL -framework GLUT -lGLEW
    2.16 +else
    2.17 +	libgl = -lGL -lGLU -lglut -lGLEW
    2.18 +endif
    2.19 +
    2.20 +$(bin): $(obj)
    2.21 +	$(CXX) -o $@ $(obj) $(LDFLAGS)
    2.22 +
    2.23 +-include $(dep)
    2.24 +
    2.25 +%.d: %.c
    2.26 +	@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.27 +
    2.28 +%.d: %.cc
    2.29 +	@$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.30 +
    2.31 +.PHONY: clean
    2.32 +clean:
    2.33 +	rm -f $(obj) $(bin) $(dep)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/sdr/dither.p.glsl	Tue Jan 08 03:16:48 2013 +0200
     3.3 @@ -0,0 +1,22 @@
     3.4 +uniform sampler2D framebuf, dither_tex;
     3.5 +uniform int dither_levels, dither_size;
     3.6 +
     3.7 +void main()
     3.8 +{
     3.9 +	float levels = float(dither_levels);
    3.10 +
    3.11 +	vec4 pixel = texture2D(framebuf, gl_TexCoord[0].xy);
    3.12 +	float lum = dot(pixel.xyz, vec3(0.2126, 0.7152, 0.0722));
    3.13 +	float coord_shift = floor(lum * levels) / levels;
    3.14 +
    3.15 +	vec2 dsz2 = vec2(float(dither_size), float(dither_size));
    3.16 +
    3.17 +	vec2 coord = mod(gl_FragCoord.xy, dsz2) / float(dither_size);
    3.18 +	coord.y = coord.y / levels + coord_shift;
    3.19 +	float val = texture2D(dither_tex, coord).x;
    3.20 +
    3.21 +	const vec3 dark_col = vec3(0.043, 0.286, 0.337);
    3.22 +	const vec3 bright_col = vec3(0.965, 0.778, 0.555);
    3.23 +
    3.24 +	gl_FragColor = vec4(mix(dark_col, bright_col, val), 1.0);
    3.25 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/sdr/dither.v.glsl	Tue Jan 08 03:16:48 2013 +0200
     4.3 @@ -0,0 +1,5 @@
     4.4 +void main()
     4.5 +{
     4.6 +	gl_Position = ftransform();
     4.7 +	gl_TexCoord[0] = gl_MultiTexCoord0;
     4.8 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/dither_matrix.h	Tue Jan 08 03:16:48 2013 +0200
     5.3 @@ -0,0 +1,26 @@
     5.4 +#ifndef DITHER_MATRIX_H_
     5.5 +#define DITHER_MATRIX_H_
     5.6 +
     5.7 +static const float dither_matrix2[2][2] = {
     5.8 +	{0, 3}, {2, 1}
     5.9 +};
    5.10 +
    5.11 +static const float dither_matrix4[4][4] = {
    5.12 +	{0, 12, 3, 15},
    5.13 +	{8, 4, 11, 7},
    5.14 +	{2, 14, 1, 13},
    5.15 +	{10, 6, 9, 5}
    5.16 +};
    5.17 +
    5.18 +static const float dither_matrix8[8][8] = {
    5.19 +	{0, 48, 12, 60, 3, 51, 15, 63},
    5.20 +	{32, 16, 44, 28, 35, 19, 47, 31},
    5.21 +	{8, 56, 4, 52, 11, 59, 7, 55},
    5.22 +	{40, 24, 36, 20, 43, 27, 39, 23},
    5.23 +	{2, 50, 14, 62, 1, 49, 13, 61},
    5.24 +	{34, 18, 46, 30, 33, 17, 45, 29},
    5.25 +	{10, 58, 6, 54, 9, 57, 5, 53},
    5.26 +	{42, 26, 38, 22, 41, 25, 37, 21}
    5.27 +};
    5.28 +
    5.29 +#endif	/* DITHER_MATRIX_H_ */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/sdr.c	Tue Jan 08 03:16:48 2013 +0200
     6.3 @@ -0,0 +1,410 @@
     6.4 +#include <stdio.h>
     6.5 +#include <stdlib.h>
     6.6 +#include <string.h>
     6.7 +#include <errno.h>
     6.8 +#include <stdarg.h>
     6.9 +#include <assert.h>
    6.10 +#include <GL/glew.h>
    6.11 +
    6.12 +#if defined(unix) || defined(__unix__)
    6.13 +#include <unistd.h>
    6.14 +#include <sys/stat.h>
    6.15 +#endif	/* unix */
    6.16 +
    6.17 +#include "sdr.h"
    6.18 +
    6.19 +static const char *sdrtypestr(unsigned int sdrtype);
    6.20 +
    6.21 +unsigned int create_vertex_shader(const char *src)
    6.22 +{
    6.23 +	return create_shader(src, GL_VERTEX_SHADER);
    6.24 +}
    6.25 +
    6.26 +unsigned int create_pixel_shader(const char *src)
    6.27 +{
    6.28 +	return create_shader(src, GL_FRAGMENT_SHADER);
    6.29 +}
    6.30 +
    6.31 +unsigned int create_tessctl_shader(const char *src)
    6.32 +{
    6.33 +	return create_shader(src, GL_TESS_CONTROL_SHADER);
    6.34 +}
    6.35 +
    6.36 +unsigned int create_tesseval_shader(const char *src)
    6.37 +{
    6.38 +	return create_shader(src, GL_TESS_EVALUATION_SHADER);
    6.39 +}
    6.40 +
    6.41 +unsigned int create_geometry_shader(const char *src)
    6.42 +{
    6.43 +	return create_shader(src, GL_GEOMETRY_SHADER);
    6.44 +}
    6.45 +
    6.46 +unsigned int create_shader(const char *src, unsigned int sdr_type)
    6.47 +{
    6.48 +	unsigned int sdr;
    6.49 +	int success, info_len;
    6.50 +	char *info_str = 0;
    6.51 +	GLenum err;
    6.52 +
    6.53 +	sdr = glCreateShader(sdr_type);
    6.54 +	assert(glGetError() == GL_NO_ERROR);
    6.55 +	glShaderSource(sdr, 1, &src, 0);
    6.56 +	err = glGetError();
    6.57 +	assert(err == GL_NO_ERROR);
    6.58 +	glCompileShader(sdr);
    6.59 +	assert(glGetError() == GL_NO_ERROR);
    6.60 +
    6.61 +	glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
    6.62 +	assert(glGetError() == GL_NO_ERROR);
    6.63 +	glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
    6.64 +	assert(glGetError() == GL_NO_ERROR);
    6.65 +
    6.66 +	if(info_len) {
    6.67 +		if((info_str = malloc(info_len + 1))) {
    6.68 +			glGetShaderInfoLog(sdr, info_len, 0, info_str);
    6.69 +			assert(glGetError() == GL_NO_ERROR);
    6.70 +		}
    6.71 +	}
    6.72 +
    6.73 +	if(success) {
    6.74 +		fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
    6.75 +	} else {
    6.76 +		fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
    6.77 +		glDeleteShader(sdr);
    6.78 +		sdr = 0;
    6.79 +	}
    6.80 +
    6.81 +	free(info_str);
    6.82 +	return sdr;
    6.83 +}
    6.84 +
    6.85 +void free_shader(unsigned int sdr)
    6.86 +{
    6.87 +	glDeleteShader(sdr);
    6.88 +}
    6.89 +
    6.90 +unsigned int load_vertex_shader(const char *fname)
    6.91 +{
    6.92 +	return load_shader(fname, GL_VERTEX_SHADER);
    6.93 +}
    6.94 +
    6.95 +unsigned int load_pixel_shader(const char *fname)
    6.96 +{
    6.97 +	return load_shader(fname, GL_FRAGMENT_SHADER);
    6.98 +}
    6.99 +
   6.100 +unsigned int load_tessctl_shader(const char *fname)
   6.101 +{
   6.102 +	return load_shader(fname, GL_TESS_CONTROL_SHADER);
   6.103 +}
   6.104 +
   6.105 +unsigned int load_tesseval_shader(const char *fname)
   6.106 +{
   6.107 +	return load_shader(fname, GL_TESS_EVALUATION_SHADER);
   6.108 +}
   6.109 +
   6.110 +unsigned int load_geometry_shader(const char *fname)
   6.111 +{
   6.112 +	return load_shader(fname, GL_GEOMETRY_SHADER);
   6.113 +}
   6.114 +
   6.115 +unsigned int load_shader(const char *fname, unsigned int sdr_type)
   6.116 +{
   6.117 +#if defined(unix) || defined(__unix__)
   6.118 +	struct stat st;
   6.119 +#endif
   6.120 +	unsigned int sdr;
   6.121 +	size_t filesize;
   6.122 +	FILE *fp;
   6.123 +	char *src;
   6.124 +
   6.125 +	if(!(fp = fopen(fname, "r"))) {
   6.126 +		fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
   6.127 +		return 0;
   6.128 +	}
   6.129 +
   6.130 +#if defined(unix) || defined(__unix__)
   6.131 +	fstat(fileno(fp), &st);
   6.132 +	filesize = st.st_size;
   6.133 +#else
   6.134 +	fseek(fp, 0, SEEK_END);
   6.135 +	filesize = ftell(fp);
   6.136 +	fseek(fp, 0, SEEK_SET);
   6.137 +#endif	/* unix */
   6.138 +
   6.139 +	if(!(src = malloc(filesize + 1))) {
   6.140 +		fclose(fp);
   6.141 +		return 0;
   6.142 +	}
   6.143 +	fread(src, 1, filesize, fp);
   6.144 +	src[filesize] = 0;
   6.145 +	fclose(fp);
   6.146 +
   6.147 +	fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
   6.148 +	sdr = create_shader(src, sdr_type);
   6.149 +
   6.150 +	free(src);
   6.151 +	return sdr;
   6.152 +}
   6.153 +
   6.154 +
   6.155 +unsigned int get_vertex_shader(const char *fname)
   6.156 +{
   6.157 +	return get_shader(fname, GL_VERTEX_SHADER);
   6.158 +}
   6.159 +
   6.160 +unsigned int get_pixel_shader(const char *fname)
   6.161 +{
   6.162 +	return get_shader(fname, GL_FRAGMENT_SHADER);
   6.163 +}
   6.164 +
   6.165 +unsigned int get_tessctl_shader(const char *fname)
   6.166 +{
   6.167 +	return get_shader(fname, GL_TESS_CONTROL_SHADER);
   6.168 +}
   6.169 +
   6.170 +unsigned int get_tesseval_shader(const char *fname)
   6.171 +{
   6.172 +	return get_shader(fname, GL_TESS_EVALUATION_SHADER);
   6.173 +}
   6.174 +
   6.175 +unsigned int get_geometry_shader(const char *fname)
   6.176 +{
   6.177 +	return get_shader(fname, GL_GEOMETRY_SHADER);
   6.178 +}
   6.179 +
   6.180 +unsigned int get_shader(const char *fname, unsigned int sdr_type)
   6.181 +{
   6.182 +	unsigned int sdr;
   6.183 +	if(!(sdr = load_shader(fname, sdr_type))) {
   6.184 +		return 0;
   6.185 +	}
   6.186 +	return sdr;
   6.187 +}
   6.188 +
   6.189 +
   6.190 +/* ---- gpu programs ---- */
   6.191 +
   6.192 +unsigned int create_program(void)
   6.193 +{
   6.194 +	unsigned int prog = glCreateProgram();
   6.195 +	assert(glGetError() == GL_NO_ERROR);
   6.196 +	return prog;
   6.197 +}
   6.198 +
   6.199 +unsigned int create_program_link(unsigned int sdr0, ...)
   6.200 +{
   6.201 +	unsigned int prog, sdr;
   6.202 +	va_list ap;
   6.203 +
   6.204 +	if(!(prog = create_program())) {
   6.205 +		return 0;
   6.206 +	}
   6.207 +
   6.208 +	attach_shader(prog, sdr0);
   6.209 +	if(glGetError()) {
   6.210 +		return 0;
   6.211 +	}
   6.212 +
   6.213 +	va_start(ap, sdr0);
   6.214 +	while((sdr = va_arg(ap, unsigned int))) {
   6.215 +		attach_shader(prog, sdr);
   6.216 +		if(glGetError()) {
   6.217 +			return 0;
   6.218 +		}
   6.219 +	}
   6.220 +	va_end(ap);
   6.221 +
   6.222 +	if(link_program(prog) == -1) {
   6.223 +		free_program(prog);
   6.224 +		return 0;
   6.225 +	}
   6.226 +	return prog;
   6.227 +}
   6.228 +
   6.229 +unsigned int create_program_load(const char *vfile, const char *pfile)
   6.230 +{
   6.231 +	unsigned int vs = 0, ps = 0;
   6.232 +
   6.233 +	if(vfile && *vfile && !(vs = get_vertex_shader(vfile))) {
   6.234 +		return 0;
   6.235 +	}
   6.236 +	if(pfile && *pfile && !(ps = get_pixel_shader(pfile))) {
   6.237 +		return 0;
   6.238 +	}
   6.239 +	return create_program_link(vs, ps, 0);
   6.240 +}
   6.241 +
   6.242 +void free_program(unsigned int sdr)
   6.243 +{
   6.244 +	glDeleteProgram(sdr);
   6.245 +}
   6.246 +
   6.247 +void attach_shader(unsigned int prog, unsigned int sdr)
   6.248 +{
   6.249 +	glAttachShader(prog, sdr);
   6.250 +	assert(glGetError() == GL_NO_ERROR);
   6.251 +}
   6.252 +
   6.253 +int link_program(unsigned int prog)
   6.254 +{
   6.255 +	int linked, info_len, retval = 0;
   6.256 +	char *info_str = 0;
   6.257 +
   6.258 +	glLinkProgram(prog);
   6.259 +	assert(glGetError() == GL_NO_ERROR);
   6.260 +	glGetProgramiv(prog, GL_LINK_STATUS, &linked);
   6.261 +	assert(glGetError() == GL_NO_ERROR);
   6.262 +	glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
   6.263 +	assert(glGetError() == GL_NO_ERROR);
   6.264 +
   6.265 +	if(info_len) {
   6.266 +		if((info_str = malloc(info_len + 1))) {
   6.267 +			glGetProgramInfoLog(prog, info_len, 0, info_str);
   6.268 +			assert(glGetError() == GL_NO_ERROR);
   6.269 +		}
   6.270 +	}
   6.271 +
   6.272 +	if(linked) {
   6.273 +		fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
   6.274 +	} else {
   6.275 +		fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
   6.276 +		retval = -1;
   6.277 +	}
   6.278 +
   6.279 +	free(info_str);
   6.280 +	return retval;
   6.281 +}
   6.282 +
   6.283 +int bind_program(unsigned int prog)
   6.284 +{
   6.285 +	GLenum err;
   6.286 +
   6.287 +	glUseProgram(prog);
   6.288 +	if(prog && (err = glGetError()) != GL_NO_ERROR) {
   6.289 +		/* maybe the program is not linked, try linking first */
   6.290 +		if(err == GL_INVALID_OPERATION) {
   6.291 +			if(link_program(prog) == -1) {
   6.292 +				return -1;
   6.293 +			}
   6.294 +			glUseProgram(prog);
   6.295 +			return glGetError() == GL_NO_ERROR ? 0 : -1;
   6.296 +		}
   6.297 +		return -1;
   6.298 +	}
   6.299 +	return 0;
   6.300 +}
   6.301 +
   6.302 +/* ugly but I'm not going to write the same bloody code over and over */
   6.303 +#define BEGIN_UNIFORM_CODE \
   6.304 +	int loc, curr_prog; \
   6.305 +	glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
   6.306 +	if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
   6.307 +		return -1; \
   6.308 +	} \
   6.309 +	if((loc = glGetUniformLocation(prog, name)) != -1)
   6.310 +
   6.311 +#define END_UNIFORM_CODE \
   6.312 +	if((unsigned int)curr_prog != prog) { \
   6.313 +		bind_program(curr_prog); \
   6.314 +	} \
   6.315 +	return loc == -1 ? -1 : 0
   6.316 +
   6.317 +int set_uniform_int(unsigned int prog, const char *name, int val)
   6.318 +{
   6.319 +	BEGIN_UNIFORM_CODE {
   6.320 +		glUniform1i(loc, val);
   6.321 +	}
   6.322 +	END_UNIFORM_CODE;
   6.323 +}
   6.324 +
   6.325 +int set_uniform_float(unsigned int prog, const char *name, float val)
   6.326 +{
   6.327 +	BEGIN_UNIFORM_CODE {
   6.328 +		glUniform1f(loc, val);
   6.329 +	}
   6.330 +	END_UNIFORM_CODE;
   6.331 +}
   6.332 +
   6.333 +int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
   6.334 +{
   6.335 +	BEGIN_UNIFORM_CODE {
   6.336 +		glUniform2f(loc, x, y);
   6.337 +	}
   6.338 +	END_UNIFORM_CODE;
   6.339 +}
   6.340 +
   6.341 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
   6.342 +{
   6.343 +	BEGIN_UNIFORM_CODE {
   6.344 +		glUniform3f(loc, x, y, z);
   6.345 +	}
   6.346 +	END_UNIFORM_CODE;
   6.347 +}
   6.348 +
   6.349 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
   6.350 +{
   6.351 +	BEGIN_UNIFORM_CODE {
   6.352 +		glUniform4f(loc, x, y, z, w);
   6.353 +	}
   6.354 +	END_UNIFORM_CODE;
   6.355 +}
   6.356 +
   6.357 +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
   6.358 +{
   6.359 +	BEGIN_UNIFORM_CODE {
   6.360 +		glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
   6.361 +	}
   6.362 +	END_UNIFORM_CODE;
   6.363 +}
   6.364 +
   6.365 +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
   6.366 +{
   6.367 +	BEGIN_UNIFORM_CODE {
   6.368 +		glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
   6.369 +	}
   6.370 +	END_UNIFORM_CODE;
   6.371 +}
   6.372 +
   6.373 +int get_attrib_loc(unsigned int prog, const char *name)
   6.374 +{
   6.375 +	int loc, curr_prog;
   6.376 +
   6.377 +	glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
   6.378 +	if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
   6.379 +		return -1;
   6.380 +	}
   6.381 +
   6.382 +	loc = glGetAttribLocation(prog, (char*)name);
   6.383 +
   6.384 +	if((unsigned int)curr_prog != prog) {
   6.385 +		bind_program(curr_prog);
   6.386 +	}
   6.387 +	return loc;
   6.388 +}
   6.389 +
   6.390 +void set_attrib_float3(int attr_loc, float x, float y, float z)
   6.391 +{
   6.392 +	glVertexAttrib3f(attr_loc, x, y, z);
   6.393 +}
   6.394 +
   6.395 +static const char *sdrtypestr(unsigned int sdrtype)
   6.396 +{
   6.397 +	switch(sdrtype) {
   6.398 +	case GL_VERTEX_SHADER:
   6.399 +		return "vertex";
   6.400 +	case GL_FRAGMENT_SHADER:
   6.401 +		return "pixel";
   6.402 +	case GL_TESS_CONTROL_SHADER:
   6.403 +		return "tessellation control";
   6.404 +	case GL_TESS_EVALUATION_SHADER:
   6.405 +		return "tessellation evaluation";
   6.406 +	case GL_GEOMETRY_SHADER:
   6.407 +		return "geometry";
   6.408 +
   6.409 +	default:
   6.410 +		break;
   6.411 +	}
   6.412 +	return "<unknown>";
   6.413 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/sdr.h	Tue Jan 08 03:16:48 2013 +0200
     7.3 @@ -0,0 +1,59 @@
     7.4 +#ifndef SDR_H_
     7.5 +#define SDR_H_
     7.6 +
     7.7 +#ifdef __cplusplus
     7.8 +extern "C" {
     7.9 +#endif	/* __cplusplus */
    7.10 +
    7.11 +/* ---- shaders ---- */
    7.12 +unsigned int create_vertex_shader(const char *src);
    7.13 +unsigned int create_pixel_shader(const char *src);
    7.14 +unsigned int create_tessctl_shader(const char *src);
    7.15 +unsigned int create_tesseval_shader(const char *src);
    7.16 +unsigned int create_geometry_shader(const char *src);
    7.17 +unsigned int create_shader(const char *src, unsigned int sdr_type);
    7.18 +void free_shader(unsigned int sdr);
    7.19 +
    7.20 +unsigned int load_vertex_shader(const char *fname);
    7.21 +unsigned int load_pixel_shader(const char *fname);
    7.22 +unsigned int load_tessctl_shader(const char *fname);
    7.23 +unsigned int load_tesseval_shader(const char *fname);
    7.24 +unsigned int load_geometry_shader(const char *fname);
    7.25 +unsigned int load_shader(const char *src, unsigned int sdr_type);
    7.26 +
    7.27 +unsigned int get_vertex_shader(const char *fname);
    7.28 +unsigned int get_pixel_shader(const char *fname);
    7.29 +unsigned int get_tessctl_shader(const char *fname);
    7.30 +unsigned int get_tesseval_shader(const char *fname);
    7.31 +unsigned int get_geometry_shader(const char *fname);
    7.32 +unsigned int get_shader(const char *fname, unsigned int sdr_type);
    7.33 +
    7.34 +int add_shader(const char *fname, unsigned int sdr);
    7.35 +int remove_shader(const char *fname);
    7.36 +
    7.37 +/* ---- gpu programs ---- */
    7.38 +unsigned int create_program(void);
    7.39 +unsigned int create_program_link(unsigned int sdr0, ...);
    7.40 +unsigned int create_program_load(const char *vfile, const char *pfile);
    7.41 +void free_program(unsigned int sdr);
    7.42 +
    7.43 +void attach_shader(unsigned int prog, unsigned int sdr);
    7.44 +int link_program(unsigned int prog);
    7.45 +int bind_program(unsigned int prog);
    7.46 +
    7.47 +int set_uniform_int(unsigned int prog, const char *name, int val);
    7.48 +int set_uniform_float(unsigned int prog, const char *name, float val);
    7.49 +int set_uniform_float2(unsigned int prog, const char *name, float x, float y);
    7.50 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z);
    7.51 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w);
    7.52 +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat);
    7.53 +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat);
    7.54 +
    7.55 +int get_attrib_loc(unsigned int prog, const char *name);
    7.56 +void set_attrib_float3(int attr_loc, float x, float y, float z);
    7.57 +
    7.58 +#ifdef __cplusplus
    7.59 +}
    7.60 +#endif	/* __cplusplus */
    7.61 +
    7.62 +#endif	/* SDR_H_ */
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/udg.cc	Tue Jan 08 03:16:48 2013 +0200
     8.3 @@ -0,0 +1,326 @@
     8.4 +#include <stdio.h>
     8.5 +#include <stdlib.h>
     8.6 +#include <math.h>
     8.7 +#include <assert.h>
     8.8 +#include <GL/glew.h>
     8.9 +
    8.10 +#ifndef __APPLE__
    8.11 +#include <GL/glut.h>
    8.12 +#else
    8.13 +#include <GLUT/glut.h>
    8.14 +#endif
    8.15 +
    8.16 +#include "sdr.h"
    8.17 +#include "dither_matrix.h"
    8.18 +
    8.19 +#define DITHER_SZ		4
    8.20 +#define DITHER_LEVELS	16
    8.21 +
    8.22 +struct render_target {
    8.23 +	unsigned int fbo;
    8.24 +	unsigned int color_tex, depth_buf;
    8.25 +};
    8.26 +
    8.27 +bool init();
    8.28 +void cleanup();
    8.29 +void disp();
    8.30 +void idle();
    8.31 +void reshape(int x, int y);
    8.32 +void keyb(unsigned char key, int x, int y);
    8.33 +void mouse(int bn, int state, int x, int y);
    8.34 +void motion(int x, int y);
    8.35 +struct render_target *create_rtarg(int xsz, int ysz);
    8.36 +void destroy_rtarg(struct render_target *rt);
    8.37 +
    8.38 +int xsz, ysz;
    8.39 +float cam_theta, cam_phi = 25, cam_dist = 8;
    8.40 +unsigned int dither_tex;
    8.41 +struct render_target *rtarg;
    8.42 +unsigned int prog;
    8.43 +
    8.44 +int main(int argc, char **argv)
    8.45 +{
    8.46 +	glutInit(&argc, argv);
    8.47 +	glutInitWindowSize(800, 600);
    8.48 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    8.49 +	glutCreateWindow("DBF UDG compo entry by Nuclear");
    8.50 +
    8.51 +	glutDisplayFunc(disp);
    8.52 +	glutIdleFunc(idle);
    8.53 +	glutReshapeFunc(reshape);
    8.54 +	glutKeyboardFunc(keyb);
    8.55 +	glutMouseFunc(mouse);
    8.56 +	glutMotionFunc(motion);
    8.57 +
    8.58 +	glewInit();
    8.59 +
    8.60 +	if(!init()) {
    8.61 +		return 1;
    8.62 +	}
    8.63 +
    8.64 +	glutMainLoop();
    8.65 +	return 0;
    8.66 +}
    8.67 +
    8.68 +bool init()
    8.69 +{
    8.70 +	float *img = new float[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
    8.71 +	float *ptr = img;
    8.72 +
    8.73 +	for(int i=0; i<DITHER_LEVELS; i++) {
    8.74 +		float val = (float)i / (float)(DITHER_LEVELS - 1);
    8.75 +		for(int y=0; y<DITHER_SZ; y++) {
    8.76 +			for(int x=0; x<DITHER_SZ; x++) {
    8.77 +				/* (1 + M) / (1 + MxN) */
    8.78 +				float thres = (1.0 + dither_matrix4[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
    8.79 +				*ptr++ = val >= thres ? 1.0 : 0.0;
    8.80 +			}
    8.81 +		}
    8.82 +	}
    8.83 +
    8.84 +	if(!(prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
    8.85 +		return false;
    8.86 +	}
    8.87 +
    8.88 +	glGenTextures(1, &dither_tex);
    8.89 +	glBindTexture(GL_TEXTURE_2D, dither_tex);
    8.90 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    8.91 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    8.92 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    8.93 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    8.94 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_FLOAT, img);
    8.95 +
    8.96 +	glEnable(GL_CULL_FACE);
    8.97 +	glEnable(GL_DEPTH_TEST);
    8.98 +	glEnable(GL_LIGHTING);
    8.99 +	glEnable(GL_LIGHT0);
   8.100 +
   8.101 +	return true;
   8.102 +}
   8.103 +
   8.104 +void draw_backdrop()
   8.105 +{
   8.106 +	glPushAttrib(GL_ENABLE_BIT);
   8.107 +	glDisable(GL_DEPTH_TEST);
   8.108 +	glDisable(GL_LIGHTING);
   8.109 +
   8.110 +	glMatrixMode(GL_MODELVIEW);
   8.111 +	glPushMatrix();
   8.112 +	glLoadIdentity();
   8.113 +	glMatrixMode(GL_PROJECTION);
   8.114 +	glPushMatrix();
   8.115 +	glLoadIdentity();
   8.116 +
   8.117 +	glBegin(GL_QUADS);
   8.118 +	glColor3f(0, 0, 0);
   8.119 +	glVertex2f(-1, -1);
   8.120 +	glVertex2f(1, -1);
   8.121 +	glColor3f(1, 1, 1);
   8.122 +	glVertex2f(1, 1);
   8.123 +	glVertex2f(-1, 1);
   8.124 +	glEnd();
   8.125 +
   8.126 +	glPopMatrix();
   8.127 +	glMatrixMode(GL_MODELVIEW);
   8.128 +	glPopMatrix();
   8.129 +
   8.130 +	glPopAttrib();
   8.131 +}
   8.132 +
   8.133 +void disp()
   8.134 +{
   8.135 +	float ldir[] = {-1, 1, 2, 0};
   8.136 +
   8.137 +	if(!rtarg) {
   8.138 +		printf("(re)creating render target (%dx%d)\n", xsz, ysz);
   8.139 +		if(!(rtarg = create_rtarg(xsz, ysz))) {
   8.140 +			exit(0);
   8.141 +		}
   8.142 +	}
   8.143 +
   8.144 +	glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
   8.145 +
   8.146 +	glClearColor(1, 1, 1, 1);
   8.147 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   8.148 +
   8.149 +	draw_backdrop();
   8.150 +
   8.151 +	glEnable(GL_DEPTH_TEST);
   8.152 +
   8.153 +	glMatrixMode(GL_MODELVIEW);
   8.154 +	glLoadIdentity();
   8.155 +
   8.156 +	glLightfv(GL_LIGHT0, GL_POSITION, ldir);
   8.157 +
   8.158 +	glTranslatef(0, 0, -cam_dist);
   8.159 +	glRotatef(cam_phi, 1, 0, 0);
   8.160 +	glRotatef(cam_theta, 0, 1, 0);
   8.161 +
   8.162 +	glFrontFace(GL_CW);
   8.163 +	glutSolidTeapot(1.0);
   8.164 +	glFrontFace(GL_CCW);
   8.165 +
   8.166 +
   8.167 +
   8.168 +	glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
   8.169 +	glClear(GL_COLOR_BUFFER_BIT);
   8.170 +
   8.171 +	glMatrixMode(GL_PROJECTION);
   8.172 +	glPushMatrix();
   8.173 +	glLoadIdentity();
   8.174 +	glMatrixMode(GL_MODELVIEW);
   8.175 +	glLoadIdentity();
   8.176 +	glPushMatrix();
   8.177 +
   8.178 +	glPushAttrib(GL_ENABLE_BIT);
   8.179 +	glDisable(GL_DEPTH_TEST);
   8.180 +
   8.181 +	bind_program(prog);
   8.182 +	set_uniform_int(prog, "framebuf", 0);
   8.183 +	set_uniform_int(prog, "dither_tex", 1);
   8.184 +	set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
   8.185 +	set_uniform_int(prog, "dither_size", DITHER_SZ);
   8.186 +
   8.187 +	glActiveTextureARB(GL_TEXTURE0);
   8.188 +	glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
   8.189 +	glActiveTextureARB(GL_TEXTURE1);
   8.190 +	glBindTexture(GL_TEXTURE_2D, dither_tex);
   8.191 +
   8.192 +	glBegin(GL_QUADS);
   8.193 +	glColor3f(0, 1, 0);
   8.194 +	glTexCoord2f(0, 0); glVertex2f(-1, -1);
   8.195 +	glTexCoord2f(1, 0); glVertex2f(1, -1);
   8.196 +	glTexCoord2f(1, 1); glVertex2f(1, 1);
   8.197 +	glTexCoord2f(0, 1); glVertex2f(-1, 1);
   8.198 +	glEnd();
   8.199 +
   8.200 +	glActiveTextureARB(GL_TEXTURE1);
   8.201 +	glDisable(GL_TEXTURE_2D);
   8.202 +	glActiveTextureARB(GL_TEXTURE0);
   8.203 +	glDisable(GL_TEXTURE_2D);
   8.204 +
   8.205 +	bind_program(0);
   8.206 +
   8.207 +	glPopAttrib();
   8.208 +
   8.209 +	glMatrixMode(GL_PROJECTION);
   8.210 +	glPopMatrix();
   8.211 +	glMatrixMode(GL_MODELVIEW);
   8.212 +	glPopMatrix();
   8.213 +
   8.214 +	glutSwapBuffers();
   8.215 +	assert(glGetError() == GL_NO_ERROR);
   8.216 +}
   8.217 +
   8.218 +void idle()
   8.219 +{
   8.220 +	glutPostRedisplay();
   8.221 +}
   8.222 +
   8.223 +void reshape(int x, int y)
   8.224 +{
   8.225 +	glViewport(0, 0, x, y);
   8.226 +
   8.227 +	glMatrixMode(GL_PROJECTION);
   8.228 +	glLoadIdentity();
   8.229 +	gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
   8.230 +
   8.231 +	if(x != xsz || y != ysz) {
   8.232 +		destroy_rtarg(rtarg);
   8.233 +		rtarg = 0;
   8.234 +		xsz = x;
   8.235 +		ysz = y;
   8.236 +	}
   8.237 +}
   8.238 +
   8.239 +void keyb(unsigned char key, int x, int y)
   8.240 +{
   8.241 +	switch(key) {
   8.242 +	case 27:
   8.243 +		exit(0);
   8.244 +	}
   8.245 +}
   8.246 +
   8.247 +bool bnstate[16];
   8.248 +int prev_x, prev_y;
   8.249 +
   8.250 +void mouse(int bn, int state, int x, int y)
   8.251 +{
   8.252 +	int idx = bn - GLUT_LEFT_BUTTON;
   8.253 +
   8.254 +	if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
   8.255 +		bnstate[idx] = state == GLUT_DOWN;
   8.256 +	}
   8.257 +	prev_x = x;
   8.258 +	prev_y = y;
   8.259 +}
   8.260 +
   8.261 +void motion(int x, int y)
   8.262 +{
   8.263 +	int dx = x - prev_x;
   8.264 +	int dy = y - prev_y;
   8.265 +	prev_x = x;
   8.266 +	prev_y = y;
   8.267 +
   8.268 +	if(bnstate[0]) {
   8.269 +		cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
   8.270 +		cam_phi += dy * 0.5;
   8.271 +		if(cam_phi < -90) {
   8.272 +			cam_phi = -90;
   8.273 +		}
   8.274 +		if(cam_phi > 90) {
   8.275 +			cam_phi = 90;
   8.276 +		}
   8.277 +	}
   8.278 +	if(bnstate[2]) {
   8.279 +		cam_dist += dy * 0.1;
   8.280 +		if(cam_dist < 0) {
   8.281 +			cam_dist = 0;
   8.282 +		}
   8.283 +	}
   8.284 +}
   8.285 +
   8.286 +struct render_target *create_rtarg(int xsz, int ysz)
   8.287 +{
   8.288 +	struct render_target *rt = new render_target;
   8.289 +
   8.290 +	glGenFramebuffersEXT(1, &rt->fbo);
   8.291 +	glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
   8.292 +
   8.293 +	// create the render target texture
   8.294 +	glGenTextures(1, &rt->color_tex);
   8.295 +	glBindTexture(GL_TEXTURE_2D, rt->color_tex);
   8.296 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   8.297 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   8.298 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   8.299 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   8.300 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
   8.301 +
   8.302 +	glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
   8.303 +
   8.304 +	// create depth buffer
   8.305 +	glGenRenderbuffersEXT(1, &rt->depth_buf);
   8.306 +	glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
   8.307 +	glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
   8.308 +
   8.309 +	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
   8.310 +
   8.311 +	if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
   8.312 +		fprintf(stderr, "incomplete fbo\n");
   8.313 +		return 0;
   8.314 +	}
   8.315 +
   8.316 +	glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
   8.317 +	return rt;
   8.318 +}
   8.319 +
   8.320 +void destroy_rtarg(struct render_target *rt)
   8.321 +{
   8.322 +	if(!rt) {
   8.323 +		return;
   8.324 +	}
   8.325 +	glDeleteFramebuffersEXT(1, &rt->fbo);
   8.326 +	glDeleteTextures(1, &rt->color_tex);
   8.327 +	glDeleteRenderbuffersEXT(1, &rt->depth_buf);
   8.328 +	delete rt;
   8.329 +}