istereo2

annotate src/istereo.c @ 35:643f4ab609a4

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