nuclear@0: #include "game.h" nuclear@2: #include "opt.h" nuclear@0: #include "opengl.h" nuclear@1: #include "level.h" nuclear@1: #include "renderer.h" nuclear@1: #include "camera.h" nuclear@1: nuclear@2: static void view_matrix(int eye); nuclear@2: static void proj_matrix(int eye); nuclear@2: nuclear@2: int win_xsz, win_ysz; nuclear@2: nuclear@2: bool keystate[GAME_MAX_KEYS]; nuclear@2: bool bnstate[GAME_MAX_BUTTONS]; nuclear@2: nuclear@2: float stereo_focus_dist = 0.25; nuclear@2: float stereo_eye_sep = stereo_focus_dist / 30.0; nuclear@1: nuclear@1: static Level *level; nuclear@1: static Renderer *rend; nuclear@1: static FpsCamera cam; nuclear@0: nuclear@0: bool game_init() nuclear@0: { nuclear@1: printf("initializing OpenGL state\n"); nuclear@1: glEnable(GL_CULL_FACE); nuclear@1: glEnable(GL_DEPTH_TEST); nuclear@1: glEnable(GL_LIGHTING); nuclear@1: glEnable(GL_LIGHT0); nuclear@1: nuclear@2: float lpos[] = {-1, 1, 1, 0}; nuclear@2: glLightfv(GL_LIGHT0, GL_POSITION, lpos); nuclear@2: nuclear@1: printf("initializing renderer\n"); nuclear@1: rend = new Renderer; nuclear@1: rend->init(); nuclear@1: nuclear@1: printf("generating level\n"); nuclear@1: level = new Level; nuclear@1: level->generate(); nuclear@1: nuclear@1: cam.input_move(0, 2, 2); nuclear@1: cam.input_rotate(0, M_PI / 5, 0); nuclear@1: nuclear@0: return true; nuclear@0: } nuclear@0: nuclear@0: void game_shutdown() nuclear@0: { nuclear@1: delete rend; nuclear@1: delete level; nuclear@0: } nuclear@0: nuclear@0: void game_iter(double dt) nuclear@0: { nuclear@2: float offs = 4.0 * dt; nuclear@1: float dx = 0, dy = 0; nuclear@1: nuclear@1: // handle key input nuclear@1: if(keystate['w'] || keystate['W']) { nuclear@1: dy -= offs; nuclear@1: } nuclear@1: if(keystate['s'] || keystate['S']) { nuclear@1: dy += offs; nuclear@1: } nuclear@1: if(keystate['d'] || keystate['D']) { nuclear@1: dx += offs; nuclear@1: } nuclear@1: if(keystate['a'] || keystate['A']) { nuclear@1: dx -= offs; nuclear@1: } nuclear@1: nuclear@1: cam.input_move(dx, 0, dy); nuclear@0: } nuclear@0: nuclear@0: void game_render() nuclear@0: { nuclear@2: if(opt.stereo) { nuclear@2: glDrawBuffer(GL_BACK_LEFT); nuclear@1: nuclear@2: glMatrixMode(GL_PROJECTION); nuclear@2: glLoadIdentity(); nuclear@2: proj_matrix(-1); nuclear@2: glMatrixMode(GL_MODELVIEW); nuclear@2: glLoadIdentity(); nuclear@2: view_matrix(-1); nuclear@1: nuclear@2: rend->render(level); nuclear@1: nuclear@2: glDrawBuffer(GL_BACK_RIGHT); nuclear@2: nuclear@2: glMatrixMode(GL_PROJECTION); nuclear@2: glLoadIdentity(); nuclear@2: proj_matrix(1); nuclear@2: glMatrixMode(GL_MODELVIEW); nuclear@2: glLoadIdentity(); nuclear@2: view_matrix(1); nuclear@2: nuclear@2: rend->render(level); nuclear@2: } else { nuclear@2: glMatrixMode(GL_PROJECTION); nuclear@2: glLoadIdentity(); nuclear@2: proj_matrix(0); nuclear@2: glMatrixMode(GL_MODELVIEW); nuclear@2: glLoadIdentity(); nuclear@2: view_matrix(0); nuclear@2: nuclear@2: rend->render(level); nuclear@2: } nuclear@0: } nuclear@1: nuclear@1: void game_input_shoot(int bn) nuclear@1: { nuclear@1: } nuclear@1: nuclear@1: void game_input_move(float x, float y, float z) nuclear@1: { nuclear@1: cam.input_move(x, y, z); nuclear@1: } nuclear@1: nuclear@1: void game_input_rot(float x, float y) nuclear@1: { nuclear@1: cam.input_rotate(x * 6.0, y * 6.0, 0); nuclear@1: } nuclear@2: nuclear@2: nuclear@2: static void view_matrix(int eye) nuclear@2: { nuclear@2: float offs = stereo_eye_sep * eye * 0.5; nuclear@2: glTranslatef(-offs, 0, 0); nuclear@2: cam.use_inverse(); nuclear@2: } nuclear@2: nuclear@2: static void proj_matrix(int eye) nuclear@2: { nuclear@2: static const float fov = M_PI / 4.0; nuclear@2: static const float near_clip = 0.1; nuclear@2: static const float far_clip = 500.0; nuclear@2: nuclear@2: float top = near_clip * tan(fov * 0.5); nuclear@2: float right = top * (float)win_xsz / (float)win_ysz; nuclear@2: nuclear@2: float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist); nuclear@2: glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip); nuclear@2: }