istereo

view src/istereo.c @ 30:8dd271942543

fixed everything
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 14:52:13 +0300
parents fd39c0198935
children bc6db80aaa58
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 glClearDepthf(1.0);
104 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
106 if(stereo) {
107 int split_pt = (int)((float)view_ysz * split);
109 /* right eye */
110 glViewport(0, 0, view_xsz, split_pt);
111 cam_aspect((float)split_pt / (float)view_xsz);
113 gl_matrix_mode(GL_PROJECTION);
114 gl_load_identity();
115 cam_stereo_proj_matrix(CAM_RIGHT);
116 gl_rotatef(-90, 0, 0, 1);
118 gl_matrix_mode(GL_MODELVIEW);
119 gl_load_identity();
120 cam_stereo_view_matrix(CAM_RIGHT);
121 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
122 /*gl_rotatef(-90, 0, 0, 1);*/
124 render(tsec);
126 /* left eye */
127 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
128 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
130 gl_matrix_mode(GL_PROJECTION);
131 gl_load_identity();
132 cam_stereo_proj_matrix(CAM_LEFT);
133 gl_rotatef(-90, 0, 0, 1);
135 gl_matrix_mode(GL_MODELVIEW);
136 gl_load_identity();
137 cam_stereo_view_matrix(CAM_LEFT);
138 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
139 /*gl_rotatef(-90, 0, 0, 1);*/
141 render(tsec);
142 } else {
143 gl_matrix_mode(GL_MODELVIEW);
144 gl_load_identity();
145 cam_view_matrix();
146 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
148 render(tsec);
149 }
151 assert(glGetError() == GL_NO_ERROR);
152 }
154 static void render(float t)
155 {
156 int i;
157 float text_line;
159 draw_tunnel(t);
161 if(use_bump) {
162 glDepthMask(0);
163 text_line = floor((text_speed * t) / text_period);
164 for(i=0; i<8; i++) {
165 vec3_t tpos = calc_text_pos(t - (float)i * 0.015);
166 draw_text(text_line, tpos, 1.5 / (float)i);
167 }
168 glDepthMask(1);
169 }
170 }
172 static void draw_tunnel(float t)
173 {
174 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
175 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
176 int i, j, k, tang_loc = -1;
177 float du, dv;
179 prog = use_bump ? prog_tunnel : prog_simple;
181 bind_program(prog);
182 set_uniform_float(prog, "t", t);
184 if(use_bump) {
185 vec3_t ltpos = calc_text_pos(t);
187 bind_texture(tex_normal, 1);
188 set_uniform_int(prog, "tex_norm", 1);
189 bind_texture(tex_stones, 0);
190 set_uniform_int(prog, "tex", 0);
192 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
193 tang_loc = get_attrib_loc(prog, "attr_tangent");
194 } else {
195 bind_texture(tex, 0);
196 set_uniform_int(prog, "tex", 0);
197 }
199 gl_matrix_mode(GL_TEXTURE);
200 gl_load_identity();
201 gl_translatef(0, -t * tunnel_speed, 0);
203 gl_begin(GL_QUADS);
204 gl_color3f(1.0, 1.0, 1.0);
206 du = 1.0 / sides;
207 dv = 1.0 / segm;
209 for(i=0; i<segm; i++) {
210 float trans_zp[2], trans_z0[2], trans_z1[2];
212 float zp = ring_height * (i - 1);
213 float z0 = ring_height * i;
214 float z1 = ring_height * (i + 1);
216 worm(t, zp, trans_zp, trans_zp + 1);
217 worm(t, z0, trans_z0, trans_z0 + 1);
218 worm(t, z1, trans_z1, trans_z1 + 1);
220 for(j=0; j<sides; j++) {
221 for(k=0; k<4; k++) {
222 float u = (j + uoffs[k]) * du;
223 float v = (i + voffs[k]) * dv;
225 tunnel_vertex(u, v, du, dv, tang_loc, t);
226 }
227 }
228 }
229 gl_end();
231 bind_texture(0, 1);
232 bind_texture(0, 0);
233 }
235 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
236 {
237 vec3_t pos, norm;
238 vec3_t dfdu, dfdv, pos_du, pos_dv;
240 float theta = 2.0 * M_PI * u;
241 float theta1 = 2.0 * M_PI * (u + du);
243 float x = cos(theta);
244 float y = sin(theta);
245 float x1 = cos(theta1);
246 float y1 = sin(theta1);
247 float z = v / dv * ring_height;
248 float z1 = (v + dv) / dv * ring_height;
250 float trans_z[2], trans_z1[2];
252 worm(t, z, trans_z, trans_z + 1);
253 worm(t, z1, trans_z1, trans_z1 + 1);
255 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
256 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
257 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
259 dfdu = v3_sub(pos_du, pos);
260 dfdv = v3_sub(pos_dv, pos);
261 norm = v3_cross(dfdv, dfdu);
263 gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
264 gl_normal3f(norm.x, norm.y, norm.z);
265 gl_texcoord2f(u * 2.0, v * 4.0);
266 gl_vertex3f(pos.x, pos.y, pos.z);
267 }
269 static vec3_t calc_text_pos(float sec)
270 {
271 float t = text_speed * sec;
272 float z = fmod(t, text_period);
273 float pan[2];
275 worm(sec, z, pan, pan + 1);
276 return v3_cons(pan[0], pan[1], z + ring_height);
277 }
279 static void draw_text(float idx, vec3_t tpos, float alpha)
280 {
281 gl_matrix_mode(GL_MODELVIEW);
282 gl_push_matrix();
283 gl_translatef(tpos.x, tpos.y, tpos.z);
285 glEnable(GL_BLEND);
286 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
288 bind_program(prog_text);
289 set_uniform_float(prog, "idx", idx);
291 bind_texture(tex_text, 0);
293 gl_begin(GL_QUADS);
294 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
296 gl_texcoord2f(0, 1);
297 gl_vertex3f(-1, -0.2, 0);
299 gl_texcoord2f(1, 1);
300 gl_vertex3f(1, -0.2, 0);
302 gl_texcoord2f(1, 0);
303 gl_vertex3f(1, 0.2, 0);
305 gl_texcoord2f(0, 0);
306 gl_vertex3f(-1, 0.2, 0);
307 gl_end();
309 bind_texture(0, 0);
310 glDisable(GL_BLEND);
312 gl_pop_matrix();
313 }
316 static void worm(float t, float z, float *tx, float *ty)
317 {
318 float x, y;
319 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
320 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
322 *tx = x * 0.5;
323 *ty = y * 0.5;
324 }
327 void reshape(int x, int y)
328 {
329 glViewport(0, 0, x, y);
331 gl_matrix_mode(GL_PROJECTION);
332 gl_load_identity();
333 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
335 view_xsz = x;
336 view_ysz = y;
337 }
339 static unsigned int get_shader_program(const char *vfile, const char *pfile)
340 {
341 unsigned int prog, vs, ps;
343 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
344 return 0;
345 }
346 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
347 return 0;
348 }
350 if(!(prog = create_program_link(vs, ps))) {
351 return 0;
352 }
353 return prog;
354 }
357 #ifdef IPHONE
358 #include <QuartzCore/QuartzCore.h>
360 static float get_sec(void)
361 {
362 static float first;
363 static int init;
365 if(!init) {
366 init = 1;
367 first = CACurrentMediaTime();
368 return 0.0f;
369 }
370 return CACurrentMediaTime() - first;
371 }
373 #else
375 static float get_sec(void)
376 {
377 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
378 }
379 #endif