istereo

view src/istereo.c @ 43:73813c1176de

better hgignore
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 14 Aug 2015 04:33:42 +0300
parents e60f9d8af28d
children
line source
1 /*
2 Stereoscopic tunnel for iOS.
3 Copyright (C) 2011 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"
34 static void render(float t);
35 static void draw_tunnel(float t);
36 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t);
37 static vec3_t calc_text_pos(float sec);
38 static void draw_text(float idx, vec3_t tpos, float alpha);
39 static void worm(float t, float z, float *tx, float *ty);
40 static unsigned int get_shader_program(const char *vfile, const char *pfile);
41 static float get_sec(void);
43 unsigned int prog, prog_simple, prog_tunnel, prog_text;
44 unsigned int tex, tex_stones, tex_normal, tex_text;
46 int view_xsz, view_ysz;
48 int stereo = 0;
49 int use_bump = 0;
51 /* construction parameters */
52 int sides = 24;
53 int segm = 20;
54 float tunnel_speed = 0.75;
55 float ring_height = 0.5;
56 float text_period = 13.0;
57 float text_speed = 2.2;
59 float split = 0.5275;
61 int init(void)
62 {
63 add_resource_path("sdr");
64 add_resource_path("data");
66 if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
67 return -1;
68 }
69 if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
70 return -1;
71 }
72 if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
73 return -1;
74 }
76 if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
77 return -1;
78 }
79 if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
80 return -1;
81 }
82 if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
83 return -1;
84 }
85 if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
86 return -1;
87 }
89 glEnable(GL_DEPTH_TEST);
90 glEnable(GL_CULL_FACE);
92 cam_fov(42.5);
93 cam_clip(0.5, 250.0);
95 return 0;
96 }
98 void cleanup(void)
99 {
100 free_program(prog);
101 }
103 void redraw(void)
104 {
105 float pan_x, pan_y, z;
106 float tsec = get_sec();
108 z = ring_height * segm;
109 worm(tsec, z, &pan_x, &pan_y);
111 if(use_bump) {
112 glClearColor(0.01, 0.01, 0.01, 1.0);
113 tunnel_speed = 0.5;
114 } else {
115 glClearColor(0.6, 0.6, 0.6, 1.0);
116 tunnel_speed = 0.75;
117 }
118 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
120 if(stereo) {
121 int split_pt = (int)((float)view_ysz * split);
123 /* right eye */
124 glViewport(0, 0, view_xsz, split_pt);
125 cam_aspect((float)split_pt / (float)view_xsz);
127 gl_matrix_mode(GL_PROJECTION);
128 gl_load_identity();
129 cam_stereo_proj_matrix(CAM_RIGHT);
130 gl_rotatef(-90, 0, 0, 1);
132 gl_matrix_mode(GL_MODELVIEW);
133 gl_load_identity();
134 cam_stereo_view_matrix(CAM_RIGHT);
135 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
137 render(tsec);
139 /* left eye */
140 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
141 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
143 gl_matrix_mode(GL_PROJECTION);
144 gl_load_identity();
145 cam_stereo_proj_matrix(CAM_LEFT);
146 gl_rotatef(-90, 0, 0, 1);
148 gl_matrix_mode(GL_MODELVIEW);
149 gl_load_identity();
150 cam_stereo_view_matrix(CAM_LEFT);
151 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
153 render(tsec);
154 } else {
155 glViewport(0, 0, view_xsz, view_ysz);
156 cam_aspect((float)view_xsz / (float)view_ysz);
158 gl_matrix_mode(GL_PROJECTION);
159 gl_load_identity();
160 gl_rotatef(-90, 0, 0, 1);
161 cam_proj_matrix();
163 gl_matrix_mode(GL_MODELVIEW);
164 gl_load_identity();
165 cam_view_matrix();
166 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
168 render(tsec);
169 }
171 assert(glGetError() == GL_NO_ERROR);
172 }
174 static void render(float t)
175 {
176 int i;
177 float text_line;
179 draw_tunnel(t);
181 if(use_bump) {
182 glDepthMask(0);
183 text_line = floor((text_speed * t) / text_period);
184 for(i=0; i<8; i++) {
185 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
186 draw_text(text_line, tpos, 1.5 / (float)i);
187 }
188 glDepthMask(1);
189 }
190 }
192 static void draw_tunnel(float t)
193 {
194 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
195 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
196 int i, j, k, tang_loc = -1;
197 float du, dv;
199 prog = use_bump ? prog_tunnel : prog_simple;
201 bind_program(prog);
202 set_uniform_float(prog, "t", t);
204 if(use_bump) {
205 vec3_t ltpos = calc_text_pos(t);
207 bind_texture(tex_normal, 1);
208 set_uniform_int(prog, "tex_norm", 1);
209 bind_texture(tex_stones, 0);
210 set_uniform_int(prog, "tex", 0);
212 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
213 tang_loc = get_attrib_loc(prog, "attr_tangent");
214 } else {
215 bind_texture(tex, 0);
216 set_uniform_int(prog, "tex", 0);
217 }
219 gl_matrix_mode(GL_TEXTURE);
220 gl_load_identity();
221 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
223 gl_begin(GL_QUADS);
224 gl_color3f(1.0, 1.0, 1.0);
226 du = 1.0 / sides;
227 dv = 1.0 / segm;
229 for(i=0; i<segm; i++) {
230 float trans_zp[2], trans_z0[2], trans_z1[2];
232 float zp = ring_height * (i - 1);
233 float z0 = ring_height * i;
234 float z1 = ring_height * (i + 1);
236 worm(t, zp, trans_zp, trans_zp + 1);
237 worm(t, z0, trans_z0, trans_z0 + 1);
238 worm(t, z1, trans_z1, trans_z1 + 1);
240 for(j=0; j<sides; j++) {
241 for(k=0; k<4; k++) {
242 float u = (j + uoffs[k]) * du;
243 float v = (i + voffs[k]) * dv;
245 tunnel_vertex(u, v, du, dv, tang_loc, t);
246 }
247 }
248 }
249 gl_end();
251 bind_texture(0, 1);
252 bind_texture(0, 0);
253 }
255 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
256 {
257 vec3_t pos, norm;
258 vec3_t dfdu, dfdv, pos_du, pos_dv;
260 float theta = 2.0 * M_PI * u;
261 float theta1 = 2.0 * M_PI * (u + du);
263 float x = cos(theta);
264 float y = sin(theta);
265 float x1 = cos(theta1);
266 float y1 = sin(theta1);
267 float z = v / dv * ring_height;
268 float z1 = (v + dv) / dv * ring_height;
270 float trans_z[2], trans_z1[2];
272 worm(t, z, trans_z, trans_z + 1);
273 worm(t, z1, trans_z1, trans_z1 + 1);
275 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
276 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
277 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
279 dfdu = v3_sub(pos_du, pos);
280 dfdv = v3_sub(pos_dv, pos);
281 norm = v3_cross(dfdv, dfdu);
283 gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
284 gl_normal3f(norm.x, norm.y, norm.z);
285 gl_texcoord2f(u * 2.0, v * 4.0);
286 gl_vertex3f(pos.x, pos.y, pos.z);
287 }
289 static vec3_t calc_text_pos(float sec)
290 {
291 float t = text_speed * sec;
292 float z = fmod(t, text_period);
293 float pan[2];
295 worm(sec, z, pan, pan + 1);
296 return v3_cons(pan[0], pan[1], z + ring_height);
297 }
299 static void draw_text(float idx, vec3_t tpos, float alpha)
300 {
301 gl_matrix_mode(GL_MODELVIEW);
302 gl_push_matrix();
303 gl_translatef(tpos.x, tpos.y, tpos.z);
305 glEnable(GL_BLEND);
306 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
308 bind_program(prog_text);
309 set_uniform_float(prog_text, "idx", idx);
311 bind_texture(tex_text, 0);
313 gl_begin(GL_QUADS);
314 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
316 gl_texcoord2f(0, 1);
317 gl_vertex3f(-1, -0.2, 0);
319 gl_texcoord2f(1, 1);
320 gl_vertex3f(1, -0.2, 0);
322 gl_texcoord2f(1, 0);
323 gl_vertex3f(1, 0.2, 0);
325 gl_texcoord2f(0, 0);
326 gl_vertex3f(-1, 0.2, 0);
327 gl_end();
329 bind_texture(0, 0);
330 glDisable(GL_BLEND);
332 gl_pop_matrix();
333 }
336 static void worm(float t, float z, float *tx, float *ty)
337 {
338 float x, y;
339 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
340 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
342 *tx = x * 0.5;
343 *ty = y * 0.5;
344 }
347 void reshape(int x, int y)
348 {
349 glViewport(0, 0, x, y);
351 gl_matrix_mode(GL_PROJECTION);
352 gl_load_identity();
353 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
355 view_xsz = x;
356 view_ysz = y;
357 }
359 static unsigned int get_shader_program(const char *vfile, const char *pfile)
360 {
361 unsigned int prog, vs, ps;
363 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
364 return 0;
365 }
366 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
367 return 0;
368 }
370 if(!(prog = create_program_link(vs, ps))) {
371 return 0;
372 }
373 return prog;
374 }
377 #ifdef IPHONE
378 #include <QuartzCore/QuartzCore.h>
380 static float get_sec(void)
381 {
382 static float first;
383 static int init;
385 if(!init) {
386 init = 1;
387 first = CACurrentMediaTime();
388 return 0.0f;
389 }
390 return CACurrentMediaTime() - first;
391 }
393 #else
395 static float get_sec(void)
396 {
397 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
398 }
399 #endif