bloboland

view src/game.cc @ 3:a39c301cdcce

terrain raytracing pretty much done
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 16 Dec 2012 14:24:16 +0200
parents 1757973feaed
children 9021a906c5d3
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("generating level\n");
35 level = new Level;
36 level->generate();
38 printf("initializing renderer\n");
39 rend = new Renderer;
40 if(!rend->init(level)) {
41 return false;
42 }
44 cam.input_move(0, 2, 2);
45 cam.input_rotate(0, M_PI / 5, 0);
47 return true;
48 }
50 void game_shutdown()
51 {
52 delete rend;
53 delete level;
54 }
56 void game_iter(double dt)
57 {
58 float offs = 4.0 * dt;
59 float dx = 0, dy = 0;
61 // handle key input
62 if(keystate['w'] || keystate['W']) {
63 dy -= offs;
64 }
65 if(keystate['s'] || keystate['S']) {
66 dy += offs;
67 }
68 if(keystate['d'] || keystate['D']) {
69 dx += offs;
70 }
71 if(keystate['a'] || keystate['A']) {
72 dx -= offs;
73 }
75 cam.input_move(dx, 0, dy);
76 }
78 void game_render()
79 {
80 rend->set_aspect((float)win_xsz / (float)win_ysz);
82 if(opt.stereo) {
83 glDrawBuffer(GL_BACK_LEFT);
85 glMatrixMode(GL_PROJECTION);
86 glLoadIdentity();
87 proj_matrix(-1);
88 glMatrixMode(GL_MODELVIEW);
89 glLoadIdentity();
90 view_matrix(-1);
92 rend->render();
94 glDrawBuffer(GL_BACK_RIGHT);
96 glMatrixMode(GL_PROJECTION);
97 glLoadIdentity();
98 proj_matrix(1);
99 glMatrixMode(GL_MODELVIEW);
100 glLoadIdentity();
101 view_matrix(1);
103 rend->render();
104 } else {
105 glMatrixMode(GL_PROJECTION);
106 glLoadIdentity();
107 proj_matrix(0);
108 glMatrixMode(GL_MODELVIEW);
109 glLoadIdentity();
110 view_matrix(0);
112 rend->render();
113 }
114 }
116 void game_input_shoot(int bn)
117 {
118 }
120 void game_input_move(float x, float y, float z)
121 {
122 cam.input_move(x, y, z);
123 }
125 void game_input_rot(float x, float y)
126 {
127 cam.input_rotate(x * 6.0, y * 6.0, 0);
128 }
131 static void view_matrix(int eye)
132 {
133 float offs = stereo_eye_sep * eye * 0.5;
134 glTranslatef(-offs, 0, 0);
135 cam.use();
136 }
138 static void proj_matrix(int eye)
139 {
140 static const float fov = M_PI / 4.0;
141 static const float near_clip = 0.1;
142 static const float far_clip = 500.0;
144 float top = near_clip * tan(fov * 0.5);
145 float right = top * (float)win_xsz / (float)win_ysz;
147 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
148 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
149 }