bloboland

annotate 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
rev   line source
nuclear@0 1 #include "game.h"
nuclear@2 2 #include "opt.h"
nuclear@0 3 #include "opengl.h"
nuclear@1 4 #include "level.h"
nuclear@1 5 #include "renderer.h"
nuclear@1 6 #include "camera.h"
nuclear@1 7
nuclear@2 8 static void view_matrix(int eye);
nuclear@2 9 static void proj_matrix(int eye);
nuclear@2 10
nuclear@2 11 int win_xsz, win_ysz;
nuclear@2 12
nuclear@2 13 bool keystate[GAME_MAX_KEYS];
nuclear@2 14 bool bnstate[GAME_MAX_BUTTONS];
nuclear@2 15
nuclear@2 16 float stereo_focus_dist = 0.25;
nuclear@2 17 float stereo_eye_sep = stereo_focus_dist / 30.0;
nuclear@1 18
nuclear@1 19 static Level *level;
nuclear@1 20 static Renderer *rend;
nuclear@1 21 static FpsCamera cam;
nuclear@0 22
nuclear@0 23 bool game_init()
nuclear@0 24 {
nuclear@1 25 printf("initializing OpenGL state\n");
nuclear@1 26 glEnable(GL_CULL_FACE);
nuclear@1 27 glEnable(GL_DEPTH_TEST);
nuclear@1 28 glEnable(GL_LIGHTING);
nuclear@1 29 glEnable(GL_LIGHT0);
nuclear@1 30
nuclear@2 31 float lpos[] = {-1, 1, 1, 0};
nuclear@2 32 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@2 33
nuclear@1 34 printf("initializing renderer\n");
nuclear@1 35 rend = new Renderer;
nuclear@1 36 rend->init();
nuclear@1 37
nuclear@1 38 printf("generating level\n");
nuclear@1 39 level = new Level;
nuclear@1 40 level->generate();
nuclear@1 41
nuclear@1 42 cam.input_move(0, 2, 2);
nuclear@1 43 cam.input_rotate(0, M_PI / 5, 0);
nuclear@1 44
nuclear@0 45 return true;
nuclear@0 46 }
nuclear@0 47
nuclear@0 48 void game_shutdown()
nuclear@0 49 {
nuclear@1 50 delete rend;
nuclear@1 51 delete level;
nuclear@0 52 }
nuclear@0 53
nuclear@0 54 void game_iter(double dt)
nuclear@0 55 {
nuclear@2 56 float offs = 4.0 * dt;
nuclear@1 57 float dx = 0, dy = 0;
nuclear@1 58
nuclear@1 59 // handle key input
nuclear@1 60 if(keystate['w'] || keystate['W']) {
nuclear@1 61 dy -= offs;
nuclear@1 62 }
nuclear@1 63 if(keystate['s'] || keystate['S']) {
nuclear@1 64 dy += offs;
nuclear@1 65 }
nuclear@1 66 if(keystate['d'] || keystate['D']) {
nuclear@1 67 dx += offs;
nuclear@1 68 }
nuclear@1 69 if(keystate['a'] || keystate['A']) {
nuclear@1 70 dx -= offs;
nuclear@1 71 }
nuclear@1 72
nuclear@1 73 cam.input_move(dx, 0, dy);
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 void game_render()
nuclear@0 77 {
nuclear@2 78 if(opt.stereo) {
nuclear@2 79 glDrawBuffer(GL_BACK_LEFT);
nuclear@1 80
nuclear@2 81 glMatrixMode(GL_PROJECTION);
nuclear@2 82 glLoadIdentity();
nuclear@2 83 proj_matrix(-1);
nuclear@2 84 glMatrixMode(GL_MODELVIEW);
nuclear@2 85 glLoadIdentity();
nuclear@2 86 view_matrix(-1);
nuclear@1 87
nuclear@2 88 rend->render(level);
nuclear@1 89
nuclear@2 90 glDrawBuffer(GL_BACK_RIGHT);
nuclear@2 91
nuclear@2 92 glMatrixMode(GL_PROJECTION);
nuclear@2 93 glLoadIdentity();
nuclear@2 94 proj_matrix(1);
nuclear@2 95 glMatrixMode(GL_MODELVIEW);
nuclear@2 96 glLoadIdentity();
nuclear@2 97 view_matrix(1);
nuclear@2 98
nuclear@2 99 rend->render(level);
nuclear@2 100 } else {
nuclear@2 101 glMatrixMode(GL_PROJECTION);
nuclear@2 102 glLoadIdentity();
nuclear@2 103 proj_matrix(0);
nuclear@2 104 glMatrixMode(GL_MODELVIEW);
nuclear@2 105 glLoadIdentity();
nuclear@2 106 view_matrix(0);
nuclear@2 107
nuclear@2 108 rend->render(level);
nuclear@2 109 }
nuclear@0 110 }
nuclear@1 111
nuclear@1 112 void game_input_shoot(int bn)
nuclear@1 113 {
nuclear@1 114 }
nuclear@1 115
nuclear@1 116 void game_input_move(float x, float y, float z)
nuclear@1 117 {
nuclear@1 118 cam.input_move(x, y, z);
nuclear@1 119 }
nuclear@1 120
nuclear@1 121 void game_input_rot(float x, float y)
nuclear@1 122 {
nuclear@1 123 cam.input_rotate(x * 6.0, y * 6.0, 0);
nuclear@1 124 }
nuclear@2 125
nuclear@2 126
nuclear@2 127 static void view_matrix(int eye)
nuclear@2 128 {
nuclear@2 129 float offs = stereo_eye_sep * eye * 0.5;
nuclear@2 130 glTranslatef(-offs, 0, 0);
nuclear@2 131 cam.use_inverse();
nuclear@2 132 }
nuclear@2 133
nuclear@2 134 static void proj_matrix(int eye)
nuclear@2 135 {
nuclear@2 136 static const float fov = M_PI / 4.0;
nuclear@2 137 static const float near_clip = 0.1;
nuclear@2 138 static const float far_clip = 500.0;
nuclear@2 139
nuclear@2 140 float top = near_clip * tan(fov * 0.5);
nuclear@2 141 float right = top * (float)win_xsz / (float)win_ysz;
nuclear@2 142
nuclear@2 143 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
nuclear@2 144 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
nuclear@2 145 }