fixed hand tracking world position, and picking up objects in VR
[laserbrain_demo] / src / app.cc
1 #include <stdio.h>
2 #include <limits.h>
3 #include <assert.h>
4 #include <goatvr.h>
5 #include "app.h"
6 #include "opengl.h"
7 #include "sdr.h"
8 #include "texture.h"
9 #include "mesh.h"
10 #include "meshgen.h"
11 #include "scene.h"
12 #include "metascene.h"
13 #include "datamap.h"
14 #include "ui.h"
15 #include "opt.h"
16 #include "post.h"
17 #include "renderer.h"
18 #include "rtarg.h"
19 #include "avatar.h"
20 #include "vrinput.h"
21 #include "exman.h"
22 #include "blob_exhibit.h"
23 #include "dbg_gui.h"
24 #include "geomdraw.h"
25 #include "ui_exhibit.h"
26
27 #define NEAR_CLIP       5.0
28 #define FAR_CLIP        10000.0
29
30 static void draw_scene();
31 static void toggle_flight();
32 static void calc_framerate();
33 static Ray calc_pick_ray(int x, int y);
34
35 long time_msec;
36 int win_width, win_height;
37 int vp_width, vp_height;
38 float win_aspect;
39 bool fb_srgb;
40 bool opt_gear_wireframe;
41
42 TextureSet texman;
43 SceneSet sceneman;
44
45 int fpexcept_enabled;
46
47 unsigned int dbg_key_pending;
48
49 static Avatar avatar;
50
51 static float cam_dist = 0.0;
52 static float floor_y;   // last floor height
53 static float user_eye_height = 165;
54
55 static float walk_speed = 300.0f;
56 static float mouse_speed = 0.5f;
57 static bool show_walk_mesh, noclip = false;
58
59 static bool have_headtracking, have_handtracking, should_swap;
60
61 static int prev_mx, prev_my;
62 static bool bnstate[8];
63 static bool keystate[256];
64 static bool gpad_bnstate[64];
65 static Vec2 joy_move, joy_look;
66 static float joy_deadzone = 0.01;
67
68 static float framerate;
69
70 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
71 static MetaScene *mscn;
72 static unsigned int sdr_post_gamma;
73
74 static long prev_msec;
75
76 static ExhibitManager *exman;
77 static bool show_blobs;
78
79 ExSelection exsel_active, exsel_hover;
80 ExSelection exsel_grab_left, exsel_grab_right;
81 #define exsel_grab_mouse exsel_grab_right
82 static ExhibitSlot exslot_left, exslot_right;
83 #define exslot_mouse exslot_right
84
85 static bool pointing;
86
87 static Renderer *rend;
88 static RenderTarget *goatvr_rtarg;
89
90 static Ray last_pick_ray;
91
92 static bool post_scene_init_pending = true;
93
94
95 bool app_init(int argc, char **argv)
96 {
97         set_log_file("demo.log");
98
99         char *env = getenv("FPEXCEPT");
100         if(env && atoi(env)) {
101                 info_log("enabling floating point exceptions\n");
102                 fpexcept_enabled = 1;
103                 enable_fpexcept();
104         }
105
106         if(init_opengl() == -1) {
107                 return false;
108         }
109
110         if(!init_options(argc, argv, "demo.conf")) {
111                 return false;
112         }
113         app_resize(opt.width, opt.height);
114         app_fullscreen(opt.fullscreen);
115
116         if(opt.vr) {
117                 if(goatvr_init() == -1) {
118                         return false;
119                 }
120                 goatvr_set_origin_mode(GOATVR_HEAD);
121                 goatvr_set_units_scale(100.0f);
122
123                 goatvr_startvr();
124                 should_swap = goatvr_should_swap() != 0;
125                 user_eye_height = goatvr_get_eye_height();
126                 have_headtracking = goatvr_have_headtracking();
127                 have_handtracking = goatvr_have_handtracking();
128
129                 goatvr_recenter();
130
131                 goatvr_rtarg = new RenderTarget;
132         }
133
134         if(fb_srgb) {
135                 int srgb_capable;
136                 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
137                 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
138                 if(srgb_capable) {
139                         glEnable(GL_FRAMEBUFFER_SRGB);
140                 } else {
141                         fb_srgb = 0;
142                 }
143         }
144
145         glEnable(GL_MULTISAMPLE);
146         glEnable(GL_DEPTH_TEST);
147         glEnable(GL_CULL_FACE);
148         glEnable(GL_NORMALIZE);
149
150         if(!init_debug_gui()) {
151                 return false;
152         }
153
154         Mesh::use_custom_sdr_attr = false;
155
156         float ambient[] = {0.0, 0.0, 0.0, 0.0};
157         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
158
159         init_audio();
160
161         if(!init_vrhands()) {
162                 return false;
163         }
164
165         mscn = new MetaScene;
166         if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
167                 return false;
168         }
169
170         avatar.pos = mscn->start_pos;
171         Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
172         dir.y = 0;
173         avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
174
175         exman = new ExhibitManager;
176         // exhibits are loaded in post_scene_init, because they need access to the scene graph
177
178         if(!exui_init()) {
179                 error_log("failed to initialize exhibit ui system\n");
180                 return false;
181         }
182         exui_setnode(&exslot_left.node);
183
184         if(!fb_srgb) {
185                 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
186         }
187
188         rend = new Renderer;
189         if(!rend->init()) {
190                 return false;
191         }
192         if(opt.reflect) {
193                 rend->ropt |= RENDER_MIRRORS;
194         } else {
195                 rend->ropt &= ~RENDER_MIRRORS;
196         }
197         rend->set_scene(mscn);
198
199         glUseProgram(0);
200
201         if(opt.vr || opt.fullscreen) {
202                 app_grab_mouse(true);
203         }
204
205         if(mscn->music && opt.music) {
206                 mscn->music->play(AUDIO_PLAYMODE_LOOP);
207         }
208         return true;
209 }
210
211 // post_scene_init is called after the scene has completed loading
212 static void post_scene_init()
213 {
214         mscn->update(0);        // update once to calculate node matrices
215
216         int num_mir = mscn->calc_mirror_planes();
217         info_log("found %d mirror planes\n", num_mir);
218
219         exman->load(mscn, "data/exhibits");
220 }
221
222 void app_cleanup()
223 {
224         if(mscn->music) {
225                 mscn->music->stop();
226         }
227         destroy_audio();
228
229         app_grab_mouse(false);
230         if(opt.vr) {
231                 delete goatvr_rtarg;
232                 goatvr_shutdown();
233         }
234         destroy_vrhands();
235
236         delete rend;
237
238         exui_shutdown();
239
240         /* this must be destroyed before the scene graph to detach exhibit nodes
241          * before the scene tries to delete them recursively
242          */
243         delete exman;
244
245         texman.clear();
246         sceneman.clear();
247
248         cleanup_debug_gui();
249 }
250
251 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
252 {
253         Mesh *wm = mscn->walk_mesh;
254         if(!wm) {
255                 *newv = v;
256                 return true;
257         }
258
259         Ray downray = Ray(v, Vec3(0, -1, 0));
260         HitPoint hit;
261         if(mscn->walk_mesh->intersect(downray, &hit)) {
262                 *newv = hit.pos;
263                 newv->y += user_eye_height;
264                 return true;
265         }
266         return false;
267 }
268
269 static void update(float dt)
270 {
271         texman.update();
272         sceneman.update();
273
274         if(post_scene_init_pending && !sceneman.pending()) {
275                 post_scene_init_pending = false;
276                 post_scene_init();
277         }
278
279         mscn->update(dt);
280         exman->update(dt);
281         exui_update(dt);
282
283         // use goatvr sticks for joystick input
284         int num_vr_sticks = goatvr_num_sticks();
285         if(num_vr_sticks > 0) {
286                 float p[2];
287                 goatvr_stick_pos(0, p);
288                 joy_move.x = p[0];
289                 joy_move.y = -p[1];
290         }
291         if(num_vr_sticks > 1) {
292                 float p[2];
293                 goatvr_stick_pos(1, p);
294                 joy_look.x = p[0];
295         }
296
297
298         float speed = walk_speed * dt;
299         Vec3 dir;
300
301         // joystick
302         float jdeadsq = joy_deadzone * joy_deadzone;
303         float jmove_lensq = length_sq(joy_move);
304         float jlook_lensq = length_sq(joy_look);
305
306         if(jmove_lensq > jdeadsq) {
307                 float len = sqrt(jmove_lensq);
308                 jmove_lensq -= jdeadsq;
309
310                 float mag = len * len;
311                 dir.x += mag * joy_move.x / len * 2.0 * speed;
312                 dir.z += mag * joy_move.y / len * 2.0 * speed;
313         }
314         if(jlook_lensq > jdeadsq) {
315                 float len = sqrt(jlook_lensq);
316                 jlook_lensq -= jdeadsq;
317
318                 float mag = len * len;
319                 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
320                 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
321                 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
322                 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
323         }
324
325         // keyboard move
326         if(keystate[(int)'w']) {
327                 dir.z -= speed;
328         }
329         if(keystate[(int)'s']) {
330                 dir.z += speed;
331         }
332         if(keystate[(int)'d']) {
333                 dir.x += speed;
334         }
335         if(keystate[(int)'a']) {
336                 dir.x -= speed;
337         }
338         if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
339                 avatar.pos.y += speed;
340         }
341         if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
342                 avatar.pos.y -= speed;
343         }
344
345         float theta = M_PI * avatar.body_rot / 180.0f;
346         Vec3 newpos;
347         newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
348         newpos.y = avatar.pos.y;
349         newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
350
351         if(noclip) {
352                 avatar.pos = newpos;
353         } else {
354                 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
355                         float dtheta = M_PI / 32.0;
356                         float theta = dtheta;
357                         Vec2 dir2d = newpos.xz() - avatar.pos.xz();
358
359                         for(int i=0; i<16; i++) {
360                                 Vec2 dvec = rotate(dir2d, theta);
361                                 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
362                                 if(constrain_walk_mesh(pos, &avatar.pos)) {
363                                         break;
364                                 }
365                                 dvec = rotate(dir2d, -theta);
366                                 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
367                                 if(constrain_walk_mesh(pos, &avatar.pos)) {
368                                         break;
369                                 }
370                                 theta += dtheta;
371                         }
372                 }
373                 floor_y = avatar.pos.y - user_eye_height;
374         }
375
376         if(have_headtracking) {
377                 Quat qhead;
378                 goatvr_head_orientation(&qhead.x);
379                 avatar.tracked_head_rotation(qhead);
380         }
381
382         // TODO move to the avatar system
383         // calculate mouselook view matrix
384         mouse_view_matrix = Mat4::identity;
385         mouse_view_matrix.pre_translate(0, 0, -cam_dist);
386         if(!have_headtracking) {
387                 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
388         }
389         mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
390         mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
391
392         // update hand-tracking
393         if(have_handtracking) {
394                 update_vrhands(&avatar);
395
396                 ExSelection *exsel_grab[] = { &exsel_grab_left, &exsel_grab_right };
397                 ExhibitSlot *exslot[] = { &exslot_left, &exslot_right };
398
399                 for(int i=0; i<2; i++) {
400                         if(vrhand[i].valid) {
401                                 exslot[i]->node.set_position(vrhand[i].pos);
402                                 exslot[i]->node.set_rotation(vrhand[i].rot * exslot[i]->rotation);
403
404                                 bool act_grab = goatvr_action(i, GOATVR_ACTION_GRAB) != 0;
405
406                                 ExSelection sel;
407                                 sel = exman->select(Sphere(vrhand[i].pos, 10));
408
409                                 if(!*exsel_grab[i]) {
410                                         // we don't have an exhibit grabbed
411                                         if(act_grab) {
412                                                 // grab an exhibit
413                                                 *exsel_grab[i] = sel;
414                                                 exslot[i]->rotation = sel.ex->node->get_rotation();
415                                                 debug_log("Grabbing with rot: %f %f %f %f\n", exslot[i]->rotation.x,
416                                                                 exslot[i]->rotation.y, exslot[i]->rotation.z, exslot[i]->rotation.w);
417                                                 exslot[i]->attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
418                                                 if(exsel_active) {
419                                                         exsel_active = ExSelection::null;       // cancel active on grab
420                                                 }
421                                         } else {
422                                                 // just hover
423                                                 exsel_hover = sel;
424                                         }
425                                 } else {
426                                         // we have an exhibit grabbed
427                                         if(!act_grab) {
428                                                 // drop it
429                                                 Exhibit *ex = exsel_grab[i]->ex;
430                                                 exslot[i]->detach_exhibit();
431
432                                                 ExhibitSlot *slot = exman->nearest_empty_slot(vrhand[i].pos, 100);
433                                                 if(!slot) {
434                                                         debug_log("no empty slot nearby\n");
435                                                         if(ex->prev_slot && ex->prev_slot->empty()) {
436                                                                 slot = ex->prev_slot;
437                                                                 debug_log("using previous slot\n");
438                                                         }
439                                                 }
440
441                                                 if(slot) {
442                                                         ex->node->set_rotation(exslot[i]->node.get_rotation());
443                                                         slot->attach_exhibit(ex);
444                                                 } else {
445                                                         // nowhere to put it, stash it for later
446                                                         exman->stash_exhibit(ex);
447                                                         debug_log("no slots available, stashing\n");
448                                                 }
449
450                                                 *exsel_grab[i] = ExSelection::null;
451                                         }
452                                 }
453                         }
454                 }
455
456                 // if there are no grabs, and we're pointing with the right finger, override active
457                 if(!exsel_grab_left && !exsel_grab_right) {
458                         if(goatvr_action(1, GOATVR_ACTION_POINT)) {
459                                 Ray ray;
460                                 ray.origin = vrhand[1].pos;
461                                 ray.dir = rotate(Vec3(0, 0, -1), vrhand[1].rot);
462                                 //exsel_active = exman->select(ray);
463                                 pointing = true;
464                         } else {
465                                 exsel_active = ExSelection::null;
466                                 pointing = false;
467                         }
468                 }
469
470         } else {
471                 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
472                 if(!exsel_grab_mouse) {
473                         Ray ray = calc_pick_ray(prev_mx, prev_my);
474                         exsel_hover = exman->select(ray);
475                 }
476
477                 // TODO do this properly
478                 // set the position of the left hand at a suitable position for the exhibit UI
479                 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
480                 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
481         }
482
483         if(!exslot_right.empty()) exslot_right.node.update(dt);
484         // always update the left slot, because it's the anchor point of the exhibit ui
485         exslot_left.node.update(dt);
486 }
487
488 void app_display()
489 {
490         float dt = (float)(time_msec - prev_msec) / 1000.0f;
491         prev_msec = time_msec;
492
493         if(debug_gui) {
494                 ImGui::GetIOPtr()->DeltaTime = dt;
495                 ImGui::NewFrame();
496
497                 //ImGui::ShowTestWindow();
498         }
499
500         glClearColor(1, 1, 1, 1);
501
502         if(opt.vr) {
503                 // VR mode
504                 goatvr_draw_start();
505                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
506
507                 unsigned int gfbo = goatvr_get_fbo();
508
509                 update(dt);
510
511                 for(int i=0; i<2; i++) {
512                         // for each eye
513                         goatvr_draw_eye(i);
514                         if(gfbo) {
515                                 vp_width = goatvr_get_fb_eye_width(i);
516                                 vp_height = goatvr_get_fb_eye_height(i);
517
518                                 // this is a lightweight operation
519                                 goatvr_rtarg->create_wrap_fbo(gfbo, vp_width, vp_height);
520                                 push_render_target(goatvr_rtarg, RT_FAKE);
521                         } else {
522                                 vp_width = win_width / 2;
523                         }
524
525                         proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
526                         glMatrixMode(GL_PROJECTION);
527                         glLoadMatrixf(proj_matrix[0]);
528
529                         view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
530                         glMatrixMode(GL_MODELVIEW);
531                         glLoadMatrixf(view_matrix[0]);
532
533                         draw_scene();
534                         if(have_handtracking) {
535                                 draw_vrhands();
536                         }
537
538                         if(debug_gui) {
539                                 ImGui::Render();
540                         }
541
542                         if(gfbo) {
543                                 pop_render_target(RT_FAKE);
544                         }
545                 }
546
547                 goatvr_draw_done();
548
549                 vp_width = win_width;
550                 vp_height = win_height;
551
552                 if(should_swap) {
553                         app_swap_buffers();
554                 }
555
556         } else {
557                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
558
559                 update(dt);
560
561                 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
562                 glMatrixMode(GL_PROJECTION);
563                 glLoadMatrixf(proj_matrix[0]);
564
565                 view_matrix = mouse_view_matrix;
566                 glMatrixMode(GL_MODELVIEW);
567                 glLoadMatrixf(view_matrix[0]);
568
569                 draw_scene();
570
571                 if(!fb_srgb && sdr_post_gamma) {
572                         slow_post(sdr_post_gamma);
573                         glUseProgram(0);
574                 }
575
576                 if(debug_gui) {
577                         ImGui::Render();
578                 }
579                 app_swap_buffers();
580         }
581         assert(glGetError() == GL_NO_ERROR);
582
583         calc_framerate();
584 }
585
586
587 static void draw_scene()
588 {
589         rend->draw();
590         exman->draw();
591
592         if(have_handtracking) {
593                 glUseProgram(0);
594                 glPushAttrib(GL_ENABLE_BIT);
595                 glDisable(GL_LIGHTING);
596                 glBegin(GL_LINES);
597                 for(int i=0; i<2; i++) {
598                         if(vrhand[i].valid) {
599                                 glColor3f(i, 1 - i, i);
600                         } else {
601                                 glColor3f(0.5, 0.5, 0.5);
602                         }
603                         Vec3 v = vrhand[i].pos;
604                         Vec3 dir = rotate(Vec3(0, 0, -1), vrhand[i].rot) * 20.0f;
605                         Vec3 up = rotate(Vec3(0, 1, 0), vrhand[i].rot) * 10.0f;
606                         Vec3 right = rotate(Vec3(1, 0, 0), vrhand[i].rot) * 10.0f;
607
608                         if(i == 1 && pointing) {
609                                 dir *= 1000.0f;
610                         }
611
612                         glVertex3f(v.x, v.y, v.z);
613                         glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
614                         glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
615                         glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
616                         glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
617                         glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
618                 }
619                 glEnd();
620                 glPopAttrib();
621         }
622
623         if(debug_gui && dbg_sel_node) {
624                 AABox bvol = dbg_sel_node->get_bounds();
625                 draw_geom_object(&bvol);
626         }
627
628         if(show_walk_mesh && mscn->walk_mesh) {
629                 glPushAttrib(GL_ENABLE_BIT);
630                 glEnable(GL_BLEND);
631                 glBlendFunc(GL_ONE, GL_ONE);
632                 glEnable(GL_POLYGON_OFFSET_FILL);
633
634                 glUseProgram(0);
635
636                 glPolygonOffset(-1, 1);
637                 glDepthMask(0);
638
639                 glColor3f(0.3, 0.08, 0.01);
640                 mscn->walk_mesh->draw();
641
642                 glDepthMask(1);
643
644                 glPopAttrib();
645         }
646
647         exui_draw();
648
649         print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
650         draw_ui();
651 }
652
653
654 void app_reshape(int x, int y)
655 {
656         glViewport(0, 0, x, y);
657         goatvr_set_fb_size(x, y, 1.0f);
658         debug_gui_reshape(x, y);
659
660         vp_width = x;
661         vp_height = y;
662 }
663
664 void app_keyboard(int key, bool pressed)
665 {
666         unsigned int mod = app_get_modifiers();
667
668         if(debug_gui && !(pressed && (key == '`' || key == 27))) {
669                 debug_gui_key(key, pressed, mod);
670                 return; // ignore all keystrokes when GUI is visible
671         }
672
673         if(pressed) {
674                 switch(key) {
675                 case 27:
676                         app_quit();
677                         break;
678
679                 case '\n':
680                 case '\r':
681                         if(mod & MOD_ALT) {
682                                 app_toggle_fullscreen();
683                         }
684                         break;
685
686                 case '`':
687                         debug_gui = !debug_gui;
688                         show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
689                         break;
690
691                 case 'm':
692                         app_toggle_grab_mouse();
693                         show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
694                         break;
695
696                 case 'w':
697                         if(mod & MOD_CTRL) {
698                                 show_walk_mesh = !show_walk_mesh;
699                                 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
700                         }
701                         break;
702
703                 case 'c':
704                         if(mod & MOD_CTRL) {
705                                 noclip = !noclip;
706                                 show_message(noclip ? "no clip" : "clip");
707                         }
708                         break;
709
710                 case 'f':
711                         toggle_flight();
712                         break;
713
714                 case 'p':
715                         if(mod & MOD_CTRL) {
716                                 fb_srgb = !fb_srgb;
717                                 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
718                         }
719                         break;
720
721                 case '=':
722                         walk_speed *= 1.25;
723                         show_message("walk speed: %g", walk_speed);
724                         break;
725
726                 case '-':
727                         walk_speed *= 0.75;
728                         show_message("walk speed: %g", walk_speed);
729                         break;
730
731                 case ']':
732                         mouse_speed *= 1.2;
733                         show_message("mouse speed: %g", mouse_speed);
734                         break;
735
736                 case '[':
737                         mouse_speed *= 0.8;
738                         show_message("mouse speed: %g", mouse_speed);
739                         break;
740
741                 case 'b':
742                         show_blobs = !show_blobs;
743                         show_message("blobs: %s\n", show_blobs ? "on" : "off");
744                         break;
745
746                 case ' ':
747                         goatvr_recenter();
748                         show_message("VR recenter\n");
749                         break;
750
751                 case KEY_UP:
752                         exui_scroll(-1);
753                         break;
754
755                 case KEY_DOWN:
756                         exui_scroll(1);
757                         break;
758
759                 case KEY_LEFT:
760                         exui_change_tab(-1);
761                         break;
762
763                 case KEY_RIGHT:
764                         exui_change_tab(1);
765                         break;
766
767                 case KEY_F5:
768                 case KEY_F6:
769                 case KEY_F7:
770                 case KEY_F8:
771                         dbg_key_pending |= 1 << (key - KEY_F5);
772                         break;
773                 }
774         }
775
776         if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
777                 keystate[key] = pressed;
778         }
779 }
780
781 void app_mouse_button(int bn, bool pressed, int x, int y)
782 {
783         static int press_x, press_y;
784
785         if(debug_gui) {
786                 debug_gui_mbutton(bn, pressed, x, y);
787                 return; // ignore mouse events while GUI is visible
788         }
789
790         prev_mx = x;
791         prev_my = y;
792         bnstate[bn] = pressed;
793
794         if(bn == 0) {
795                 ExSelection sel;
796                 Ray ray = calc_pick_ray(x, y);
797                 sel = exman->select(ray);
798
799                 if(pressed) {
800                         if(sel && (app_get_modifiers() & MOD_CTRL)) {
801                                 exsel_grab_mouse = sel;
802                                 Vec3 pos = sel.ex->node->get_position();
803                                 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
804                                 exslot_mouse.node.set_position(pos);
805                                 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
806                                 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
807                                 if(exsel_active) {
808                                         exsel_active = ExSelection::null;       // cancel active on grab
809                                 }
810                         }
811                         press_x = x;
812                         press_y = y;
813
814                 } else {
815                         if(exsel_grab_mouse) {
816                                 // cancel grab on mouse release
817                                 Exhibit *ex = exsel_grab_mouse.ex;
818                                 Vec3 pos = exslot_mouse.node.get_position();
819
820                                 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
821
822                                 exslot_mouse.detach_exhibit();
823
824                                 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
825                                 if(!slot) {
826                                         debug_log("no empty slot nearby\n");
827                                         if(ex->prev_slot && ex->prev_slot->empty()) {
828                                                 slot = ex->prev_slot;
829                                                 debug_log("using previous slot\n");
830                                         }
831                                 }
832
833                                 if(slot) {
834                                         slot->attach_exhibit(ex);
835                                 } else {
836                                         // nowhere to put it, so stash it for later
837                                         exslot_mouse.detach_exhibit();
838                                         exman->stash_exhibit(ex);
839                                         debug_log("no slots available, stashing\n");
840                                 }
841
842                                 exsel_grab_mouse = ExSelection::null;
843                         }
844
845                         if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
846                                 exsel_active = sel;     // select or deselect active exhibit
847                                 if(sel) {
848                                         debug_log("selecting...\n");
849                                 } else {
850                                         debug_log("deselecting...\n");
851                                 }
852                         }
853
854                         press_x = press_y = INT_MIN;
855                 }
856         }
857 }
858
859 static inline void mouse_look(float dx, float dy)
860 {
861         float scrsz = (float)win_height;
862         avatar.body_rot += dx * 512.0 / scrsz;
863         avatar.head_alt += dy * 512.0 / scrsz;
864
865         if(avatar.head_alt < -90) avatar.head_alt = -90;
866         if(avatar.head_alt > 90) avatar.head_alt = 90;
867 }
868
869 static void mouse_zoom(float dx, float dy)
870 {
871         cam_dist += dy * 0.1;
872         if(cam_dist < 0.0) cam_dist = 0.0;
873 }
874
875 void app_mouse_motion(int x, int y)
876 {
877         if(debug_gui) {
878                 debug_gui_mmotion(x, y);
879                 return; // ignore mouse events while GUI is visible
880         }
881
882         int dx = x - prev_mx;
883         int dy = y - prev_my;
884         prev_mx = x;
885         prev_my = y;
886
887         if(!dx && !dy) return;
888
889         if(exsel_grab_mouse) {
890                 Vec3 pos = exslot_mouse.node.get_node_position();
891                 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
892
893                 exslot_mouse.node.set_position(pos + dir);
894         }
895
896         if(bnstate[2]) {
897                 mouse_look(dx, dy);
898         }
899 }
900
901 void app_mouse_delta(int dx, int dy)
902 {
903         if(bnstate[2]) {
904                 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
905         } else {
906                 mouse_look(dx * mouse_speed, dy * mouse_speed);
907         }
908 }
909
910 void app_mouse_wheel(int dir)
911 {
912         if(debug_gui) {
913                 debug_gui_wheel(dir);
914         }
915 }
916
917 void app_gamepad_axis(int axis, float val)
918 {
919         switch(axis) {
920         case 0:
921                 joy_move.x = val;
922                 break;
923         case 1:
924                 joy_move.y = val;
925                 break;
926
927         case 2:
928                 joy_look.x = val;
929                 break;
930         case 3:
931                 joy_look.y = val;
932                 break;
933         }
934 }
935
936 void app_gamepad_button(int bn, bool pressed)
937 {
938         gpad_bnstate[bn] = pressed;
939
940         if(pressed) {
941                 switch(bn) {
942                 case GPAD_LSTICK:
943                         toggle_flight();
944                         break;
945
946                 case GPAD_X:
947                         show_blobs = !show_blobs;
948                         show_message("blobs: %s\n", show_blobs ? "on" : "off");
949                         break;
950
951                 case GPAD_START:
952                         goatvr_recenter();
953                         show_message("VR recenter\n");
954                         break;
955
956                 default:
957                         break;
958                 }
959         }
960 }
961
962 static void toggle_flight()
963 {
964         static float prev_walk_speed = -1.0;
965         if(prev_walk_speed < 0.0) {
966                 noclip = true;
967                 prev_walk_speed = walk_speed;
968                 walk_speed = 1000.0;
969                 show_message("fly mode\n");
970         } else {
971                 noclip = false;
972                 walk_speed = prev_walk_speed;
973                 prev_walk_speed = -1.0;
974                 show_message("walk mode\n");
975         }
976 }
977
978 static void calc_framerate()
979 {
980         //static int ncalc;
981         static int nframes;
982         static long prev_upd;
983
984         long elapsed = time_msec - prev_upd;
985         if(elapsed >= 1000) {
986                 framerate = (float)nframes / (float)(elapsed * 0.001);
987                 nframes = 1;
988                 prev_upd = time_msec;
989
990                 /*if(++ncalc >= 5) {
991                         printf("fps: %f\n", framerate);
992                         ncalc = 0;
993                 }*/
994         } else {
995                 ++nframes;
996         }
997 }
998
999 static Ray calc_pick_ray(int x, int y)
1000 {
1001         float nx = (float)x / (float)win_width;
1002         float ny = (float)(win_height - y) / (float)win_height;
1003
1004         last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
1005         return last_pick_ray;
1006 }