bloboland

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