istereo

annotate src/istereo.c @ 36:834503dcb486

fixed the rotated gui problem
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Sep 2011 10:25:03 +0300
parents 23e5d274b2a2
children e60f9d8af28d
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@36 139 cam_aspect((float)view_xsz / (float)view_ysz);
nuclear@35 140
nuclear@35 141 gl_matrix_mode(GL_PROJECTION);
nuclear@35 142 gl_load_identity();
nuclear@35 143 cam_proj_matrix();
nuclear@36 144
nuclear@19 145 gl_matrix_mode(GL_MODELVIEW);
nuclear@19 146 gl_load_identity();
nuclear@19 147 cam_view_matrix();
nuclear@19 148 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@19 149
nuclear@19 150 render(tsec);
nuclear@19 151 }
nuclear@19 152
nuclear@2 153 assert(glGetError() == GL_NO_ERROR);
nuclear@0 154 }
nuclear@2 155
nuclear@19 156 static void render(float t)
nuclear@19 157 {
nuclear@27 158 int i;
nuclear@27 159 float text_line;
nuclear@27 160
nuclear@19 161 draw_tunnel(t);
nuclear@27 162
nuclear@30 163 if(use_bump) {
nuclear@30 164 glDepthMask(0);
nuclear@30 165 text_line = floor((text_speed * t) / text_period);
nuclear@30 166 for(i=0; i<8; i++) {
nuclear@34 167 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
nuclear@30 168 draw_text(text_line, tpos, 1.5 / (float)i);
nuclear@30 169 }
nuclear@30 170 glDepthMask(1);
nuclear@27 171 }
nuclear@19 172 }
nuclear@19 173
nuclear@19 174 static void draw_tunnel(float t)
nuclear@19 175 {
nuclear@19 176 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
nuclear@19 177 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
nuclear@27 178 int i, j, k, tang_loc = -1;
nuclear@19 179 float du, dv;
nuclear@19 180
nuclear@27 181 prog = use_bump ? prog_tunnel : prog_simple;
nuclear@27 182
nuclear@19 183 bind_program(prog);
nuclear@19 184 set_uniform_float(prog, "t", t);
nuclear@27 185
nuclear@27 186 if(use_bump) {
nuclear@27 187 vec3_t ltpos = calc_text_pos(t);
nuclear@27 188
nuclear@30 189 bind_texture(tex_normal, 1);
nuclear@27 190 set_uniform_int(prog, "tex_norm", 1);
nuclear@30 191 bind_texture(tex_stones, 0);
nuclear@30 192 set_uniform_int(prog, "tex", 0);
nuclear@30 193
nuclear@27 194 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
nuclear@27 195 tang_loc = get_attrib_loc(prog, "attr_tangent");
nuclear@30 196 } else {
nuclear@30 197 bind_texture(tex, 0);
nuclear@30 198 set_uniform_int(prog, "tex", 0);
nuclear@27 199 }
nuclear@19 200
nuclear@24 201 gl_matrix_mode(GL_TEXTURE);
nuclear@24 202 gl_load_identity();
nuclear@32 203 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
nuclear@19 204
nuclear@19 205 gl_begin(GL_QUADS);
nuclear@19 206 gl_color3f(1.0, 1.0, 1.0);
nuclear@19 207
nuclear@19 208 du = 1.0 / sides;
nuclear@19 209 dv = 1.0 / segm;
nuclear@19 210
nuclear@19 211 for(i=0; i<segm; i++) {
nuclear@19 212 float trans_zp[2], trans_z0[2], trans_z1[2];
nuclear@19 213
nuclear@19 214 float zp = ring_height * (i - 1);
nuclear@19 215 float z0 = ring_height * i;
nuclear@19 216 float z1 = ring_height * (i + 1);
nuclear@19 217
nuclear@19 218 worm(t, zp, trans_zp, trans_zp + 1);
nuclear@19 219 worm(t, z0, trans_z0, trans_z0 + 1);
nuclear@19 220 worm(t, z1, trans_z1, trans_z1 + 1);
nuclear@19 221
nuclear@19 222 for(j=0; j<sides; j++) {
nuclear@19 223 for(k=0; k<4; k++) {
nuclear@19 224 float u = (j + uoffs[k]) * du;
nuclear@19 225 float v = (i + voffs[k]) * dv;
nuclear@19 226
nuclear@27 227 tunnel_vertex(u, v, du, dv, tang_loc, t);
nuclear@19 228 }
nuclear@19 229 }
nuclear@19 230 }
nuclear@19 231 gl_end();
nuclear@19 232
nuclear@27 233 bind_texture(0, 1);
nuclear@19 234 bind_texture(0, 0);
nuclear@19 235 }
nuclear@19 236
nuclear@27 237 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
nuclear@19 238 {
nuclear@27 239 vec3_t pos, norm;
nuclear@27 240 vec3_t dfdu, dfdv, pos_du, pos_dv;
nuclear@19 241
nuclear@19 242 float theta = 2.0 * M_PI * u;
nuclear@19 243 float theta1 = 2.0 * M_PI * (u + du);
nuclear@19 244
nuclear@19 245 float x = cos(theta);
nuclear@19 246 float y = sin(theta);
nuclear@19 247 float x1 = cos(theta1);
nuclear@19 248 float y1 = sin(theta1);
nuclear@19 249 float z = v / dv * ring_height;
nuclear@19 250 float z1 = (v + dv) / dv * ring_height;
nuclear@19 251
nuclear@19 252 float trans_z[2], trans_z1[2];
nuclear@19 253
nuclear@19 254 worm(t, z, trans_z, trans_z + 1);
nuclear@19 255 worm(t, z1, trans_z1, trans_z1 + 1);
nuclear@19 256
nuclear@27 257 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
nuclear@27 258 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
nuclear@27 259 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
nuclear@19 260
nuclear@27 261 dfdu = v3_sub(pos_du, pos);
nuclear@27 262 dfdv = v3_sub(pos_dv, pos);
nuclear@27 263 norm = v3_cross(dfdv, dfdu);
nuclear@19 264
nuclear@27 265 gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
nuclear@27 266 gl_normal3f(norm.x, norm.y, norm.z);
nuclear@19 267 gl_texcoord2f(u * 2.0, v * 4.0);
nuclear@27 268 gl_vertex3f(pos.x, pos.y, pos.z);
nuclear@19 269 }
nuclear@19 270
nuclear@27 271 static vec3_t calc_text_pos(float sec)
nuclear@27 272 {
nuclear@27 273 float t = text_speed * sec;
nuclear@27 274 float z = fmod(t, text_period);
nuclear@27 275 float pan[2];
nuclear@27 276
nuclear@27 277 worm(sec, z, pan, pan + 1);
nuclear@27 278 return v3_cons(pan[0], pan[1], z + ring_height);
nuclear@27 279 }
nuclear@27 280
nuclear@27 281 static void draw_text(float idx, vec3_t tpos, float alpha)
nuclear@27 282 {
nuclear@27 283 gl_matrix_mode(GL_MODELVIEW);
nuclear@27 284 gl_push_matrix();
nuclear@27 285 gl_translatef(tpos.x, tpos.y, tpos.z);
nuclear@27 286
nuclear@27 287 glEnable(GL_BLEND);
nuclear@27 288 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
nuclear@27 289
nuclear@27 290 bind_program(prog_text);
nuclear@33 291 set_uniform_float(prog_text, "idx", idx);
nuclear@27 292
nuclear@27 293 bind_texture(tex_text, 0);
nuclear@27 294
nuclear@27 295 gl_begin(GL_QUADS);
nuclear@27 296 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
nuclear@27 297
nuclear@27 298 gl_texcoord2f(0, 1);
nuclear@27 299 gl_vertex3f(-1, -0.2, 0);
nuclear@27 300
nuclear@27 301 gl_texcoord2f(1, 1);
nuclear@27 302 gl_vertex3f(1, -0.2, 0);
nuclear@27 303
nuclear@27 304 gl_texcoord2f(1, 0);
nuclear@27 305 gl_vertex3f(1, 0.2, 0);
nuclear@27 306
nuclear@27 307 gl_texcoord2f(0, 0);
nuclear@27 308 gl_vertex3f(-1, 0.2, 0);
nuclear@27 309 gl_end();
nuclear@27 310
nuclear@27 311 bind_texture(0, 0);
nuclear@27 312 glDisable(GL_BLEND);
nuclear@27 313
nuclear@27 314 gl_pop_matrix();
nuclear@27 315 }
nuclear@19 316
nuclear@19 317
nuclear@19 318 static void worm(float t, float z, float *tx, float *ty)
nuclear@19 319 {
nuclear@19 320 float x, y;
nuclear@19 321 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
nuclear@19 322 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
nuclear@19 323
nuclear@19 324 *tx = x * 0.5;
nuclear@19 325 *ty = y * 0.5;
nuclear@19 326 }
nuclear@19 327
nuclear@19 328
nuclear@2 329 void reshape(int x, int y)
nuclear@2 330 {
nuclear@2 331 glViewport(0, 0, x, y);
nuclear@2 332
nuclear@2 333 gl_matrix_mode(GL_PROJECTION);
nuclear@2 334 gl_load_identity();
nuclear@21 335 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
nuclear@21 336
nuclear@21 337 view_xsz = x;
nuclear@21 338 view_ysz = y;
nuclear@2 339 }
nuclear@4 340
nuclear@4 341 static unsigned int get_shader_program(const char *vfile, const char *pfile)
nuclear@4 342 {
nuclear@4 343 unsigned int prog, vs, ps;
nuclear@4 344
nuclear@4 345 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
nuclear@27 346 return 0;
nuclear@4 347 }
nuclear@4 348 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
nuclear@27 349 return 0;
nuclear@4 350 }
nuclear@4 351
nuclear@4 352 if(!(prog = create_program_link(vs, ps))) {
nuclear@27 353 return 0;
nuclear@4 354 }
nuclear@4 355 return prog;
nuclear@4 356 }
nuclear@18 357
nuclear@18 358
nuclear@18 359 #ifdef IPHONE
nuclear@18 360 #include <QuartzCore/QuartzCore.h>
nuclear@18 361
nuclear@18 362 static float get_sec(void)
nuclear@18 363 {
nuclear@18 364 static float first;
nuclear@18 365 static int init;
nuclear@18 366
nuclear@18 367 if(!init) {
nuclear@18 368 init = 1;
nuclear@18 369 first = CACurrentMediaTime();
nuclear@18 370 return 0.0f;
nuclear@18 371 }
nuclear@18 372 return CACurrentMediaTime() - first;
nuclear@18 373 }
nuclear@18 374
nuclear@18 375 #else
nuclear@18 376
nuclear@18 377 static float get_sec(void)
nuclear@18 378 {
nuclear@18 379 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
nuclear@18 380 }
nuclear@18 381 #endif