istereo

changeset 19:ab4972098eb7

tunnel first take
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 10:15:19 +0300
parents 6851489e70c2
children 21d58290e634
files src/istereo.c
diffstat 1 files changed, 160 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/src/istereo.c	Wed Sep 07 09:53:01 2011 +0300
     1.2 +++ b/src/istereo.c	Wed Sep 07 10:15:19 2011 +0300
     1.3 @@ -8,8 +8,13 @@
     1.4  #include "sdr.h"
     1.5  #include "respath.h"
     1.6  #include "tex.h"
     1.7 +#include "cam.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 worm(float t, float z, float *tx, float *ty);
    1.14  static unsigned int get_shader_program(const char *vfile, const char *pfile);
    1.15  static float get_sec(void);
    1.16  
    1.17 @@ -49,9 +54,53 @@
    1.18  
    1.19  void redraw(void)
    1.20  {
    1.21 -	float t = get_sec();
    1.22 +	float pan_x, pan_y, z;
    1.23 +	float tsec = get_sec();
    1.24  
    1.25 -	glClearColor(0.4, 0.6, 1.0, 1.0);
    1.26 +	z = ring_height * (segm - 1);
    1.27 +	worm(tsec, z, &pan_x, &pan_y);
    1.28 +
    1.29 +	if(stereo) {
    1.30 +#if 0
    1.31 +		/* left eye */
    1.32 +		glDrawBuffer(GL_BACK_LEFT);
    1.33 +
    1.34 +		glMatrixMode(GL_PROJECTION);
    1.35 +		glLoadIdentity();
    1.36 +		cam_stereo_proj_matrix(CAM_LEFT);
    1.37 +
    1.38 +		glMatrixMode(GL_MODELVIEW);
    1.39 +		glLoadIdentity();
    1.40 +		cam_stereo_view_matrix(CAM_LEFT);
    1.41 +		glTranslatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
    1.42 +
    1.43 +		render(tsec);
    1.44 +
    1.45 +		/* right eye */
    1.46 +		glDrawBuffer(GL_BACK_RIGHT);
    1.47 +
    1.48 +		glMatrixMode(GL_PROJECTION);
    1.49 +		glLoadIdentity();
    1.50 +		cam_stereo_proj_matrix(CAM_RIGHT);
    1.51 +
    1.52 +		glMatrixMode(GL_MODELVIEW);
    1.53 +		glLoadIdentity();
    1.54 +		cam_stereo_view_matrix(CAM_RIGHT);
    1.55 +		glTranslatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
    1.56 +
    1.57 +		render(tsec);
    1.58 +#endif
    1.59 +	} else {
    1.60 +		gl_matrix_mode(GL_MODELVIEW);
    1.61 +		gl_load_identity();
    1.62 +		cam_view_matrix();
    1.63 +		gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
    1.64 +
    1.65 +		render(tsec);
    1.66 +	}
    1.67 +
    1.68 +
    1.69 +	/*glClearColor(0.4, 0.6, 1.0, 1.0);
    1.70  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.71  
    1.72  	bind_program(prog);
    1.73 @@ -79,11 +128,119 @@
    1.74  	gl_vertex3f(-1, 1, 0);
    1.75  	gl_end();
    1.76  
    1.77 -	bind_texture(0, 0);
    1.78 +	bind_texture(0, 0);*/
    1.79  
    1.80  	assert(glGetError() == GL_NO_ERROR);
    1.81  }
    1.82  
    1.83 +static void render(float t)
    1.84 +{
    1.85 +	glClear(GL_COLOR_BUFFER_BIT);
    1.86 +
    1.87 +	draw_tunnel(t);
    1.88 +}
    1.89 +
    1.90 +static void draw_tunnel(float t)
    1.91 +{
    1.92 +	static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
    1.93 +	static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
    1.94 +	int i, j, k;
    1.95 +	float du, dv;
    1.96 +
    1.97 +	bind_program(prog);
    1.98 +	set_uniform_float(prog, "t", t);
    1.99 +
   1.100 +	bind_texture(tex, 0);
   1.101 +
   1.102 +	/*glMatrixMode(GL_TEXTURE);
   1.103 +	glLoadIdentity();
   1.104 +	//glTranslatef(0, -t * 1.5, 0);
   1.105 +	glTranslatef(0, -t * 0.5, 0);*/
   1.106 +
   1.107 +	gl_begin(GL_QUADS);
   1.108 +	gl_color3f(1.0, 1.0, 1.0);
   1.109 +
   1.110 +	du = 1.0 / sides;
   1.111 +	dv = 1.0 / segm;
   1.112 +
   1.113 +	for(i=0; i<segm; i++) {
   1.114 +		float trans_zp[2], trans_z0[2], trans_z1[2];
   1.115 +
   1.116 +		float zp = ring_height * (i - 1);
   1.117 +		float z0 = ring_height * i;
   1.118 +		float z1 = ring_height * (i + 1);
   1.119 +
   1.120 +		float v0 = (float)i / (float)segm;
   1.121 +		float v1 = (float)(i + 1) / (float)segm;
   1.122 +
   1.123 +		worm(t, zp, trans_zp, trans_zp + 1);
   1.124 +		worm(t, z0, trans_z0, trans_z0 + 1);
   1.125 +		worm(t, z1, trans_z1, trans_z1 + 1);
   1.126 +
   1.127 +		for(j=0; j<sides; j++) {
   1.128 +			for(k=0; k<4; k++) {
   1.129 +				float u = (j + uoffs[k]) * du;
   1.130 +				float v = (i + voffs[k]) * dv;
   1.131 +
   1.132 +				tunnel_vertex(u, v, du, dv, t);
   1.133 +			}
   1.134 +		}
   1.135 +	}
   1.136 +	gl_end();
   1.137 +
   1.138 +	bind_texture(0, 0);
   1.139 +}
   1.140 +
   1.141 +static void tunnel_vertex(float u, float v, float du, float dv, float t)
   1.142 +{
   1.143 +	float pos[3];
   1.144 +
   1.145 +	float theta = 2.0 * M_PI * u;
   1.146 +	float theta1 = 2.0 * M_PI * (u + du);
   1.147 +
   1.148 +	float x = cos(theta);
   1.149 +	float y = sin(theta);
   1.150 +	float x1 = cos(theta1);
   1.151 +	float y1 = sin(theta1);
   1.152 +	float z = v / dv * ring_height;
   1.153 +	float z1 = (v + dv) / dv * ring_height;
   1.154 +
   1.155 +	float trans_z[2], trans_z1[2];
   1.156 +
   1.157 +	worm(t, z, trans_z, trans_z + 1);
   1.158 +	worm(t, z1, trans_z1, trans_z1 + 1);
   1.159 +
   1.160 +	pos[0] = x + trans_z[0];
   1.161 +	pos[1] = y + trans_z[1];
   1.162 +	pos[2] = z;
   1.163 +
   1.164 +	/*v3_cons(pos, x + trans_z[0], y + trans_z[1], z);
   1.165 +	v3_cons(pos_du, x1 + trans_z[0], y1 + trans_z[1], z);
   1.166 +	v3_cons(pos_dv, x + trans_z1[0], y + trans_z1[1], z1);*/
   1.167 +
   1.168 +	/*v3_sub(dfdu, pos_du, pos);
   1.169 +	v3_sub(dfdv, pos_dv, pos);
   1.170 +	v3_cross(norm, dfdv, dfdu);*/
   1.171 +
   1.172 +	/*glVertexAttrib3f(tloc, dfdu[0], dfdu[1], dfdu[2]);
   1.173 +	glNormal3f(norm[0], norm[1], norm[2]);*/
   1.174 +	gl_texcoord2f(u * 2.0, v * 4.0);
   1.175 +	gl_vertex3f(pos[0], pos[1], pos[2]);
   1.176 +}
   1.177 +
   1.178 +
   1.179 +
   1.180 +static void worm(float t, float z, float *tx, float *ty)
   1.181 +{
   1.182 +	float x, y;
   1.183 +	x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
   1.184 +	y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
   1.185 +
   1.186 +	*tx = x * 0.5;
   1.187 +	*ty = y * 0.5;
   1.188 +}
   1.189 +
   1.190 +
   1.191  void reshape(int x, int y)
   1.192  {
   1.193  	glViewport(0, 0, x, y);