istereo

annotate src/istereo.c @ 35:23e5d274b2a2

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