istereo2

view src/istereo.c @ 15:7bd4264bf74a

gui done?
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 30 Sep 2015 04:41:21 +0300
parents ea928c313344
children f4dd01c36b0c
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 int stereo = 0;
55 int use_bump = 0;
56 int show_opt = 1;
58 /* construction parameters */
59 int sides = 24;
60 int segm = 20;
61 float tunnel_speed = 0.75;
62 float ring_height = 0.5;
63 float text_period = 13.0;
64 float text_speed = 2.2;
66 float split = 0.5275;
68 int init(void)
69 {
70 add_resource_path("sdr");
71 add_resource_path("data");
73 if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
74 return -1;
75 }
76 if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
77 return -1;
78 }
79 if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
80 return -1;
81 }
82 if(!(prog_color = get_shader_program("color.v.glsl", "color.p.glsl"))) {
83 return -1;
84 }
85 if(!(prog_ui = get_shader_program("ui.v.glsl", "ui.p.glsl"))) {
86 return -1;
87 }
88 if(!(prog_font = get_shader_program("ui.v.glsl", "font.p.glsl"))) {
89 return -1;
90 }
92 if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
93 return -1;
94 }
95 if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
96 return -1;
97 }
98 if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
99 return -1;
100 }
101 if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
102 return -1;
103 }
105 if(!(font = dtx_open_font_glyphmap(find_resource("droidsans_s24.glyphmap", 0, 0)))) {
106 fprintf(stderr, "failed to load font\n");
107 return -1;
108 }
109 dtx_vertex_attribs(get_attrib_loc(prog_ui, "attr_vertex"), get_attrib_loc(prog_ui, "attr_texcoord"));
111 glEnable(GL_DEPTH_TEST);
112 glEnable(GL_CULL_FACE);
114 if(ui_init() == -1) {
115 return -1;
116 }
117 if(show_opt) {
118 ui_show();
119 }
121 cam_fov(42.5);
122 cam_clip(0.5, 250.0);
124 return 0;
125 }
127 void cleanup(void)
128 {
129 ui_shutdown();
130 free_program(prog_simple);
131 free_program(prog_tunnel);
132 free_program(prog_color);
133 free_program(prog_ui);
134 free_program(prog_font);
135 dtx_close_font(font);
136 }
138 void redraw(void)
139 {
140 float pan_x, pan_y, z;
141 float tsec = get_time_sec();
143 z = ring_height * segm;
144 worm(tsec, z, &pan_x, &pan_y);
146 if(use_bump) {
147 glClearColor(0.01, 0.01, 0.01, 1.0);
148 tunnel_speed = 0.5;
149 } else {
150 glClearColor(0.6, 0.6, 0.6, 1.0);
151 tunnel_speed = 0.75;
152 }
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155 if(stereo) {
156 int split_pt = (int)((float)view_xsz * split);
158 /* right eye */
159 glViewport(0, 0, split_pt, view_ysz);
160 cam_aspect((float)split_pt / (float)view_ysz);
162 gl_matrix_mode(GL_PROJECTION);
163 gl_load_identity();
164 cam_stereo_proj_matrix(CAM_RIGHT);
165 //gl_rotatef(-90, 0, 0, 1);
167 gl_matrix_mode(GL_MODELVIEW);
168 gl_load_identity();
169 cam_stereo_view_matrix(CAM_RIGHT);
170 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
172 render(tsec);
174 /* left eye */
175 glViewport(split_pt, 0, view_xsz - split_pt, view_ysz);
176 cam_aspect((float)(view_xsz - split_pt) / (float)view_ysz);
178 gl_matrix_mode(GL_PROJECTION);
179 gl_load_identity();
180 cam_stereo_proj_matrix(CAM_LEFT);
181 //gl_rotatef(-90, 0, 0, 1);
183 gl_matrix_mode(GL_MODELVIEW);
184 gl_load_identity();
185 cam_stereo_view_matrix(CAM_LEFT);
186 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
188 render(tsec);
189 } else {
190 glViewport(0, 0, view_xsz, view_ysz);
191 cam_aspect((float)view_xsz / (float)view_ysz);
193 gl_matrix_mode(GL_PROJECTION);
194 gl_load_identity();
195 //gl_rotatef(-90, 0, 0, 1);
196 cam_proj_matrix();
198 gl_matrix_mode(GL_MODELVIEW);
199 gl_load_identity();
200 cam_view_matrix();
201 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
203 render(tsec);
204 }
206 #ifndef STEREO_GUI
207 if(ui_visible()) {
208 glViewport(0, 0, view_xsz, view_ysz);
209 ui_draw();
210 }
211 #endif
213 assert(glGetError() == GL_NO_ERROR);
214 }
216 static void render(float t)
217 {
218 int i;
219 float text_line;
221 draw_tunnel(t);
223 if(use_bump) {
224 glDepthMask(0);
225 text_line = floor((text_speed * t) / text_period);
226 for(i=0; i<8; i++) {
227 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
228 draw_text(text_line, tpos, 1.5 / (float)i);
229 }
230 glDepthMask(1);
231 }
233 #ifdef STEREO_GUI
234 if(ui_visible()) {
235 ui_reshape(stereo ? view_xsz / 2.0 : view_xsz, view_ysz);
236 ui_draw();
237 }
238 #endif
239 }
241 static void draw_tunnel(float t)
242 {
243 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
244 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
245 int i, j, k, tang_loc = -1;
246 float du, dv;
248 prog = use_bump ? prog_tunnel : prog_simple;
250 bind_program(prog);
251 set_uniform_float(prog, "t", t);
253 if(use_bump) {
254 vec3_t ltpos = calc_text_pos(t);
256 bind_texture(tex_normal, 1);
257 set_uniform_int(prog, "tex_norm", 1);
258 bind_texture(tex_stones, 0);
259 set_uniform_int(prog, "tex", 0);
261 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
262 tang_loc = get_attrib_loc(prog, "attr_tangent");
263 } else {
264 bind_texture(tex, 0);
265 set_uniform_int(prog, "tex", 0);
266 }
268 gl_matrix_mode(GL_TEXTURE);
269 gl_load_identity();
270 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
272 gl_begin(GL_QUADS);
273 gl_color3f(1.0, 1.0, 1.0);
275 du = 1.0 / sides;
276 dv = 1.0 / segm;
278 for(i=0; i<segm; i++) {
279 float trans_zp[2], trans_z0[2], trans_z1[2];
281 float zp = ring_height * (i - 1);
282 float z0 = ring_height * i;
283 float z1 = ring_height * (i + 1);
285 worm(t, zp, trans_zp, trans_zp + 1);
286 worm(t, z0, trans_z0, trans_z0 + 1);
287 worm(t, z1, trans_z1, trans_z1 + 1);
289 for(j=0; j<sides; j++) {
290 for(k=0; k<4; k++) {
291 float u = (j + uoffs[k]) * du;
292 float v = (i + voffs[k]) * dv;
294 tunnel_vertex(u, v, du, dv, tang_loc, t);
295 }
296 }
297 }
298 gl_end();
300 bind_texture(0, 1);
301 bind_texture(0, 0);
302 }
304 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
305 {
306 vec3_t pos, norm;
307 vec3_t dfdu, dfdv, pos_du, pos_dv;
309 float theta = 2.0 * M_PI * u;
310 float theta1 = 2.0 * M_PI * (u + du);
312 float x = cos(theta);
313 float y = sin(theta);
314 float x1 = cos(theta1);
315 float y1 = sin(theta1);
316 float z = v / dv * ring_height;
317 float z1 = (v + dv) / dv * ring_height;
319 float trans_z[2], trans_z1[2];
321 worm(t, z, trans_z, trans_z + 1);
322 worm(t, z1, trans_z1, trans_z1 + 1);
324 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
325 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
326 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
328 dfdu = v3_sub(pos_du, pos);
329 dfdv = v3_sub(pos_dv, pos);
330 norm = v3_cross(dfdv, dfdu);
332 gl_vertex_attrib3f(tang_loc, dfdu.x, -dfdu.y, dfdu.z);
333 gl_normal3f(norm.x, norm.y, norm.z);
334 gl_texcoord2f(u * 2.0, v * 4.0);
335 gl_vertex3f(pos.x, pos.y, pos.z);
336 }
338 static vec3_t calc_text_pos(float sec)
339 {
340 float t = text_speed * sec;
341 float z = fmod(t, text_period);
342 float pan[2];
344 worm(sec, z, pan, pan + 1);
345 return v3_cons(pan[0], pan[1], z + ring_height);
346 }
348 static void draw_text(float idx, vec3_t tpos, float alpha)
349 {
350 gl_matrix_mode(GL_MODELVIEW);
351 gl_push_matrix();
352 gl_translatef(tpos.x, tpos.y, tpos.z);
354 glEnable(GL_BLEND);
355 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
357 bind_program(prog_text);
358 set_uniform_float(prog_text, "idx", idx);
360 bind_texture(tex_text, 0);
362 gl_begin(GL_QUADS);
363 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
365 gl_texcoord2f(0, 1);
366 gl_vertex3f(-1, -0.2, 0);
368 gl_texcoord2f(1, 1);
369 gl_vertex3f(1, -0.2, 0);
371 gl_texcoord2f(1, 0);
372 gl_vertex3f(1, 0.2, 0);
374 gl_texcoord2f(0, 0);
375 gl_vertex3f(-1, 0.2, 0);
376 gl_end();
378 bind_texture(0, 0);
379 glDisable(GL_BLEND);
381 gl_pop_matrix();
382 }
385 static void worm(float t, float z, float *tx, float *ty)
386 {
387 float x, y;
388 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
389 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
391 *tx = x * 0.5;
392 *ty = y * 0.5;
393 }
396 void reshape(int x, int y)
397 {
398 glViewport(0, 0, x, y);
400 float aspect = (float)x / (float)y;
401 float maxfov = 40.0;
402 float vfov = aspect > 1.0 ? maxfov / aspect : maxfov;
404 cam_fov(vfov);
406 gl_matrix_mode(GL_PROJECTION);
407 gl_load_identity();
408 glu_perspective(vfov, aspect, 0.5, 500.0);
410 view_xsz = x;
411 view_ysz = y;
412 view_aspect = aspect;
414 ui_reshape(x, y);
415 }
417 void mouse_button(int bn, int press, int x, int y)
418 {
419 if(show_opt) {
420 ui_button(bn, press, x, y);
421 } else {
422 if(press) {
423 show_opt = 1;
424 ui_show();
425 }
426 }
427 }
429 void mouse_motion(int x, int y)
430 {
431 if(show_opt) {
432 ui_motion(x, y);
433 }
434 }
436 static unsigned int get_shader_program(const char *vfile, const char *pfile)
437 {
438 unsigned int prog, vs, ps;
440 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
441 return 0;
442 }
443 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
444 return 0;
445 }
447 if(!(prog = create_program_link(vs, ps))) {
448 return 0;
449 }
450 return prog;
451 }