istereo

view src/istereo.c @ 36:834503dcb486

fixed the rotated gui problem
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Sep 2011 10:25:03 +0300
parents 23e5d274b2a2
children e60f9d8af28d
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_xsz / (float)view_ysz);
141 gl_matrix_mode(GL_PROJECTION);
142 gl_load_identity();
143 cam_proj_matrix();
145 gl_matrix_mode(GL_MODELVIEW);
146 gl_load_identity();
147 cam_view_matrix();
148 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
150 render(tsec);
151 }
153 assert(glGetError() == GL_NO_ERROR);
154 }
156 static void render(float t)
157 {
158 int i;
159 float text_line;
161 draw_tunnel(t);
163 if(use_bump) {
164 glDepthMask(0);
165 text_line = floor((text_speed * t) / text_period);
166 for(i=0; i<8; i++) {
167 vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
168 draw_text(text_line, tpos, 1.5 / (float)i);
169 }
170 glDepthMask(1);
171 }
172 }
174 static void draw_tunnel(float t)
175 {
176 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
177 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
178 int i, j, k, tang_loc = -1;
179 float du, dv;
181 prog = use_bump ? prog_tunnel : prog_simple;
183 bind_program(prog);
184 set_uniform_float(prog, "t", t);
186 if(use_bump) {
187 vec3_t ltpos = calc_text_pos(t);
189 bind_texture(tex_normal, 1);
190 set_uniform_int(prog, "tex_norm", 1);
191 bind_texture(tex_stones, 0);
192 set_uniform_int(prog, "tex", 0);
194 set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
195 tang_loc = get_attrib_loc(prog, "attr_tangent");
196 } else {
197 bind_texture(tex, 0);
198 set_uniform_int(prog, "tex", 0);
199 }
201 gl_matrix_mode(GL_TEXTURE);
202 gl_load_identity();
203 gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
205 gl_begin(GL_QUADS);
206 gl_color3f(1.0, 1.0, 1.0);
208 du = 1.0 / sides;
209 dv = 1.0 / segm;
211 for(i=0; i<segm; i++) {
212 float trans_zp[2], trans_z0[2], trans_z1[2];
214 float zp = ring_height * (i - 1);
215 float z0 = ring_height * i;
216 float z1 = ring_height * (i + 1);
218 worm(t, zp, trans_zp, trans_zp + 1);
219 worm(t, z0, trans_z0, trans_z0 + 1);
220 worm(t, z1, trans_z1, trans_z1 + 1);
222 for(j=0; j<sides; j++) {
223 for(k=0; k<4; k++) {
224 float u = (j + uoffs[k]) * du;
225 float v = (i + voffs[k]) * dv;
227 tunnel_vertex(u, v, du, dv, tang_loc, t);
228 }
229 }
230 }
231 gl_end();
233 bind_texture(0, 1);
234 bind_texture(0, 0);
235 }
237 static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
238 {
239 vec3_t pos, norm;
240 vec3_t dfdu, dfdv, pos_du, pos_dv;
242 float theta = 2.0 * M_PI * u;
243 float theta1 = 2.0 * M_PI * (u + du);
245 float x = cos(theta);
246 float y = sin(theta);
247 float x1 = cos(theta1);
248 float y1 = sin(theta1);
249 float z = v / dv * ring_height;
250 float z1 = (v + dv) / dv * ring_height;
252 float trans_z[2], trans_z1[2];
254 worm(t, z, trans_z, trans_z + 1);
255 worm(t, z1, trans_z1, trans_z1 + 1);
257 pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
258 pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
259 pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
261 dfdu = v3_sub(pos_du, pos);
262 dfdv = v3_sub(pos_dv, pos);
263 norm = v3_cross(dfdv, dfdu);
265 gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
266 gl_normal3f(norm.x, norm.y, norm.z);
267 gl_texcoord2f(u * 2.0, v * 4.0);
268 gl_vertex3f(pos.x, pos.y, pos.z);
269 }
271 static vec3_t calc_text_pos(float sec)
272 {
273 float t = text_speed * sec;
274 float z = fmod(t, text_period);
275 float pan[2];
277 worm(sec, z, pan, pan + 1);
278 return v3_cons(pan[0], pan[1], z + ring_height);
279 }
281 static void draw_text(float idx, vec3_t tpos, float alpha)
282 {
283 gl_matrix_mode(GL_MODELVIEW);
284 gl_push_matrix();
285 gl_translatef(tpos.x, tpos.y, tpos.z);
287 glEnable(GL_BLEND);
288 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
290 bind_program(prog_text);
291 set_uniform_float(prog_text, "idx", idx);
293 bind_texture(tex_text, 0);
295 gl_begin(GL_QUADS);
296 gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
298 gl_texcoord2f(0, 1);
299 gl_vertex3f(-1, -0.2, 0);
301 gl_texcoord2f(1, 1);
302 gl_vertex3f(1, -0.2, 0);
304 gl_texcoord2f(1, 0);
305 gl_vertex3f(1, 0.2, 0);
307 gl_texcoord2f(0, 0);
308 gl_vertex3f(-1, 0.2, 0);
309 gl_end();
311 bind_texture(0, 0);
312 glDisable(GL_BLEND);
314 gl_pop_matrix();
315 }
318 static void worm(float t, float z, float *tx, float *ty)
319 {
320 float x, y;
321 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
322 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
324 *tx = x * 0.5;
325 *ty = y * 0.5;
326 }
329 void reshape(int x, int y)
330 {
331 glViewport(0, 0, x, y);
333 gl_matrix_mode(GL_PROJECTION);
334 gl_load_identity();
335 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
337 view_xsz = x;
338 view_ysz = y;
339 }
341 static unsigned int get_shader_program(const char *vfile, const char *pfile)
342 {
343 unsigned int prog, vs, ps;
345 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
346 return 0;
347 }
348 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
349 return 0;
350 }
352 if(!(prog = create_program_link(vs, ps))) {
353 return 0;
354 }
355 return prog;
356 }
359 #ifdef IPHONE
360 #include <QuartzCore/QuartzCore.h>
362 static float get_sec(void)
363 {
364 static float first;
365 static int init;
367 if(!init) {
368 init = 1;
369 first = CACurrentMediaTime();
370 return 0.0f;
371 }
372 return CACurrentMediaTime() - first;
373 }
375 #else
377 static float get_sec(void)
378 {
379 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
380 }
381 #endif