istereo2

view src/istereo.c @ 20:2b85d05df3f2

animation controls for easier screenshot grabbing
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 02 Oct 2015 04:54:55 +0300
parents 25d821ab1ca2
children c6971ff4795e
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;
61 /* construction parameters */
62 int sides = 24;
63 int segm = 20;
64 float tunnel_speed = 0.75;
65 float ring_height = 0.5;
66 float text_period = 13.0;
67 float text_speed = 2.2;
69 float split = 0.4725;
71 int init(void)
72 {
73 add_resource_path("sdr");
74 add_resource_path("data");
76 if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
77 return -1;
78 }
79 if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
80 return -1;
81 }
82 if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
83 return -1;
84 }
85 if(!(prog_color = get_shader_program("color.v.glsl", "color.p.glsl"))) {
86 return -1;
87 }
88 if(!(prog_ui = get_shader_program("ui.v.glsl", "ui.p.glsl"))) {
89 return -1;
90 }
91 if(!(prog_font = get_shader_program("ui.v.glsl", "font.p.glsl"))) {
92 return -1;
93 }
95 if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
96 return -1;
97 }
98 if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
99 return -1;
100 }
101 if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
102 return -1;
103 }
104 if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
105 return -1;
106 }
108 if(!(font = dtx_open_font_glyphmap(find_resource("droidsans_s24.glyphmap", 0, 0)))) {
109 fprintf(stderr, "failed to load font\n");
110 return -1;
111 }
112 dtx_vertex_attribs(get_attrib_loc(prog_ui, "attr_vertex"), get_attrib_loc(prog_ui, "attr_texcoord"));
114 glEnable(GL_DEPTH_TEST);
115 glEnable(GL_CULL_FACE);
117 if(ui_init() == -1) {
118 return -1;
119 }
120 if(show_opt) {
121 ui_show();
122 }
124 cam_fov(42.5);
125 cam_clip(0.5, 250.0);
127 return 0;
128 }
130 void cleanup(void)
131 {
132 ui_shutdown();
133 free_program(prog_simple);
134 free_program(prog_tunnel);
135 free_program(prog_color);
136 free_program(prog_ui);
137 free_program(prog_font);
138 dtx_close_font(font);
139 }
141 void redraw(void)
142 {
143 float pan_x, pan_y, z;
144 sys_msec = get_time_msec();
145 time_msec = (paused ? last_pause : sys_msec) + time_offset;
146 double tsec = time_msec / 1000.0;
148 z = ring_height * segm;
149 worm(tsec, z, &pan_x, &pan_y);
151 if(use_bump) {
152 glClearColor(0.01, 0.01, 0.01, 1.0);
153 tunnel_speed = 0.5;
154 } else {
155 glClearColor(0.6, 0.6, 0.6, 1.0);
156 tunnel_speed = 0.75;
157 }
158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
160 if(stereo) {
161 int split_pt = (int)((float)view_xsz * split);
163 /* left eye */
164 glViewport(0, 0, split_pt, view_ysz);
165 cam_aspect((float)split_pt / (float)view_ysz);
167 gl_matrix_mode(GL_PROJECTION);
168 gl_load_identity();
169 cam_stereo_proj_matrix(CAM_LEFT);
170 //gl_rotatef(-90, 0, 0, 1);
172 gl_matrix_mode(GL_MODELVIEW);
173 gl_load_identity();
174 cam_stereo_view_matrix(CAM_LEFT);
175 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
177 render(tsec);
179 /* right eye */
180 glViewport(split_pt, 0, view_xsz - split_pt, view_ysz);
181 cam_aspect((float)(view_xsz - split_pt) / (float)view_ysz);
183 gl_matrix_mode(GL_PROJECTION);
184 gl_load_identity();
185 cam_stereo_proj_matrix(CAM_RIGHT);
186 //gl_rotatef(-90, 0, 0, 1);
188 gl_matrix_mode(GL_MODELVIEW);
189 gl_load_identity();
190 cam_stereo_view_matrix(CAM_RIGHT);
191 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
193 render(tsec);
194 } else {
195 glViewport(0, 0, view_xsz, view_ysz);
196 cam_aspect((float)view_xsz / (float)view_ysz);
198 gl_matrix_mode(GL_PROJECTION);
199 gl_load_identity();
200 //gl_rotatef(-90, 0, 0, 1);
201 cam_proj_matrix();
203 gl_matrix_mode(GL_MODELVIEW);
204 gl_load_identity();
205 cam_view_matrix();
206 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
208 render(tsec);
209 }
211 #ifndef STEREO_GUI
212 if(ui_visible()) {
213 glViewport(0, 0, view_xsz, view_ysz);
214 ui_draw();
215 }
216 #endif
218 assert(glGetError() == GL_NO_ERROR);
219 }
221 static void render(float t)
222 {
223 int i;
224 float text_line;
226 draw_tunnel(t);
228 if(use_bump) {
229 glDepthMask(0);
230 text_line = floor((text_speed * t) / text_period);
231 for(i=0; i<8; i++) {
232 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
233 draw_text(text_line, tpos, 1.5 / (float)i);
234 }
235 glDepthMask(1);
236 }
238 #ifdef STEREO_GUI
239 if(ui_visible()) {
240 ui_reshape(stereo ? view_xsz / 2.0 : view_xsz, view_ysz);
241 ui_draw();
242 }
243 #endif
244 }
246 static void draw_tunnel(float t)
247 {
248 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
249 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
250 int i, j, k, tang_loc = -1;
251 float du, dv;
253 prog = use_bump ? prog_tunnel : prog_simple;
255 bind_program(prog);
256 set_uniform_float(prog, "t", t);
258 if(use_bump) {
259 vec3_t ltpos = calc_text_pos(t);
261 bind_texture(tex_normal, 1);
262 set_uniform_int(prog, "tex_norm", 1);
263 bind_texture(tex_stones, 0);
264 set_uniform_int(prog, "tex", 0);
266 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
267 tang_loc = get_attrib_loc(prog, "attr_tangent");
268 } else {
269 bind_texture(tex, 0);
270 set_uniform_int(prog, "tex", 0);
271 }
273 gl_matrix_mode(GL_TEXTURE);
274 gl_load_identity();
275 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
277 gl_begin(GL_QUADS);
278 gl_color3f(1.0, 1.0, 1.0);
280 du = 1.0 / sides;
281 dv = 1.0 / segm;
283 for(i=0; i<segm; i++) {
284 float trans_zp[2], trans_z0[2], trans_z1[2];
286 float zp = ring_height * (i - 1);
287 float z0 = ring_height * i;
288 float z1 = ring_height * (i + 1);
290 worm(t, zp, trans_zp, trans_zp + 1);
291 worm(t, z0, trans_z0, trans_z0 + 1);
292 worm(t, z1, trans_z1, trans_z1 + 1);
294 for(j=0; j<sides; j++) {
295 for(k=0; k<4; k++) {
296 float u = (j + uoffs[k]) * du;
297 float v = (i + voffs[k]) * dv;
299 tunnel_vertex(u, v, du, dv, tang_loc, t);
300 }
301 }
302 }
303 gl_end();
305 bind_texture(0, 1);
306 bind_texture(0, 0);
307 }
309 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
310 {
311 vec3_t pos, norm;
312 vec3_t dfdu, dfdv, pos_du, pos_dv;
314 float theta = 2.0 * M_PI * u;
315 float theta1 = 2.0 * M_PI * (u + du);
317 float x = cos(theta);
318 float y = sin(theta);
319 float x1 = cos(theta1);
320 float y1 = sin(theta1);
321 float z = v / dv * ring_height;
322 float z1 = (v + dv) / dv * ring_height;
324 float trans_z[2], trans_z1[2];
326 worm(t, z, trans_z, trans_z + 1);
327 worm(t, z1, trans_z1, trans_z1 + 1);
329 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
330 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
331 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
333 dfdu = v3_sub(pos_du, pos);
334 dfdv = v3_sub(pos_dv, pos);
335 norm = v3_cross(dfdv, dfdu);
337 gl_vertex_attrib3f(tang_loc, dfdu.x, -dfdu.y, dfdu.z);
338 gl_normal3f(norm.x, norm.y, norm.z);
339 gl_texcoord2f(u * 2.0, v * 4.0);
340 gl_vertex3f(pos.x, pos.y, pos.z);
341 }
343 static vec3_t calc_text_pos(float sec)
344 {
345 float t = text_speed * sec;
346 float z = fmod(t, text_period);
347 float pan[2];
349 worm(sec, z, pan, pan + 1);
350 return v3_cons(pan[0], pan[1], z + ring_height);
351 }
353 static void draw_text(float idx, vec3_t tpos, float alpha)
354 {
355 gl_matrix_mode(GL_MODELVIEW);
356 gl_push_matrix();
357 gl_translatef(tpos.x, tpos.y, tpos.z);
359 glEnable(GL_BLEND);
360 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
362 bind_program(prog_text);
363 set_uniform_float(prog_text, "idx", idx);
365 bind_texture(tex_text, 0);
367 gl_begin(GL_QUADS);
368 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
370 gl_texcoord2f(0, 1);
371 gl_vertex3f(-1, -0.2, 0);
373 gl_texcoord2f(1, 1);
374 gl_vertex3f(1, -0.2, 0);
376 gl_texcoord2f(1, 0);
377 gl_vertex3f(1, 0.2, 0);
379 gl_texcoord2f(0, 0);
380 gl_vertex3f(-1, 0.2, 0);
381 gl_end();
383 bind_texture(0, 0);
384 glDisable(GL_BLEND);
386 gl_pop_matrix();
387 }
390 static void worm(float t, float z, float *tx, float *ty)
391 {
392 float x, y;
393 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
394 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
396 *tx = x * 0.5;
397 *ty = y * 0.5;
398 }
401 void reshape(int x, int y)
402 {
403 glViewport(0, 0, x, y);
405 float aspect = (float)x / (float)y;
406 float maxfov = 40.0;
407 float vfov = aspect > 1.0 ? maxfov / aspect : maxfov;
409 cam_fov(vfov);
411 gl_matrix_mode(GL_PROJECTION);
412 gl_load_identity();
413 glu_perspective(vfov, aspect, 0.5, 500.0);
415 view_xsz = x;
416 view_ysz = y;
417 view_aspect = aspect;
419 ui_reshape(x, y);
420 }
422 void mouse_button(int bn, int press, int x, int y)
423 {
424 if(show_opt) {
425 ui_button(bn, press, x, y);
426 } else {
427 if(press) {
428 show_opt = 1;
429 ui_show();
430 }
431 }
432 }
434 void mouse_motion(int x, int y)
435 {
436 if(show_opt) {
437 ui_motion(x, y);
438 }
439 }
441 void playpause(void)
442 {
443 paused = !paused;
444 if(paused) {
445 last_pause = sys_msec;
446 } else {
447 time_offset -= sys_msec - last_pause;
448 }
449 }
451 void seektime(long msec)
452 {
453 time_offset -= msec;
454 }
456 static unsigned int get_shader_program(const char *vfile, const char *pfile)
457 {
458 unsigned int prog, vs, ps;
460 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
461 return 0;
462 }
463 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
464 return 0;
465 }
467 if(!(prog = create_program_link(vs, ps))) {
468 return 0;
469 }
470 return prog;
471 }