istereo

annotate src/istereo.c @ 19:ab4972098eb7

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