istereo
view src/istereo.c @ 33:33e9eedad390
ah.. blunder
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Fri, 09 Sep 2011 00:03:37 +0300 |
parents | bc6db80aaa58 |
children | 634c7f7c1f0f |
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 = 0;
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.5275;
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(42.5);
78 cam_clip(0.5, 250.0);
80 return 0;
81 }
83 void cleanup(void)
84 {
85 free_program(prog);
86 }
88 void redraw(void)
89 {
90 float pan_x, pan_y, z;
91 float tsec = get_sec();
93 z = ring_height * segm;
94 worm(tsec, z, &pan_x, &pan_y);
96 if(use_bump) {
97 glClearColor(0.01, 0.01, 0.01, 1.0);
98 tunnel_speed = 0.5;
99 } else {
100 glClearColor(0.6, 0.6, 0.6, 1.0);
101 tunnel_speed = 0.75;
102 }
103 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105 if(stereo) {
106 int split_pt = (int)((float)view_ysz * split);
108 /* right eye */
109 glViewport(0, 0, view_xsz, split_pt);
110 cam_aspect((float)split_pt / (float)view_xsz);
112 gl_matrix_mode(GL_PROJECTION);
113 gl_load_identity();
114 cam_stereo_proj_matrix(CAM_RIGHT);
115 gl_rotatef(-90, 0, 0, 1);
117 gl_matrix_mode(GL_MODELVIEW);
118 gl_load_identity();
119 cam_stereo_view_matrix(CAM_RIGHT);
120 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
121 /*gl_rotatef(-90, 0, 0, 1);*/
123 render(tsec);
125 /* left eye */
126 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
127 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
129 gl_matrix_mode(GL_PROJECTION);
130 gl_load_identity();
131 cam_stereo_proj_matrix(CAM_LEFT);
132 gl_rotatef(-90, 0, 0, 1);
134 gl_matrix_mode(GL_MODELVIEW);
135 gl_load_identity();
136 cam_stereo_view_matrix(CAM_LEFT);
137 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
138 /*gl_rotatef(-90, 0, 0, 1);*/
140 render(tsec);
141 } else {
142 gl_matrix_mode(GL_MODELVIEW);
143 gl_load_identity();
144 cam_view_matrix();
145 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
147 render(tsec);
148 }
150 assert(glGetError() == GL_NO_ERROR);
151 }
153 static void render(float t)
154 {
155 int i;
156 float text_line;
158 draw_tunnel(t);
160 if(use_bump) {
161 glDepthMask(0);
162 text_line = floor((text_speed * t) / text_period);
163 for(i=0; i<8; i++) {
164 vec3_t tpos = calc_text_pos(t - (float)i * 0.015);
165 draw_text(text_line, tpos, 1.5 / (float)i);
166 }
167 glDepthMask(1);
168 }
169 }
171 static void draw_tunnel(float t)
172 {
173 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
174 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
175 int i, j, k, tang_loc = -1;
176 float du, dv;
178 prog = use_bump ? prog_tunnel : prog_simple;
180 bind_program(prog);
181 set_uniform_float(prog, "t", t);
183 if(use_bump) {
184 vec3_t ltpos = calc_text_pos(t);
186 bind_texture(tex_normal, 1);
187 set_uniform_int(prog, "tex_norm", 1);
188 bind_texture(tex_stones, 0);
189 set_uniform_int(prog, "tex", 0);
191 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
192 tang_loc = get_attrib_loc(prog, "attr_tangent");
193 } else {
194 bind_texture(tex, 0);
195 set_uniform_int(prog, "tex", 0);
196 }
198 gl_matrix_mode(GL_TEXTURE);
199 gl_load_identity();
200 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
202 gl_begin(GL_QUADS);
203 gl_color3f(1.0, 1.0, 1.0);
205 du = 1.0 / sides;
206 dv = 1.0 / segm;
208 for(i=0; i<segm; i++) {
209 float trans_zp[2], trans_z0[2], trans_z1[2];
211 float zp = ring_height * (i - 1);
212 float z0 = ring_height * i;
213 float z1 = ring_height * (i + 1);
215 worm(t, zp, trans_zp, trans_zp + 1);
216 worm(t, z0, trans_z0, trans_z0 + 1);
217 worm(t, z1, trans_z1, trans_z1 + 1);
219 for(j=0; j<sides; j++) {
220 for(k=0; k<4; k++) {
221 float u = (j + uoffs[k]) * du;
222 float v = (i + voffs[k]) * dv;
224 tunnel_vertex(u, v, du, dv, tang_loc, t);
225 }
226 }
227 }
228 gl_end();
230 bind_texture(0, 1);
231 bind_texture(0, 0);
232 }
234 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
235 {
236 vec3_t pos, norm;
237 vec3_t dfdu, dfdv, pos_du, pos_dv;
239 float theta = 2.0 * M_PI * u;
240 float theta1 = 2.0 * M_PI * (u + du);
242 float x = cos(theta);
243 float y = sin(theta);
244 float x1 = cos(theta1);
245 float y1 = sin(theta1);
246 float z = v / dv * ring_height;
247 float z1 = (v + dv) / dv * ring_height;
249 float trans_z[2], trans_z1[2];
251 worm(t, z, trans_z, trans_z + 1);
252 worm(t, z1, trans_z1, trans_z1 + 1);
254 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
255 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
256 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
258 dfdu = v3_sub(pos_du, pos);
259 dfdv = v3_sub(pos_dv, pos);
260 norm = v3_cross(dfdv, dfdu);
262 gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
263 gl_normal3f(norm.x, norm.y, norm.z);
264 gl_texcoord2f(u * 2.0, v * 4.0);
265 gl_vertex3f(pos.x, pos.y, pos.z);
266 }
268 static vec3_t calc_text_pos(float sec)
269 {
270 float t = text_speed * sec;
271 float z = fmod(t, text_period);
272 float pan[2];
274 worm(sec, z, pan, pan + 1);
275 return v3_cons(pan[0], pan[1], z + ring_height);
276 }
278 static void draw_text(float idx, vec3_t tpos, float alpha)
279 {
280 gl_matrix_mode(GL_MODELVIEW);
281 gl_push_matrix();
282 gl_translatef(tpos.x, tpos.y, tpos.z);
284 glEnable(GL_BLEND);
285 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
287 bind_program(prog_text);
288 set_uniform_float(prog_text, "idx", idx);
290 bind_texture(tex_text, 0);
292 gl_begin(GL_QUADS);
293 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
295 gl_texcoord2f(0, 1);
296 gl_vertex3f(-1, -0.2, 0);
298 gl_texcoord2f(1, 1);
299 gl_vertex3f(1, -0.2, 0);
301 gl_texcoord2f(1, 0);
302 gl_vertex3f(1, 0.2, 0);
304 gl_texcoord2f(0, 0);
305 gl_vertex3f(-1, 0.2, 0);
306 gl_end();
308 bind_texture(0, 0);
309 glDisable(GL_BLEND);
311 gl_pop_matrix();
312 }
315 static void worm(float t, float z, float *tx, float *ty)
316 {
317 float x, y;
318 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
319 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
321 *tx = x * 0.5;
322 *ty = y * 0.5;
323 }
326 void reshape(int x, int y)
327 {
328 glViewport(0, 0, x, y);
330 gl_matrix_mode(GL_PROJECTION);
331 gl_load_identity();
332 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
334 view_xsz = x;
335 view_ysz = y;
336 }
338 static unsigned int get_shader_program(const char *vfile, const char *pfile)
339 {
340 unsigned int prog, vs, ps;
342 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
343 return 0;
344 }
345 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
346 return 0;
347 }
349 if(!(prog = create_program_link(vs, ps))) {
350 return 0;
351 }
352 return prog;
353 }
356 #ifdef IPHONE
357 #include <QuartzCore/QuartzCore.h>
359 static float get_sec(void)
360 {
361 static float first;
362 static int init;
364 if(!init) {
365 init = 1;
366 first = CACurrentMediaTime();
367 return 0.0f;
368 }
369 return CACurrentMediaTime() - first;
370 }
372 #else
374 static float get_sec(void)
375 {
376 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
377 }
378 #endif