istereo

view src/istereo.c @ 25:206348366635

trying to figure out why the textures are looking crappy on ipod
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 04:23:56 +0300
parents 70309d71c899
children 862a3329a8f0
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 #ifdef IPHONE
27 int stereo = 1;
28 #else
29 int stereo = 0;
30 #endif
32 /* construction parameters */
33 int sides = 24;
34 int segm = 20;
35 float ring_height = 0.5;
37 float split = 0.53;
39 int init(void)
40 {
41 add_resource_path("sdr");
42 add_resource_path("data");
44 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
45 fprintf(stderr, "failed to load shader program\n");
46 return -1;
47 }
49 if(!(tex = load_texture(find_resource("tiles.ppm", 0, 0)))) {
50 fprintf(stderr, "failed to load texture\n");
51 return -1;
52 }
54 cam_fov(42.0);
56 return 0;
57 }
59 void cleanup(void)
60 {
61 free_program(prog);
62 }
64 void redraw(void)
65 {
66 float pan_x, pan_y, z;
67 float tsec = get_sec();
69 z = ring_height * segm;
70 worm(tsec, z, &pan_x, &pan_y);
72 glClearColor(0.6, 0.6, 0.6, 1.0);
73 glClear(GL_COLOR_BUFFER_BIT);
75 if(stereo) {
76 int split_pt = (int)((float)view_ysz * split);
78 /* right eye */
79 glViewport(0, 0, view_xsz, split_pt);
80 cam_aspect((float)split_pt / (float)view_xsz);
82 gl_matrix_mode(GL_PROJECTION);
83 gl_load_identity();
84 cam_stereo_proj_matrix(CAM_RIGHT);
85 gl_rotatef(-90, 0, 0, 1);
87 gl_matrix_mode(GL_MODELVIEW);
88 gl_load_identity();
89 cam_stereo_view_matrix(CAM_RIGHT);
90 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
91 /*gl_rotatef(-90, 0, 0, 1);*/
93 render(tsec);
95 /* left eye */
96 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
97 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
99 gl_matrix_mode(GL_PROJECTION);
100 gl_load_identity();
101 cam_stereo_proj_matrix(CAM_LEFT);
102 gl_rotatef(-90, 0, 0, 1);
104 gl_matrix_mode(GL_MODELVIEW);
105 gl_load_identity();
106 cam_stereo_view_matrix(CAM_LEFT);
107 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
108 /*gl_rotatef(-90, 0, 0, 1);*/
110 render(tsec);
111 } else {
112 gl_matrix_mode(GL_MODELVIEW);
113 gl_load_identity();
114 cam_view_matrix();
115 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
117 render(tsec);
118 }
121 /*glClearColor(0.4, 0.6, 1.0, 1.0);
122 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
124 bind_program(prog);
126 gl_matrix_mode(GL_MODELVIEW);
127 gl_load_identity();
128 gl_translatef(0, 0, -8);
129 gl_rotatef(t * 100.0, 0, 0, 1);
131 bind_texture(tex, 0);
132 set_uniform_int(prog, "tex", 0);
134 gl_begin(GL_QUADS);
135 gl_texcoord2f(0, 0);
136 gl_color3f(1, 0, 0);
137 gl_vertex3f(-1, -1, 0);
138 gl_texcoord2f(1, 0);
139 gl_color3f(0, 1, 0);
140 gl_vertex3f(1, -1, 0);
141 gl_texcoord2f(1, 1);
142 gl_color3f(0, 0, 1);
143 gl_vertex3f(1, 1, 0);
144 gl_texcoord2f(0, 1);
145 gl_color3f(1, 1, 0);
146 gl_vertex3f(-1, 1, 0);
147 gl_end();
149 bind_texture(0, 0);*/
151 assert(glGetError() == GL_NO_ERROR);
152 }
154 static void render(float t)
155 {
156 draw_tunnel(t);
157 }
159 static void draw_tunnel(float t)
160 {
161 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
162 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
163 int i, j, k;
164 float du, dv;
166 bind_program(prog);
167 set_uniform_float(prog, "t", t);
169 bind_texture(tex, 0);
171 gl_matrix_mode(GL_TEXTURE);
172 gl_load_identity();
173 gl_translatef(0, -t * 0.75, 0);
175 gl_begin(GL_QUADS);
176 gl_color3f(1.0, 1.0, 1.0);
178 du = 1.0 / sides;
179 dv = 1.0 / segm;
181 for(i=0; i<segm; i++) {
182 float trans_zp[2], trans_z0[2], trans_z1[2];
184 float zp = ring_height * (i - 1);
185 float z0 = ring_height * i;
186 float z1 = ring_height * (i + 1);
188 float v0 = (float)i / (float)segm;
189 float v1 = (float)(i + 1) / (float)segm;
191 worm(t, zp, trans_zp, trans_zp + 1);
192 worm(t, z0, trans_z0, trans_z0 + 1);
193 worm(t, z1, trans_z1, trans_z1 + 1);
195 for(j=0; j<sides; j++) {
196 for(k=0; k<4; k++) {
197 float u = (j + uoffs[k]) * du;
198 float v = (i + voffs[k]) * dv;
200 tunnel_vertex(u, v, du, dv, t);
201 }
202 }
203 }
204 gl_end();
206 /*gl_begin(GL_QUADS);
207 gl_color3f(1.0, 0.3, 0.2);
208 gl_vertex2f(-100, -100);
209 gl_vertex2f(100, -100);
210 gl_vertex2f(100, 0);
211 gl_vertex2f(-100, 0);
212 gl_end();*/
214 bind_texture(0, 0);
215 }
217 static void tunnel_vertex(float u, float v, float du, float dv, float t)
218 {
219 float pos[3];
221 float theta = 2.0 * M_PI * u;
222 float theta1 = 2.0 * M_PI * (u + du);
224 float x = cos(theta);
225 float y = sin(theta);
226 float x1 = cos(theta1);
227 float y1 = sin(theta1);
228 float z = v / dv * ring_height;
229 float z1 = (v + dv) / dv * ring_height;
231 float trans_z[2], trans_z1[2];
233 worm(t, z, trans_z, trans_z + 1);
234 worm(t, z1, trans_z1, trans_z1 + 1);
236 pos[0] = x + trans_z[0];
237 pos[1] = y + trans_z[1];
238 pos[2] = z;
240 /*v3_cons(pos, x + trans_z[0], y + trans_z[1], z);
241 v3_cons(pos_du, x1 + trans_z[0], y1 + trans_z[1], z);
242 v3_cons(pos_dv, x + trans_z1[0], y + trans_z1[1], z1);*/
244 /*v3_sub(dfdu, pos_du, pos);
245 v3_sub(dfdv, pos_dv, pos);
246 v3_cross(norm, dfdv, dfdu);*/
248 /*glVertexAttrib3f(tloc, dfdu[0], dfdu[1], dfdu[2]);
249 glNormal3f(norm[0], norm[1], norm[2]);*/
250 gl_texcoord2f(u * 2.0, v * 4.0);
251 gl_vertex3f(pos[0], pos[1], pos[2]);
252 }
256 static void worm(float t, float z, float *tx, float *ty)
257 {
258 float x, y;
259 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
260 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
262 *tx = x * 0.5;
263 *ty = y * 0.5;
264 }
267 void reshape(int x, int y)
268 {
269 glViewport(0, 0, x, y);
271 gl_matrix_mode(GL_PROJECTION);
272 gl_load_identity();
273 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
275 view_xsz = x;
276 view_ysz = y;
277 }
279 static unsigned int get_shader_program(const char *vfile, const char *pfile)
280 {
281 unsigned int prog, vs, ps;
283 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
284 return -1;
285 }
286 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
287 return -1;
288 }
290 if(!(prog = create_program_link(vs, ps))) {
291 return -1;
292 }
293 return prog;
294 }
297 #ifdef IPHONE
298 #include <QuartzCore/QuartzCore.h>
300 static float get_sec(void)
301 {
302 static float first;
303 static int init;
305 if(!init) {
306 init = 1;
307 first = CACurrentMediaTime();
308 return 0.0f;
309 }
310 return CACurrentMediaTime() - first;
311 }
313 #else
315 static float get_sec(void)
316 {
317 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
318 }
319 #endif