istereo
view src/istereo.c @ 35:23e5d274b2a2
added options panel, also added the xib files to the repository as they're needed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 09 Sep 2011 10:03:42 +0300 |
parents | 634c7f7c1f0f |
children | 834503dcb486 |
line source
1 #include <stdio.h>
2 #include <math.h>
3 #include <assert.h>
4 #include <unistd.h>
5 #include "opengl.h"
6 #include "istereo.h"
7 #include "sanegl.h"
8 #include "sdr.h"
9 #include "respath.h"
10 #include "tex.h"
11 #include "cam.h"
12 #include "vmath.h"
13 #include "config.h"
15 static void render(float t);
16 static void draw_tunnel(float t);
17 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t);
18 static vec3_t calc_text_pos(float sec);
19 static void draw_text(float idx, vec3_t tpos, float alpha);
20 static void worm(float t, float z, float *tx, float *ty);
21 static unsigned int get_shader_program(const char *vfile, const char *pfile);
22 static float get_sec(void);
24 unsigned int prog, prog_simple, prog_tunnel, prog_text;
25 unsigned int tex, tex_stones, tex_normal, tex_text;
27 int view_xsz, view_ysz;
29 int stereo = 0;
30 int use_bump = 0;
32 /* construction parameters */
33 int sides = 24;
34 int segm = 20;
35 float tunnel_speed = 0.75;
36 float ring_height = 0.5;
37 float text_period = 13.0;
38 float text_speed = 2.2;
40 float split = 0.5275;
42 int init(void)
43 {
44 add_resource_path("sdr");
45 add_resource_path("data");
47 if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
48 return -1;
49 }
50 if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
51 return -1;
52 }
53 if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
54 return -1;
55 }
57 if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
58 return -1;
59 }
60 if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
61 return -1;
62 }
63 if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
64 return -1;
65 }
66 if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
67 return -1;
68 }
70 glEnable(GL_DEPTH_TEST);
71 glEnable(GL_CULL_FACE);
73 cam_fov(42.5);
74 cam_clip(0.5, 250.0);
76 return 0;
77 }
79 void cleanup(void)
80 {
81 free_program(prog);
82 }
84 void redraw(void)
85 {
86 float pan_x, pan_y, z;
87 float tsec = get_sec();
89 z = ring_height * segm;
90 worm(tsec, z, &pan_x, &pan_y);
92 if(use_bump) {
93 glClearColor(0.01, 0.01, 0.01, 1.0);
94 tunnel_speed = 0.5;
95 } else {
96 glClearColor(0.6, 0.6, 0.6, 1.0);
97 tunnel_speed = 0.75;
98 }
99 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
101 if(stereo) {
102 int split_pt = (int)((float)view_ysz * split);
104 /* right eye */
105 glViewport(0, 0, view_xsz, split_pt);
106 cam_aspect((float)split_pt / (float)view_xsz);
108 gl_matrix_mode(GL_PROJECTION);
109 gl_load_identity();
110 cam_stereo_proj_matrix(CAM_RIGHT);
111 gl_rotatef(-90, 0, 0, 1);
113 gl_matrix_mode(GL_MODELVIEW);
114 gl_load_identity();
115 cam_stereo_view_matrix(CAM_RIGHT);
116 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
117 /*gl_rotatef(-90, 0, 0, 1);*/
119 render(tsec);
121 /* left eye */
122 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
123 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
125 gl_matrix_mode(GL_PROJECTION);
126 gl_load_identity();
127 cam_stereo_proj_matrix(CAM_LEFT);
128 gl_rotatef(-90, 0, 0, 1);
130 gl_matrix_mode(GL_MODELVIEW);
131 gl_load_identity();
132 cam_stereo_view_matrix(CAM_LEFT);
133 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
134 /*gl_rotatef(-90, 0, 0, 1);*/
136 render(tsec);
137 } else {
138 glViewport(0, 0, view_xsz, view_ysz);
139 cam_aspect((float)view_ysz / (float)view_xsz);
141 gl_matrix_mode(GL_PROJECTION);
142 gl_load_identity();
143 cam_proj_matrix();
144 gl_rotatef(-90, 0, 0, 1);
146 gl_matrix_mode(GL_MODELVIEW);
147 gl_load_identity();
148 cam_view_matrix();
149 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
151 render(tsec);
152 }
154 assert(glGetError() == GL_NO_ERROR);
155 }
157 static void render(float t)
158 {
159 int i;
160 float text_line;
162 draw_tunnel(t);
164 if(use_bump) {
165 glDepthMask(0);
166 text_line = floor((text_speed * t) / text_period);
167 for(i=0; i<8; i++) {
168 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
169 draw_text(text_line, tpos, 1.5 / (float)i);
170 }
171 glDepthMask(1);
172 }
173 }
175 static void draw_tunnel(float t)
176 {
177 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
178 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
179 int i, j, k, tang_loc = -1;
180 float du, dv;
182 prog = use_bump ? prog_tunnel : prog_simple;
184 bind_program(prog);
185 set_uniform_float(prog, "t", t);
187 if(use_bump) {
188 vec3_t ltpos = calc_text_pos(t);
190 bind_texture(tex_normal, 1);
191 set_uniform_int(prog, "tex_norm", 1);
192 bind_texture(tex_stones, 0);
193 set_uniform_int(prog, "tex", 0);
195 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
196 tang_loc = get_attrib_loc(prog, "attr_tangent");
197 } else {
198 bind_texture(tex, 0);
199 set_uniform_int(prog, "tex", 0);
200 }
202 gl_matrix_mode(GL_TEXTURE);
203 gl_load_identity();
204 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
206 gl_begin(GL_QUADS);
207 gl_color3f(1.0, 1.0, 1.0);
209 du = 1.0 / sides;
210 dv = 1.0 / segm;
212 for(i=0; i<segm; i++) {
213 float trans_zp[2], trans_z0[2], trans_z1[2];
215 float zp = ring_height * (i - 1);
216 float z0 = ring_height * i;
217 float z1 = ring_height * (i + 1);
219 worm(t, zp, trans_zp, trans_zp + 1);
220 worm(t, z0, trans_z0, trans_z0 + 1);
221 worm(t, z1, trans_z1, trans_z1 + 1);
223 for(j=0; j<sides; j++) {
224 for(k=0; k<4; k++) {
225 float u = (j + uoffs[k]) * du;
226 float v = (i + voffs[k]) * dv;
228 tunnel_vertex(u, v, du, dv, tang_loc, t);
229 }
230 }
231 }
232 gl_end();
234 bind_texture(0, 1);
235 bind_texture(0, 0);
236 }
238 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
239 {
240 vec3_t pos, norm;
241 vec3_t dfdu, dfdv, pos_du, pos_dv;
243 float theta = 2.0 * M_PI * u;
244 float theta1 = 2.0 * M_PI * (u + du);
246 float x = cos(theta);
247 float y = sin(theta);
248 float x1 = cos(theta1);
249 float y1 = sin(theta1);
250 float z = v / dv * ring_height;
251 float z1 = (v + dv) / dv * ring_height;
253 float trans_z[2], trans_z1[2];
255 worm(t, z, trans_z, trans_z + 1);
256 worm(t, z1, trans_z1, trans_z1 + 1);
258 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
259 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
260 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
262 dfdu = v3_sub(pos_du, pos);
263 dfdv = v3_sub(pos_dv, pos);
264 norm = v3_cross(dfdv, dfdu);
266 gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
267 gl_normal3f(norm.x, norm.y, norm.z);
268 gl_texcoord2f(u * 2.0, v * 4.0);
269 gl_vertex3f(pos.x, pos.y, pos.z);
270 }
272 static vec3_t calc_text_pos(float sec)
273 {
274 float t = text_speed * sec;
275 float z = fmod(t, text_period);
276 float pan[2];
278 worm(sec, z, pan, pan + 1);
279 return v3_cons(pan[0], pan[1], z + ring_height);
280 }
282 static void draw_text(float idx, vec3_t tpos, float alpha)
283 {
284 gl_matrix_mode(GL_MODELVIEW);
285 gl_push_matrix();
286 gl_translatef(tpos.x, tpos.y, tpos.z);
288 glEnable(GL_BLEND);
289 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
291 bind_program(prog_text);
292 set_uniform_float(prog_text, "idx", idx);
294 bind_texture(tex_text, 0);
296 gl_begin(GL_QUADS);
297 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
299 gl_texcoord2f(0, 1);
300 gl_vertex3f(-1, -0.2, 0);
302 gl_texcoord2f(1, 1);
303 gl_vertex3f(1, -0.2, 0);
305 gl_texcoord2f(1, 0);
306 gl_vertex3f(1, 0.2, 0);
308 gl_texcoord2f(0, 0);
309 gl_vertex3f(-1, 0.2, 0);
310 gl_end();
312 bind_texture(0, 0);
313 glDisable(GL_BLEND);
315 gl_pop_matrix();
316 }
319 static void worm(float t, float z, float *tx, float *ty)
320 {
321 float x, y;
322 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
323 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
325 *tx = x * 0.5;
326 *ty = y * 0.5;
327 }
330 void reshape(int x, int y)
331 {
332 glViewport(0, 0, x, y);
334 gl_matrix_mode(GL_PROJECTION);
335 gl_load_identity();
336 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
338 view_xsz = x;
339 view_ysz = y;
340 }
342 static unsigned int get_shader_program(const char *vfile, const char *pfile)
343 {
344 unsigned int prog, vs, ps;
346 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
347 return 0;
348 }
349 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
350 return 0;
351 }
353 if(!(prog = create_program_link(vs, ps))) {
354 return 0;
355 }
356 return prog;
357 }
360 #ifdef IPHONE
361 #include <QuartzCore/QuartzCore.h>
363 static float get_sec(void)
364 {
365 static float first;
366 static int init;
368 if(!init) {
369 init = 1;
370 first = CACurrentMediaTime();
371 return 0.0f;
372 }
373 return CACurrentMediaTime() - first;
374 }
376 #else
378 static float get_sec(void)
379 {
380 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
381 }
382 #endif