hand-tracking and exhibits part one
[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
353         // update hand-tracking
354         if(have_handtracking) {
355                 update_vrhands(&avatar);
356
357                 if(vrhand[0].valid) {
358                         exslot_left.node.set_position(vrhand[0].pos);
359                         exslot_left.node.set_rotation(vrhand[0].rot);
360
361                         // right hand takes precedence for hover
362                         if(!exsel_grab_left && !exsel_hover) {
363                                 exsel_hover = exman->select(Sphere(vrhand[0].pos, 5));
364                         }
365                 }
366                 if(vrhand[1].valid) {
367                         exslot_right.node.set_position(vrhand[1].pos);
368                         exslot_right.node.set_rotation(vrhand[1].rot);
369
370                         if(!exsel_grab_right) {
371                                 exsel_hover = exman->select(Sphere(vrhand[1].pos, 5));
372                         }
373                 }
374
375         } else {
376                 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
377                 if(!exsel_grab_mouse) {
378                         Ray ray = calc_pick_ray(prev_mx, prev_my);
379                         exsel_hover = exman->select(ray);
380                 }
381
382                 // TODO do this properly
383                 // set the position of the left hand at a suitable position for the exhibit UI
384                 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
385                 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
386         }
387
388         if(!exslot_right.empty()) exslot_right.node.update(dt);
389         // always update the left slot, because it's the anchor point of the exhibit ui
390         exslot_left.node.update(dt);
391 }
392
393 void app_display()
394 {
395         float dt = (float)(time_msec - prev_msec) / 1000.0f;
396         prev_msec = time_msec;
397
398         if(debug_gui) {
399                 ImGui::GetIOPtr()->DeltaTime = dt;
400                 ImGui::NewFrame();
401
402                 ImGui::ShowTestWindow();
403         }
404
405         glClearColor(1, 1, 1, 1);
406
407         if(opt.vr) {
408                 // VR mode
409                 goatvr_draw_start();
410                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
411
412                 update(dt);
413
414                 for(int i=0; i<2; i++) {
415                         // for each eye
416                         goatvr_draw_eye(i);
417
418                         proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
419                         glMatrixMode(GL_PROJECTION);
420                         glLoadMatrixf(proj_matrix[0]);
421
422                         view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
423                         glMatrixMode(GL_MODELVIEW);
424                         glLoadMatrixf(view_matrix[0]);
425
426                         draw_scene();
427                         if(have_handtracking) {
428                                 draw_vrhands();
429                         }
430
431                         if(debug_gui) {
432                                 ImGui::Render();
433                         }
434                 }
435                 goatvr_draw_done();
436
437                 if(should_swap) {
438                         app_swap_buffers();
439                 }
440
441         } else {
442                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
443
444                 update(dt);
445
446                 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
447                 glMatrixMode(GL_PROJECTION);
448                 glLoadMatrixf(proj_matrix[0]);
449
450                 view_matrix = mouse_view_matrix;
451                 glMatrixMode(GL_MODELVIEW);
452                 glLoadMatrixf(view_matrix[0]);
453
454                 draw_scene();
455
456                 if(!fb_srgb && sdr_post_gamma) {
457                         slow_post(sdr_post_gamma);
458                         glUseProgram(0);
459                 }
460
461                 if(debug_gui) {
462                         ImGui::Render();
463                 }
464                 app_swap_buffers();
465         }
466         assert(glGetError() == GL_NO_ERROR);
467
468         calc_framerate();
469 }
470
471
472 static void draw_scene()
473 {
474         rend->draw();
475         exman->draw();
476
477         if(have_handtracking) {
478                 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
479                 Mat4 head_dir_xform = head_xform.upper3x3();
480
481                 glUseProgram(0);
482                 glPushAttrib(GL_ENABLE_BIT);
483                 glDisable(GL_LIGHTING);
484                 glBegin(GL_LINES);
485                 for(int i=0; i<2; i++) {
486                         if(vrhand[i].valid) {
487                                 glColor3f(i, 1 - i, i);
488                         } else {
489                                 glColor3f(0.5, 0.5, 0.5);
490                         }
491                         Vec3 v = head_xform * vrhand[i].pos;
492                         Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), vrhand[i].rot) * 20.0f;
493                         Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), vrhand[i].rot) * 10.0f;
494                         Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), vrhand[i].rot) * 10.0f;
495
496                         glVertex3f(v.x, v.y, v.z);
497                         glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
498                         glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
499                         glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
500                         glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
501                         glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
502                 }
503                 glEnd();
504                 glPopAttrib();
505         }
506
507         if(debug_gui && dbg_sel_node) {
508                 AABox bvol = dbg_sel_node->get_bounds();
509                 draw_geom_object(&bvol);
510         }
511
512         if(show_walk_mesh && mscn->walk_mesh) {
513                 glPushAttrib(GL_ENABLE_BIT);
514                 glEnable(GL_BLEND);
515                 glBlendFunc(GL_ONE, GL_ONE);
516                 glEnable(GL_POLYGON_OFFSET_FILL);
517
518                 glUseProgram(0);
519
520                 glPolygonOffset(-1, 1);
521                 glDepthMask(0);
522
523                 glColor3f(0.3, 0.08, 0.01);
524                 mscn->walk_mesh->draw();
525
526                 glDepthMask(1);
527
528                 glPopAttrib();
529         }
530
531         exui_draw();
532
533         print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
534         draw_ui();
535 }
536
537
538 void app_reshape(int x, int y)
539 {
540         glViewport(0, 0, x, y);
541         goatvr_set_fb_size(x, y, 1.0f);
542         debug_gui_reshape(x, y);
543 }
544
545 void app_keyboard(int key, bool pressed)
546 {
547         unsigned int mod = app_get_modifiers();
548
549         if(debug_gui && !(pressed && (key == '`' || key == 27))) {
550                 debug_gui_key(key, pressed, mod);
551                 return; // ignore all keystrokes when GUI is visible
552         }
553
554         if(pressed) {
555                 switch(key) {
556                 case 27:
557                         app_quit();
558                         break;
559
560                 case '\n':
561                 case '\r':
562                         if(mod & MOD_ALT) {
563                                 app_toggle_fullscreen();
564                         }
565                         break;
566
567                 case '`':
568                         debug_gui = !debug_gui;
569                         show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
570                         break;
571
572                 case 'm':
573                         app_toggle_grab_mouse();
574                         show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
575                         break;
576
577                 case 'w':
578                         if(mod & MOD_CTRL) {
579                                 show_walk_mesh = !show_walk_mesh;
580                                 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
581                         }
582                         break;
583
584                 case 'c':
585                         if(mod & MOD_CTRL) {
586                                 noclip = !noclip;
587                                 show_message(noclip ? "no clip" : "clip");
588                         }
589                         break;
590
591                 case 'f':
592                         toggle_flight();
593                         break;
594
595                 case 'p':
596                         if(mod & MOD_CTRL) {
597                                 fb_srgb = !fb_srgb;
598                                 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
599                         }
600                         break;
601
602                 case '=':
603                         walk_speed *= 1.25;
604                         show_message("walk speed: %g", walk_speed);
605                         break;
606
607                 case '-':
608                         walk_speed *= 0.75;
609                         show_message("walk speed: %g", walk_speed);
610                         break;
611
612                 case ']':
613                         mouse_speed *= 1.2;
614                         show_message("mouse speed: %g", mouse_speed);
615                         break;
616
617                 case '[':
618                         mouse_speed *= 0.8;
619                         show_message("mouse speed: %g", mouse_speed);
620                         break;
621
622                 case 'b':
623                         show_blobs = !show_blobs;
624                         show_message("blobs: %s\n", show_blobs ? "on" : "off");
625                         break;
626
627                 case ' ':
628                         goatvr_recenter();
629                         show_message("VR recenter\n");
630                         break;
631
632                 case 'x':
633                         exman->load(mscn, "data/exhibits");
634                         break;
635
636                 case KEY_UP:
637                         exui_scroll(-1);
638                         break;
639
640                 case KEY_DOWN:
641                         exui_scroll(1);
642                         break;
643
644                 case KEY_LEFT:
645                         exui_change_tab(-1);
646                         break;
647
648                 case KEY_RIGHT:
649                         exui_change_tab(1);
650                         break;
651                 }
652         }
653
654         if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
655                 keystate[key] = pressed;
656         }
657 }
658
659 void app_mouse_button(int bn, bool pressed, int x, int y)
660 {
661         static int press_x, press_y;
662
663         if(debug_gui) {
664                 debug_gui_mbutton(bn, pressed, x, y);
665                 return; // ignore mouse events while GUI is visible
666         }
667
668         prev_mx = x;
669         prev_my = y;
670         bnstate[bn] = pressed;
671
672         if(bn == 0) {
673                 ExSelection sel;
674                 Ray ray = calc_pick_ray(x, y);
675                 sel = exman->select(ray);
676
677                 if(pressed) {
678                         if(sel && (app_get_modifiers() & MOD_CTRL)) {
679                                 exsel_grab_mouse = sel;
680                                 Vec3 pos = sel.ex->node->get_position();
681                                 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
682                                 exslot_mouse.node.set_position(pos);
683                                 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
684                                 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
685                                 if(exsel_active) {
686                                         exsel_active = ExSelection::null;       // cancel active on grab
687                                 }
688                         }
689                         press_x = x;
690                         press_y = y;
691
692                 } else {
693                         if(exsel_grab_mouse) {
694                                 // cancel grab on mouse release
695                                 Exhibit *ex = exsel_grab_mouse.ex;
696                                 Vec3 pos = exslot_mouse.node.get_position();
697
698                                 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
699
700                                 exslot_mouse.detach_exhibit();
701
702                                 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
703                                 if(!slot) {
704                                         debug_log("no empty slot nearby\n");
705                                         if(ex->prev_slot && ex->prev_slot->empty()) {
706                                                 slot = ex->prev_slot;
707                                                 debug_log("using previous slot");
708                                         }
709                                 }
710
711                                 if(slot) {
712                                         slot->attach_exhibit(ex);
713                                 } else {
714                                         // nowhere to put it, so stash it for later
715                                         exslot_mouse.detach_exhibit();
716                                         exman->stash_exhibit(ex);
717                                         debug_log("no slots available, stashing\n");
718                                 }
719
720                                 exsel_grab_mouse = ExSelection::null;
721                         }
722
723                         if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
724                                 exsel_active = sel;     // select or deselect active exhibit
725                                 if(sel) {
726                                         debug_log("selecting...\n");
727                                 } else {
728                                         debug_log("deselecting...\n");
729                                 }
730                         }
731
732                         press_x = press_y = INT_MIN;
733                 }
734         }
735 }
736
737 static inline void mouse_look(float dx, float dy)
738 {
739         float scrsz = (float)win_height;
740         avatar.body_rot += dx * 512.0 / scrsz;
741         avatar.head_alt += dy * 512.0 / scrsz;
742
743         if(avatar.head_alt < -90) avatar.head_alt = -90;
744         if(avatar.head_alt > 90) avatar.head_alt = 90;
745 }
746
747 static void mouse_zoom(float dx, float dy)
748 {
749         cam_dist += dy * 0.1;
750         if(cam_dist < 0.0) cam_dist = 0.0;
751 }
752
753 void app_mouse_motion(int x, int y)
754 {
755         if(debug_gui) {
756                 debug_gui_mmotion(x, y);
757                 return; // ignore mouse events while GUI is visible
758         }
759
760         int dx = x - prev_mx;
761         int dy = y - prev_my;
762         prev_mx = x;
763         prev_my = y;
764
765         if(!dx && !dy) return;
766
767         if(exsel_grab_mouse) {
768                 Vec3 pos = exslot_mouse.node.get_node_position();
769                 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
770
771                 exslot_mouse.node.set_position(pos + dir);
772         }
773
774         if(bnstate[2]) {
775                 mouse_look(dx, dy);
776         }
777 }
778
779 void app_mouse_delta(int dx, int dy)
780 {
781         if(bnstate[2]) {
782                 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
783         } else {
784                 mouse_look(dx * mouse_speed, dy * mouse_speed);
785         }
786 }
787
788 void app_mouse_wheel(int dir)
789 {
790         if(debug_gui) {
791                 debug_gui_wheel(dir);
792         }
793 }
794
795 void app_gamepad_axis(int axis, float val)
796 {
797         switch(axis) {
798         case 0:
799                 joy_move.x = val;
800                 break;
801         case 1:
802                 joy_move.y = val;
803                 break;
804
805         case 2:
806                 joy_look.x = val;
807                 break;
808         case 3:
809                 joy_look.y = val;
810                 break;
811         }
812 }
813
814 void app_gamepad_button(int bn, bool pressed)
815 {
816         gpad_bnstate[bn] = pressed;
817
818         if(pressed) {
819                 switch(bn) {
820                 case GPAD_LSTICK:
821                         toggle_flight();
822                         break;
823
824                 case GPAD_X:
825                         show_blobs = !show_blobs;
826                         show_message("blobs: %s\n", show_blobs ? "on" : "off");
827                         break;
828
829                 case GPAD_START:
830                         goatvr_recenter();
831                         show_message("VR recenter\n");
832                         break;
833
834                 default:
835                         break;
836                 }
837         }
838 }
839
840 static void toggle_flight()
841 {
842         static float prev_walk_speed = -1.0;
843         if(prev_walk_speed < 0.0) {
844                 noclip = true;
845                 prev_walk_speed = walk_speed;
846                 walk_speed = 1000.0;
847                 show_message("fly mode\n");
848         } else {
849                 noclip = false;
850                 walk_speed = prev_walk_speed;
851                 prev_walk_speed = -1.0;
852                 show_message("walk mode\n");
853         }
854 }
855
856 static void calc_framerate()
857 {
858         //static int ncalc;
859         static int nframes;
860         static long prev_upd;
861
862         long elapsed = time_msec - prev_upd;
863         if(elapsed >= 1000) {
864                 framerate = (float)nframes / (float)(elapsed * 0.001);
865                 nframes = 1;
866                 prev_upd = time_msec;
867
868                 /*if(++ncalc >= 5) {
869                         printf("fps: %f\n", framerate);
870                         ncalc = 0;
871                 }*/
872         } else {
873                 ++nframes;
874         }
875 }
876
877 static Ray calc_pick_ray(int x, int y)
878 {
879         float nx = (float)x / (float)win_width;
880         float ny = (float)(win_height - y) / (float)win_height;
881
882         last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
883         return last_pick_ray;
884 }