conworlds

view src/game.cc @ 13:283cdfa7dda2

added a crapload of code from goat3dgfx
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 24 Aug 2014 09:41:24 +0300
parents 778ed91cb7fd
children 9b0db7dbde6e
line source
1 #include "game.h"
2 #include "gameopt.h"
3 #include "opengl.h"
4 #include "camera.h"
5 #include "texture.h"
6 #include "vr/vr.h"
8 static void game_render_eye(int eye);
9 static void draw_scene();
10 static bool setup_rtarg(int x, int y);
12 static Texture *rtarg;
13 static unsigned int fbo, rtarg_depth;
14 static int rtwidth, rtheight;
16 static const float move_speed = 10.0f;
18 static int fb_width, fb_height;
19 static FlyCamera cam;
20 static Texture floor_tex;
21 static bool keystate[256];
22 static float player_height = 1.68;
24 bool game_init()
25 {
26 if(opt.vr) {
27 vr_init();
28 if(opt.vr_module) {
29 vr_use_module_named(opt.vr_module);
30 }
32 player_height = vr_get_optf(VR_OPT_EYE_HEIGHT);
33 }
35 glEnable(GL_DEPTH_TEST);
36 glEnable(GL_CULL_FACE);
37 glEnable(GL_LIGHTING);
38 glEnable(GL_LIGHT0);
39 glEnable(GL_NORMALIZE);
41 glClearColor(0.1, 0.1, 0.1, 1);
44 if(!floor_tex.load("data/tiles.png")) {
45 return false;
46 }
48 cam.input_move(0, player_height, 0);
49 return true;
50 }
52 void game_cleanup()
53 {
54 floor_tex.destroy();
55 if(opt.vr) {
56 vr_shutdown();
57 }
59 if(fbo) {
60 glDeleteFramebuffers(1, &fbo);
61 glDeleteRenderbuffers(1, &rtarg_depth);
62 delete rtarg;
63 }
64 }
66 void game_update(unsigned int msec)
67 {
68 static unsigned int prev_msec;
69 float dt = (msec - prev_msec) / 1000.0f;
70 float offs = dt * move_speed;
71 prev_msec = msec;
73 Vector3 move;
74 float roll = 0.0f;
76 if(keystate['d'] || keystate['D']) {
77 move.x += offs;
78 }
79 if(keystate['a'] || keystate['A']) {
80 move.x -= offs;
81 }
82 if(keystate['s'] || keystate['S']) {
83 move.z += offs;
84 }
85 if(keystate['w'] || keystate['W']) {
86 move.z -= offs;
87 }
88 if(keystate['e'] || keystate['E']) {
89 roll += dt;
90 }
91 if(keystate['q'] || keystate['Q']) {
92 roll -= dt;
93 }
95 cam.input_move(move.x, move.y, move.z);
96 cam.input_rotate(0, 0, roll);
97 }
99 void game_render()
100 {
101 if(opt.vr) {
102 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
103 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105 glViewport(0, 0, rtwidth / 2.0, rtheight);
106 vr_begin(VR_EYE_LEFT);
107 game_render_eye(-1);
108 vr_end();
110 glViewport(rtwidth / 2, 0, rtwidth / 2.0, rtheight);
111 vr_begin(VR_EYE_RIGHT);
112 game_render_eye(1);
113 vr_end();
115 glBindFramebuffer(GL_FRAMEBUFFER, 0);
116 glViewport(0, 0, fb_width, fb_height);
118 vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
119 (float)rtheight / (float)rtarg->get_height());
120 } else {
121 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
123 game_render_eye(0);
124 }
126 vr_swap_buffers();
127 }
129 static void game_render_eye(int eye)
130 {
131 float mat[16];
132 Matrix4x4 view_matrix = cam.get_matrix().inverse();
134 glMatrixMode(GL_PROJECTION);
135 glLoadIdentity();
136 if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
137 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
138 } else {
139 glLoadMatrixf(mat);
140 }
142 glMatrixMode(GL_MODELVIEW);
143 if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
144 glLoadIdentity();
145 } else {
146 glLoadMatrixf(mat);
147 }
148 glMultTransposeMatrixf(view_matrix[0]);
150 draw_scene();
151 }
153 void game_reshape(int x, int y)
154 {
155 glViewport(0, 0, x, y);
156 fb_width = x;
157 fb_height = y;
159 int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
160 if(lxres) {
161 int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
162 int rxres = vr_get_opti(VR_OPT_REYE_XRES);
163 int ryres = vr_get_opti(VR_OPT_REYE_YRES);
165 rtwidth = lxres + rxres;
166 rtheight = lyres > ryres ? lyres : ryres;
167 } else {
168 rtwidth = x;
169 rtheight = y;
170 }
172 setup_rtarg(rtwidth, rtheight);
173 }
175 void game_keyboard(int key, bool pressed, int x, int y)
176 {
177 if(pressed) {
178 switch(key) {
179 case 27:
180 exit(0);
182 case ' ':
183 vr_recenter();
184 break;
185 }
186 }
188 if(key < 256) {
189 keystate[key] = pressed;
190 }
191 }
193 static int prev_x, prev_y;
194 static bool bnstate[32];
196 void game_mouse(int bn, bool pressed, int x, int y)
197 {
198 bnstate[bn] = pressed;
199 prev_x = x;
200 prev_y = y;
201 }
203 void game_motion(int x, int y)
204 {
205 int dx = x - prev_x;
206 int dy = y - prev_y;
207 prev_x = x;
208 prev_y = y;
210 if(!dx && !dy) return;
212 if(bnstate[0]) {
213 float xrot = dy * 0.5;
214 float yrot = dx * 0.5;
215 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
216 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
217 }
218 }
220 void game_mwheel(int dir)
221 {
222 cam.input_move(0, dir * 0.1, 0);
223 }
225 void game_6dof_move(float x, float y, float z)
226 {
227 cam.input_move(x, y, z);
228 }
230 void game_6dof_rotate(float x, float y, float z)
231 {
232 cam.input_rotate(x, y, z);
233 }
235 static void draw_scene()
236 {
237 float lpos[] = {-20, 30, 10, 1};
238 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
240 glEnable(GL_TEXTURE_2D);
241 bind_texture(&floor_tex);
243 glMatrixMode(GL_TEXTURE);
244 glScalef(8, 8, 8);
246 glBegin(GL_QUADS);
247 glNormal3f(0, 1, 0);
248 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
249 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
250 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
251 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
252 glEnd();
253 glDisable(GL_TEXTURE_2D);
254 glLoadIdentity();
256 glMatrixMode(GL_MODELVIEW);
258 for(int i=0; i<4; i++) {
259 glPushMatrix();
260 glTranslatef(i & 1 ? -10 : 10, 0, i & 2 ? -10 : 10);
261 glScalef(2.0, 2.0, 2.0);
263 glBegin(GL_TRIANGLES);
264 glNormal3f(0, 1, 1);
265 glVertex3f(-1, 0, 1);
266 glVertex3f(1, 0, 1);
267 glVertex3f(0, 1.75, 0);
268 glNormal3f(1, 1, 0);
269 glVertex3f(1, 0, 1);
270 glVertex3f(1, 0, -1);
271 glVertex3f(0, 1.75, 0);
272 glNormal3f(0, 1, -1);
273 glVertex3f(1, 0, -1);
274 glVertex3f(-1, 0, -1);
275 glVertex3f(0, 1.75, 0);
276 glNormal3f(-1, 1, 0);
277 glVertex3f(-1, 0, -1);
278 glVertex3f(-1, 0, 1);
279 glVertex3f(0, 1.75, 0);
280 glEnd();
282 glPopMatrix();
283 }
284 }
286 static bool setup_rtarg(int x, int y)
287 {
288 int tex_width = next_pow2(x);
289 int tex_height = next_pow2(y);
291 /* create render targets for each eye */
292 if(!fbo) {
293 glGenFramebuffers(1, &fbo);
294 glGenRenderbuffers(1, &rtarg_depth);
295 rtarg = new Texture;
296 }
298 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
300 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
301 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
302 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
304 rtarg->create2d(tex_width, tex_height);
305 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
306 rtarg->get_texture_id(), 0);
308 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
309 fprintf(stderr, "incomplete framebuffer!\n");
310 return false;
311 }
312 glBindFramebuffer(GL_FRAMEBUFFER, 0);
314 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
315 return true;
316 }