istereo

view src/istereo.c @ 23:e3742aafc85b

oh yeah
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 Sep 2011 02:57:29 +0300
parents 889dade25667
children 70309d71c899
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 "config.h"
14 static void render(float t);
15 static void draw_tunnel(float t);
16 static void tunnel_vertex(float u, float v, float du, float dv, float t);
17 static void worm(float t, float z, float *tx, float *ty);
18 static unsigned int get_shader_program(const char *vfile, const char *pfile);
19 static float get_sec(void);
21 unsigned int prog;
22 unsigned int tex;
24 int view_xsz, view_ysz;
26 int stereo = 1;
28 /* construction parameters */
29 int sides = 24;
30 int segm = 20;
31 float ring_height = 0.5;
33 float split = 0.53;
35 int init(void)
36 {
37 add_resource_path("sdr");
38 add_resource_path("data");
40 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
41 fprintf(stderr, "failed to load shader program\n");
42 return -1;
43 }
45 if(!(tex = load_texture(find_resource("tiles.ppm", 0, 0)))) {
46 fprintf(stderr, "failed to load texture\n");
47 return -1;
48 }
50 cam_fov(43.0);
52 return 0;
53 }
55 void cleanup(void)
56 {
57 free_program(prog);
58 }
60 void redraw(void)
61 {
62 float pan_x, pan_y, z;
63 float tsec = get_sec();
65 z = ring_height * segm;
66 worm(tsec, z, &pan_x, &pan_y);
68 glClearColor(0.6, 0.6, 0.6, 1.0);
69 glClear(GL_COLOR_BUFFER_BIT);
71 if(stereo) {
72 int split_pt = (int)((float)view_ysz * split);
74 /* right eye */
75 glViewport(0, 0, view_xsz, split_pt);
76 cam_aspect((float)split_pt / (float)view_xsz);
78 gl_matrix_mode(GL_PROJECTION);
79 gl_load_identity();
80 cam_stereo_proj_matrix(CAM_RIGHT);
81 gl_rotatef(-90, 0, 0, 1);
83 gl_matrix_mode(GL_MODELVIEW);
84 gl_load_identity();
85 cam_stereo_view_matrix(CAM_RIGHT);
86 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
87 /*gl_rotatef(-90, 0, 0, 1);*/
89 render(tsec);
91 /* left eye */
92 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
93 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
95 gl_matrix_mode(GL_PROJECTION);
96 gl_load_identity();
97 cam_stereo_proj_matrix(CAM_LEFT);
98 gl_rotatef(-90, 0, 0, 1);
100 gl_matrix_mode(GL_MODELVIEW);
101 gl_load_identity();
102 cam_stereo_view_matrix(CAM_LEFT);
103 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
104 /*gl_rotatef(-90, 0, 0, 1);*/
106 render(tsec);
107 } else {
108 gl_matrix_mode(GL_MODELVIEW);
109 gl_load_identity();
110 cam_view_matrix();
111 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
113 render(tsec);
114 }
117 /*glClearColor(0.4, 0.6, 1.0, 1.0);
118 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
120 bind_program(prog);
122 gl_matrix_mode(GL_MODELVIEW);
123 gl_load_identity();
124 gl_translatef(0, 0, -8);
125 gl_rotatef(t * 100.0, 0, 0, 1);
127 bind_texture(tex, 0);
128 set_uniform_int(prog, "tex", 0);
130 gl_begin(GL_QUADS);
131 gl_texcoord2f(0, 0);
132 gl_color3f(1, 0, 0);
133 gl_vertex3f(-1, -1, 0);
134 gl_texcoord2f(1, 0);
135 gl_color3f(0, 1, 0);
136 gl_vertex3f(1, -1, 0);
137 gl_texcoord2f(1, 1);
138 gl_color3f(0, 0, 1);
139 gl_vertex3f(1, 1, 0);
140 gl_texcoord2f(0, 1);
141 gl_color3f(1, 1, 0);
142 gl_vertex3f(-1, 1, 0);
143 gl_end();
145 bind_texture(0, 0);*/
147 assert(glGetError() == GL_NO_ERROR);
148 }
150 static void render(float t)
151 {
152 draw_tunnel(t);
153 }
155 static void draw_tunnel(float t)
156 {
157 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
158 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
159 int i, j, k;
160 float du, dv;
162 bind_program(prog);
163 set_uniform_float(prog, "t", t);
165 bind_texture(tex, 0);
167 /*glMatrixMode(GL_TEXTURE);
168 glLoadIdentity();
169 //glTranslatef(0, -t * 1.5, 0);
170 glTranslatef(0, -t * 0.5, 0);*/
172 gl_begin(GL_QUADS);
173 gl_color3f(1.0, 1.0, 1.0);
175 du = 1.0 / sides;
176 dv = 1.0 / segm;
178 for(i=0; i<segm; i++) {
179 float trans_zp[2], trans_z0[2], trans_z1[2];
181 float zp = ring_height * (i - 1);
182 float z0 = ring_height * i;
183 float z1 = ring_height * (i + 1);
185 float v0 = (float)i / (float)segm;
186 float v1 = (float)(i + 1) / (float)segm;
188 worm(t, zp, trans_zp, trans_zp + 1);
189 worm(t, z0, trans_z0, trans_z0 + 1);
190 worm(t, z1, trans_z1, trans_z1 + 1);
192 for(j=0; j<sides; j++) {
193 for(k=0; k<4; k++) {
194 float u = (j + uoffs[k]) * du;
195 float v = (i + voffs[k]) * dv;
197 tunnel_vertex(u, v, du, dv, t);
198 }
199 }
200 }
201 gl_end();
203 /*gl_begin(GL_QUADS);
204 gl_color3f(1.0, 0.3, 0.2);
205 gl_vertex2f(-100, -100);
206 gl_vertex2f(100, -100);
207 gl_vertex2f(100, 0);
208 gl_vertex2f(-100, 0);
209 gl_end();*/
211 bind_texture(0, 0);
212 }
214 static void tunnel_vertex(float u, float v, float du, float dv, float t)
215 {
216 float pos[3];
218 float theta = 2.0 * M_PI * u;
219 float theta1 = 2.0 * M_PI * (u + du);
221 float x = cos(theta);
222 float y = sin(theta);
223 float x1 = cos(theta1);
224 float y1 = sin(theta1);
225 float z = v / dv * ring_height;
226 float z1 = (v + dv) / dv * ring_height;
228 float trans_z[2], trans_z1[2];
230 worm(t, z, trans_z, trans_z + 1);
231 worm(t, z1, trans_z1, trans_z1 + 1);
233 pos[0] = x + trans_z[0];
234 pos[1] = y + trans_z[1];
235 pos[2] = z;
237 /*v3_cons(pos, x + trans_z[0], y + trans_z[1], z);
238 v3_cons(pos_du, x1 + trans_z[0], y1 + trans_z[1], z);
239 v3_cons(pos_dv, x + trans_z1[0], y + trans_z1[1], z1);*/
241 /*v3_sub(dfdu, pos_du, pos);
242 v3_sub(dfdv, pos_dv, pos);
243 v3_cross(norm, dfdv, dfdu);*/
245 /*glVertexAttrib3f(tloc, dfdu[0], dfdu[1], dfdu[2]);
246 glNormal3f(norm[0], norm[1], norm[2]);*/
247 gl_texcoord2f(u * 2.0, v * 4.0);
248 gl_vertex3f(pos[0], pos[1], pos[2]);
249 }
253 static void worm(float t, float z, float *tx, float *ty)
254 {
255 float x, y;
256 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
257 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
259 *tx = x * 0.5;
260 *ty = y * 0.5;
261 }
264 void reshape(int x, int y)
265 {
266 glViewport(0, 0, x, y);
268 gl_matrix_mode(GL_PROJECTION);
269 gl_load_identity();
270 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
272 view_xsz = x;
273 view_ysz = y;
274 }
276 static unsigned int get_shader_program(const char *vfile, const char *pfile)
277 {
278 unsigned int prog, vs, ps;
280 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
281 return -1;
282 }
283 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
284 return -1;
285 }
287 if(!(prog = create_program_link(vs, ps))) {
288 return -1;
289 }
290 return prog;
291 }
294 #ifdef IPHONE
295 #include <QuartzCore/QuartzCore.h>
297 static float get_sec(void)
298 {
299 static float first;
300 static int init;
302 if(!init) {
303 init = 1;
304 first = CACurrentMediaTime();
305 return 0.0f;
306 }
307 return CACurrentMediaTime() - first;
308 }
310 #else
312 static float get_sec(void)
313 {
314 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
315 }
316 #endif