istereo

annotate 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
rev   line source
nuclear@2 1 #include <stdio.h>
nuclear@18 2 #include <math.h>
nuclear@2 3 #include <assert.h>
nuclear@3 4 #include <unistd.h>
nuclear@1 5 #include "opengl.h"
nuclear@0 6 #include "istereo.h"
nuclear@2 7 #include "sanegl.h"
nuclear@2 8 #include "sdr.h"
nuclear@4 9 #include "respath.h"
nuclear@14 10 #include "tex.h"
nuclear@19 11 #include "cam.h"
nuclear@18 12 #include "config.h"
nuclear@4 13
nuclear@19 14 static void render(float t);
nuclear@19 15 static void draw_tunnel(float t);
nuclear@19 16 static void tunnel_vertex(float u, float v, float du, float dv, float t);
nuclear@19 17 static void worm(float t, float z, float *tx, float *ty);
nuclear@4 18 static unsigned int get_shader_program(const char *vfile, const char *pfile);
nuclear@18 19 static float get_sec(void);
nuclear@2 20
nuclear@2 21 unsigned int prog;
nuclear@14 22 unsigned int tex;
nuclear@0 23
nuclear@21 24 int view_xsz, view_ysz;
nuclear@21 25
nuclear@21 26 int stereo = 1;
nuclear@18 27
nuclear@18 28 /* construction parameters */
nuclear@18 29 int sides = 24;
nuclear@18 30 int segm = 20;
nuclear@18 31 float ring_height = 0.5;
nuclear@18 32
nuclear@21 33 float split = 0.5;
nuclear@18 34
nuclear@0 35 int init(void)
nuclear@0 36 {
nuclear@4 37 add_resource_path("sdr");
nuclear@14 38 add_resource_path("data");
nuclear@3 39
nuclear@4 40 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
nuclear@2 41 fprintf(stderr, "failed to load shader program\n");
nuclear@2 42 return -1;
nuclear@2 43 }
nuclear@14 44
nuclear@14 45 if(!(tex = load_texture(find_resource("tiles.ppm", 0, 0)))) {
nuclear@14 46 fprintf(stderr, "failed to load texture\n");
nuclear@14 47 return -1;
nuclear@14 48 }
nuclear@14 49
nuclear@21 50 cam_fov(30.0);
nuclear@21 51
nuclear@0 52 return 0;
nuclear@0 53 }
nuclear@0 54
nuclear@0 55 void cleanup(void)
nuclear@0 56 {
nuclear@2 57 free_program(prog);
nuclear@0 58 }
nuclear@0 59
nuclear@0 60 void redraw(void)
nuclear@0 61 {
nuclear@19 62 float pan_x, pan_y, z;
nuclear@19 63 float tsec = get_sec();
nuclear@18 64
nuclear@21 65 z = ring_height * segm;
nuclear@19 66 worm(tsec, z, &pan_x, &pan_y);
nuclear@19 67
nuclear@21 68 glClear(GL_COLOR_BUFFER_BIT);
nuclear@21 69
nuclear@19 70 if(stereo) {
nuclear@21 71 int split_pt = (int)((float)view_ysz * split);
nuclear@21 72
nuclear@19 73 /* left eye */
nuclear@21 74 glViewport(0, 0, view_xsz, split_pt);
nuclear@21 75 cam_aspect((float)split_pt / (float)view_xsz);
nuclear@19 76
nuclear@21 77 gl_matrix_mode(GL_PROJECTION);
nuclear@21 78 gl_load_identity();
nuclear@19 79 cam_stereo_proj_matrix(CAM_LEFT);
nuclear@21 80 gl_rotatef(-90, 0, 0, 1);
nuclear@19 81
nuclear@21 82 gl_matrix_mode(GL_MODELVIEW);
nuclear@21 83 gl_load_identity();
nuclear@19 84 cam_stereo_view_matrix(CAM_LEFT);
nuclear@21 85 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@21 86 /*gl_rotatef(-90, 0, 0, 1);*/
nuclear@19 87
nuclear@19 88 render(tsec);
nuclear@19 89
nuclear@19 90 /* right eye */
nuclear@21 91 glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
nuclear@21 92 cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
nuclear@19 93
nuclear@21 94 gl_matrix_mode(GL_PROJECTION);
nuclear@21 95 gl_load_identity();
nuclear@19 96 cam_stereo_proj_matrix(CAM_RIGHT);
nuclear@21 97 gl_rotatef(-90, 0, 0, 1);
nuclear@19 98
nuclear@21 99 gl_matrix_mode(GL_MODELVIEW);
nuclear@21 100 gl_load_identity();
nuclear@19 101 cam_stereo_view_matrix(CAM_RIGHT);
nuclear@21 102 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@21 103 /*gl_rotatef(-90, 0, 0, 1);*/
nuclear@19 104
nuclear@19 105 render(tsec);
nuclear@19 106 } else {
nuclear@19 107 gl_matrix_mode(GL_MODELVIEW);
nuclear@19 108 gl_load_identity();
nuclear@19 109 cam_view_matrix();
nuclear@19 110 gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
nuclear@19 111
nuclear@19 112 render(tsec);
nuclear@19 113 }
nuclear@19 114
nuclear@19 115
nuclear@19 116 /*glClearColor(0.4, 0.6, 1.0, 1.0);
nuclear@0 117 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@2 118
nuclear@2 119 bind_program(prog);
nuclear@2 120
nuclear@13 121 gl_matrix_mode(GL_MODELVIEW);
nuclear@2 122 gl_load_identity();
nuclear@13 123 gl_translatef(0, 0, -8);
nuclear@18 124 gl_rotatef(t * 100.0, 0, 0, 1);
nuclear@2 125
nuclear@16 126 bind_texture(tex, 0);
nuclear@17 127 set_uniform_int(prog, "tex", 0);
nuclear@6 128
nuclear@13 129 gl_begin(GL_QUADS);
nuclear@14 130 gl_texcoord2f(0, 0);
nuclear@2 131 gl_color3f(1, 0, 0);
nuclear@2 132 gl_vertex3f(-1, -1, 0);
nuclear@14 133 gl_texcoord2f(1, 0);
nuclear@2 134 gl_color3f(0, 1, 0);
nuclear@2 135 gl_vertex3f(1, -1, 0);
nuclear@14 136 gl_texcoord2f(1, 1);
nuclear@2 137 gl_color3f(0, 0, 1);
nuclear@2 138 gl_vertex3f(1, 1, 0);
nuclear@14 139 gl_texcoord2f(0, 1);
nuclear@2 140 gl_color3f(1, 1, 0);
nuclear@2 141 gl_vertex3f(-1, 1, 0);
nuclear@13 142 gl_end();
nuclear@2 143
nuclear@19 144 bind_texture(0, 0);*/
nuclear@14 145
nuclear@2 146 assert(glGetError() == GL_NO_ERROR);
nuclear@0 147 }
nuclear@2 148
nuclear@19 149 static void render(float t)
nuclear@19 150 {
nuclear@19 151 draw_tunnel(t);
nuclear@19 152 }
nuclear@19 153
nuclear@19 154 static void draw_tunnel(float t)
nuclear@19 155 {
nuclear@19 156 static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
nuclear@19 157 static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
nuclear@19 158 int i, j, k;
nuclear@19 159 float du, dv;
nuclear@19 160
nuclear@19 161 bind_program(prog);
nuclear@19 162 set_uniform_float(prog, "t", t);
nuclear@19 163
nuclear@19 164 bind_texture(tex, 0);
nuclear@19 165
nuclear@19 166 /*glMatrixMode(GL_TEXTURE);
nuclear@19 167 glLoadIdentity();
nuclear@19 168 //glTranslatef(0, -t * 1.5, 0);
nuclear@19 169 glTranslatef(0, -t * 0.5, 0);*/
nuclear@19 170
nuclear@19 171 gl_begin(GL_QUADS);
nuclear@19 172 gl_color3f(1.0, 1.0, 1.0);
nuclear@19 173
nuclear@19 174 du = 1.0 / sides;
nuclear@19 175 dv = 1.0 / segm;
nuclear@19 176
nuclear@19 177 for(i=0; i<segm; i++) {
nuclear@19 178 float trans_zp[2], trans_z0[2], trans_z1[2];
nuclear@19 179
nuclear@19 180 float zp = ring_height * (i - 1);
nuclear@19 181 float z0 = ring_height * i;
nuclear@19 182 float z1 = ring_height * (i + 1);
nuclear@19 183
nuclear@19 184 float v0 = (float)i / (float)segm;
nuclear@19 185 float v1 = (float)(i + 1) / (float)segm;
nuclear@19 186
nuclear@19 187 worm(t, zp, trans_zp, trans_zp + 1);
nuclear@19 188 worm(t, z0, trans_z0, trans_z0 + 1);
nuclear@19 189 worm(t, z1, trans_z1, trans_z1 + 1);
nuclear@19 190
nuclear@19 191 for(j=0; j<sides; j++) {
nuclear@19 192 for(k=0; k<4; k++) {
nuclear@19 193 float u = (j + uoffs[k]) * du;
nuclear@19 194 float v = (i + voffs[k]) * dv;
nuclear@19 195
nuclear@19 196 tunnel_vertex(u, v, du, dv, t);
nuclear@19 197 }
nuclear@19 198 }
nuclear@19 199 }
nuclear@19 200 gl_end();
nuclear@19 201
nuclear@21 202 /*gl_begin(GL_QUADS);
nuclear@21 203 gl_color3f(1.0, 0.3, 0.2);
nuclear@21 204 gl_vertex2f(-100, -100);
nuclear@21 205 gl_vertex2f(100, -100);
nuclear@21 206 gl_vertex2f(100, 0);
nuclear@21 207 gl_vertex2f(-100, 0);
nuclear@21 208 gl_end();*/
nuclear@21 209
nuclear@19 210 bind_texture(0, 0);
nuclear@19 211 }
nuclear@19 212
nuclear@19 213 static void tunnel_vertex(float u, float v, float du, float dv, float t)
nuclear@19 214 {
nuclear@19 215 float pos[3];
nuclear@19 216
nuclear@19 217 float theta = 2.0 * M_PI * u;
nuclear@19 218 float theta1 = 2.0 * M_PI * (u + du);
nuclear@19 219
nuclear@19 220 float x = cos(theta);
nuclear@19 221 float y = sin(theta);
nuclear@19 222 float x1 = cos(theta1);
nuclear@19 223 float y1 = sin(theta1);
nuclear@19 224 float z = v / dv * ring_height;
nuclear@19 225 float z1 = (v + dv) / dv * ring_height;
nuclear@19 226
nuclear@19 227 float trans_z[2], trans_z1[2];
nuclear@19 228
nuclear@19 229 worm(t, z, trans_z, trans_z + 1);
nuclear@19 230 worm(t, z1, trans_z1, trans_z1 + 1);
nuclear@19 231
nuclear@19 232 pos[0] = x + trans_z[0];
nuclear@19 233 pos[1] = y + trans_z[1];
nuclear@19 234 pos[2] = z;
nuclear@19 235
nuclear@19 236 /*v3_cons(pos, x + trans_z[0], y + trans_z[1], z);
nuclear@19 237 v3_cons(pos_du, x1 + trans_z[0], y1 + trans_z[1], z);
nuclear@19 238 v3_cons(pos_dv, x + trans_z1[0], y + trans_z1[1], z1);*/
nuclear@19 239
nuclear@19 240 /*v3_sub(dfdu, pos_du, pos);
nuclear@19 241 v3_sub(dfdv, pos_dv, pos);
nuclear@19 242 v3_cross(norm, dfdv, dfdu);*/
nuclear@19 243
nuclear@19 244 /*glVertexAttrib3f(tloc, dfdu[0], dfdu[1], dfdu[2]);
nuclear@19 245 glNormal3f(norm[0], norm[1], norm[2]);*/
nuclear@19 246 gl_texcoord2f(u * 2.0, v * 4.0);
nuclear@19 247 gl_vertex3f(pos[0], pos[1], pos[2]);
nuclear@19 248 }
nuclear@19 249
nuclear@19 250
nuclear@19 251
nuclear@19 252 static void worm(float t, float z, float *tx, float *ty)
nuclear@19 253 {
nuclear@19 254 float x, y;
nuclear@19 255 x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
nuclear@19 256 y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
nuclear@19 257
nuclear@19 258 *tx = x * 0.5;
nuclear@19 259 *ty = y * 0.5;
nuclear@19 260 }
nuclear@19 261
nuclear@19 262
nuclear@2 263 void reshape(int x, int y)
nuclear@2 264 {
nuclear@2 265 glViewport(0, 0, x, y);
nuclear@2 266
nuclear@2 267 gl_matrix_mode(GL_PROJECTION);
nuclear@2 268 gl_load_identity();
nuclear@21 269 glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
nuclear@21 270
nuclear@21 271 view_xsz = x;
nuclear@21 272 view_ysz = y;
nuclear@2 273 }
nuclear@4 274
nuclear@4 275 static unsigned int get_shader_program(const char *vfile, const char *pfile)
nuclear@4 276 {
nuclear@4 277 unsigned int prog, vs, ps;
nuclear@4 278
nuclear@4 279 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
nuclear@4 280 return -1;
nuclear@4 281 }
nuclear@4 282 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
nuclear@4 283 return -1;
nuclear@4 284 }
nuclear@4 285
nuclear@4 286 if(!(prog = create_program_link(vs, ps))) {
nuclear@4 287 return -1;
nuclear@4 288 }
nuclear@4 289 return prog;
nuclear@4 290 }
nuclear@18 291
nuclear@18 292
nuclear@18 293 #ifdef IPHONE
nuclear@18 294 #include <QuartzCore/QuartzCore.h>
nuclear@18 295
nuclear@18 296 static float get_sec(void)
nuclear@18 297 {
nuclear@18 298 static float first;
nuclear@18 299 static int init;
nuclear@18 300
nuclear@18 301 if(!init) {
nuclear@18 302 init = 1;
nuclear@18 303 first = CACurrentMediaTime();
nuclear@18 304 return 0.0f;
nuclear@18 305 }
nuclear@18 306 return CACurrentMediaTime() - first;
nuclear@18 307 }
nuclear@18 308
nuclear@18 309 #else
nuclear@18 310
nuclear@18 311 static float get_sec(void)
nuclear@18 312 {
nuclear@18 313 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
nuclear@18 314 }
nuclear@18 315 #endif