bloboland
changeset 2:1757973feaed
added stereoscopic rendering for no apparent reason
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 16 Dec 2012 00:37:35 +0200 |
parents | cfe68befb7cc |
children | a39c301cdcce |
files | Makefile src/game.cc src/game.h src/main.cc |
diffstat | 4 files changed, 101 insertions(+), 15 deletions(-) [+] |
line diff
1.1 --- a/Makefile Sat Dec 15 23:43:03 2012 +0200 1.2 +++ b/Makefile Sun Dec 16 00:37:35 2012 +0200 1.3 @@ -3,7 +3,10 @@ 1.4 dep = $(obj:.o=.d) 1.5 bin = blobo 1.6 1.7 -CXXFLAGS = -ansi -pedantic -Wall -g 1.8 +opt = -O3 -ffast-math 1.9 +dbg = -g 1.10 + 1.11 +CXXFLAGS = -ansi -pedantic -Wall $(opt) $(dbg) 1.12 LDFLAGS = $(libgl) -lvmath -limago 1.13 1.14 ifeq ($(shell uname -s), Darwin)
2.1 --- a/src/game.cc Sat Dec 15 23:43:03 2012 +0200 2.2 +++ b/src/game.cc Sun Dec 16 00:37:35 2012 +0200 2.3 @@ -1,11 +1,20 @@ 2.4 #include "game.h" 2.5 +#include "opt.h" 2.6 #include "opengl.h" 2.7 #include "level.h" 2.8 #include "renderer.h" 2.9 #include "camera.h" 2.10 2.11 -bool keystate[256]; 2.12 -bool bnstate[16]; 2.13 +static void view_matrix(int eye); 2.14 +static void proj_matrix(int eye); 2.15 + 2.16 +int win_xsz, win_ysz; 2.17 + 2.18 +bool keystate[GAME_MAX_KEYS]; 2.19 +bool bnstate[GAME_MAX_BUTTONS]; 2.20 + 2.21 +float stereo_focus_dist = 0.25; 2.22 +float stereo_eye_sep = stereo_focus_dist / 30.0; 2.23 2.24 static Level *level; 2.25 static Renderer *rend; 2.26 @@ -19,6 +28,9 @@ 2.27 glEnable(GL_LIGHTING); 2.28 glEnable(GL_LIGHT0); 2.29 2.30 + float lpos[] = {-1, 1, 1, 0}; 2.31 + glLightfv(GL_LIGHT0, GL_POSITION, lpos); 2.32 + 2.33 printf("initializing renderer\n"); 2.34 rend = new Renderer; 2.35 rend->init(); 2.36 @@ -41,7 +53,7 @@ 2.37 2.38 void game_iter(double dt) 2.39 { 2.40 - float offs = 0.05 * dt; 2.41 + float offs = 4.0 * dt; 2.42 float dx = 0, dy = 0; 2.43 2.44 // handle key input 2.45 @@ -63,15 +75,38 @@ 2.46 2.47 void game_render() 2.48 { 2.49 - glMatrixMode(GL_MODELVIEW); 2.50 - glLoadIdentity(); 2.51 + if(opt.stereo) { 2.52 + glDrawBuffer(GL_BACK_LEFT); 2.53 2.54 - float lpos[] = {-1, 1, 2, 0}; 2.55 - glLightfv(GL_LIGHT0, GL_POSITION, lpos); 2.56 + glMatrixMode(GL_PROJECTION); 2.57 + glLoadIdentity(); 2.58 + proj_matrix(-1); 2.59 + glMatrixMode(GL_MODELVIEW); 2.60 + glLoadIdentity(); 2.61 + view_matrix(-1); 2.62 2.63 - cam.use_inverse(); 2.64 + rend->render(level); 2.65 2.66 - rend->render(level); 2.67 + glDrawBuffer(GL_BACK_RIGHT); 2.68 + 2.69 + glMatrixMode(GL_PROJECTION); 2.70 + glLoadIdentity(); 2.71 + proj_matrix(1); 2.72 + glMatrixMode(GL_MODELVIEW); 2.73 + glLoadIdentity(); 2.74 + view_matrix(1); 2.75 + 2.76 + rend->render(level); 2.77 + } else { 2.78 + glMatrixMode(GL_PROJECTION); 2.79 + glLoadIdentity(); 2.80 + proj_matrix(0); 2.81 + glMatrixMode(GL_MODELVIEW); 2.82 + glLoadIdentity(); 2.83 + view_matrix(0); 2.84 + 2.85 + rend->render(level); 2.86 + } 2.87 } 2.88 2.89 void game_input_shoot(int bn) 2.90 @@ -87,3 +122,24 @@ 2.91 { 2.92 cam.input_rotate(x * 6.0, y * 6.0, 0); 2.93 } 2.94 + 2.95 + 2.96 +static void view_matrix(int eye) 2.97 +{ 2.98 + float offs = stereo_eye_sep * eye * 0.5; 2.99 + glTranslatef(-offs, 0, 0); 2.100 + cam.use_inverse(); 2.101 +} 2.102 + 2.103 +static void proj_matrix(int eye) 2.104 +{ 2.105 + static const float fov = M_PI / 4.0; 2.106 + static const float near_clip = 0.1; 2.107 + static const float far_clip = 500.0; 2.108 + 2.109 + float top = near_clip * tan(fov * 0.5); 2.110 + float right = top * (float)win_xsz / (float)win_ysz; 2.111 + 2.112 + float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist); 2.113 + glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip); 2.114 +}
3.1 --- a/src/game.h Sat Dec 15 23:43:03 2012 +0200 3.2 +++ b/src/game.h Sun Dec 16 00:37:35 2012 +0200 3.3 @@ -1,11 +1,14 @@ 3.4 #ifndef GAME_H_ 3.5 #define GAME_H_ 3.6 3.7 +extern int win_xsz, win_ysz; 3.8 + 3.9 #define GAME_MAX_KEYS 256 3.10 #define GAME_MAX_BUTTONS 16 3.11 3.12 extern bool keystate[GAME_MAX_KEYS]; 3.13 extern bool bnstate[GAME_MAX_BUTTONS]; 3.14 +extern float stereo_focus_dist, stereo_eye_sep; 3.15 3.16 bool game_init(); 3.17 void game_shutdown();
4.1 --- a/src/main.cc Sat Dec 15 23:43:03 2012 +0200 4.2 +++ b/src/main.cc Sun Dec 16 00:37:35 2012 +0200 4.3 @@ -18,9 +18,11 @@ 4.4 static void spacerot(int x, int y, int z); 4.5 static void spacebut(int bn, int state); 4.6 4.7 -static int win_xsz, win_ysz, centerx, centery; 4.8 +static int centerx, centery; 4.9 static unsigned int prev_msec; 4.10 4.11 +static bool stereo_shift_pressed; 4.12 + 4.13 int main(int argc, char **argv) 4.14 { 4.15 glutInit(&argc, argv); 4.16 @@ -59,6 +61,7 @@ 4.17 { 4.18 unsigned int msec = glutGet(GLUT_ELAPSED_TIME); 4.19 game_iter((msec - prev_msec) / 1000.0); 4.20 + prev_msec = msec; 4.21 4.22 4.23 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 4.24 @@ -101,8 +104,14 @@ 4.25 4.26 static void skeydown(int key, int x, int y) 4.27 { 4.28 - if(key == 27) { 4.29 + switch(key) { 4.30 + case 27: 4.31 exit(0); 4.32 + 4.33 + case 'z': 4.34 + case 'Z': 4.35 + stereo_shift_pressed = true; 4.36 + break; 4.37 } 4.38 4.39 if(key < GAME_MAX_KEYS) { 4.40 @@ -112,6 +121,13 @@ 4.41 4.42 static void skeyup(int key, int x, int y) 4.43 { 4.44 + switch(key) { 4.45 + case 'z': 4.46 + case 'Z': 4.47 + stereo_shift_pressed = false; 4.48 + break; 4.49 + } 4.50 + 4.51 if(key < GAME_MAX_KEYS) { 4.52 keystate[key] = false; 4.53 } 4.54 @@ -155,6 +171,15 @@ 4.55 prev_x = x; 4.56 prev_y = y; 4.57 4.58 + if(stereo_shift_pressed) { 4.59 + if(dy != 0) { 4.60 + stereo_focus_dist += dy * 0.01; 4.61 + stereo_eye_sep = stereo_focus_dist / 30.0; 4.62 + printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep); 4.63 + } 4.64 + return; 4.65 + } 4.66 + 4.67 if(bnstate[0]) { 4.68 game_input_rot((float)dx / win_xsz, (float)dy / win_ysz); 4.69 } 4.70 @@ -162,16 +187,15 @@ 4.71 4.72 static void spacemove(int x, int y, int z) 4.73 { 4.74 - game_input_move(x * 0.1, y * 0.1, z * 0.1); 4.75 + game_input_move(x * 0.0025, y * 0.0025, -z * 0.0025); 4.76 } 4.77 4.78 static void spacerot(int x, int y, int z) 4.79 { 4.80 - game_input_rot(y * 0.1, x * 0.1); 4.81 + game_input_rot(-y * 0.0001, -x * 0.0001); 4.82 } 4.83 4.84 static void spacebut(int bn, int state) 4.85 { 4.86 game_input_shoot(bn); 4.87 } 4.88 -