istereo2
view src/istereo.c @ 24:9d53a4938ce8
port to android mostly complete, ads not done, and needs some polishing
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 04 Oct 2015 08:15:24 +0300 |
parents | c6971ff4795e |
children | 33ba8618972c |
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 static int time_print_pending;
143 void redraw(void)
144 {
145 float pan_x, pan_y, z;
146 sys_msec = get_time_msec();
147 time_msec = (paused ? last_pause : sys_msec) + time_offset;
148 double tsec = time_msec / 1000.0;
150 z = ring_height * segm;
151 worm(tsec, z, &pan_x, &pan_y);
153 if(use_bump) {
154 glClearColor(0.01, 0.01, 0.01, 1.0);
155 tunnel_speed = 0.5;
156 } else {
157 glClearColor(0.6, 0.6, 0.6, 1.0);
158 tunnel_speed = 0.75;
159 }
160 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
162 if(stereo) {
163 int split_pt = (int)((float)view_xsz * split);
165 /* left eye */
166 glViewport(0, 0, split_pt, view_ysz);
167 cam_aspect((float)split_pt / (float)view_ysz);
169 gl_matrix_mode(GL_PROJECTION);
170 gl_load_identity();
171 cam_stereo_proj_matrix(CAM_LEFT);
172 //gl_rotatef(-90, 0, 0, 1);
174 gl_matrix_mode(GL_MODELVIEW);
175 gl_load_identity();
176 cam_stereo_view_matrix(CAM_LEFT);
177 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
179 render(tsec);
181 /* right eye */
182 glViewport(split_pt, 0, view_xsz - split_pt, view_ysz);
183 cam_aspect((float)(view_xsz - split_pt) / (float)view_ysz);
185 gl_matrix_mode(GL_PROJECTION);
186 gl_load_identity();
187 cam_stereo_proj_matrix(CAM_RIGHT);
188 //gl_rotatef(-90, 0, 0, 1);
190 gl_matrix_mode(GL_MODELVIEW);
191 gl_load_identity();
192 cam_stereo_view_matrix(CAM_RIGHT);
193 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
195 render(tsec);
196 } else {
197 glViewport(0, 0, view_xsz, view_ysz);
198 cam_aspect((float)view_xsz / (float)view_ysz);
200 gl_matrix_mode(GL_PROJECTION);
201 gl_load_identity();
202 //gl_rotatef(-90, 0, 0, 1);
203 cam_proj_matrix();
205 gl_matrix_mode(GL_MODELVIEW);
206 gl_load_identity();
207 cam_view_matrix();
208 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
210 render(tsec);
211 }
213 #ifndef STEREO_GUI
214 if(ui_visible()) {
215 glViewport(0, 0, view_xsz, view_ysz);
216 ui_draw();
217 }
218 #endif
220 assert(glGetError() == GL_NO_ERROR);
222 if(time_print_pending) {
223 printf("t: %lds %ldms\n", time_msec / 1000, time_msec % 1000);
224 time_print_pending = 0;
225 }
226 }
228 static void render(float t)
229 {
230 int i;
231 float text_line;
233 draw_tunnel(t);
235 if(use_bump) {
236 glDepthMask(0);
237 text_line = floor((text_speed * t) / text_period);
238 for(i=0; i<8; i++) {
239 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
240 draw_text(text_line, tpos, 1.5 / (float)i);
241 }
242 glDepthMask(1);
243 }
245 #ifdef STEREO_GUI
246 if(ui_visible()) {
247 ui_reshape(stereo ? view_xsz / 2.0 : view_xsz, view_ysz);
248 ui_draw();
249 }
250 #endif
251 }
253 static void draw_tunnel(float t)
254 {
255 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
256 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
257 int i, j, k, tang_loc = -1;
258 float du, dv;
260 prog = use_bump ? prog_tunnel : prog_simple;
262 bind_program(prog);
263 set_uniform_float(prog, "t", t);
265 if(use_bump) {
266 vec3_t ltpos = calc_text_pos(t);
268 bind_texture(tex_normal, 1);
269 set_uniform_int(prog, "tex_norm", 1);
270 bind_texture(tex_stones, 0);
271 set_uniform_int(prog, "tex", 0);
273 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
274 tang_loc = get_attrib_loc(prog, "attr_tangent");
275 } else {
276 bind_texture(tex, 0);
277 set_uniform_int(prog, "tex", 0);
278 }
280 gl_matrix_mode(GL_TEXTURE);
281 gl_load_identity();
282 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
284 gl_begin(GL_QUADS);
285 gl_color3f(1.0, 1.0, 1.0);
287 du = 1.0 / sides;
288 dv = 1.0 / segm;
290 for(i=0; i<segm; i++) {
291 float trans_zp[2], trans_z0[2], trans_z1[2];
293 float zp = ring_height * (i - 1);
294 float z0 = ring_height * i;
295 float z1 = ring_height * (i + 1);
297 worm(t, zp, trans_zp, trans_zp + 1);
298 worm(t, z0, trans_z0, trans_z0 + 1);
299 worm(t, z1, trans_z1, trans_z1 + 1);
301 for(j=0; j<sides; j++) {
302 for(k=0; k<4; k++) {
303 float u = (j + uoffs[k]) * du;
304 float v = (i + voffs[k]) * dv;
306 tunnel_vertex(u, v, du, dv, tang_loc, t);
307 }
308 }
309 }
310 gl_end();
312 bind_texture(0, 1);
313 bind_texture(0, 0);
314 }
316 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
317 {
318 vec3_t pos, norm;
319 vec3_t dfdu, dfdv, pos_du, pos_dv;
321 float theta = 2.0 * M_PI * u;
322 float theta1 = 2.0 * M_PI * (u + du);
324 float x = cos(theta);
325 float y = sin(theta);
326 float x1 = cos(theta1);
327 float y1 = sin(theta1);
328 float z = v / dv * ring_height;
329 float z1 = (v + dv) / dv * ring_height;
331 float trans_z[2], trans_z1[2];
333 worm(t, z, trans_z, trans_z + 1);
334 worm(t, z1, trans_z1, trans_z1 + 1);
336 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
337 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
338 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
340 dfdu = v3_sub(pos_du, pos);
341 dfdv = v3_sub(pos_dv, pos);
342 norm = v3_cross(dfdv, dfdu);
344 gl_vertex_attrib3f(tang_loc, dfdu.x, -dfdu.y, dfdu.z);
345 gl_normal3f(norm.x, norm.y, norm.z);
346 gl_texcoord2f(u * 2.0, v * 4.0);
347 gl_vertex3f(pos.x, pos.y, pos.z);
348 }
350 static vec3_t calc_text_pos(float sec)
351 {
352 float t = text_speed * sec;
353 float z = fmod(t, text_period);
354 float pan[2];
356 worm(sec, z, pan, pan + 1);
357 return v3_cons(pan[0], pan[1], z + ring_height);
358 }
360 static void draw_text(float idx, vec3_t tpos, float alpha)
361 {
362 gl_matrix_mode(GL_MODELVIEW);
363 gl_push_matrix();
364 gl_translatef(tpos.x, tpos.y, tpos.z);
366 glEnable(GL_BLEND);
367 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
369 bind_program(prog_text);
370 set_uniform_float(prog_text, "idx", idx);
372 bind_texture(tex_text, 0);
374 gl_begin(GL_QUADS);
375 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
377 gl_texcoord2f(0, 1);
378 gl_vertex3f(-1, -0.2, 0);
380 gl_texcoord2f(1, 1);
381 gl_vertex3f(1, -0.2, 0);
383 gl_texcoord2f(1, 0);
384 gl_vertex3f(1, 0.2, 0);
386 gl_texcoord2f(0, 0);
387 gl_vertex3f(-1, 0.2, 0);
388 gl_end();
390 bind_texture(0, 0);
391 glDisable(GL_BLEND);
393 gl_pop_matrix();
394 }
397 static void worm(float t, float z, float *tx, float *ty)
398 {
399 float x, y;
400 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
401 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
403 *tx = x * 0.5;
404 *ty = y * 0.5;
405 }
408 void reshape(int x, int y)
409 {
410 glViewport(0, 0, x, y);
412 float aspect = (float)x / (float)y;
413 float maxfov = 42.0;
414 float vfov = aspect > 1.0 ? maxfov / aspect : maxfov;
416 cam_fov(vfov);
418 gl_matrix_mode(GL_PROJECTION);
419 gl_load_identity();
420 glu_perspective(vfov, aspect, 0.5, 500.0);
422 view_xsz = x;
423 view_ysz = y;
424 view_aspect = aspect;
426 ui_reshape(x, y);
427 }
429 void mouse_button(int bn, int press, int x, int y)
430 {
431 if(show_opt) {
432 ui_button(bn, press, x, y);
433 } else {
434 if(press) {
435 show_opt = 1;
436 ui_show();
437 }
438 }
439 }
441 void mouse_motion(int x, int y)
442 {
443 if(show_opt) {
444 ui_motion(x, y);
445 }
446 }
448 void playpause(void)
449 {
450 paused = !paused;
451 if(paused) {
452 last_pause = sys_msec;
453 time_print_pending = 1;
454 } else {
455 time_offset -= sys_msec - last_pause;
456 }
457 }
459 void seektime(long msec)
460 {
461 time_offset += msec;
462 time_print_pending = 1;
463 }
465 static unsigned int get_shader_program(const char *vfile, const char *pfile)
466 {
467 unsigned int prog, vs, ps;
469 if(!(vs = load_vertex_shader(find_resource(vfile, 0, 0)))) {
470 return 0;
471 }
472 if(!(ps = load_pixel_shader(find_resource(pfile, 0, 0)))) {
473 return 0;
474 }
476 if(!(prog = create_program_link(vs, ps, 0))) {
477 return 0;
478 }
479 return prog;
480 }