istereo2

view src/istereo.c @ 6:3bccfc7d10fe

goatkit is drawing
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 23 Sep 2015 05:44:58 +0300
parents d4fed8aac9a6
children a3c4fcc9f8f3
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"
35 static void render(float t);
36 static void draw_tunnel(float t);
37 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t);
38 static vec3_t calc_text_pos(float sec);
39 static void draw_text(float idx, vec3_t tpos, float alpha);
40 static void worm(float t, float z, float *tx, float *ty);
41 static unsigned int get_shader_program(const char *vfile, const char *pfile);
42 static float get_sec(void);
44 unsigned int prog, prog_simple, prog_tunnel, prog_text, prog_color, prog_ui;
45 unsigned int tex, tex_stones, tex_normal, tex_text;
47 int view_xsz, view_ysz;
49 int stereo = 0;
50 int use_bump = 0;
51 int show_opt = 1;
53 /* construction parameters */
54 int sides = 24;
55 int segm = 20;
56 float tunnel_speed = 0.75;
57 float ring_height = 0.5;
58 float text_period = 13.0;
59 float text_speed = 2.2;
61 float split = 0.5275;
63 int init(void)
64 {
65 add_resource_path("sdr");
66 add_resource_path("data");
68 if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
69 return -1;
70 }
71 if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
72 return -1;
73 }
74 if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
75 return -1;
76 }
77 if(!(prog_color = get_shader_program("color.v.glsl", "color.p.glsl"))) {
78 return -1;
79 }
80 if(!(prog_ui = get_shader_program("ui.v.glsl", "ui.p.glsl"))) {
81 return -1;
82 }
84 if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
85 return -1;
86 }
87 if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
88 return -1;
89 }
90 if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
91 return -1;
92 }
93 if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
94 return -1;
95 }
97 glEnable(GL_DEPTH_TEST);
98 glEnable(GL_CULL_FACE);
100 if(ui_init() == -1) {
101 return -1;
102 }
104 cam_fov(42.5);
105 cam_clip(0.5, 250.0);
107 return 0;
108 }
110 void cleanup(void)
111 {
112 ui_shutdown();
113 free_program(prog_simple);
114 free_program(prog_tunnel);
115 free_program(prog_color);
116 free_program(prog_ui);
117 }
119 void redraw(void)
120 {
121 float pan_x, pan_y, z;
122 float tsec = get_sec();
124 z = ring_height * segm;
125 worm(tsec, z, &pan_x, &pan_y);
127 if(use_bump) {
128 glClearColor(0.01, 0.01, 0.01, 1.0);
129 tunnel_speed = 0.5;
130 } else {
131 glClearColor(0.6, 0.6, 0.6, 1.0);
132 tunnel_speed = 0.75;
133 }
134 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
136 if(stereo) {
137 int split_pt = (int)((float)view_xsz * split);
139 /* right eye */
140 glViewport(0, 0, split_pt, view_ysz);
141 cam_aspect((float)split_pt / (float)view_ysz);
143 gl_matrix_mode(GL_PROJECTION);
144 gl_load_identity();
145 cam_stereo_proj_matrix(CAM_RIGHT);
146 //gl_rotatef(-90, 0, 0, 1);
148 gl_matrix_mode(GL_MODELVIEW);
149 gl_load_identity();
150 cam_stereo_view_matrix(CAM_RIGHT);
151 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
153 render(tsec);
155 /* left eye */
156 glViewport(split_pt, 0, view_xsz - split_pt, view_ysz);
157 cam_aspect((float)(view_xsz - split_pt) / (float)view_ysz);
159 gl_matrix_mode(GL_PROJECTION);
160 gl_load_identity();
161 cam_stereo_proj_matrix(CAM_LEFT);
162 //gl_rotatef(-90, 0, 0, 1);
164 gl_matrix_mode(GL_MODELVIEW);
165 gl_load_identity();
166 cam_stereo_view_matrix(CAM_LEFT);
167 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
169 render(tsec);
170 } else {
171 glViewport(0, 0, view_xsz, view_ysz);
172 cam_aspect((float)view_xsz / (float)view_ysz);
174 gl_matrix_mode(GL_PROJECTION);
175 gl_load_identity();
176 //gl_rotatef(-90, 0, 0, 1);
177 cam_proj_matrix();
179 gl_matrix_mode(GL_MODELVIEW);
180 gl_load_identity();
181 cam_view_matrix();
182 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
184 render(tsec);
185 }
187 assert(glGetError() == GL_NO_ERROR);
188 }
190 static void render(float t)
191 {
192 int i;
193 float text_line;
195 draw_tunnel(t);
197 if(use_bump) {
198 glDepthMask(0);
199 text_line = floor((text_speed * t) / text_period);
200 for(i=0; i<8; i++) {
201 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
202 draw_text(text_line, tpos, 1.5 / (float)i);
203 }
204 glDepthMask(1);
205 }
207 if(show_opt) {
208 ui_draw();
209 }
210 }
212 static void draw_tunnel(float t)
213 {
214 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
215 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
216 int i, j, k, tang_loc = -1;
217 float du, dv;
219 prog = use_bump ? prog_tunnel : prog_simple;
221 bind_program(prog);
222 set_uniform_float(prog, "t", t);
224 if(use_bump) {
225 vec3_t ltpos = calc_text_pos(t);
227 bind_texture(tex_normal, 1);
228 set_uniform_int(prog, "tex_norm", 1);
229 bind_texture(tex_stones, 0);
230 set_uniform_int(prog, "tex", 0);
232 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
233 tang_loc = get_attrib_loc(prog, "attr_tangent");
234 } else {
235 bind_texture(tex, 0);
236 set_uniform_int(prog, "tex", 0);
237 }
239 gl_matrix_mode(GL_TEXTURE);
240 gl_load_identity();
241 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
243 gl_begin(GL_QUADS);
244 gl_color3f(1.0, 1.0, 1.0);
246 du = 1.0 / sides;
247 dv = 1.0 / segm;
249 for(i=0; i<segm; i++) {
250 float trans_zp[2], trans_z0[2], trans_z1[2];
252 float zp = ring_height * (i - 1);
253 float z0 = ring_height * i;
254 float z1 = ring_height * (i + 1);
256 worm(t, zp, trans_zp, trans_zp + 1);
257 worm(t, z0, trans_z0, trans_z0 + 1);
258 worm(t, z1, trans_z1, trans_z1 + 1);
260 for(j=0; j<sides; j++) {
261 for(k=0; k<4; k++) {
262 float u = (j + uoffs[k]) * du;
263 float v = (i + voffs[k]) * dv;
265 tunnel_vertex(u, v, du, dv, tang_loc, t);
266 }
267 }
268 }
269 gl_end();
271 bind_texture(0, 1);
272 bind_texture(0, 0);
273 }
275 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
276 {
277 vec3_t pos, norm;
278 vec3_t dfdu, dfdv, pos_du, pos_dv;
280 float theta = 2.0 * M_PI * u;
281 float theta1 = 2.0 * M_PI * (u + du);
283 float x = cos(theta);
284 float y = sin(theta);
285 float x1 = cos(theta1);
286 float y1 = sin(theta1);
287 float z = v / dv * ring_height;
288 float z1 = (v + dv) / dv * ring_height;
290 float trans_z[2], trans_z1[2];
292 worm(t, z, trans_z, trans_z + 1);
293 worm(t, z1, trans_z1, trans_z1 + 1);
295 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
296 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
297 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
299 dfdu = v3_sub(pos_du, pos);
300 dfdv = v3_sub(pos_dv, pos);
301 norm = v3_cross(dfdv, dfdu);
303 gl_vertex_attrib3f(tang_loc, dfdu.x, -dfdu.y, dfdu.z);
304 gl_normal3f(norm.x, norm.y, norm.z);
305 gl_texcoord2f(u * 2.0, v * 4.0);
306 gl_vertex3f(pos.x, pos.y, pos.z);
307 }
309 static vec3_t calc_text_pos(float sec)
310 {
311 float t = text_speed * sec;
312 float z = fmod(t, text_period);
313 float pan[2];
315 worm(sec, z, pan, pan + 1);
316 return v3_cons(pan[0], pan[1], z + ring_height);
317 }
319 static void draw_text(float idx, vec3_t tpos, float alpha)
320 {
321 gl_matrix_mode(GL_MODELVIEW);
322 gl_push_matrix();
323 gl_translatef(tpos.x, tpos.y, tpos.z);
325 glEnable(GL_BLEND);
326 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
328 bind_program(prog_text);
329 set_uniform_float(prog_text, "idx", idx);
331 bind_texture(tex_text, 0);
333 gl_begin(GL_QUADS);
334 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
336 gl_texcoord2f(0, 1);
337 gl_vertex3f(-1, -0.2, 0);
339 gl_texcoord2f(1, 1);
340 gl_vertex3f(1, -0.2, 0);
342 gl_texcoord2f(1, 0);
343 gl_vertex3f(1, 0.2, 0);
345 gl_texcoord2f(0, 0);
346 gl_vertex3f(-1, 0.2, 0);
347 gl_end();
349 bind_texture(0, 0);
350 glDisable(GL_BLEND);
352 gl_pop_matrix();
353 }
356 static void worm(float t, float z, float *tx, float *ty)
357 {
358 float x, y;
359 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
360 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
362 *tx = x * 0.5;
363 *ty = y * 0.5;
364 }
367 void reshape(int x, int y)
368 {
369 glViewport(0, 0, x, y);
371 float aspect = (float)x / (float)y;
372 float maxfov = 40.0;
373 float vfov = aspect > 1.0 ? maxfov / aspect : maxfov;
375 cam_fov(vfov);
377 gl_matrix_mode(GL_PROJECTION);
378 gl_load_identity();
379 glu_perspective(vfov, aspect, 0.5, 500.0);
381 view_xsz = x;
382 view_ysz = y;
384 ui_reshape(x, y);
385 }
387 void mouse_button(int bn, int press, int x, int y)
388 {
389 if(show_opt) {
390 ui_button(bn, press, x, y);
391 }
392 }
394 void mouse_motion(int x, int y)
395 {
396 if(show_opt) {
397 ui_motion(x, y);
398 }
399 }
401 static unsigned int get_shader_program(const char *vfile, const char *pfile)
402 {
403 unsigned int prog, vs, ps;
405 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
406 return 0;
407 }
408 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
409 return 0;
410 }
412 if(!(prog = create_program_link(vs, ps))) {
413 return 0;
414 }
415 return prog;
416 }
419 #ifdef IPHONE
420 #include <QuartzCore/QuartzCore.h>
422 static float get_sec(void)
423 {
424 static float first;
425 static int init;
427 if(!init) {
428 init = 1;
429 first = CACurrentMediaTime();
430 return 0.0f;
431 }
432 return CACurrentMediaTime() - first;
433 }
435 #else
437 static float get_sec(void)
438 {
439 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
440 }
441 #endif