# HG changeset patch # User John Tsiombikas # Date 1355611055 -7200 # Node ID 1757973feaed2e99242c07512a158999c94095ed # Parent cfe68befb7cc2d1b0478d3984134c52345710c25 added stereoscopic rendering for no apparent reason diff -r cfe68befb7cc -r 1757973feaed Makefile --- a/Makefile Sat Dec 15 23:43:03 2012 +0200 +++ b/Makefile Sun Dec 16 00:37:35 2012 +0200 @@ -3,7 +3,10 @@ dep = $(obj:.o=.d) bin = blobo -CXXFLAGS = -ansi -pedantic -Wall -g +opt = -O3 -ffast-math +dbg = -g + +CXXFLAGS = -ansi -pedantic -Wall $(opt) $(dbg) LDFLAGS = $(libgl) -lvmath -limago ifeq ($(shell uname -s), Darwin) diff -r cfe68befb7cc -r 1757973feaed src/game.cc --- a/src/game.cc Sat Dec 15 23:43:03 2012 +0200 +++ b/src/game.cc Sun Dec 16 00:37:35 2012 +0200 @@ -1,11 +1,20 @@ #include "game.h" +#include "opt.h" #include "opengl.h" #include "level.h" #include "renderer.h" #include "camera.h" -bool keystate[256]; -bool bnstate[16]; +static void view_matrix(int eye); +static void proj_matrix(int eye); + +int win_xsz, win_ysz; + +bool keystate[GAME_MAX_KEYS]; +bool bnstate[GAME_MAX_BUTTONS]; + +float stereo_focus_dist = 0.25; +float stereo_eye_sep = stereo_focus_dist / 30.0; static Level *level; static Renderer *rend; @@ -19,6 +28,9 @@ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); + float lpos[] = {-1, 1, 1, 0}; + glLightfv(GL_LIGHT0, GL_POSITION, lpos); + printf("initializing renderer\n"); rend = new Renderer; rend->init(); @@ -41,7 +53,7 @@ void game_iter(double dt) { - float offs = 0.05 * dt; + float offs = 4.0 * dt; float dx = 0, dy = 0; // handle key input @@ -63,15 +75,38 @@ void game_render() { - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); + if(opt.stereo) { + glDrawBuffer(GL_BACK_LEFT); - float lpos[] = {-1, 1, 2, 0}; - glLightfv(GL_LIGHT0, GL_POSITION, lpos); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + proj_matrix(-1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + view_matrix(-1); - cam.use_inverse(); + rend->render(level); - rend->render(level); + glDrawBuffer(GL_BACK_RIGHT); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + proj_matrix(1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + view_matrix(1); + + rend->render(level); + } else { + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + proj_matrix(0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + view_matrix(0); + + rend->render(level); + } } void game_input_shoot(int bn) @@ -87,3 +122,24 @@ { cam.input_rotate(x * 6.0, y * 6.0, 0); } + + +static void view_matrix(int eye) +{ + float offs = stereo_eye_sep * eye * 0.5; + glTranslatef(-offs, 0, 0); + cam.use_inverse(); +} + +static void proj_matrix(int eye) +{ + static const float fov = M_PI / 4.0; + static const float near_clip = 0.1; + static const float far_clip = 500.0; + + float top = near_clip * tan(fov * 0.5); + float right = top * (float)win_xsz / (float)win_ysz; + + float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist); + glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip); +} diff -r cfe68befb7cc -r 1757973feaed src/game.h --- a/src/game.h Sat Dec 15 23:43:03 2012 +0200 +++ b/src/game.h Sun Dec 16 00:37:35 2012 +0200 @@ -1,11 +1,14 @@ #ifndef GAME_H_ #define GAME_H_ +extern int win_xsz, win_ysz; + #define GAME_MAX_KEYS 256 #define GAME_MAX_BUTTONS 16 extern bool keystate[GAME_MAX_KEYS]; extern bool bnstate[GAME_MAX_BUTTONS]; +extern float stereo_focus_dist, stereo_eye_sep; bool game_init(); void game_shutdown(); diff -r cfe68befb7cc -r 1757973feaed src/main.cc --- a/src/main.cc Sat Dec 15 23:43:03 2012 +0200 +++ b/src/main.cc Sun Dec 16 00:37:35 2012 +0200 @@ -18,9 +18,11 @@ static void spacerot(int x, int y, int z); static void spacebut(int bn, int state); -static int win_xsz, win_ysz, centerx, centery; +static int centerx, centery; static unsigned int prev_msec; +static bool stereo_shift_pressed; + int main(int argc, char **argv) { glutInit(&argc, argv); @@ -59,6 +61,7 @@ { unsigned int msec = glutGet(GLUT_ELAPSED_TIME); game_iter((msec - prev_msec) / 1000.0); + prev_msec = msec; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -101,8 +104,14 @@ static void skeydown(int key, int x, int y) { - if(key == 27) { + switch(key) { + case 27: exit(0); + + case 'z': + case 'Z': + stereo_shift_pressed = true; + break; } if(key < GAME_MAX_KEYS) { @@ -112,6 +121,13 @@ static void skeyup(int key, int x, int y) { + switch(key) { + case 'z': + case 'Z': + stereo_shift_pressed = false; + break; + } + if(key < GAME_MAX_KEYS) { keystate[key] = false; } @@ -155,6 +171,15 @@ prev_x = x; prev_y = y; + if(stereo_shift_pressed) { + if(dy != 0) { + stereo_focus_dist += dy * 0.01; + stereo_eye_sep = stereo_focus_dist / 30.0; + printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep); + } + return; + } + if(bnstate[0]) { game_input_rot((float)dx / win_xsz, (float)dy / win_ysz); } @@ -162,16 +187,15 @@ static void spacemove(int x, int y, int z) { - game_input_move(x * 0.1, y * 0.1, z * 0.1); + game_input_move(x * 0.0025, y * 0.0025, -z * 0.0025); } static void spacerot(int x, int y, int z) { - game_input_rot(y * 0.1, x * 0.1); + game_input_rot(-y * 0.0001, -x * 0.0001); } static void spacebut(int bn, int state) { game_input_shoot(bn); } -