vrheights

view src/game.cc @ 10:3e6757655fe2

fixed climbing stairs
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 03 Oct 2014 21:27:58 +0300
parents df3a70664a7d
children 537db3079134
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 cam_vvel = 0;
144 }
145 } else {
146 cam_pos.y = floor_pos.y;
147 cam_vvel = 0;
148 }
149 }
151 void game_display()
152 {
153 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
154 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
156 for(int i=0; i<2; i++) {
157 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
158 vr_begin(i);
160 float proj[16];
161 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
162 glMatrixMode(GL_PROJECTION);
163 glLoadMatrixf(proj);
164 }
166 glMatrixMode(GL_MODELVIEW);
168 float view[16];
169 vr_view_matrix(i, view);
170 glLoadMatrixf(view);
171 glRotatef(cam_phi, 1, 0, 0);
172 glRotatef(cam_theta, 0, 1, 0);
173 /* move the camera to the eye level of the user */
174 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
175 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
177 draw_scene();
178 con.draw();
180 vr_end();
181 }
183 glBindFramebuffer(GL_FRAMEBUFFER, 0);
184 glViewport(0, 0, win_width, win_height);
186 vr_swap_buffers();
187 assert(glGetError() == GL_NO_ERROR);
188 }
190 void game_reshape(int x, int y)
191 {
192 win_width = x;
193 win_height = y;
195 create_rtarg(vr_geti_def(VR_RENDER_XRES, x * 2), vr_geti_def(VR_RENDER_YRES, y));
196 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
198 /* these might be overriden in VR mode (see game_display) */
199 glViewport(0, 0, x, y);
200 glMatrixMode(GL_PROJECTION);
201 glLoadIdentity();
202 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
203 }
205 void game_keyboard(int key, bool pressed)
206 {
207 if(pressed) {
208 if(con.is_visible()) {
209 if(key == 27 || key == '`') {
210 con.hide();
211 } else {
212 con.input_key(key);
213 }
214 } else {
215 switch(key) {
216 case 27:
217 exit_game();
218 break;
220 case 'f':
221 toggle_hmd_fullscr();
222 break;
224 case 'r':
225 vr_recenter();
226 break;
228 case '`':
229 con.show();
230 break;
232 default:
233 break;
234 }
235 }
236 }
238 if(key < 256) {
239 keystate[key] = pressed;
240 }
241 }
244 static int prev_x, prev_y;
245 static bool bnstate[32];
247 void game_mouse_button(int bn, bool state, int x, int y)
248 {
249 bnstate[bn] = state;
250 prev_x = x;
251 prev_y = y;
252 }
254 void game_mouse_motion(int x, int y)
255 {
256 int dx = x - prev_x;
257 int dy = y - prev_y;
258 prev_x = x;
259 prev_y = y;
261 if(!dx && !dy) {
262 return;
263 }
264 if(bnstate[0]) {
265 cam_theta += dx * 0.5;
266 cam_phi += dy * 0.5;
268 if(cam_phi < -90) cam_phi = -90;
269 if(cam_phi > 90) cam_phi = 90;
270 }
271 }
273 MeshFace dbg_walk_face;
275 static void draw_scene()
276 {
277 float lpos[][4] = {
278 {-0.7, 0.7, 1, 0},
279 {1, 0, 1, 0}
280 };
281 float lcol[][4] = {
282 {0.9, 0.7, 0.6, 1},
283 {0.3, 0.4, 0.75, 1}
284 };
285 for(int i=0; i<2; i++) {
286 glEnable(GL_LIGHT0 + i);
287 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
288 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
289 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
290 }
292 glMatrixMode(GL_MODELVIEW);
294 /*
295 glBindTexture(GL_TEXTURE_2D, chess_tex);
296 glEnable(GL_TEXTURE_2D);
297 material(1, 1, 1, 1);
298 glBegin(GL_QUADS);
299 glNormal3f(0, 1, 0);
300 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
301 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
302 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
303 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
304 glEnd();
305 glDisable(GL_TEXTURE_2D);
306 */
308 /*material(1, 1, 1, 0.4);
309 glPushMatrix();
310 glTranslatef(0, 1.3, -10);
311 glFrontFace(GL_CW);
312 bezier_teapot(2.0);
313 glFrontFace(GL_CCW);
314 glPopMatrix();*/
316 for(size_t i=0; i<scn.obj.size(); i++) {
317 scn.obj[i]->draw();
318 }
320 /*glPushAttrib(GL_ENABLE_BIT);
321 glDisable(GL_DEPTH_TEST);
322 glDisable(GL_LIGHTING);
324 glBegin(GL_TRIANGLES);
325 glColor3f(0, 1, 0);
326 glVertex3fv(&dbg_walk_face.v[0].x);
327 glVertex3fv(&dbg_walk_face.v[1].x);
328 glVertex3fv(&dbg_walk_face.v[2].x);
329 glColor3f(1, 1, 1);
330 glEnd();
332 glPopAttrib();*/
333 }
335 static void material(float r, float g, float b, float roughness)
336 {
337 float gloss = 1.0 - roughness;
338 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
339 float specular[] = {gloss, gloss, gloss, 1.0};
340 float shin = gloss * 128.0;
342 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
343 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
344 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
345 }
348 static void toggle_hmd_fullscr()
349 {
350 static bool fullscr;
351 static int prev_x, prev_y;
352 //static int prev_xsz, prev_ysz;
354 fullscr = !fullscr;
355 if(fullscr) {
356 /* entering fullscreen on the HMD */
357 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
358 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
359 if(xoffs != -1) {
360 get_window_pos(&prev_x, &prev_y);
361 move_window(xoffs, yoffs);
362 }
364 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
365 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
366 if(xsz != -1) {
367 //prev_xsz = win_width;
368 //prev_ysz = win_height;
369 resize_window(xsz, ysz);
370 }
371 enter_fullscreen();
373 } else {
374 /* leaving fullscreen */
375 leave_fullscreen();
376 /*move_window(prev_x, prev_y);
377 resize_window(prev_xsz, prev_ysz);*/
378 }
379 }
381 static void create_rtarg(int x, int y)
382 {
383 if(x == fb_xsz && y == fb_ysz) {
384 return; // nothing changed
385 }
387 fb_xsz = x;
388 fb_ysz = y;
389 fb_tex_xsz = next_pow2(fb_xsz);
390 fb_tex_ysz = next_pow2(fb_ysz);
392 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
394 if(!fbo) {
395 glGenFramebuffers(1, &fbo);
397 glGenTextures(1, &fb_tex);
398 glBindTexture(GL_TEXTURE_2D, fb_tex);
399 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
402 glGenRenderbuffers(1, &fb_depth);
403 }
405 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
407 glBindTexture(GL_TEXTURE_2D, fb_tex);
408 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
409 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
411 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
412 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
413 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
415 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
416 glBindFramebuffer(GL_FRAMEBUFFER, 0);
417 }
419 static int next_pow2(int x)
420 {
421 x -= 1;
422 x |= x >> 1;
423 x |= x >> 2;
424 x |= x >> 4;
425 x |= x >> 8;
426 x |= x >> 16;
427 return x + 1;
428 }
430 static const char *strip_spaces(const char *str)
431 {
432 while(*str && isspace(*str)) str++;
433 return str;
434 }
436 struct Variable {
437 const char *var;
438 enum { BOOL, NUM } type;
439 bool bool_val;
440 float num_val;
441 };
443 static void con_handle(const char *cmd)
444 {
445 /* set debug values */
446 set_gvar_num("cam-pos-x", cam_pos[0]);
447 set_gvar_num("cam-pos-y", cam_pos[1]);
448 set_gvar_num("cam-pos-z", cam_pos[2]);
450 std::vector<char*> argv;
451 cmd = strip_spaces(cmd);
453 if(!*cmd || *cmd == '#') {
454 return;
455 }
457 char *line = (char*)alloca(strlen(cmd) + 1);
458 strcpy(line, cmd);
460 char *tok = 0;
461 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
462 argv.push_back(tok);
463 }
465 if(strcmp(argv[0], "set") == 0) {
466 if(argv.size() < 3) {
467 con.printf("set must be followed by 2 arguments\n");
468 return;
469 }
470 if(have_gvar(argv[1])) {
471 GameVariable &gv = get_gvar(argv[1]);
472 con.printf("%s->%s (prev: %s)\n", argv[1], argv[2], gv.to_str().c_str());
473 } else {
474 con.printf("%s->%s\n", argv[1], argv[2]);
475 }
476 set_gvar_parse(argv[1], argv[2]);
478 } else if(strcmp(argv[0], "get") == 0) {
479 if(argv.size() < 2) {
480 con.printf("get must be followed by the variable name\n");
481 return;
482 }
483 if(have_gvar(argv[1])) {
484 GameVariable &gv = get_gvar(argv[1]);
485 con.printf("%s: %s\n", argv[1], gv.to_str().c_str());
486 }
488 } else if(strcmp(argv[0], "vars") == 0) {
489 std::list<std::string> vars = get_gvar_list();
490 std::list<std::string>::const_iterator it = vars.begin();
491 while(it != vars.end()) {
492 con.printf(" %s\n", it++->c_str());
493 }
495 } else if(strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
496 exit_game();
498 } else if(strcmp(argv[0], "help") == 0) {
499 con.printf("available commands:\n");
500 con.printf(" set <var> <val> set value to a variable\n");
501 con.printf(" get <var> print the value of a variable\n");
502 con.printf(" vars print a list of all variables\n");
503 con.printf(" help print this help screen\n");
504 con.printf(" exit or quit quit the program\n");
506 } else {
507 con.printf("invalid command: %s\n", argv[0]);
508 }
509 }