vrheights

view src/game.cc @ 9:df3a70664a7d

falling
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 03 Oct 2014 20:36:24 +0300
parents 3f221bdc9bab
children 3e6757655fe2
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include <vector>
4 #include <algorithm>
5 #include <vmath/vmath.h>
6 #include "opengl.h"
7 #include "game.h"
8 #include "goatvr.h"
9 #include "teapot.h"
10 #include "console.h"
11 #include "drawtext.h"
12 #include "game_var.h"
13 #include "scenefile.h"
15 static void draw_scene();
16 static void material(float r, float g, float b, float roughness);
17 static void toggle_hmd_fullscr();
18 static void create_rtarg(int x, int y);
19 static int next_pow2(int x);
20 static void con_handle(const char *cmd);
22 static int win_width, win_height;
23 static unsigned int fb_tex;
24 static unsigned int fbo, fb_depth;
25 static int fb_xsz, fb_ysz;
26 static int fb_tex_xsz, fb_tex_ysz;
28 static unsigned int chess_tex;
30 static float cam_theta = -90, cam_phi;
31 static Vector3 cam_pos = Vector3(15.4, 0, 0);
32 static bool keystate[256];
34 static float cam_vvel;
35 static const float grav = -9.8;
37 static Console con;
38 static dtx_font *con_font;
40 static SceneFile scn;
42 #define GVAR_LOOK_WALK "look-walk"
43 #define GVAR_MAX_STEP "max-step"
45 bool game_init()
46 {
47 init_opengl();
49 if(!(con_font = dtx_open_font_glyphmap("data/mono14.glyphmap"))) {
50 fprintf(stderr, "failed to open console font\n");
51 return false;
52 }
53 con.set_font(con_font, 14);
54 con.set_size(7, 52);
55 con.set_position(0, 0, Console::CENTER);
56 con.set_command_func(con_handle);
57 con.set_echo(false);
60 if(vr_init() == -1) {
61 return false;
62 }
64 glEnable(GL_DEPTH_TEST);
65 glEnable(GL_CULL_FACE);
66 glEnable(GL_LIGHTING);
68 unsigned char chess_pixels[] = {
69 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff,
70 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff
71 };
72 glGenTextures(1, &chess_tex);
73 glBindTexture(GL_TEXTURE_2D, chess_tex);
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
76 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, chess_pixels);
78 if(!scn.load("data/sibenik.goatsce")) {
79 fprintf(stderr, "failed to load scene file\n");
80 return false;
81 }
83 /* initialize all option variables */
84 set_gvar_bool(GVAR_LOOK_WALK, true);
85 set_gvar_num(GVAR_MAX_STEP, 0.5);
87 return true;
88 }
90 void game_cleanup()
91 {
92 vr_shutdown();
93 }
95 void game_update(long tm)
96 {
97 static long prev_upd;
98 float dt = (tm - prev_upd) / 1000.0;
99 prev_upd = tm;
101 if(con.is_visible()) {
102 con.update();
103 return;
104 }
106 float offs = dt * 5.0;
107 Vector3 dir;
109 if(keystate['d'] || keystate['D']) {
110 dir += Vector3(offs, 0, 0);
111 }
112 if(keystate['a'] || keystate['A']) {
113 dir += Vector3(-offs, 0, 0);
114 }
115 if(keystate['w'] || keystate['W']) {
116 dir += Vector3(0, 0, -offs);
117 }
118 if(keystate['s'] || keystate['S']) {
119 dir += Vector3(0, 0, offs);
120 }
122 if(get_gvar_bool(GVAR_LOOK_WALK)) {
123 float rot[4];
124 vr_view_rotation(0, rot);
125 Quaternion q(rot[3], rot[0], rot[1], rot[2]);
127 dir.transform(q);
128 }
130 float cos_theta = cos(DEG_TO_RAD(cam_theta));
131 float sin_theta = sin(DEG_TO_RAD(cam_theta));
132 cam_pos.x += dir.x * cos_theta - dir.z * sin_theta;
133 cam_pos.z += dir.x * sin_theta + dir.z * cos_theta;
135 float max_step = get_gvar_num(GVAR_MAX_STEP);
136 Vector3 floor_pos = scn.find_walk_pos(cam_pos + Vector3(0, max_step, 0));
138 if(floor_pos.y < cam_pos.y) {
139 cam_vvel += grav * dt;
140 cam_pos.y += cam_vvel * dt;
141 if(cam_pos.y < floor_pos.y) {
142 cam_pos.y = floor_pos.y;
143 }
144 }
145 }
147 void game_display()
148 {
149 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
150 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
152 for(int i=0; i<2; i++) {
153 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
154 vr_begin(i);
156 float proj[16];
157 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
158 glMatrixMode(GL_PROJECTION);
159 glLoadMatrixf(proj);
160 }
162 glMatrixMode(GL_MODELVIEW);
164 float view[16];
165 vr_view_matrix(i, view);
166 glLoadMatrixf(view);
167 glRotatef(cam_phi, 1, 0, 0);
168 glRotatef(cam_theta, 0, 1, 0);
169 /* move the camera to the eye level of the user */
170 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
171 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
173 draw_scene();
174 con.draw();
176 vr_end();
177 }
179 glBindFramebuffer(GL_FRAMEBUFFER, 0);
180 glViewport(0, 0, win_width, win_height);
182 vr_swap_buffers();
183 assert(glGetError() == GL_NO_ERROR);
184 }
186 void game_reshape(int x, int y)
187 {
188 win_width = x;
189 win_height = y;
191 create_rtarg(vr_geti_def(VR_RENDER_XRES, x * 2), vr_geti_def(VR_RENDER_YRES, y));
192 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
194 /* these might be overriden in VR mode (see game_display) */
195 glViewport(0, 0, x, y);
196 glMatrixMode(GL_PROJECTION);
197 glLoadIdentity();
198 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
199 }
201 void game_keyboard(int key, bool pressed)
202 {
203 if(pressed) {
204 if(con.is_visible()) {
205 if(key == 27 || key == '`') {
206 con.hide();
207 } else {
208 con.input_key(key);
209 }
210 } else {
211 switch(key) {
212 case 27:
213 exit_game();
214 break;
216 case 'f':
217 toggle_hmd_fullscr();
218 break;
220 case 'r':
221 vr_recenter();
222 break;
224 case '`':
225 con.show();
226 break;
228 default:
229 break;
230 }
231 }
232 }
234 if(key < 256) {
235 keystate[key] = pressed;
236 }
237 }
240 static int prev_x, prev_y;
241 static bool bnstate[32];
243 void game_mouse_button(int bn, bool state, int x, int y)
244 {
245 bnstate[bn] = state;
246 prev_x = x;
247 prev_y = y;
248 }
250 void game_mouse_motion(int x, int y)
251 {
252 int dx = x - prev_x;
253 int dy = y - prev_y;
254 prev_x = x;
255 prev_y = y;
257 if(!dx && !dy) {
258 return;
259 }
260 if(bnstate[0]) {
261 cam_theta += dx * 0.5;
262 cam_phi += dy * 0.5;
264 if(cam_phi < -90) cam_phi = -90;
265 if(cam_phi > 90) cam_phi = 90;
266 }
267 }
269 MeshFace dbg_walk_face;
271 static void draw_scene()
272 {
273 float lpos[][4] = {
274 {-0.7, 0.7, 1, 0},
275 {1, 0, 1, 0}
276 };
277 float lcol[][4] = {
278 {0.9, 0.7, 0.6, 1},
279 {0.3, 0.4, 0.75, 1}
280 };
281 for(int i=0; i<2; i++) {
282 glEnable(GL_LIGHT0 + i);
283 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
284 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
285 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
286 }
288 glMatrixMode(GL_MODELVIEW);
290 /*
291 glBindTexture(GL_TEXTURE_2D, chess_tex);
292 glEnable(GL_TEXTURE_2D);
293 material(1, 1, 1, 1);
294 glBegin(GL_QUADS);
295 glNormal3f(0, 1, 0);
296 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
297 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
298 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
299 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
300 glEnd();
301 glDisable(GL_TEXTURE_2D);
302 */
304 /*material(1, 1, 1, 0.4);
305 glPushMatrix();
306 glTranslatef(0, 1.3, -10);
307 glFrontFace(GL_CW);
308 bezier_teapot(2.0);
309 glFrontFace(GL_CCW);
310 glPopMatrix();*/
312 for(size_t i=0; i<scn.obj.size(); i++) {
313 scn.obj[i]->draw();
314 }
316 glPushAttrib(GL_ENABLE_BIT);
317 glDisable(GL_DEPTH_TEST);
318 glDisable(GL_LIGHTING);
320 glBegin(GL_TRIANGLES);
321 glColor3f(0, 1, 0);
322 glVertex3fv(&dbg_walk_face.v[0].x);
323 glVertex3fv(&dbg_walk_face.v[1].x);
324 glVertex3fv(&dbg_walk_face.v[2].x);
325 glColor3f(1, 1, 1);
326 glEnd();
328 glPopAttrib();
329 }
331 static void material(float r, float g, float b, float roughness)
332 {
333 float gloss = 1.0 - roughness;
334 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
335 float specular[] = {gloss, gloss, gloss, 1.0};
336 float shin = gloss * 128.0;
338 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
339 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
340 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
341 }
344 static void toggle_hmd_fullscr()
345 {
346 static bool fullscr;
347 static int prev_x, prev_y;
348 //static int prev_xsz, prev_ysz;
350 fullscr = !fullscr;
351 if(fullscr) {
352 /* entering fullscreen on the HMD */
353 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
354 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
355 if(xoffs != -1) {
356 get_window_pos(&prev_x, &prev_y);
357 move_window(xoffs, yoffs);
358 }
360 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
361 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
362 if(xsz != -1) {
363 //prev_xsz = win_width;
364 //prev_ysz = win_height;
365 resize_window(xsz, ysz);
366 }
367 enter_fullscreen();
369 } else {
370 /* leaving fullscreen */
371 leave_fullscreen();
372 /*move_window(prev_x, prev_y);
373 resize_window(prev_xsz, prev_ysz);*/
374 }
375 }
377 static void create_rtarg(int x, int y)
378 {
379 if(x == fb_xsz && y == fb_ysz) {
380 return; // nothing changed
381 }
383 fb_xsz = x;
384 fb_ysz = y;
385 fb_tex_xsz = next_pow2(fb_xsz);
386 fb_tex_ysz = next_pow2(fb_ysz);
388 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
390 if(!fbo) {
391 glGenFramebuffers(1, &fbo);
393 glGenTextures(1, &fb_tex);
394 glBindTexture(GL_TEXTURE_2D, fb_tex);
395 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
396 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
398 glGenRenderbuffers(1, &fb_depth);
399 }
401 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
403 glBindTexture(GL_TEXTURE_2D, fb_tex);
404 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
405 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
407 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
408 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
409 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
411 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
412 glBindFramebuffer(GL_FRAMEBUFFER, 0);
413 }
415 static int next_pow2(int x)
416 {
417 x -= 1;
418 x |= x >> 1;
419 x |= x >> 2;
420 x |= x >> 4;
421 x |= x >> 8;
422 x |= x >> 16;
423 return x + 1;
424 }
426 static const char *strip_spaces(const char *str)
427 {
428 while(*str && isspace(*str)) str++;
429 return str;
430 }
432 struct Variable {
433 const char *var;
434 enum { BOOL, NUM } type;
435 bool bool_val;
436 float num_val;
437 };
439 static void con_handle(const char *cmd)
440 {
441 /* set debug values */
442 set_gvar_num("cam-pos-x", cam_pos[0]);
443 set_gvar_num("cam-pos-y", cam_pos[1]);
444 set_gvar_num("cam-pos-z", cam_pos[2]);
446 std::vector<char*> argv;
447 cmd = strip_spaces(cmd);
449 if(!*cmd || *cmd == '#') {
450 return;
451 }
453 char *line = (char*)alloca(strlen(cmd) + 1);
454 strcpy(line, cmd);
456 char *tok = 0;
457 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
458 argv.push_back(tok);
459 }
461 if(strcmp(argv[0], "set") == 0) {
462 if(argv.size() < 3) {
463 con.printf("set must be followed by 2 arguments\n");
464 return;
465 }
466 if(have_gvar(argv[1])) {
467 GameVariable &gv = get_gvar(argv[1]);
468 con.printf("%s->%s (prev: %s)\n", argv[1], argv[2], gv.to_str().c_str());
469 } else {
470 con.printf("%s->%s\n", argv[1], argv[2]);
471 }
472 set_gvar_parse(argv[1], argv[2]);
474 } else if(strcmp(argv[0], "get") == 0) {
475 if(argv.size() < 2) {
476 con.printf("get must be followed by the variable name\n");
477 return;
478 }
479 if(have_gvar(argv[1])) {
480 GameVariable &gv = get_gvar(argv[1]);
481 con.printf("%s: %s\n", argv[1], gv.to_str().c_str());
482 }
484 } else if(strcmp(argv[0], "vars") == 0) {
485 std::list<std::string> vars = get_gvar_list();
486 std::list<std::string>::const_iterator it = vars.begin();
487 while(it != vars.end()) {
488 con.printf(" %s\n", it++->c_str());
489 }
491 } else if(strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
492 exit_game();
494 } else if(strcmp(argv[0], "help") == 0) {
495 con.printf("available commands:\n");
496 con.printf(" set <var> <val> set value to a variable\n");
497 con.printf(" get <var> print the value of a variable\n");
498 con.printf(" vars print a list of all variables\n");
499 con.printf(" help print this help screen\n");
500 con.printf(" exit or quit quit the program\n");
502 } else {
503 con.printf("invalid command: %s\n", argv[0]);
504 }
505 }