istereo

view src/istereo.c @ 20:21d58290e634

forgot the cam files
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 10:15:35 +0300
parents 6851489e70c2
children 75a63f9ab7cc
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 stereo;
26 /* construction parameters */
27 int sides = 24;
28 int segm = 20;
29 float ring_height = 0.5;
32 int init(void)
33 {
34 add_resource_path("sdr");
35 add_resource_path("data");
37 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
38 fprintf(stderr, "failed to load shader program\n");
39 return -1;
40 }
42 if(!(tex = load_texture(find_resource("tiles.ppm", 0, 0)))) {
43 fprintf(stderr, "failed to load texture\n");
44 return -1;
45 }
47 return 0;
48 }
50 void cleanup(void)
51 {
52 free_program(prog);
53 }
55 void redraw(void)
56 {
57 float pan_x, pan_y, z;
58 float tsec = get_sec();
60 z = ring_height * (segm - 1);
61 worm(tsec, z, &pan_x, &pan_y);
63 if(stereo) {
64 #if 0
65 /* left eye */
66 glDrawBuffer(GL_BACK_LEFT);
68 glMatrixMode(GL_PROJECTION);
69 glLoadIdentity();
70 cam_stereo_proj_matrix(CAM_LEFT);
72 glMatrixMode(GL_MODELVIEW);
73 glLoadIdentity();
74 cam_stereo_view_matrix(CAM_LEFT);
75 glTranslatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
77 render(tsec);
79 /* right eye */
80 glDrawBuffer(GL_BACK_RIGHT);
82 glMatrixMode(GL_PROJECTION);
83 glLoadIdentity();
84 cam_stereo_proj_matrix(CAM_RIGHT);
86 glMatrixMode(GL_MODELVIEW);
87 glLoadIdentity();
88 cam_stereo_view_matrix(CAM_RIGHT);
89 glTranslatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
91 render(tsec);
92 #endif
93 } else {
94 gl_matrix_mode(GL_MODELVIEW);
95 gl_load_identity();
96 cam_view_matrix();
97 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
99 render(tsec);
100 }
103 /*glClearColor(0.4, 0.6, 1.0, 1.0);
104 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
106 bind_program(prog);
108 gl_matrix_mode(GL_MODELVIEW);
109 gl_load_identity();
110 gl_translatef(0, 0, -8);
111 gl_rotatef(t * 100.0, 0, 0, 1);
113 bind_texture(tex, 0);
114 set_uniform_int(prog, "tex", 0);
116 gl_begin(GL_QUADS);
117 gl_texcoord2f(0, 0);
118 gl_color3f(1, 0, 0);
119 gl_vertex3f(-1, -1, 0);
120 gl_texcoord2f(1, 0);
121 gl_color3f(0, 1, 0);
122 gl_vertex3f(1, -1, 0);
123 gl_texcoord2f(1, 1);
124 gl_color3f(0, 0, 1);
125 gl_vertex3f(1, 1, 0);
126 gl_texcoord2f(0, 1);
127 gl_color3f(1, 1, 0);
128 gl_vertex3f(-1, 1, 0);
129 gl_end();
131 bind_texture(0, 0);*/
133 assert(glGetError() == GL_NO_ERROR);
134 }
136 static void render(float t)
137 {
138 glClear(GL_COLOR_BUFFER_BIT);
140 draw_tunnel(t);
141 }
143 static void draw_tunnel(float t)
144 {
145 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
146 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
147 int i, j, k;
148 float du, dv;
150 bind_program(prog);
151 set_uniform_float(prog, "t", t);
153 bind_texture(tex, 0);
155 /*glMatrixMode(GL_TEXTURE);
156 glLoadIdentity();
157 //glTranslatef(0, -t * 1.5, 0);
158 glTranslatef(0, -t * 0.5, 0);*/
160 gl_begin(GL_QUADS);
161 gl_color3f(1.0, 1.0, 1.0);
163 du = 1.0 / sides;
164 dv = 1.0 / segm;
166 for(i=0; i<segm; i++) {
167 float trans_zp[2], trans_z0[2], trans_z1[2];
169 float zp = ring_height * (i - 1);
170 float z0 = ring_height * i;
171 float z1 = ring_height * (i + 1);
173 float v0 = (float)i / (float)segm;
174 float v1 = (float)(i + 1) / (float)segm;
176 worm(t, zp, trans_zp, trans_zp + 1);
177 worm(t, z0, trans_z0, trans_z0 + 1);
178 worm(t, z1, trans_z1, trans_z1 + 1);
180 for(j=0; j<sides; j++) {
181 for(k=0; k<4; k++) {
182 float u = (j + uoffs[k]) * du;
183 float v = (i + voffs[k]) * dv;
185 tunnel_vertex(u, v, du, dv, t);
186 }
187 }
188 }
189 gl_end();
191 bind_texture(0, 0);
192 }
194 static void tunnel_vertex(float u, float v, float du, float dv, float t)
195 {
196 float pos[3];
198 float theta = 2.0 * M_PI * u;
199 float theta1 = 2.0 * M_PI * (u + du);
201 float x = cos(theta);
202 float y = sin(theta);
203 float x1 = cos(theta1);
204 float y1 = sin(theta1);
205 float z = v / dv * ring_height;
206 float z1 = (v + dv) / dv * ring_height;
208 float trans_z[2], trans_z1[2];
210 worm(t, z, trans_z, trans_z + 1);
211 worm(t, z1, trans_z1, trans_z1 + 1);
213 pos[0] = x + trans_z[0];
214 pos[1] = y + trans_z[1];
215 pos[2] = z;
217 /*v3_cons(pos, x + trans_z[0], y + trans_z[1], z);
218 v3_cons(pos_du, x1 + trans_z[0], y1 + trans_z[1], z);
219 v3_cons(pos_dv, x + trans_z1[0], y + trans_z1[1], z1);*/
221 /*v3_sub(dfdu, pos_du, pos);
222 v3_sub(dfdv, pos_dv, pos);
223 v3_cross(norm, dfdv, dfdu);*/
225 /*glVertexAttrib3f(tloc, dfdu[0], dfdu[1], dfdu[2]);
226 glNormal3f(norm[0], norm[1], norm[2]);*/
227 gl_texcoord2f(u * 2.0, v * 4.0);
228 gl_vertex3f(pos[0], pos[1], pos[2]);
229 }
233 static void worm(float t, float z, float *tx, float *ty)
234 {
235 float x, y;
236 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
237 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
239 *tx = x * 0.5;
240 *ty = y * 0.5;
241 }
244 void reshape(int x, int y)
245 {
246 glViewport(0, 0, x, y);
248 gl_matrix_mode(GL_PROJECTION);
249 gl_load_identity();
250 glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0);
251 }
253 static unsigned int get_shader_program(const char *vfile, const char *pfile)
254 {
255 unsigned int prog, vs, ps;
257 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
258 return -1;
259 }
260 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
261 return -1;
262 }
264 if(!(prog = create_program_link(vs, ps))) {
265 return -1;
266 }
267 return prog;
268 }
271 #ifdef IPHONE
272 #include <QuartzCore/QuartzCore.h>
274 static float get_sec(void)
275 {
276 static float first;
277 static int init;
279 if(!init) {
280 init = 1;
281 first = CACurrentMediaTime();
282 return 0.0f;
283 }
284 return CACurrentMediaTime() - first;
285 }
287 #else
289 static float get_sec(void)
290 {
291 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
292 }
293 #endif