istereo
view src/istereo.c @ 21:75a63f9ab7cc
ha!
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Wed, 07 Sep 2011 10:49:11 +0300 |
parents | ab4972098eb7 |
children | 889dade25667 |
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.5;
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(30.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 glClear(GL_COLOR_BUFFER_BIT);
70 if(stereo) {
71 int split_pt = (int)((float)view_ysz * split);
73 /* left eye */
74 glViewport(0, 0, view_xsz, split_pt);
75 cam_aspect((float)split_pt / (float)view_xsz);
77 gl_matrix_mode(GL_PROJECTION);
78 gl_load_identity();
79 cam_stereo_proj_matrix(CAM_LEFT);
80 gl_rotatef(-90, 0, 0, 1);
82 gl_matrix_mode(GL_MODELVIEW);
83 gl_load_identity();
84 cam_stereo_view_matrix(CAM_LEFT);
85 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
86 /*gl_rotatef(-90, 0, 0, 1);*/
88 render(tsec);
90 /* right eye */
91 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
92 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
94 gl_matrix_mode(GL_PROJECTION);
95 gl_load_identity();
96 cam_stereo_proj_matrix(CAM_RIGHT);
97 gl_rotatef(-90, 0, 0, 1);
99 gl_matrix_mode(GL_MODELVIEW);
100 gl_load_identity();
101 cam_stereo_view_matrix(CAM_RIGHT);
102 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
103 /*gl_rotatef(-90, 0, 0, 1);*/
105 render(tsec);
106 } else {
107 gl_matrix_mode(GL_MODELVIEW);
108 gl_load_identity();
109 cam_view_matrix();
110 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
112 render(tsec);
113 }
116 /*glClearColor(0.4, 0.6, 1.0, 1.0);
117 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
119 bind_program(prog);
121 gl_matrix_mode(GL_MODELVIEW);
122 gl_load_identity();
123 gl_translatef(0, 0, -8);
124 gl_rotatef(t * 100.0, 0, 0, 1);
126 bind_texture(tex, 0);
127 set_uniform_int(prog, "tex", 0);
129 gl_begin(GL_QUADS);
130 gl_texcoord2f(0, 0);
131 gl_color3f(1, 0, 0);
132 gl_vertex3f(-1, -1, 0);
133 gl_texcoord2f(1, 0);
134 gl_color3f(0, 1, 0);
135 gl_vertex3f(1, -1, 0);
136 gl_texcoord2f(1, 1);
137 gl_color3f(0, 0, 1);
138 gl_vertex3f(1, 1, 0);
139 gl_texcoord2f(0, 1);
140 gl_color3f(1, 1, 0);
141 gl_vertex3f(-1, 1, 0);
142 gl_end();
144 bind_texture(0, 0);*/
146 assert(glGetError() == GL_NO_ERROR);
147 }
149 static void render(float t)
150 {
151 draw_tunnel(t);
152 }
154 static void draw_tunnel(float t)
155 {
156 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
157 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
158 int i, j, k;
159 float du, dv;
161 bind_program(prog);
162 set_uniform_float(prog, "t", t);
164 bind_texture(tex, 0);
166 /*glMatrixMode(GL_TEXTURE);
167 glLoadIdentity();
168 //glTranslatef(0, -t * 1.5, 0);
169 glTranslatef(0, -t * 0.5, 0);*/
171 gl_begin(GL_QUADS);
172 gl_color3f(1.0, 1.0, 1.0);
174 du = 1.0 / sides;
175 dv = 1.0 / segm;
177 for(i=0; i<segm; i++) {
178 float trans_zp[2], trans_z0[2], trans_z1[2];
180 float zp = ring_height * (i - 1);
181 float z0 = ring_height * i;
182 float z1 = ring_height * (i + 1);
184 float v0 = (float)i / (float)segm;
185 float v1 = (float)(i + 1) / (float)segm;
187 worm(t, zp, trans_zp, trans_zp + 1);
188 worm(t, z0, trans_z0, trans_z0 + 1);
189 worm(t, z1, trans_z1, trans_z1 + 1);
191 for(j=0; j<sides; j++) {
192 for(k=0; k<4; k++) {
193 float u = (j + uoffs[k]) * du;
194 float v = (i + voffs[k]) * dv;
196 tunnel_vertex(u, v, du, dv, t);
197 }
198 }
199 }
200 gl_end();
202 /*gl_begin(GL_QUADS);
203 gl_color3f(1.0, 0.3, 0.2);
204 gl_vertex2f(-100, -100);
205 gl_vertex2f(100, -100);
206 gl_vertex2f(100, 0);
207 gl_vertex2f(-100, 0);
208 gl_end();*/
210 bind_texture(0, 0);
211 }
213 static void tunnel_vertex(float u, float v, float du, float dv, float t)
214 {
215 float pos[3];
217 float theta = 2.0 * M_PI * u;
218 float theta1 = 2.0 * M_PI * (u + du);
220 float x = cos(theta);
221 float y = sin(theta);
222 float x1 = cos(theta1);
223 float y1 = sin(theta1);
224 float z = v / dv * ring_height;
225 float z1 = (v + dv) / dv * ring_height;
227 float trans_z[2], trans_z1[2];
229 worm(t, z, trans_z, trans_z + 1);
230 worm(t, z1, trans_z1, trans_z1 + 1);
232 pos[0] = x + trans_z[0];
233 pos[1] = y + trans_z[1];
234 pos[2] = z;
236 /*v3_cons(pos, x + trans_z[0], y + trans_z[1], z);
237 v3_cons(pos_du, x1 + trans_z[0], y1 + trans_z[1], z);
238 v3_cons(pos_dv, x + trans_z1[0], y + trans_z1[1], z1);*/
240 /*v3_sub(dfdu, pos_du, pos);
241 v3_sub(dfdv, pos_dv, pos);
242 v3_cross(norm, dfdv, dfdu);*/
244 /*glVertexAttrib3f(tloc, dfdu[0], dfdu[1], dfdu[2]);
245 glNormal3f(norm[0], norm[1], norm[2]);*/
246 gl_texcoord2f(u * 2.0, v * 4.0);
247 gl_vertex3f(pos[0], pos[1], pos[2]);
248 }
252 static void worm(float t, float z, float *tx, float *ty)
253 {
254 float x, y;
255 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
256 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
258 *tx = x * 0.5;
259 *ty = y * 0.5;
260 }
263 void reshape(int x, int y)
264 {
265 glViewport(0, 0, x, y);
267 gl_matrix_mode(GL_PROJECTION);
268 gl_load_identity();
269 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
271 view_xsz = x;
272 view_ysz = y;
273 }
275 static unsigned int get_shader_program(const char *vfile, const char *pfile)
276 {
277 unsigned int prog, vs, ps;
279 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
280 return -1;
281 }
282 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
283 return -1;
284 }
286 if(!(prog = create_program_link(vs, ps))) {
287 return -1;
288 }
289 return prog;
290 }
293 #ifdef IPHONE
294 #include <QuartzCore/QuartzCore.h>
296 static float get_sec(void)
297 {
298 static float first;
299 static int init;
301 if(!init) {
302 init = 1;
303 first = CACurrentMediaTime();
304 return 0.0f;
305 }
306 return CACurrentMediaTime() - first;
307 }
309 #else
311 static float get_sec(void)
312 {
313 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
314 }
315 #endif