istereo2

annotate src/istereo.c @ 7:a3c4fcc9f8f3

- started a goatkit UI theme - font rendering with drawtext and shaders - asset manager (only used by drawtext for now, will replace respath eventually)
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 24 Sep 2015 06:49:25 +0300
parents 3bccfc7d10fe
children 661bf09db398
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@2 35
nuclear@2 36 static void render(float t);
nuclear@2 37 static void draw_tunnel(float t);
nuclear@2 38 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t);
nuclear@2 39 static vec3_t calc_text_pos(float sec);
nuclear@2 40 static void draw_text(float idx, vec3_t tpos, float alpha);
nuclear@2 41 static void worm(float t, float z, float *tx, float *ty);
nuclear@2 42 static unsigned int get_shader_program(const char *vfile, const char *pfile);
nuclear@2 43 static float get_sec(void);
nuclear@2 44
nuclear@7 45 unsigned int prog, prog_simple, prog_tunnel, prog_text, prog_color, prog_ui, prog_font;
nuclear@2 46 unsigned int tex, tex_stones, tex_normal, tex_text;
nuclear@7 47 struct dtx_font *font;
nuclear@2 48
nuclear@2 49 int view_xsz, view_ysz;
nuclear@2 50
nuclear@2 51 int stereo = 0;
nuclear@2 52 int use_bump = 0;
nuclear@6 53 int show_opt = 1;
nuclear@2 54
nuclear@2 55 /* construction parameters */
nuclear@2 56 int sides = 24;
nuclear@2 57 int segm = 20;
nuclear@2 58 float tunnel_speed = 0.75;
nuclear@2 59 float ring_height = 0.5;
nuclear@2 60 float text_period = 13.0;
nuclear@2 61 float text_speed = 2.2;
nuclear@2 62
nuclear@2 63 float split = 0.5275;
nuclear@2 64
nuclear@2 65 int init(void)
nuclear@2 66 {
nuclear@2 67 add_resource_path("sdr");
nuclear@2 68 add_resource_path("data");
nuclear@2 69
nuclear@2 70 if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
nuclear@2 71 return -1;
nuclear@2 72 }
nuclear@2 73 if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
nuclear@2 74 return -1;
nuclear@2 75 }
nuclear@2 76 if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
nuclear@2 77 return -1;
nuclear@2 78 }
nuclear@6 79 if(!(prog_color = get_shader_program("color.v.glsl", "color.p.glsl"))) {
nuclear@6 80 return -1;
nuclear@6 81 }
nuclear@6 82 if(!(prog_ui = get_shader_program("ui.v.glsl", "ui.p.glsl"))) {
nuclear@6 83 return -1;
nuclear@6 84 }
nuclear@7 85 if(!(prog_font = get_shader_program("ui.v.glsl", "font.p.glsl"))) {
nuclear@7 86 return -1;
nuclear@7 87 }
nuclear@2 88
nuclear@2 89 if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
nuclear@2 90 return -1;
nuclear@2 91 }
nuclear@2 92 if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
nuclear@2 93 return -1;
nuclear@2 94 }
nuclear@2 95 if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
nuclear@2 96 return -1;
nuclear@2 97 }
nuclear@2 98 if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
nuclear@2 99 return -1;
nuclear@2 100 }
nuclear@2 101
nuclear@7 102 if(!(font = dtx_open_font_glyphmap(find_resource("linux-libertine_s24.glyphmap", 0, 0)))) {
nuclear@7 103 fprintf(stderr, "failed to load font\n");
nuclear@7 104 return -1;
nuclear@7 105 }
nuclear@7 106 dtx_vertex_attribs(get_attrib_loc(prog_ui, "attr_vertex"), get_attrib_loc(prog_ui, "attr_texcoord"));
nuclear@7 107 dtx_use_font(font, 24);
nuclear@7 108
nuclear@2 109 glEnable(GL_DEPTH_TEST);
nuclear@2 110 glEnable(GL_CULL_FACE);
nuclear@2 111
nuclear@6 112 if(ui_init() == -1) {
nuclear@6 113 return -1;
nuclear@6 114 }
nuclear@6 115
nuclear@2 116 cam_fov(42.5);
nuclear@2 117 cam_clip(0.5, 250.0);
nuclear@2 118
nuclear@2 119 return 0;
nuclear@2 120 }
nuclear@2 121
nuclear@2 122 void cleanup(void)
nuclear@2 123 {
nuclear@6 124 ui_shutdown();
nuclear@6 125 free_program(prog_simple);
nuclear@6 126 free_program(prog_tunnel);
nuclear@6 127 free_program(prog_color);
nuclear@6 128 free_program(prog_ui);
nuclear@7 129 free_program(prog_font);
nuclear@7 130 dtx_close_font(font);
nuclear@2 131 }
nuclear@2 132
nuclear@2 133 void redraw(void)
nuclear@2 134 {
nuclear@2 135 float pan_x, pan_y, z;
nuclear@2 136 float tsec = get_sec();
nuclear@2 137
nuclear@2 138 z = ring_height * segm;
nuclear@2 139 worm(tsec, z, &pan_x, &pan_y);
nuclear@2 140
nuclear@2 141 if(use_bump) {
nuclear@2 142 glClearColor(0.01, 0.01, 0.01, 1.0);
nuclear@2 143 tunnel_speed = 0.5;
nuclear@2 144 } else {
nuclear@2 145 glClearColor(0.6, 0.6, 0.6, 1.0);
nuclear@2 146 tunnel_speed = 0.75;
nuclear@2 147 }
nuclear@2 148 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@2 149
nuclear@2 150 if(stereo) {
nuclear@4 151 int split_pt = (int)((float)view_xsz * split);
nuclear@2 152
nuclear@2 153 /* right eye */
nuclear@4 154 glViewport(0, 0, split_pt, view_ysz);
nuclear@4 155 cam_aspect((float)split_pt / (float)view_ysz);
nuclear@2 156
nuclear@2 157 gl_matrix_mode(GL_PROJECTION);
nuclear@2 158 gl_load_identity();
nuclear@2 159 cam_stereo_proj_matrix(CAM_RIGHT);
nuclear@4 160 //gl_rotatef(-90, 0, 0, 1);
nuclear@2 161
nuclear@2 162 gl_matrix_mode(GL_MODELVIEW);
nuclear@2 163 gl_load_identity();
nuclear@2 164 cam_stereo_view_matrix(CAM_RIGHT);
nuclear@2 165 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@2 166
nuclear@2 167 render(tsec);
nuclear@2 168
nuclear@2 169 /* left eye */
nuclear@4 170 glViewport(split_pt, 0, view_xsz - split_pt, view_ysz);
nuclear@4 171 cam_aspect((float)(view_xsz - split_pt) / (float)view_ysz);
nuclear@2 172
nuclear@2 173 gl_matrix_mode(GL_PROJECTION);
nuclear@2 174 gl_load_identity();
nuclear@2 175 cam_stereo_proj_matrix(CAM_LEFT);
nuclear@4 176 //gl_rotatef(-90, 0, 0, 1);
nuclear@2 177
nuclear@2 178 gl_matrix_mode(GL_MODELVIEW);
nuclear@2 179 gl_load_identity();
nuclear@2 180 cam_stereo_view_matrix(CAM_LEFT);
nuclear@2 181 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@2 182
nuclear@2 183 render(tsec);
nuclear@2 184 } else {
nuclear@2 185 glViewport(0, 0, view_xsz, view_ysz);
nuclear@2 186 cam_aspect((float)view_xsz / (float)view_ysz);
nuclear@2 187
nuclear@2 188 gl_matrix_mode(GL_PROJECTION);
nuclear@2 189 gl_load_identity();
nuclear@4 190 //gl_rotatef(-90, 0, 0, 1);
nuclear@2 191 cam_proj_matrix();
nuclear@2 192
nuclear@2 193 gl_matrix_mode(GL_MODELVIEW);
nuclear@2 194 gl_load_identity();
nuclear@2 195 cam_view_matrix();
nuclear@2 196 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@2 197
nuclear@2 198 render(tsec);
nuclear@2 199 }
nuclear@2 200
nuclear@7 201 /* TEST */
nuclear@7 202 /*bind_program(prog_ui);
nuclear@7 203
nuclear@7 204 gl_matrix_mode(GL_PROJECTION);
nuclear@7 205 gl_load_identity();
nuclear@7 206 gl_ortho(0, view_xsz, 0, view_ysz, -1, 1);
nuclear@7 207 gl_matrix_mode(GL_MODELVIEW);
nuclear@7 208 gl_load_identity();
nuclear@7 209 gl_apply_xform(prog_ui);
nuclear@7 210
nuclear@7 211 glDisable(GL_DEPTH_TEST);
nuclear@7 212 dtx_printf("hello world\n");
nuclear@7 213 glEnable(GL_DEPTH_TEST);*/
nuclear@7 214
nuclear@2 215 assert(glGetError() == GL_NO_ERROR);
nuclear@2 216 }
nuclear@2 217
nuclear@2 218 static void render(float t)
nuclear@2 219 {
nuclear@2 220 int i;
nuclear@2 221 float text_line;
nuclear@2 222
nuclear@2 223 draw_tunnel(t);
nuclear@2 224
nuclear@2 225 if(use_bump) {
nuclear@2 226 glDepthMask(0);
nuclear@2 227 text_line = floor((text_speed * t) / text_period);
nuclear@2 228 for(i=0; i<8; i++) {
nuclear@2 229 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
nuclear@2 230 draw_text(text_line, tpos, 1.5 / (float)i);
nuclear@2 231 }
nuclear@2 232 glDepthMask(1);
nuclear@2 233 }
nuclear@6 234
nuclear@6 235 if(show_opt) {
nuclear@6 236 ui_draw();
nuclear@6 237 }
nuclear@2 238 }
nuclear@2 239
nuclear@2 240 static void draw_tunnel(float t)
nuclear@2 241 {
nuclear@2 242 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
nuclear@2 243 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
nuclear@2 244 int i, j, k, tang_loc = -1;
nuclear@2 245 float du, dv;
nuclear@2 246
nuclear@2 247 prog = use_bump ? prog_tunnel : prog_simple;
nuclear@2 248
nuclear@2 249 bind_program(prog);
nuclear@2 250 set_uniform_float(prog, "t", t);
nuclear@2 251
nuclear@2 252 if(use_bump) {
nuclear@2 253 vec3_t ltpos = calc_text_pos(t);
nuclear@2 254
nuclear@2 255 bind_texture(tex_normal, 1);
nuclear@2 256 set_uniform_int(prog, "tex_norm", 1);
nuclear@2 257 bind_texture(tex_stones, 0);
nuclear@2 258 set_uniform_int(prog, "tex", 0);
nuclear@2 259
nuclear@2 260 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
nuclear@2 261 tang_loc = get_attrib_loc(prog, "attr_tangent");
nuclear@2 262 } else {
nuclear@2 263 bind_texture(tex, 0);
nuclear@2 264 set_uniform_int(prog, "tex", 0);
nuclear@2 265 }
nuclear@2 266
nuclear@2 267 gl_matrix_mode(GL_TEXTURE);
nuclear@2 268 gl_load_identity();
nuclear@2 269 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
nuclear@2 270
nuclear@2 271 gl_begin(GL_QUADS);
nuclear@2 272 gl_color3f(1.0, 1.0, 1.0);
nuclear@2 273
nuclear@2 274 du = 1.0 / sides;
nuclear@2 275 dv = 1.0 / segm;
nuclear@2 276
nuclear@2 277 for(i=0; i<segm; i++) {
nuclear@2 278 float trans_zp[2], trans_z0[2], trans_z1[2];
nuclear@2 279
nuclear@2 280 float zp = ring_height * (i - 1);
nuclear@2 281 float z0 = ring_height * i;
nuclear@2 282 float z1 = ring_height * (i + 1);
nuclear@2 283
nuclear@2 284 worm(t, zp, trans_zp, trans_zp + 1);
nuclear@2 285 worm(t, z0, trans_z0, trans_z0 + 1);
nuclear@2 286 worm(t, z1, trans_z1, trans_z1 + 1);
nuclear@2 287
nuclear@2 288 for(j=0; j<sides; j++) {
nuclear@2 289 for(k=0; k<4; k++) {
nuclear@2 290 float u = (j + uoffs[k]) * du;
nuclear@2 291 float v = (i + voffs[k]) * dv;
nuclear@2 292
nuclear@2 293 tunnel_vertex(u, v, du, dv, tang_loc, t);
nuclear@2 294 }
nuclear@2 295 }
nuclear@2 296 }
nuclear@2 297 gl_end();
nuclear@2 298
nuclear@2 299 bind_texture(0, 1);
nuclear@2 300 bind_texture(0, 0);
nuclear@2 301 }
nuclear@2 302
nuclear@2 303 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
nuclear@2 304 {
nuclear@2 305 vec3_t pos, norm;
nuclear@2 306 vec3_t dfdu, dfdv, pos_du, pos_dv;
nuclear@2 307
nuclear@2 308 float theta = 2.0 * M_PI * u;
nuclear@2 309 float theta1 = 2.0 * M_PI * (u + du);
nuclear@2 310
nuclear@2 311 float x = cos(theta);
nuclear@2 312 float y = sin(theta);
nuclear@2 313 float x1 = cos(theta1);
nuclear@2 314 float y1 = sin(theta1);
nuclear@2 315 float z = v / dv * ring_height;
nuclear@2 316 float z1 = (v + dv) / dv * ring_height;
nuclear@2 317
nuclear@2 318 float trans_z[2], trans_z1[2];
nuclear@2 319
nuclear@2 320 worm(t, z, trans_z, trans_z + 1);
nuclear@2 321 worm(t, z1, trans_z1, trans_z1 + 1);
nuclear@2 322
nuclear@2 323 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
nuclear@2 324 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
nuclear@2 325 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
nuclear@2 326
nuclear@2 327 dfdu = v3_sub(pos_du, pos);
nuclear@2 328 dfdv = v3_sub(pos_dv, pos);
nuclear@2 329 norm = v3_cross(dfdv, dfdu);
nuclear@2 330
nuclear@4 331 gl_vertex_attrib3f(tang_loc, dfdu.x, -dfdu.y, dfdu.z);
nuclear@2 332 gl_normal3f(norm.x, norm.y, norm.z);
nuclear@2 333 gl_texcoord2f(u * 2.0, v * 4.0);
nuclear@2 334 gl_vertex3f(pos.x, pos.y, pos.z);
nuclear@2 335 }
nuclear@2 336
nuclear@2 337 static vec3_t calc_text_pos(float sec)
nuclear@2 338 {
nuclear@2 339 float t = text_speed * sec;
nuclear@2 340 float z = fmod(t, text_period);
nuclear@2 341 float pan[2];
nuclear@2 342
nuclear@2 343 worm(sec, z, pan, pan + 1);
nuclear@2 344 return v3_cons(pan[0], pan[1], z + ring_height);
nuclear@2 345 }
nuclear@2 346
nuclear@2 347 static void draw_text(float idx, vec3_t tpos, float alpha)
nuclear@2 348 {
nuclear@2 349 gl_matrix_mode(GL_MODELVIEW);
nuclear@2 350 gl_push_matrix();
nuclear@2 351 gl_translatef(tpos.x, tpos.y, tpos.z);
nuclear@2 352
nuclear@2 353 glEnable(GL_BLEND);
nuclear@2 354 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
nuclear@2 355
nuclear@2 356 bind_program(prog_text);
nuclear@2 357 set_uniform_float(prog_text, "idx", idx);
nuclear@2 358
nuclear@2 359 bind_texture(tex_text, 0);
nuclear@2 360
nuclear@2 361 gl_begin(GL_QUADS);
nuclear@2 362 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
nuclear@2 363
nuclear@2 364 gl_texcoord2f(0, 1);
nuclear@2 365 gl_vertex3f(-1, -0.2, 0);
nuclear@2 366
nuclear@2 367 gl_texcoord2f(1, 1);
nuclear@2 368 gl_vertex3f(1, -0.2, 0);
nuclear@2 369
nuclear@2 370 gl_texcoord2f(1, 0);
nuclear@2 371 gl_vertex3f(1, 0.2, 0);
nuclear@2 372
nuclear@2 373 gl_texcoord2f(0, 0);
nuclear@2 374 gl_vertex3f(-1, 0.2, 0);
nuclear@2 375 gl_end();
nuclear@2 376
nuclear@2 377 bind_texture(0, 0);
nuclear@2 378 glDisable(GL_BLEND);
nuclear@2 379
nuclear@2 380 gl_pop_matrix();
nuclear@2 381 }
nuclear@2 382
nuclear@2 383
nuclear@2 384 static void worm(float t, float z, float *tx, float *ty)
nuclear@2 385 {
nuclear@2 386 float x, y;
nuclear@2 387 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
nuclear@2 388 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
nuclear@2 389
nuclear@2 390 *tx = x * 0.5;
nuclear@2 391 *ty = y * 0.5;
nuclear@2 392 }
nuclear@2 393
nuclear@2 394
nuclear@2 395 void reshape(int x, int y)
nuclear@2 396 {
nuclear@2 397 glViewport(0, 0, x, y);
nuclear@2 398
nuclear@4 399 float aspect = (float)x / (float)y;
nuclear@4 400 float maxfov = 40.0;
nuclear@4 401 float vfov = aspect > 1.0 ? maxfov / aspect : maxfov;
nuclear@4 402
nuclear@4 403 cam_fov(vfov);
nuclear@4 404
nuclear@2 405 gl_matrix_mode(GL_PROJECTION);
nuclear@2 406 gl_load_identity();
nuclear@4 407 glu_perspective(vfov, aspect, 0.5, 500.0);
nuclear@2 408
nuclear@2 409 view_xsz = x;
nuclear@2 410 view_ysz = y;
nuclear@6 411
nuclear@6 412 ui_reshape(x, y);
nuclear@6 413 }
nuclear@6 414
nuclear@6 415 void mouse_button(int bn, int press, int x, int y)
nuclear@6 416 {
nuclear@6 417 if(show_opt) {
nuclear@6 418 ui_button(bn, press, x, y);
nuclear@6 419 }
nuclear@6 420 }
nuclear@6 421
nuclear@6 422 void mouse_motion(int x, int y)
nuclear@6 423 {
nuclear@6 424 if(show_opt) {
nuclear@6 425 ui_motion(x, y);
nuclear@6 426 }
nuclear@2 427 }
nuclear@2 428
nuclear@2 429 static unsigned int get_shader_program(const char *vfile, const char *pfile)
nuclear@2 430 {
nuclear@2 431 unsigned int prog, vs, ps;
nuclear@2 432
nuclear@2 433 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
nuclear@2 434 return 0;
nuclear@2 435 }
nuclear@2 436 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
nuclear@2 437 return 0;
nuclear@2 438 }
nuclear@2 439
nuclear@2 440 if(!(prog = create_program_link(vs, ps))) {
nuclear@2 441 return 0;
nuclear@2 442 }
nuclear@2 443 return prog;
nuclear@2 444 }
nuclear@2 445
nuclear@2 446
nuclear@2 447 #ifdef IPHONE
nuclear@2 448 #include <QuartzCore/QuartzCore.h>
nuclear@2 449
nuclear@2 450 static float get_sec(void)
nuclear@2 451 {
nuclear@2 452 static float first;
nuclear@2 453 static int init;
nuclear@2 454
nuclear@2 455 if(!init) {
nuclear@2 456 init = 1;
nuclear@2 457 first = CACurrentMediaTime();
nuclear@2 458 return 0.0f;
nuclear@2 459 }
nuclear@2 460 return CACurrentMediaTime() - first;
nuclear@2 461 }
nuclear@2 462
nuclear@2 463 #else
nuclear@2 464
nuclear@2 465 static float get_sec(void)
nuclear@2 466 {
nuclear@2 467 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
nuclear@2 468 }
nuclear@2 469 #endif