istereo

annotate src/istereo.c @ 34:634c7f7c1f0f

the text blur distance was too much man
author John Tsiombikas <nuclear@mutantstargoat.com>
date Fri, 09 Sep 2011 00:31:54 +0300
parents 33e9eedad390
children 23e5d274b2a2
rev   line source
nuclear@2 1 #include <stdio.h>
nuclear@18 2 #include <math.h>
nuclear@2 3 #include <assert.h>
nuclear@3 4 #include <unistd.h>
nuclear@1 5 #include "opengl.h"
nuclear@0 6 #include "istereo.h"
nuclear@2 7 #include "sanegl.h"
nuclear@2 8 #include "sdr.h"
nuclear@4 9 #include "respath.h"
nuclear@14 10 #include "tex.h"
nuclear@19 11 #include "cam.h"
nuclear@27 12 #include "vmath.h"
nuclear@18 13 #include "config.h"
nuclear@4 14
nuclear@19 15 static void render(float t);
nuclear@19 16 static void draw_tunnel(float t);
nuclear@27 17 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t);
nuclear@27 18 static vec3_t calc_text_pos(float sec);
nuclear@27 19 static void draw_text(float idx, vec3_t tpos, float alpha);
nuclear@19 20 static void worm(float t, float z, float *tx, float *ty);
nuclear@4 21 static unsigned int get_shader_program(const char *vfile, const char *pfile);
nuclear@18 22 static float get_sec(void);
nuclear@2 23
nuclear@27 24 unsigned int prog, prog_simple, prog_tunnel, prog_text;
nuclear@26 25 unsigned int tex, tex_stones, tex_normal, tex_text;
nuclear@0 26
nuclear@21 27 int view_xsz, view_ysz;
nuclear@21 28
nuclear@24 29 #ifdef IPHONE
nuclear@21 30 int stereo = 1;
nuclear@24 31 #else
nuclear@24 32 int stereo = 0;
nuclear@24 33 #endif
nuclear@30 34 int use_bump = 0;
nuclear@18 35
nuclear@18 36 /* construction parameters */
nuclear@18 37 int sides = 24;
nuclear@18 38 int segm = 20;
nuclear@27 39 float tunnel_speed = 0.75;
nuclear@18 40 float ring_height = 0.5;
nuclear@27 41 float text_period = 13.0;
nuclear@27 42 float text_speed = 2.2;
nuclear@18 43
nuclear@30 44 float split = 0.5275;
nuclear@18 45
nuclear@0 46 int init(void)
nuclear@0 47 {
nuclear@4 48 add_resource_path("sdr");
nuclear@14 49 add_resource_path("data");
nuclear@3 50
nuclear@27 51 if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
nuclear@27 52 return -1;
nuclear@27 53 }
nuclear@27 54 if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
nuclear@27 55 return -1;
nuclear@27 56 }
nuclear@27 57 if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
nuclear@2 58 return -1;
nuclear@2 59 }
nuclear@14 60
nuclear@26 61 if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
nuclear@27 62 return -1;
nuclear@27 63 }
nuclear@27 64 if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
nuclear@27 65 return -1;
nuclear@27 66 }
nuclear@27 67 if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
nuclear@27 68 return -1;
nuclear@27 69 }
nuclear@27 70 if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
nuclear@14 71 return -1;
nuclear@14 72 }
nuclear@14 73
nuclear@27 74 glEnable(GL_DEPTH_TEST);
nuclear@27 75 glEnable(GL_CULL_FACE);
nuclear@27 76
nuclear@30 77 cam_fov(42.5);
nuclear@30 78 cam_clip(0.5, 250.0);
nuclear@21 79
nuclear@0 80 return 0;
nuclear@0 81 }
nuclear@0 82
nuclear@0 83 void cleanup(void)
nuclear@0 84 {
nuclear@2 85 free_program(prog);
nuclear@0 86 }
nuclear@0 87
nuclear@0 88 void redraw(void)
nuclear@0 89 {
nuclear@19 90 float pan_x, pan_y, z;
nuclear@19 91 float tsec = get_sec();
nuclear@18 92
nuclear@21 93 z = ring_height * segm;
nuclear@19 94 worm(tsec, z, &pan_x, &pan_y);
nuclear@19 95
nuclear@27 96 if(use_bump) {
nuclear@27 97 glClearColor(0.01, 0.01, 0.01, 1.0);
nuclear@27 98 tunnel_speed = 0.5;
nuclear@27 99 } else {
nuclear@27 100 glClearColor(0.6, 0.6, 0.6, 1.0);
nuclear@27 101 tunnel_speed = 0.75;
nuclear@27 102 }
nuclear@27 103 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@21 104
nuclear@19 105 if(stereo) {
nuclear@21 106 int split_pt = (int)((float)view_ysz * split);
nuclear@21 107
nuclear@23 108 /* right eye */
nuclear@21 109 glViewport(0, 0, view_xsz, split_pt);
nuclear@21 110 cam_aspect((float)split_pt / (float)view_xsz);
nuclear@19 111
nuclear@21 112 gl_matrix_mode(GL_PROJECTION);
nuclear@21 113 gl_load_identity();
nuclear@23 114 cam_stereo_proj_matrix(CAM_RIGHT);
nuclear@23 115 gl_rotatef(-90, 0, 0, 1);
nuclear@23 116
nuclear@23 117 gl_matrix_mode(GL_MODELVIEW);
nuclear@23 118 gl_load_identity();
nuclear@23 119 cam_stereo_view_matrix(CAM_RIGHT);
nuclear@23 120 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@23 121 /*gl_rotatef(-90, 0, 0, 1);*/
nuclear@23 122
nuclear@23 123 render(tsec);
nuclear@23 124
nuclear@23 125 /* left eye */
nuclear@23 126 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
nuclear@23 127 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
nuclear@23 128
nuclear@23 129 gl_matrix_mode(GL_PROJECTION);
nuclear@23 130 gl_load_identity();
nuclear@19 131 cam_stereo_proj_matrix(CAM_LEFT);
nuclear@21 132 gl_rotatef(-90, 0, 0, 1);
nuclear@19 133
nuclear@21 134 gl_matrix_mode(GL_MODELVIEW);
nuclear@21 135 gl_load_identity();
nuclear@19 136 cam_stereo_view_matrix(CAM_LEFT);
nuclear@21 137 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@21 138 /*gl_rotatef(-90, 0, 0, 1);*/
nuclear@19 139
nuclear@19 140 render(tsec);
nuclear@19 141 } else {
nuclear@19 142 gl_matrix_mode(GL_MODELVIEW);
nuclear@19 143 gl_load_identity();
nuclear@19 144 cam_view_matrix();
nuclear@19 145 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@19 146
nuclear@19 147 render(tsec);
nuclear@19 148 }
nuclear@19 149
nuclear@2 150 assert(glGetError() == GL_NO_ERROR);
nuclear@0 151 }
nuclear@2 152
nuclear@19 153 static void render(float t)
nuclear@19 154 {
nuclear@27 155 int i;
nuclear@27 156 float text_line;
nuclear@27 157
nuclear@19 158 draw_tunnel(t);
nuclear@27 159
nuclear@30 160 if(use_bump) {
nuclear@30 161 glDepthMask(0);
nuclear@30 162 text_line = floor((text_speed * t) / text_period);
nuclear@30 163 for(i=0; i<8; i++) {
nuclear@34 164 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
nuclear@30 165 draw_text(text_line, tpos, 1.5 / (float)i);
nuclear@30 166 }
nuclear@30 167 glDepthMask(1);
nuclear@27 168 }
nuclear@19 169 }
nuclear@19 170
nuclear@19 171 static void draw_tunnel(float t)
nuclear@19 172 {
nuclear@19 173 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
nuclear@19 174 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
nuclear@27 175 int i, j, k, tang_loc = -1;
nuclear@19 176 float du, dv;
nuclear@19 177
nuclear@27 178 prog = use_bump ? prog_tunnel : prog_simple;
nuclear@27 179
nuclear@19 180 bind_program(prog);
nuclear@19 181 set_uniform_float(prog, "t", t);
nuclear@27 182
nuclear@27 183 if(use_bump) {
nuclear@27 184 vec3_t ltpos = calc_text_pos(t);
nuclear@27 185
nuclear@30 186 bind_texture(tex_normal, 1);
nuclear@27 187 set_uniform_int(prog, "tex_norm", 1);
nuclear@30 188 bind_texture(tex_stones, 0);
nuclear@30 189 set_uniform_int(prog, "tex", 0);
nuclear@30 190
nuclear@27 191 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
nuclear@27 192 tang_loc = get_attrib_loc(prog, "attr_tangent");
nuclear@30 193 } else {
nuclear@30 194 bind_texture(tex, 0);
nuclear@30 195 set_uniform_int(prog, "tex", 0);
nuclear@27 196 }
nuclear@19 197
nuclear@24 198 gl_matrix_mode(GL_TEXTURE);
nuclear@24 199 gl_load_identity();
nuclear@32 200 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
nuclear@19 201
nuclear@19 202 gl_begin(GL_QUADS);
nuclear@19 203 gl_color3f(1.0, 1.0, 1.0);
nuclear@19 204
nuclear@19 205 du = 1.0 / sides;
nuclear@19 206 dv = 1.0 / segm;
nuclear@19 207
nuclear@19 208 for(i=0; i<segm; i++) {
nuclear@19 209 float trans_zp[2], trans_z0[2], trans_z1[2];
nuclear@19 210
nuclear@19 211 float zp = ring_height * (i - 1);
nuclear@19 212 float z0 = ring_height * i;
nuclear@19 213 float z1 = ring_height * (i + 1);
nuclear@19 214
nuclear@19 215 worm(t, zp, trans_zp, trans_zp + 1);
nuclear@19 216 worm(t, z0, trans_z0, trans_z0 + 1);
nuclear@19 217 worm(t, z1, trans_z1, trans_z1 + 1);
nuclear@19 218
nuclear@19 219 for(j=0; j<sides; j++) {
nuclear@19 220 for(k=0; k<4; k++) {
nuclear@19 221 float u = (j + uoffs[k]) * du;
nuclear@19 222 float v = (i + voffs[k]) * dv;
nuclear@19 223
nuclear@27 224 tunnel_vertex(u, v, du, dv, tang_loc, t);
nuclear@19 225 }
nuclear@19 226 }
nuclear@19 227 }
nuclear@19 228 gl_end();
nuclear@19 229
nuclear@27 230 bind_texture(0, 1);
nuclear@19 231 bind_texture(0, 0);
nuclear@19 232 }
nuclear@19 233
nuclear@27 234 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
nuclear@19 235 {
nuclear@27 236 vec3_t pos, norm;
nuclear@27 237 vec3_t dfdu, dfdv, pos_du, pos_dv;
nuclear@19 238
nuclear@19 239 float theta = 2.0 * M_PI * u;
nuclear@19 240 float theta1 = 2.0 * M_PI * (u + du);
nuclear@19 241
nuclear@19 242 float x = cos(theta);
nuclear@19 243 float y = sin(theta);
nuclear@19 244 float x1 = cos(theta1);
nuclear@19 245 float y1 = sin(theta1);
nuclear@19 246 float z = v / dv * ring_height;
nuclear@19 247 float z1 = (v + dv) / dv * ring_height;
nuclear@19 248
nuclear@19 249 float trans_z[2], trans_z1[2];
nuclear@19 250
nuclear@19 251 worm(t, z, trans_z, trans_z + 1);
nuclear@19 252 worm(t, z1, trans_z1, trans_z1 + 1);
nuclear@19 253
nuclear@27 254 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
nuclear@27 255 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
nuclear@27 256 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
nuclear@19 257
nuclear@27 258 dfdu = v3_sub(pos_du, pos);
nuclear@27 259 dfdv = v3_sub(pos_dv, pos);
nuclear@27 260 norm = v3_cross(dfdv, dfdu);
nuclear@19 261
nuclear@27 262 gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
nuclear@27 263 gl_normal3f(norm.x, norm.y, norm.z);
nuclear@19 264 gl_texcoord2f(u * 2.0, v * 4.0);
nuclear@27 265 gl_vertex3f(pos.x, pos.y, pos.z);
nuclear@19 266 }
nuclear@19 267
nuclear@27 268 static vec3_t calc_text_pos(float sec)
nuclear@27 269 {
nuclear@27 270 float t = text_speed * sec;
nuclear@27 271 float z = fmod(t, text_period);
nuclear@27 272 float pan[2];
nuclear@27 273
nuclear@27 274 worm(sec, z, pan, pan + 1);
nuclear@27 275 return v3_cons(pan[0], pan[1], z + ring_height);
nuclear@27 276 }
nuclear@27 277
nuclear@27 278 static void draw_text(float idx, vec3_t tpos, float alpha)
nuclear@27 279 {
nuclear@27 280 gl_matrix_mode(GL_MODELVIEW);
nuclear@27 281 gl_push_matrix();
nuclear@27 282 gl_translatef(tpos.x, tpos.y, tpos.z);
nuclear@27 283
nuclear@27 284 glEnable(GL_BLEND);
nuclear@27 285 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
nuclear@27 286
nuclear@27 287 bind_program(prog_text);
nuclear@33 288 set_uniform_float(prog_text, "idx", idx);
nuclear@27 289
nuclear@27 290 bind_texture(tex_text, 0);
nuclear@27 291
nuclear@27 292 gl_begin(GL_QUADS);
nuclear@27 293 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
nuclear@27 294
nuclear@27 295 gl_texcoord2f(0, 1);
nuclear@27 296 gl_vertex3f(-1, -0.2, 0);
nuclear@27 297
nuclear@27 298 gl_texcoord2f(1, 1);
nuclear@27 299 gl_vertex3f(1, -0.2, 0);
nuclear@27 300
nuclear@27 301 gl_texcoord2f(1, 0);
nuclear@27 302 gl_vertex3f(1, 0.2, 0);
nuclear@27 303
nuclear@27 304 gl_texcoord2f(0, 0);
nuclear@27 305 gl_vertex3f(-1, 0.2, 0);
nuclear@27 306 gl_end();
nuclear@27 307
nuclear@27 308 bind_texture(0, 0);
nuclear@27 309 glDisable(GL_BLEND);
nuclear@27 310
nuclear@27 311 gl_pop_matrix();
nuclear@27 312 }
nuclear@19 313
nuclear@19 314
nuclear@19 315 static void worm(float t, float z, float *tx, float *ty)
nuclear@19 316 {
nuclear@19 317 float x, y;
nuclear@19 318 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
nuclear@19 319 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
nuclear@19 320
nuclear@19 321 *tx = x * 0.5;
nuclear@19 322 *ty = y * 0.5;
nuclear@19 323 }
nuclear@19 324
nuclear@19 325
nuclear@2 326 void reshape(int x, int y)
nuclear@2 327 {
nuclear@2 328 glViewport(0, 0, x, y);
nuclear@2 329
nuclear@2 330 gl_matrix_mode(GL_PROJECTION);
nuclear@2 331 gl_load_identity();
nuclear@21 332 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
nuclear@21 333
nuclear@21 334 view_xsz = x;
nuclear@21 335 view_ysz = y;
nuclear@2 336 }
nuclear@4 337
nuclear@4 338 static unsigned int get_shader_program(const char *vfile, const char *pfile)
nuclear@4 339 {
nuclear@4 340 unsigned int prog, vs, ps;
nuclear@4 341
nuclear@4 342 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
nuclear@27 343 return 0;
nuclear@4 344 }
nuclear@4 345 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
nuclear@27 346 return 0;
nuclear@4 347 }
nuclear@4 348
nuclear@4 349 if(!(prog = create_program_link(vs, ps))) {
nuclear@27 350 return 0;
nuclear@4 351 }
nuclear@4 352 return prog;
nuclear@4 353 }
nuclear@18 354
nuclear@18 355
nuclear@18 356 #ifdef IPHONE
nuclear@18 357 #include <QuartzCore/QuartzCore.h>
nuclear@18 358
nuclear@18 359 static float get_sec(void)
nuclear@18 360 {
nuclear@18 361 static float first;
nuclear@18 362 static int init;
nuclear@18 363
nuclear@18 364 if(!init) {
nuclear@18 365 init = 1;
nuclear@18 366 first = CACurrentMediaTime();
nuclear@18 367 return 0.0f;
nuclear@18 368 }
nuclear@18 369 return CACurrentMediaTime() - first;
nuclear@18 370 }
nuclear@18 371
nuclear@18 372 #else
nuclear@18 373
nuclear@18 374 static float get_sec(void)
nuclear@18 375 {
nuclear@18 376 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
nuclear@18 377 }
nuclear@18 378 #endif