istereo

annotate src/istereo.c @ 22:889dade25667

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