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