istereo
view src/istereo.c @ 28:c0ae8e668447
added vmath library
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Thu, 08 Sep 2011 08:30:42 +0300 |
parents | 862a3329a8f0 |
children | 8dd271942543 |
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 #ifdef IPHONE
30 int stereo = 1;
31 #else
32 int stereo = 0;
33 #endif
34 int use_bump = 1;
36 /* construction parameters */
37 int sides = 24;
38 int segm = 20;
39 float tunnel_speed = 0.75;
40 float ring_height = 0.5;
41 float text_period = 13.0;
42 float text_speed = 2.2;
44 float split = 0.53;
46 int init(void)
47 {
48 add_resource_path("sdr");
49 add_resource_path("data");
51 if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
52 return -1;
53 }
54 if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
55 return -1;
56 }
57 if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
58 return -1;
59 }
61 if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
62 return -1;
63 }
64 if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
65 return -1;
66 }
67 if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
68 return -1;
69 }
70 if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
71 return -1;
72 }
74 glEnable(GL_DEPTH_TEST);
75 glEnable(GL_CULL_FACE);
77 cam_fov(45.0);
79 return 0;
80 }
82 void cleanup(void)
83 {
84 free_program(prog);
85 }
87 void redraw(void)
88 {
89 float pan_x, pan_y, z;
90 float tsec = get_sec();
92 z = ring_height * segm;
93 worm(tsec, z, &pan_x, &pan_y);
95 if(use_bump) {
96 glClearColor(0.01, 0.01, 0.01, 1.0);
97 tunnel_speed = 0.5;
98 } else {
99 glClearColor(0.6, 0.6, 0.6, 1.0);
100 tunnel_speed = 0.75;
101 }
102 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104 if(stereo) {
105 int split_pt = (int)((float)view_ysz * split);
107 /* right eye */
108 glViewport(0, 0, view_xsz, split_pt);
109 cam_aspect((float)split_pt / (float)view_xsz);
111 gl_matrix_mode(GL_PROJECTION);
112 gl_load_identity();
113 cam_stereo_proj_matrix(CAM_RIGHT);
114 gl_rotatef(-90, 0, 0, 1);
116 gl_matrix_mode(GL_MODELVIEW);
117 gl_load_identity();
118 cam_stereo_view_matrix(CAM_RIGHT);
119 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
120 /*gl_rotatef(-90, 0, 0, 1);*/
122 render(tsec);
124 /* left eye */
125 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
126 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
128 gl_matrix_mode(GL_PROJECTION);
129 gl_load_identity();
130 cam_stereo_proj_matrix(CAM_LEFT);
131 gl_rotatef(-90, 0, 0, 1);
133 gl_matrix_mode(GL_MODELVIEW);
134 gl_load_identity();
135 cam_stereo_view_matrix(CAM_LEFT);
136 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
137 /*gl_rotatef(-90, 0, 0, 1);*/
139 render(tsec);
140 } else {
141 gl_matrix_mode(GL_MODELVIEW);
142 gl_load_identity();
143 cam_view_matrix();
144 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
146 render(tsec);
147 }
149 assert(glGetError() == GL_NO_ERROR);
150 }
152 static void render(float t)
153 {
154 int i;
155 float text_line;
157 draw_tunnel(t);
159 glDepthMask(0);
160 text_line = floor((text_speed * t) / text_period);
161 for(i=0; i<15; i++) {
162 vec3_t tpos = calc_text_pos(t - (float)i * 0.01);
163 draw_text(text_line, tpos, 1.5 / (float)i);
164 }
165 glDepthMask(1);
166 }
168 static void draw_tunnel(float t)
169 {
170 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
171 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
172 int i, j, k, tang_loc = -1;
173 float du, dv;
175 prog = use_bump ? prog_tunnel : prog_simple;
177 bind_program(prog);
178 set_uniform_float(prog, "t", t);
179 set_uniform_int(prog, "tex", 0);
181 if(use_bump) {
182 vec3_t ltpos = calc_text_pos(t);
184 set_uniform_int(prog, "tex_norm", 1);
185 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
186 tang_loc = get_attrib_loc(prog, "attr_tangent");
187 bind_texture(tex_normal, 1);
188 }
190 bind_texture(tex, 0);
192 gl_matrix_mode(GL_TEXTURE);
193 gl_load_identity();
194 gl_translatef(0, -t * tunnel_speed, 0);
196 gl_begin(GL_QUADS);
197 gl_color3f(1.0, 1.0, 1.0);
199 du = 1.0 / sides;
200 dv = 1.0 / segm;
202 for(i=0; i<segm; i++) {
203 float trans_zp[2], trans_z0[2], trans_z1[2];
205 float zp = ring_height * (i - 1);
206 float z0 = ring_height * i;
207 float z1 = ring_height * (i + 1);
209 float v0 = (float)i / (float)segm;
210 float v1 = (float)(i + 1) / (float)segm;
212 worm(t, zp, trans_zp, trans_zp + 1);
213 worm(t, z0, trans_z0, trans_z0 + 1);
214 worm(t, z1, trans_z1, trans_z1 + 1);
216 for(j=0; j<sides; j++) {
217 for(k=0; k<4; k++) {
218 float u = (j + uoffs[k]) * du;
219 float v = (i + voffs[k]) * dv;
221 tunnel_vertex(u, v, du, dv, tang_loc, t);
222 }
223 }
224 }
225 gl_end();
227 bind_texture(0, 1);
228 bind_texture(0, 0);
229 }
231 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
232 {
233 vec3_t pos, norm;
234 vec3_t dfdu, dfdv, pos_du, pos_dv;
236 float theta = 2.0 * M_PI * u;
237 float theta1 = 2.0 * M_PI * (u + du);
239 float x = cos(theta);
240 float y = sin(theta);
241 float x1 = cos(theta1);
242 float y1 = sin(theta1);
243 float z = v / dv * ring_height;
244 float z1 = (v + dv) / dv * ring_height;
246 float trans_z[2], trans_z1[2];
248 worm(t, z, trans_z, trans_z + 1);
249 worm(t, z1, trans_z1, trans_z1 + 1);
251 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
252 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
253 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
255 dfdu = v3_sub(pos_du, pos);
256 dfdv = v3_sub(pos_dv, pos);
257 norm = v3_cross(dfdv, dfdu);
259 gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
260 gl_normal3f(norm.x, norm.y, norm.z);
261 gl_texcoord2f(u * 2.0, v * 4.0);
262 gl_vertex3f(pos.x, pos.y, pos.z);
263 }
265 static vec3_t calc_text_pos(float sec)
266 {
267 float t = text_speed * sec;
268 float z = fmod(t, text_period);
269 float pan[2];
271 worm(sec, z, pan, pan + 1);
272 return v3_cons(pan[0], pan[1], z + ring_height);
273 }
275 static void draw_text(float idx, vec3_t tpos, float alpha)
276 {
277 gl_matrix_mode(GL_MODELVIEW);
278 gl_push_matrix();
279 gl_translatef(tpos.x, tpos.y, tpos.z);
281 glEnable(GL_BLEND);
282 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
284 bind_program(prog_text);
285 set_uniform_float(prog, "idx", idx);
287 bind_texture(tex_text, 0);
289 gl_begin(GL_QUADS);
290 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
292 gl_texcoord2f(0, 1);
293 gl_vertex3f(-1, -0.2, 0);
295 gl_texcoord2f(1, 1);
296 gl_vertex3f(1, -0.2, 0);
298 gl_texcoord2f(1, 0);
299 gl_vertex3f(1, 0.2, 0);
301 gl_texcoord2f(0, 0);
302 gl_vertex3f(-1, 0.2, 0);
303 gl_end();
305 bind_texture(0, 0);
306 glDisable(GL_BLEND);
308 gl_pop_matrix();
309 }
312 static void worm(float t, float z, float *tx, float *ty)
313 {
314 float x, y;
315 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
316 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
318 *tx = x * 0.5;
319 *ty = y * 0.5;
320 }
323 void reshape(int x, int y)
324 {
325 glViewport(0, 0, x, y);
327 gl_matrix_mode(GL_PROJECTION);
328 gl_load_identity();
329 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
331 view_xsz = x;
332 view_ysz = y;
333 }
335 static unsigned int get_shader_program(const char *vfile, const char *pfile)
336 {
337 unsigned int prog, vs, ps;
339 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
340 return 0;
341 }
342 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
343 return 0;
344 }
346 if(!(prog = create_program_link(vs, ps))) {
347 return 0;
348 }
349 return prog;
350 }
353 #ifdef IPHONE
354 #include <QuartzCore/QuartzCore.h>
356 static float get_sec(void)
357 {
358 static float first;
359 static int init;
361 if(!init) {
362 init = 1;
363 first = CACurrentMediaTime();
364 return 0.0f;
365 }
366 return CACurrentMediaTime() - first;
367 }
369 #else
371 static float get_sec(void)
372 {
373 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
374 }
375 #endif