bloboland

diff src/game.cc @ 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
line diff
     1.1 --- a/src/game.cc	Sat Dec 15 23:43:03 2012 +0200
     1.2 +++ b/src/game.cc	Sun Dec 16 00:37:35 2012 +0200
     1.3 @@ -1,11 +1,20 @@
     1.4  #include "game.h"
     1.5 +#include "opt.h"
     1.6  #include "opengl.h"
     1.7  #include "level.h"
     1.8  #include "renderer.h"
     1.9  #include "camera.h"
    1.10  
    1.11 -bool keystate[256];
    1.12 -bool bnstate[16];
    1.13 +static void view_matrix(int eye);
    1.14 +static void proj_matrix(int eye);
    1.15 +
    1.16 +int win_xsz, win_ysz;
    1.17 +
    1.18 +bool keystate[GAME_MAX_KEYS];
    1.19 +bool bnstate[GAME_MAX_BUTTONS];
    1.20 +
    1.21 +float stereo_focus_dist = 0.25;
    1.22 +float stereo_eye_sep = stereo_focus_dist / 30.0;
    1.23  
    1.24  static Level *level;
    1.25  static Renderer *rend;
    1.26 @@ -19,6 +28,9 @@
    1.27  	glEnable(GL_LIGHTING);
    1.28  	glEnable(GL_LIGHT0);
    1.29  
    1.30 +	float lpos[] = {-1, 1, 1, 0};
    1.31 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    1.32 +
    1.33  	printf("initializing renderer\n");
    1.34  	rend = new Renderer;
    1.35  	rend->init();
    1.36 @@ -41,7 +53,7 @@
    1.37  
    1.38  void game_iter(double dt)
    1.39  {
    1.40 -	float offs = 0.05 * dt;
    1.41 +	float offs = 4.0 * dt;
    1.42  	float dx = 0, dy = 0;
    1.43  
    1.44  	// handle key input
    1.45 @@ -63,15 +75,38 @@
    1.46  
    1.47  void game_render()
    1.48  {
    1.49 -	glMatrixMode(GL_MODELVIEW);
    1.50 -	glLoadIdentity();
    1.51 +	if(opt.stereo) {
    1.52 +		glDrawBuffer(GL_BACK_LEFT);
    1.53  
    1.54 -	float lpos[] = {-1, 1, 2, 0};
    1.55 -	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    1.56 +		glMatrixMode(GL_PROJECTION);
    1.57 +		glLoadIdentity();
    1.58 +		proj_matrix(-1);
    1.59 +		glMatrixMode(GL_MODELVIEW);
    1.60 +		glLoadIdentity();
    1.61 +		view_matrix(-1);
    1.62  
    1.63 -	cam.use_inverse();
    1.64 +		rend->render(level);
    1.65  
    1.66 -	rend->render(level);
    1.67 +		glDrawBuffer(GL_BACK_RIGHT);
    1.68 +
    1.69 +		glMatrixMode(GL_PROJECTION);
    1.70 +		glLoadIdentity();
    1.71 +		proj_matrix(1);
    1.72 +		glMatrixMode(GL_MODELVIEW);
    1.73 +		glLoadIdentity();
    1.74 +		view_matrix(1);
    1.75 +
    1.76 +		rend->render(level);
    1.77 +	} else {
    1.78 +		glMatrixMode(GL_PROJECTION);
    1.79 +		glLoadIdentity();
    1.80 +		proj_matrix(0);
    1.81 +		glMatrixMode(GL_MODELVIEW);
    1.82 +		glLoadIdentity();
    1.83 +		view_matrix(0);
    1.84 +
    1.85 +		rend->render(level);
    1.86 +	}
    1.87  }
    1.88  
    1.89  void game_input_shoot(int bn)
    1.90 @@ -87,3 +122,24 @@
    1.91  {
    1.92  	cam.input_rotate(x * 6.0, y * 6.0, 0);
    1.93  }
    1.94 +
    1.95 +
    1.96 +static void view_matrix(int eye)
    1.97 +{
    1.98 +	float offs = stereo_eye_sep * eye * 0.5;
    1.99 +	glTranslatef(-offs, 0, 0);
   1.100 +	cam.use_inverse();
   1.101 +}
   1.102 +
   1.103 +static void proj_matrix(int eye)
   1.104 +{
   1.105 +	static const float fov = M_PI / 4.0;
   1.106 +	static const float near_clip = 0.1;
   1.107 +	static const float far_clip = 500.0;
   1.108 +
   1.109 +	float top = near_clip * tan(fov * 0.5);
   1.110 +	float right = top * (float)win_xsz / (float)win_ysz;
   1.111 +
   1.112 +	float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
   1.113 +	glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
   1.114 +}