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