istereo

annotate src/istereo.c @ 39:ff055bff6a15

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