istereo

diff src/istereo.c @ 27:fd39c0198935

normal mapped tunnel
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 08:30:00 +0300
parents 862a3329a8f0
children 8dd271942543
line diff
     1.1 --- a/src/istereo.c	Thu Sep 08 06:28:38 2011 +0300
     1.2 +++ b/src/istereo.c	Thu Sep 08 08:30:00 2011 +0300
     1.3 @@ -9,16 +9,19 @@
     1.4  #include "respath.h"
     1.5  #include "tex.h"
     1.6  #include "cam.h"
     1.7 +#include "vmath.h"
     1.8  #include "config.h"
     1.9  
    1.10  static void render(float t);
    1.11  static void draw_tunnel(float t);
    1.12 -static void tunnel_vertex(float u, float v, float du, float dv, float t);
    1.13 +static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t);
    1.14 +static vec3_t calc_text_pos(float sec);
    1.15 +static void draw_text(float idx, vec3_t tpos, float alpha);
    1.16  static void worm(float t, float z, float *tx, float *ty);
    1.17  static unsigned int get_shader_program(const char *vfile, const char *pfile);
    1.18  static float get_sec(void);
    1.19  
    1.20 -unsigned int prog, prog_tunnel, prog_text;
    1.21 +unsigned int prog, prog_simple, prog_tunnel, prog_text;
    1.22  unsigned int tex, tex_stones, tex_normal, tex_text;
    1.23  
    1.24  int view_xsz, view_ysz;
    1.25 @@ -28,11 +31,15 @@
    1.26  #else
    1.27  int stereo = 0;
    1.28  #endif
    1.29 +int use_bump = 1;
    1.30  
    1.31  /* construction parameters */
    1.32  int sides = 24;
    1.33  int segm = 20;
    1.34 +float tunnel_speed = 0.75;
    1.35  float ring_height = 0.5;
    1.36 +float text_period = 13.0;
    1.37 +float text_speed = 2.2;
    1.38  
    1.39  float split = 0.53;
    1.40  
    1.41 @@ -41,17 +48,33 @@
    1.42  	add_resource_path("sdr");
    1.43  	add_resource_path("data");
    1.44  
    1.45 -	if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
    1.46 -		fprintf(stderr, "failed to load shader program\n");
    1.47 +	if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
    1.48 +		return -1;
    1.49 +	}
    1.50 +	if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
    1.51 +		return -1;
    1.52 +	}
    1.53 +	if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
    1.54  		return -1;
    1.55  	}
    1.56  
    1.57  	if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
    1.58 -		fprintf(stderr, "failed to load texture\n");
    1.59 +		return -1;
    1.60 +	}
    1.61 +	if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
    1.62 +		return -1;
    1.63 +	}
    1.64 +	if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
    1.65 +		return -1;
    1.66 +	}
    1.67 +	if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
    1.68  		return -1;
    1.69  	}
    1.70  
    1.71 -	cam_fov(42.0);
    1.72 +	glEnable(GL_DEPTH_TEST);
    1.73 +	glEnable(GL_CULL_FACE);
    1.74 +
    1.75 +	cam_fov(45.0);
    1.76  
    1.77  	return 0;
    1.78  }
    1.79 @@ -69,8 +92,14 @@
    1.80  	z = ring_height * segm;
    1.81  	worm(tsec, z, &pan_x, &pan_y);
    1.82  
    1.83 -	glClearColor(0.6, 0.6, 0.6, 1.0);
    1.84 -	glClear(GL_COLOR_BUFFER_BIT);
    1.85 +	if(use_bump) {
    1.86 +		glClearColor(0.01, 0.01, 0.01, 1.0);
    1.87 +		tunnel_speed = 0.5;
    1.88 +	} else {
    1.89 +		glClearColor(0.6, 0.6, 0.6, 1.0);
    1.90 +		tunnel_speed = 0.75;
    1.91 +	}
    1.92 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.93  
    1.94  	if(stereo) {
    1.95  		int split_pt = (int)((float)view_ysz * split);
    1.96 @@ -117,60 +146,52 @@
    1.97  		render(tsec);
    1.98  	}
    1.99  
   1.100 -
   1.101 -	/*glClearColor(0.4, 0.6, 1.0, 1.0);
   1.102 -	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.103 -
   1.104 -	bind_program(prog);
   1.105 -
   1.106 -	gl_matrix_mode(GL_MODELVIEW);
   1.107 -	gl_load_identity();
   1.108 -	gl_translatef(0, 0, -8);
   1.109 -	gl_rotatef(t * 100.0, 0, 0, 1);
   1.110 -
   1.111 -	bind_texture(tex, 0);
   1.112 -	set_uniform_int(prog, "tex", 0);
   1.113 -
   1.114 -	gl_begin(GL_QUADS);
   1.115 -	gl_texcoord2f(0, 0);
   1.116 -	gl_color3f(1, 0, 0);
   1.117 -	gl_vertex3f(-1, -1, 0);
   1.118 -	gl_texcoord2f(1, 0);
   1.119 -	gl_color3f(0, 1, 0);
   1.120 -	gl_vertex3f(1, -1, 0);
   1.121 -	gl_texcoord2f(1, 1);
   1.122 -	gl_color3f(0, 0, 1);
   1.123 -	gl_vertex3f(1, 1, 0);
   1.124 -	gl_texcoord2f(0, 1);
   1.125 -	gl_color3f(1, 1, 0);
   1.126 -	gl_vertex3f(-1, 1, 0);
   1.127 -	gl_end();
   1.128 -
   1.129 -	bind_texture(0, 0);*/
   1.130 -
   1.131  	assert(glGetError() == GL_NO_ERROR);
   1.132  }
   1.133  
   1.134  static void render(float t)
   1.135  {
   1.136 +	int i;
   1.137 +	float text_line;
   1.138 +
   1.139  	draw_tunnel(t);
   1.140 +
   1.141 +	glDepthMask(0);
   1.142 +	text_line = floor((text_speed * t) / text_period);
   1.143 +	for(i=0; i<15; i++) {
   1.144 +		vec3_t tpos = calc_text_pos(t - (float)i * 0.01);
   1.145 +		draw_text(text_line, tpos, 1.5 / (float)i);
   1.146 +	}
   1.147 +	glDepthMask(1);
   1.148  }
   1.149  
   1.150  static void draw_tunnel(float t)
   1.151  {
   1.152  	static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
   1.153  	static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
   1.154 -	int i, j, k;
   1.155 +	int i, j, k, tang_loc = -1;
   1.156  	float du, dv;
   1.157  
   1.158 +	prog = use_bump ? prog_tunnel : prog_simple;
   1.159 +
   1.160  	bind_program(prog);
   1.161  	set_uniform_float(prog, "t", t);
   1.162 +	set_uniform_int(prog, "tex", 0);
   1.163 +
   1.164 +	if(use_bump) {
   1.165 +		vec3_t ltpos = calc_text_pos(t);
   1.166 +
   1.167 +		set_uniform_int(prog, "tex_norm", 1);
   1.168 +		set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
   1.169 +		tang_loc = get_attrib_loc(prog, "attr_tangent");
   1.170 +		bind_texture(tex_normal, 1);
   1.171 +	}
   1.172  
   1.173  	bind_texture(tex, 0);
   1.174  
   1.175  	gl_matrix_mode(GL_TEXTURE);
   1.176  	gl_load_identity();
   1.177 -	gl_translatef(0, -t * 0.75, 0);
   1.178 +	gl_translatef(0, -t * tunnel_speed, 0);
   1.179  
   1.180  	gl_begin(GL_QUADS);
   1.181  	gl_color3f(1.0, 1.0, 1.0);
   1.182 @@ -197,26 +218,20 @@
   1.183  				float u = (j + uoffs[k]) * du;
   1.184  				float v = (i + voffs[k]) * dv;
   1.185  
   1.186 -				tunnel_vertex(u, v, du, dv, t);
   1.187 +				tunnel_vertex(u, v, du, dv, tang_loc, t);
   1.188  			}
   1.189  		}
   1.190  	}
   1.191  	gl_end();
   1.192  
   1.193 -	/*gl_begin(GL_QUADS);
   1.194 -	gl_color3f(1.0, 0.3, 0.2);
   1.195 -	gl_vertex2f(-100, -100);
   1.196 -	gl_vertex2f(100, -100);
   1.197 -	gl_vertex2f(100, 0);
   1.198 -	gl_vertex2f(-100, 0);
   1.199 -	gl_end();*/
   1.200 -
   1.201 +	bind_texture(0, 1);
   1.202  	bind_texture(0, 0);
   1.203  }
   1.204  
   1.205 -static void tunnel_vertex(float u, float v, float du, float dv, float t)
   1.206 +static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
   1.207  {
   1.208 -	float pos[3];
   1.209 +	vec3_t pos, norm;
   1.210 +	vec3_t dfdu, dfdv, pos_du, pos_dv;
   1.211  
   1.212  	float theta = 2.0 * M_PI * u;
   1.213  	float theta1 = 2.0 * M_PI * (u + du);
   1.214 @@ -233,24 +248,65 @@
   1.215  	worm(t, z, trans_z, trans_z + 1);
   1.216  	worm(t, z1, trans_z1, trans_z1 + 1);
   1.217  
   1.218 -	pos[0] = x + trans_z[0];
   1.219 -	pos[1] = y + trans_z[1];
   1.220 -	pos[2] = z;
   1.221 +	pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
   1.222 +	pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
   1.223 +	pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
   1.224  
   1.225 -	/*v3_cons(pos, x + trans_z[0], y + trans_z[1], z);
   1.226 -	v3_cons(pos_du, x1 + trans_z[0], y1 + trans_z[1], z);
   1.227 -	v3_cons(pos_dv, x + trans_z1[0], y + trans_z1[1], z1);*/
   1.228 +	dfdu = v3_sub(pos_du, pos);
   1.229 +	dfdv = v3_sub(pos_dv, pos);
   1.230 +	norm = v3_cross(dfdv, dfdu);
   1.231  
   1.232 -	/*v3_sub(dfdu, pos_du, pos);
   1.233 -	v3_sub(dfdv, pos_dv, pos);
   1.234 -	v3_cross(norm, dfdv, dfdu);*/
   1.235 -
   1.236 -	/*glVertexAttrib3f(tloc, dfdu[0], dfdu[1], dfdu[2]);
   1.237 -	glNormal3f(norm[0], norm[1], norm[2]);*/
   1.238 +	gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
   1.239 +	gl_normal3f(norm.x, norm.y, norm.z);
   1.240  	gl_texcoord2f(u * 2.0, v * 4.0);
   1.241 -	gl_vertex3f(pos[0], pos[1], pos[2]);
   1.242 +	gl_vertex3f(pos.x, pos.y, pos.z);
   1.243  }
   1.244  
   1.245 +static vec3_t calc_text_pos(float sec)
   1.246 +{
   1.247 +	float t = text_speed * sec;
   1.248 +	float z = fmod(t, text_period);
   1.249 +	float pan[2];
   1.250 +
   1.251 +	worm(sec, z, pan, pan + 1);
   1.252 +	return v3_cons(pan[0], pan[1], z + ring_height);
   1.253 +}
   1.254 +
   1.255 +static void draw_text(float idx, vec3_t tpos, float alpha)
   1.256 +{
   1.257 +	gl_matrix_mode(GL_MODELVIEW);
   1.258 +	gl_push_matrix();
   1.259 +	gl_translatef(tpos.x, tpos.y, tpos.z);
   1.260 +
   1.261 +	glEnable(GL_BLEND);
   1.262 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   1.263 +
   1.264 +	bind_program(prog_text);
   1.265 +	set_uniform_float(prog, "idx", idx);
   1.266 +
   1.267 +	bind_texture(tex_text, 0);
   1.268 +
   1.269 +	gl_begin(GL_QUADS);
   1.270 +	gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
   1.271 +
   1.272 +	gl_texcoord2f(0, 1);
   1.273 +	gl_vertex3f(-1, -0.2, 0);
   1.274 +
   1.275 +	gl_texcoord2f(1, 1);
   1.276 +	gl_vertex3f(1, -0.2, 0);
   1.277 +
   1.278 +	gl_texcoord2f(1, 0);
   1.279 +	gl_vertex3f(1, 0.2, 0);
   1.280 +
   1.281 +	gl_texcoord2f(0, 0);
   1.282 +	gl_vertex3f(-1, 0.2, 0);
   1.283 +	gl_end();
   1.284 +
   1.285 +	bind_texture(0, 0);
   1.286 +	glDisable(GL_BLEND);
   1.287 +
   1.288 +	gl_pop_matrix();
   1.289 +}
   1.290  
   1.291  
   1.292  static void worm(float t, float z, float *tx, float *ty)
   1.293 @@ -281,14 +337,14 @@
   1.294  	unsigned int prog, vs, ps;
   1.295  
   1.296  	if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
   1.297 -		return -1;
   1.298 +		return 0;
   1.299  	}
   1.300  	if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
   1.301 -		return -1;
   1.302 +		return 0;
   1.303  	}
   1.304  
   1.305  	if(!(prog = create_program_link(vs, ps))) {
   1.306 -		return -1;
   1.307 +		return 0;
   1.308  	}
   1.309  	return prog;
   1.310  }