vrheights

view src/game.cc @ 13:0168104ec568

- added new files to the vs project - fixed bug caused by LibOVR's failure to restore OpenGL state
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 04 Oct 2014 03:41:59 +0300
parents 537db3079134
children
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 /*glBindTexture(GL_TEXTURE_2D, chess_tex);
295 glEnable(GL_TEXTURE_2D);
296 material(1, 1, 1, 1);
297 glBegin(GL_QUADS);
298 glNormal3f(0, 1, 0);
299 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
300 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
301 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
302 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
303 glEnd();
304 glDisable(GL_TEXTURE_2D);
306 material(1, 1, 1, 0.4);
307 glPushMatrix();
308 glTranslatef(0, 1.3, -10);
309 glFrontFace(GL_CW);
310 bezier_teapot(2.0);
311 glFrontFace(GL_CCW);
312 glPopMatrix();*/
314 glBindBuffer(GL_ARRAY_BUFFER, 0);
315 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
317 for(size_t i=0; i<scn.obj.size(); i++) {
318 scn.obj[i]->draw();
319 }
321 /*glPushAttrib(GL_ENABLE_BIT);
322 glDisable(GL_DEPTH_TEST);
323 glDisable(GL_LIGHTING);
325 glBegin(GL_TRIANGLES);
326 glColor3f(0, 1, 0);
327 glVertex3fv(&dbg_walk_face.v[0].x);
328 glVertex3fv(&dbg_walk_face.v[1].x);
329 glVertex3fv(&dbg_walk_face.v[2].x);
330 glColor3f(1, 1, 1);
331 glEnd();
333 glPopAttrib();*/
334 }
336 static void material(float r, float g, float b, float roughness)
337 {
338 float gloss = 1.0 - roughness;
339 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
340 float specular[] = {gloss, gloss, gloss, 1.0};
341 float shin = gloss * 128.0;
343 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
344 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
345 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
346 }
349 static void toggle_hmd_fullscr()
350 {
351 static bool fullscr;
352 static int prev_x, prev_y;
353 //static int prev_xsz, prev_ysz;
355 fullscr = !fullscr;
356 if(fullscr) {
357 /* entering fullscreen on the HMD */
358 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
359 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
360 if(xoffs != -1) {
361 get_window_pos(&prev_x, &prev_y);
362 move_window(xoffs, yoffs);
363 }
365 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
366 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
367 if(xsz != -1) {
368 //prev_xsz = win_width;
369 //prev_ysz = win_height;
370 resize_window(xsz, ysz);
371 }
372 enter_fullscreen();
374 } else {
375 /* leaving fullscreen */
376 leave_fullscreen();
377 /*move_window(prev_x, prev_y);
378 resize_window(prev_xsz, prev_ysz);*/
379 }
380 }
382 static void create_rtarg(int x, int y)
383 {
384 if(x == fb_xsz && y == fb_ysz) {
385 return; // nothing changed
386 }
388 fb_xsz = x;
389 fb_ysz = y;
390 fb_tex_xsz = next_pow2(fb_xsz);
391 fb_tex_ysz = next_pow2(fb_ysz);
393 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
395 if(!fbo) {
396 glGenFramebuffers(1, &fbo);
398 glGenTextures(1, &fb_tex);
399 glBindTexture(GL_TEXTURE_2D, fb_tex);
400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
403 glGenRenderbuffers(1, &fb_depth);
404 }
406 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
408 glBindTexture(GL_TEXTURE_2D, fb_tex);
409 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
410 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
412 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
413 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
414 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
416 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
417 glBindFramebuffer(GL_FRAMEBUFFER, 0);
418 }
420 static int next_pow2(int x)
421 {
422 x -= 1;
423 x |= x >> 1;
424 x |= x >> 2;
425 x |= x >> 4;
426 x |= x >> 8;
427 x |= x >> 16;
428 return x + 1;
429 }
431 static const char *strip_spaces(const char *str)
432 {
433 while(*str && isspace(*str)) str++;
434 return str;
435 }
437 struct Variable {
438 const char *var;
439 enum { BOOL, NUM } type;
440 bool bool_val;
441 float num_val;
442 };
444 static void con_handle(const char *cmd)
445 {
446 /* set debug values */
447 set_gvar_num("cam-pos-x", cam_pos[0]);
448 set_gvar_num("cam-pos-y", cam_pos[1]);
449 set_gvar_num("cam-pos-z", cam_pos[2]);
451 std::vector<char*> argv;
452 cmd = strip_spaces(cmd);
454 if(!*cmd || *cmd == '#') {
455 return;
456 }
458 char *line = (char*)alloca(strlen(cmd) + 1);
459 strcpy(line, cmd);
461 char *tok = 0;
462 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
463 argv.push_back(tok);
464 }
466 if(strcmp(argv[0], "set") == 0) {
467 if(argv.size() < 3) {
468 con.printf("set must be followed by 2 arguments\n");
469 return;
470 }
471 if(have_gvar(argv[1])) {
472 GameVariable &gv = get_gvar(argv[1]);
473 con.printf("%s->%s (prev: %s)\n", argv[1], argv[2], gv.to_str().c_str());
474 } else {
475 con.printf("%s->%s\n", argv[1], argv[2]);
476 }
478 int type = set_gvar_parse(argv[1], argv[2]);
479 if(type == GameVariable::NUMBER) {
480 vr_setf(argv[1], atof(argv[2]));
481 } else if(type == GameVariable::BOOL) {
482 vr_seti(argv[1], get_gvar_bool(argv[1]) ? 1 : 0);
483 }
485 // update state vars
486 cam_pos[0] = get_gvar_num("cam-pos-x");
487 cam_pos[1] = get_gvar_num("cam-pos-y");
488 cam_pos[2] = get_gvar_num("cam-pos-z");
490 } else if(strcmp(argv[0], "get") == 0) {
491 if(argv.size() < 2) {
492 con.printf("get must be followed by the variable name\n");
493 return;
494 }
495 if(have_gvar(argv[1])) {
496 GameVariable &gv = get_gvar(argv[1]);
497 con.printf("%s: %s\n", argv[1], gv.to_str().c_str());
498 }
500 } else if(strcmp(argv[0], "vars") == 0) {
501 std::list<std::string> vars = get_gvar_list();
502 std::list<std::string>::const_iterator it = vars.begin();
503 while(it != vars.end()) {
504 con.printf(" %s\n", it++->c_str());
505 }
507 } else if(strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
508 exit_game();
510 } else if(strcmp(argv[0], "help") == 0) {
511 con.printf("available commands:\n");
512 con.printf(" set <var> <val> set value to a variable\n");
513 con.printf(" get <var> print the value of a variable\n");
514 con.printf(" vars print a list of all variables\n");
515 con.printf(" help print this help screen\n");
516 con.printf(" exit or quit quit the program\n");
518 } else {
519 con.printf("invalid command: %s\n", argv[0]);
520 }
521 }