istereo

annotate src/istereo.c @ 28:c0ae8e668447

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