istereo

view src/istereo.c @ 37:e60f9d8af28d

fixed the orientation of the tunnel when in non-stereo mode
author John Tsiombikas <nuclear@mutantstargoat.com>
date Fri, 09 Sep 2011 23:37:38 +0300
parents 834503dcb486
children ff055bff6a15
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);
118 render(tsec);
120 /* left eye */
121 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
122 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
124 gl_matrix_mode(GL_PROJECTION);
125 gl_load_identity();
126 cam_stereo_proj_matrix(CAM_LEFT);
127 gl_rotatef(-90, 0, 0, 1);
129 gl_matrix_mode(GL_MODELVIEW);
130 gl_load_identity();
131 cam_stereo_view_matrix(CAM_LEFT);
132 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
134 render(tsec);
135 } else {
136 glViewport(0, 0, view_xsz, view_ysz);
137 cam_aspect((float)view_xsz / (float)view_ysz);
139 gl_matrix_mode(GL_PROJECTION);
140 gl_load_identity();
141 gl_rotatef(-90, 0, 0, 1);
142 cam_proj_matrix();
144 gl_matrix_mode(GL_MODELVIEW);
145 gl_load_identity();
146 cam_view_matrix();
147 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
149 render(tsec);
150 }
152 assert(glGetError() == GL_NO_ERROR);
153 }
155 static void render(float t)
156 {
157 int i;
158 float text_line;
160 draw_tunnel(t);
162 if(use_bump) {
163 glDepthMask(0);
164 text_line = floor((text_speed * t) / text_period);
165 for(i=0; i<8; i++) {
166 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
167 draw_text(text_line, tpos, 1.5 / (float)i);
168 }
169 glDepthMask(1);
170 }
171 }
173 static void draw_tunnel(float t)
174 {
175 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
176 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
177 int i, j, k, tang_loc = -1;
178 float du, dv;
180 prog = use_bump ? prog_tunnel : prog_simple;
182 bind_program(prog);
183 set_uniform_float(prog, "t", t);
185 if(use_bump) {
186 vec3_t ltpos = calc_text_pos(t);
188 bind_texture(tex_normal, 1);
189 set_uniform_int(prog, "tex_norm", 1);
190 bind_texture(tex_stones, 0);
191 set_uniform_int(prog, "tex", 0);
193 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
194 tang_loc = get_attrib_loc(prog, "attr_tangent");
195 } else {
196 bind_texture(tex, 0);
197 set_uniform_int(prog, "tex", 0);
198 }
200 gl_matrix_mode(GL_TEXTURE);
201 gl_load_identity();
202 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
204 gl_begin(GL_QUADS);
205 gl_color3f(1.0, 1.0, 1.0);
207 du = 1.0 / sides;
208 dv = 1.0 / segm;
210 for(i=0; i<segm; i++) {
211 float trans_zp[2], trans_z0[2], trans_z1[2];
213 float zp = ring_height * (i - 1);
214 float z0 = ring_height * i;
215 float z1 = ring_height * (i + 1);
217 worm(t, zp, trans_zp, trans_zp + 1);
218 worm(t, z0, trans_z0, trans_z0 + 1);
219 worm(t, z1, trans_z1, trans_z1 + 1);
221 for(j=0; j<sides; j++) {
222 for(k=0; k<4; k++) {
223 float u = (j + uoffs[k]) * du;
224 float v = (i + voffs[k]) * dv;
226 tunnel_vertex(u, v, du, dv, tang_loc, t);
227 }
228 }
229 }
230 gl_end();
232 bind_texture(0, 1);
233 bind_texture(0, 0);
234 }
236 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
237 {
238 vec3_t pos, norm;
239 vec3_t dfdu, dfdv, pos_du, pos_dv;
241 float theta = 2.0 * M_PI * u;
242 float theta1 = 2.0 * M_PI * (u + du);
244 float x = cos(theta);
245 float y = sin(theta);
246 float x1 = cos(theta1);
247 float y1 = sin(theta1);
248 float z = v / dv * ring_height;
249 float z1 = (v + dv) / dv * ring_height;
251 float trans_z[2], trans_z1[2];
253 worm(t, z, trans_z, trans_z + 1);
254 worm(t, z1, trans_z1, trans_z1 + 1);
256 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
257 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
258 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
260 dfdu = v3_sub(pos_du, pos);
261 dfdv = v3_sub(pos_dv, pos);
262 norm = v3_cross(dfdv, dfdu);
264 gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
265 gl_normal3f(norm.x, norm.y, norm.z);
266 gl_texcoord2f(u * 2.0, v * 4.0);
267 gl_vertex3f(pos.x, pos.y, pos.z);
268 }
270 static vec3_t calc_text_pos(float sec)
271 {
272 float t = text_speed * sec;
273 float z = fmod(t, text_period);
274 float pan[2];
276 worm(sec, z, pan, pan + 1);
277 return v3_cons(pan[0], pan[1], z + ring_height);
278 }
280 static void draw_text(float idx, vec3_t tpos, float alpha)
281 {
282 gl_matrix_mode(GL_MODELVIEW);
283 gl_push_matrix();
284 gl_translatef(tpos.x, tpos.y, tpos.z);
286 glEnable(GL_BLEND);
287 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
289 bind_program(prog_text);
290 set_uniform_float(prog_text, "idx", idx);
292 bind_texture(tex_text, 0);
294 gl_begin(GL_QUADS);
295 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
297 gl_texcoord2f(0, 1);
298 gl_vertex3f(-1, -0.2, 0);
300 gl_texcoord2f(1, 1);
301 gl_vertex3f(1, -0.2, 0);
303 gl_texcoord2f(1, 0);
304 gl_vertex3f(1, 0.2, 0);
306 gl_texcoord2f(0, 0);
307 gl_vertex3f(-1, 0.2, 0);
308 gl_end();
310 bind_texture(0, 0);
311 glDisable(GL_BLEND);
313 gl_pop_matrix();
314 }
317 static void worm(float t, float z, float *tx, float *ty)
318 {
319 float x, y;
320 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
321 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
323 *tx = x * 0.5;
324 *ty = y * 0.5;
325 }
328 void reshape(int x, int y)
329 {
330 glViewport(0, 0, x, y);
332 gl_matrix_mode(GL_PROJECTION);
333 gl_load_identity();
334 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
336 view_xsz = x;
337 view_ysz = y;
338 }
340 static unsigned int get_shader_program(const char *vfile, const char *pfile)
341 {
342 unsigned int prog, vs, ps;
344 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
345 return 0;
346 }
347 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
348 return 0;
349 }
351 if(!(prog = create_program_link(vs, ps))) {
352 return 0;
353 }
354 return prog;
355 }
358 #ifdef IPHONE
359 #include <QuartzCore/QuartzCore.h>
361 static float get_sec(void)
362 {
363 static float first;
364 static int init;
366 if(!init) {
367 init = 1;
368 first = CACurrentMediaTime();
369 return 0.0f;
370 }
371 return CACurrentMediaTime() - first;
372 }
374 #else
376 static float get_sec(void)
377 {
378 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
379 }
380 #endif