istereo

changeset 27:fd39c0198935

normal mapped tunnel
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 08:30:00 +0300
parents 862a3329a8f0
children c0ae8e668447
files libs/Makefile src/istereo.c src/respath.c src/sanegl.c src/tex.c
diffstat 5 files changed, 133 insertions(+), 71 deletions(-) [+]
line diff
     1.1 --- a/libs/Makefile	Thu Sep 08 06:28:38 2011 +0300
     1.2 +++ b/libs/Makefile	Thu Sep 08 08:30:00 2011 +0300
     1.3 @@ -1,6 +1,7 @@
     1.4  src += $(wildcard libs/zlib/*.c) \
     1.5  	   $(wildcard libs/libpng/*.c) \
     1.6  	   $(wildcard libs/libjpeg/*.c) \
     1.7 -	   $(wildcard libs/imago2/*.c)
     1.8 +	   $(wildcard libs/imago2/*.c) \
     1.9 +	   $(wildcard libs/vmath/*.c)
    1.10  
    1.11 -incdir += -Ilibs/imago2 -Ilibs/zlib -Ilibs/libpng -Ilibs/libjpeg
    1.12 +incdir += -Ilibs/imago2 -Ilibs/zlib -Ilibs/libpng -Ilibs/libjpeg -Ilibs/vmath
     2.1 --- a/src/istereo.c	Thu Sep 08 06:28:38 2011 +0300
     2.2 +++ b/src/istereo.c	Thu Sep 08 08:30:00 2011 +0300
     2.3 @@ -9,16 +9,19 @@
     2.4  #include "respath.h"
     2.5  #include "tex.h"
     2.6  #include "cam.h"
     2.7 +#include "vmath.h"
     2.8  #include "config.h"
     2.9  
    2.10  static void render(float t);
    2.11  static void draw_tunnel(float t);
    2.12 -static void tunnel_vertex(float u, float v, float du, float dv, float t);
    2.13 +static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t);
    2.14 +static vec3_t calc_text_pos(float sec);
    2.15 +static void draw_text(float idx, vec3_t tpos, float alpha);
    2.16  static void worm(float t, float z, float *tx, float *ty);
    2.17  static unsigned int get_shader_program(const char *vfile, const char *pfile);
    2.18  static float get_sec(void);
    2.19  
    2.20 -unsigned int prog, prog_tunnel, prog_text;
    2.21 +unsigned int prog, prog_simple, prog_tunnel, prog_text;
    2.22  unsigned int tex, tex_stones, tex_normal, tex_text;
    2.23  
    2.24  int view_xsz, view_ysz;
    2.25 @@ -28,11 +31,15 @@
    2.26  #else
    2.27  int stereo = 0;
    2.28  #endif
    2.29 +int use_bump = 1;
    2.30  
    2.31  /* construction parameters */
    2.32  int sides = 24;
    2.33  int segm = 20;
    2.34 +float tunnel_speed = 0.75;
    2.35  float ring_height = 0.5;
    2.36 +float text_period = 13.0;
    2.37 +float text_speed = 2.2;
    2.38  
    2.39  float split = 0.53;
    2.40  
    2.41 @@ -41,17 +48,33 @@
    2.42  	add_resource_path("sdr");
    2.43  	add_resource_path("data");
    2.44  
    2.45 -	if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
    2.46 -		fprintf(stderr, "failed to load shader program\n");
    2.47 +	if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
    2.48 +		return -1;
    2.49 +	}
    2.50 +	if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
    2.51 +		return -1;
    2.52 +	}
    2.53 +	if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
    2.54  		return -1;
    2.55  	}
    2.56  
    2.57  	if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
    2.58 -		fprintf(stderr, "failed to load texture\n");
    2.59 +		return -1;
    2.60 +	}
    2.61 +	if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
    2.62 +		return -1;
    2.63 +	}
    2.64 +	if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
    2.65 +		return -1;
    2.66 +	}
    2.67 +	if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
    2.68  		return -1;
    2.69  	}
    2.70  
    2.71 -	cam_fov(42.0);
    2.72 +	glEnable(GL_DEPTH_TEST);
    2.73 +	glEnable(GL_CULL_FACE);
    2.74 +
    2.75 +	cam_fov(45.0);
    2.76  
    2.77  	return 0;
    2.78  }
    2.79 @@ -69,8 +92,14 @@
    2.80  	z = ring_height * segm;
    2.81  	worm(tsec, z, &pan_x, &pan_y);
    2.82  
    2.83 -	glClearColor(0.6, 0.6, 0.6, 1.0);
    2.84 -	glClear(GL_COLOR_BUFFER_BIT);
    2.85 +	if(use_bump) {
    2.86 +		glClearColor(0.01, 0.01, 0.01, 1.0);
    2.87 +		tunnel_speed = 0.5;
    2.88 +	} else {
    2.89 +		glClearColor(0.6, 0.6, 0.6, 1.0);
    2.90 +		tunnel_speed = 0.75;
    2.91 +	}
    2.92 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    2.93  
    2.94  	if(stereo) {
    2.95  		int split_pt = (int)((float)view_ysz * split);
    2.96 @@ -117,60 +146,52 @@
    2.97  		render(tsec);
    2.98  	}
    2.99  
   2.100 -
   2.101 -	/*glClearColor(0.4, 0.6, 1.0, 1.0);
   2.102 -	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   2.103 -
   2.104 -	bind_program(prog);
   2.105 -
   2.106 -	gl_matrix_mode(GL_MODELVIEW);
   2.107 -	gl_load_identity();
   2.108 -	gl_translatef(0, 0, -8);
   2.109 -	gl_rotatef(t * 100.0, 0, 0, 1);
   2.110 -
   2.111 -	bind_texture(tex, 0);
   2.112 -	set_uniform_int(prog, "tex", 0);
   2.113 -
   2.114 -	gl_begin(GL_QUADS);
   2.115 -	gl_texcoord2f(0, 0);
   2.116 -	gl_color3f(1, 0, 0);
   2.117 -	gl_vertex3f(-1, -1, 0);
   2.118 -	gl_texcoord2f(1, 0);
   2.119 -	gl_color3f(0, 1, 0);
   2.120 -	gl_vertex3f(1, -1, 0);
   2.121 -	gl_texcoord2f(1, 1);
   2.122 -	gl_color3f(0, 0, 1);
   2.123 -	gl_vertex3f(1, 1, 0);
   2.124 -	gl_texcoord2f(0, 1);
   2.125 -	gl_color3f(1, 1, 0);
   2.126 -	gl_vertex3f(-1, 1, 0);
   2.127 -	gl_end();
   2.128 -
   2.129 -	bind_texture(0, 0);*/
   2.130 -
   2.131  	assert(glGetError() == GL_NO_ERROR);
   2.132  }
   2.133  
   2.134  static void render(float t)
   2.135  {
   2.136 +	int i;
   2.137 +	float text_line;
   2.138 +
   2.139  	draw_tunnel(t);
   2.140 +
   2.141 +	glDepthMask(0);
   2.142 +	text_line = floor((text_speed * t) / text_period);
   2.143 +	for(i=0; i<15; i++) {
   2.144 +		vec3_t tpos = calc_text_pos(t - (float)i * 0.01);
   2.145 +		draw_text(text_line, tpos, 1.5 / (float)i);
   2.146 +	}
   2.147 +	glDepthMask(1);
   2.148  }
   2.149  
   2.150  static void draw_tunnel(float t)
   2.151  {
   2.152  	static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
   2.153  	static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
   2.154 -	int i, j, k;
   2.155 +	int i, j, k, tang_loc = -1;
   2.156  	float du, dv;
   2.157  
   2.158 +	prog = use_bump ? prog_tunnel : prog_simple;
   2.159 +
   2.160  	bind_program(prog);
   2.161  	set_uniform_float(prog, "t", t);
   2.162 +	set_uniform_int(prog, "tex", 0);
   2.163 +
   2.164 +	if(use_bump) {
   2.165 +		vec3_t ltpos = calc_text_pos(t);
   2.166 +
   2.167 +		set_uniform_int(prog, "tex_norm", 1);
   2.168 +		set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
   2.169 +		tang_loc = get_attrib_loc(prog, "attr_tangent");
   2.170 +		bind_texture(tex_normal, 1);
   2.171 +	}
   2.172  
   2.173  	bind_texture(tex, 0);
   2.174  
   2.175  	gl_matrix_mode(GL_TEXTURE);
   2.176  	gl_load_identity();
   2.177 -	gl_translatef(0, -t * 0.75, 0);
   2.178 +	gl_translatef(0, -t * tunnel_speed, 0);
   2.179  
   2.180  	gl_begin(GL_QUADS);
   2.181  	gl_color3f(1.0, 1.0, 1.0);
   2.182 @@ -197,26 +218,20 @@
   2.183  				float u = (j + uoffs[k]) * du;
   2.184  				float v = (i + voffs[k]) * dv;
   2.185  
   2.186 -				tunnel_vertex(u, v, du, dv, t);
   2.187 +				tunnel_vertex(u, v, du, dv, tang_loc, t);
   2.188  			}
   2.189  		}
   2.190  	}
   2.191  	gl_end();
   2.192  
   2.193 -	/*gl_begin(GL_QUADS);
   2.194 -	gl_color3f(1.0, 0.3, 0.2);
   2.195 -	gl_vertex2f(-100, -100);
   2.196 -	gl_vertex2f(100, -100);
   2.197 -	gl_vertex2f(100, 0);
   2.198 -	gl_vertex2f(-100, 0);
   2.199 -	gl_end();*/
   2.200 -
   2.201 +	bind_texture(0, 1);
   2.202  	bind_texture(0, 0);
   2.203  }
   2.204  
   2.205 -static void tunnel_vertex(float u, float v, float du, float dv, float t)
   2.206 +static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
   2.207  {
   2.208 -	float pos[3];
   2.209 +	vec3_t pos, norm;
   2.210 +	vec3_t dfdu, dfdv, pos_du, pos_dv;
   2.211  
   2.212  	float theta = 2.0 * M_PI * u;
   2.213  	float theta1 = 2.0 * M_PI * (u + du);
   2.214 @@ -233,24 +248,65 @@
   2.215  	worm(t, z, trans_z, trans_z + 1);
   2.216  	worm(t, z1, trans_z1, trans_z1 + 1);
   2.217  
   2.218 -	pos[0] = x + trans_z[0];
   2.219 -	pos[1] = y + trans_z[1];
   2.220 -	pos[2] = z;
   2.221 +	pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
   2.222 +	pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
   2.223 +	pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
   2.224  
   2.225 -	/*v3_cons(pos, x + trans_z[0], y + trans_z[1], z);
   2.226 -	v3_cons(pos_du, x1 + trans_z[0], y1 + trans_z[1], z);
   2.227 -	v3_cons(pos_dv, x + trans_z1[0], y + trans_z1[1], z1);*/
   2.228 +	dfdu = v3_sub(pos_du, pos);
   2.229 +	dfdv = v3_sub(pos_dv, pos);
   2.230 +	norm = v3_cross(dfdv, dfdu);
   2.231  
   2.232 -	/*v3_sub(dfdu, pos_du, pos);
   2.233 -	v3_sub(dfdv, pos_dv, pos);
   2.234 -	v3_cross(norm, dfdv, dfdu);*/
   2.235 -
   2.236 -	/*glVertexAttrib3f(tloc, dfdu[0], dfdu[1], dfdu[2]);
   2.237 -	glNormal3f(norm[0], norm[1], norm[2]);*/
   2.238 +	gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
   2.239 +	gl_normal3f(norm.x, norm.y, norm.z);
   2.240  	gl_texcoord2f(u * 2.0, v * 4.0);
   2.241 -	gl_vertex3f(pos[0], pos[1], pos[2]);
   2.242 +	gl_vertex3f(pos.x, pos.y, pos.z);
   2.243  }
   2.244  
   2.245 +static vec3_t calc_text_pos(float sec)
   2.246 +{
   2.247 +	float t = text_speed * sec;
   2.248 +	float z = fmod(t, text_period);
   2.249 +	float pan[2];
   2.250 +
   2.251 +	worm(sec, z, pan, pan + 1);
   2.252 +	return v3_cons(pan[0], pan[1], z + ring_height);
   2.253 +}
   2.254 +
   2.255 +static void draw_text(float idx, vec3_t tpos, float alpha)
   2.256 +{
   2.257 +	gl_matrix_mode(GL_MODELVIEW);
   2.258 +	gl_push_matrix();
   2.259 +	gl_translatef(tpos.x, tpos.y, tpos.z);
   2.260 +
   2.261 +	glEnable(GL_BLEND);
   2.262 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   2.263 +
   2.264 +	bind_program(prog_text);
   2.265 +	set_uniform_float(prog, "idx", idx);
   2.266 +
   2.267 +	bind_texture(tex_text, 0);
   2.268 +
   2.269 +	gl_begin(GL_QUADS);
   2.270 +	gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
   2.271 +
   2.272 +	gl_texcoord2f(0, 1);
   2.273 +	gl_vertex3f(-1, -0.2, 0);
   2.274 +
   2.275 +	gl_texcoord2f(1, 1);
   2.276 +	gl_vertex3f(1, -0.2, 0);
   2.277 +
   2.278 +	gl_texcoord2f(1, 0);
   2.279 +	gl_vertex3f(1, 0.2, 0);
   2.280 +
   2.281 +	gl_texcoord2f(0, 0);
   2.282 +	gl_vertex3f(-1, 0.2, 0);
   2.283 +	gl_end();
   2.284 +
   2.285 +	bind_texture(0, 0);
   2.286 +	glDisable(GL_BLEND);
   2.287 +
   2.288 +	gl_pop_matrix();
   2.289 +}
   2.290  
   2.291  
   2.292  static void worm(float t, float z, float *tx, float *ty)
   2.293 @@ -281,14 +337,14 @@
   2.294  	unsigned int prog, vs, ps;
   2.295  
   2.296  	if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
   2.297 -		return -1;
   2.298 +		return 0;
   2.299  	}
   2.300  	if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
   2.301 -		return -1;
   2.302 +		return 0;
   2.303  	}
   2.304  
   2.305  	if(!(prog = create_program_link(vs, ps))) {
   2.306 -		return -1;
   2.307 +		return 0;
   2.308  	}
   2.309  	return prog;
   2.310  }
     3.1 --- a/src/respath.c	Thu Sep 08 06:28:38 2011 +0300
     3.2 +++ b/src/respath.c	Thu Sep 08 08:30:00 2011 +0300
     3.3 @@ -51,6 +51,8 @@
     3.4  		}
     3.5  		node = node->next;
     3.6  	}
     3.7 +
     3.8 +	fprintf(stderr, "can't find resource: %s\n", fname);
     3.9  	return 0;
    3.10  }
    3.11  
     4.1 --- a/src/sanegl.c	Thu Sep 08 06:28:38 2011 +0300
     4.2 +++ b/src/sanegl.c	Thu Sep 08 08:30:00 2011 +0300
     4.3 @@ -325,7 +325,7 @@
     4.4  	}
     4.5  
     4.6  	if(cloc != -1) {
     4.7 -		glVertexAttribPointer(cloc, 4, GL_FLOAT, 0, 0, col_arr);
     4.8 +		glVertexAttribPointer(cloc, 4, GL_FLOAT, 1, 0, col_arr);
     4.9  		glEnableVertexAttribArray(cloc);
    4.10  	}
    4.11  
     5.1 --- a/src/tex.c	Thu Sep 08 06:28:38 2011 +0300
     5.2 +++ b/src/tex.c	Thu Sep 08 08:30:00 2011 +0300
     5.3 @@ -13,6 +13,9 @@
     5.4  	unsigned int tex;
     5.5  	void *pixels;
     5.6  
     5.7 +	if(!fname) {
     5.8 +		return 0;
     5.9 +	}
    5.10  	if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
    5.11  		fprintf(stderr, "failed to load image: %s\n", fname);
    5.12  		return 0;