istereo2

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