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