new render target class while working on the 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         glClearColor(1, 1, 1, 1);
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 void app_cleanup()
212 {
213         if(mscn->music) {
214                 mscn->music->stop();
215         }
216         destroy_audio();
217
218         app_grab_mouse(false);
219         if(opt.vr) {
220                 goatvr_shutdown();
221         }
222         destroy_vrhands();
223
224         delete rend;
225
226         exui_shutdown();
227
228         /* this must be destroyed before the scene graph to detach exhibit nodes
229          * before the scene tries to delete them recursively
230          */
231         delete exman;
232
233         texman.clear();
234         sceneman.clear();
235
236         cleanup_debug_gui();
237 }
238
239 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
240 {
241         Mesh *wm = mscn->walk_mesh;
242         if(!wm) {
243                 *newv = v;
244                 return true;
245         }
246
247         Ray downray = Ray(v, Vec3(0, -1, 0));
248         HitPoint hit;
249         if(mscn->walk_mesh->intersect(downray, &hit)) {
250                 *newv = hit.pos;
251                 newv->y += user_eye_height;
252                 return true;
253         }
254         return false;
255 }
256
257 static void update(float dt)
258 {
259         texman.update();
260         sceneman.update();
261
262         mscn->update(dt);
263         exman->update(dt);
264         exui_update(dt);
265
266         float speed = walk_speed * dt;
267         Vec3 dir;
268
269         // joystick
270         float jdeadsq = joy_deadzone * joy_deadzone;
271         float jmove_lensq = length_sq(joy_move);
272         float jlook_lensq = length_sq(joy_look);
273
274         if(jmove_lensq > jdeadsq) {
275                 float len = sqrt(jmove_lensq);
276                 jmove_lensq -= jdeadsq;
277
278                 float mag = len * len;
279                 dir.x += mag * joy_move.x / len * 2.0 * speed;
280                 dir.z += mag * joy_move.y / len * 2.0 * speed;
281         }
282         if(jlook_lensq > jdeadsq) {
283                 float len = sqrt(jlook_lensq);
284                 jlook_lensq -= jdeadsq;
285
286                 float mag = len * len;
287                 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
288                 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
289                 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
290                 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
291         }
292
293         // keyboard move
294         if(keystate[(int)'w']) {
295                 dir.z -= speed;
296         }
297         if(keystate[(int)'s']) {
298                 dir.z += speed;
299         }
300         if(keystate[(int)'d']) {
301                 dir.x += speed;
302         }
303         if(keystate[(int)'a']) {
304                 dir.x -= speed;
305         }
306         if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
307                 avatar.pos.y += speed;
308         }
309         if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
310                 avatar.pos.y -= speed;
311         }
312
313         float theta = M_PI * avatar.body_rot / 180.0f;
314         Vec3 newpos;
315         newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
316         newpos.y = avatar.pos.y;
317         newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
318
319         if(noclip) {
320                 avatar.pos = newpos;
321         } else {
322                 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
323                         float dtheta = M_PI / 32.0;
324                         float theta = dtheta;
325                         Vec2 dir2d = newpos.xz() - avatar.pos.xz();
326
327                         for(int i=0; i<16; i++) {
328                                 Vec2 dvec = rotate(dir2d, theta);
329                                 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
330                                 if(constrain_walk_mesh(pos, &avatar.pos)) {
331                                         break;
332                                 }
333                                 dvec = rotate(dir2d, -theta);
334                                 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
335                                 if(constrain_walk_mesh(pos, &avatar.pos)) {
336                                         break;
337                                 }
338                                 theta += dtheta;
339                         }
340                 }
341                 floor_y = avatar.pos.y - user_eye_height;
342         }
343
344         // TODO move to the avatar system
345         // calculate mouselook view matrix
346         mouse_view_matrix = Mat4::identity;
347         mouse_view_matrix.pre_translate(0, 0, -cam_dist);
348         if(!have_headtracking) {
349                 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
350         }
351         mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
352         mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
353
354         // check if an exhibit is hovered-over by mouse or 6dof (only if we don't have one grabbed)
355         if(!exsel_grab_mouse) {
356                 // XXX note: using previous view/proj matrix lattency shouldn't be an issue but
357                 //           make sure state-creep doesn't get us
358                 // XXX also this mouse-picking probably should only be active in non-VR mode
359                 Ray ray = calc_pick_ray(prev_mx, prev_my);
360                 exsel_hover = exman->select(ray);
361         }
362
363         // update hand-tracking
364         if(have_handtracking) {
365                 update_vrhands(&avatar);
366         } else {
367                 // set the position of the left hand at a suitable position for the exhibit UI
368                 Vec3 dir = transpose(mouse_view_matrix.upper3x3()) * Vec3(0, 0, -1);
369                 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
370         }
371
372         if(!exslot_right.empty()) exslot_right.node.update(dt);
373         // always update the left slot, because it's the anchor point of the exhibit ui
374         exslot_left.node.update(dt);
375 }
376
377 void app_display()
378 {
379         float dt = (float)(time_msec - prev_msec) / 1000.0f;
380         prev_msec = time_msec;
381
382         if(debug_gui) {
383                 ImGui::GetIOPtr()->DeltaTime = dt;
384                 ImGui::NewFrame();
385
386                 ImGui::ShowTestWindow();
387         }
388
389         if(opt.vr) {
390                 // VR mode
391                 goatvr_draw_start();
392                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
393
394                 update(dt);
395
396                 for(int i=0; i<2; i++) {
397                         // for each eye
398                         goatvr_draw_eye(i);
399
400                         proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
401                         glMatrixMode(GL_PROJECTION);
402                         glLoadMatrixf(proj_matrix[0]);
403
404                         view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
405                         glMatrixMode(GL_MODELVIEW);
406                         glLoadMatrixf(view_matrix[0]);
407
408                         draw_scene();
409                         if(have_handtracking) {
410                                 draw_vrhands();
411                         }
412
413                         if(debug_gui) {
414                                 ImGui::Render();
415                         }
416                 }
417                 goatvr_draw_done();
418
419                 if(should_swap) {
420                         app_swap_buffers();
421                 }
422
423         } else {
424                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
425
426                 update(dt);
427
428                 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
429                 glMatrixMode(GL_PROJECTION);
430                 glLoadMatrixf(proj_matrix[0]);
431
432                 view_matrix = mouse_view_matrix;
433                 glMatrixMode(GL_MODELVIEW);
434                 glLoadMatrixf(view_matrix[0]);
435
436                 draw_scene();
437
438                 if(!fb_srgb && sdr_post_gamma) {
439                         slow_post(sdr_post_gamma);
440                         glUseProgram(0);
441                 }
442
443                 if(debug_gui) {
444                         ImGui::Render();
445                 }
446                 app_swap_buffers();
447         }
448         assert(glGetError() == GL_NO_ERROR);
449
450         calc_framerate();
451 }
452
453
454 static void draw_scene()
455 {
456         rend->draw();
457         exman->draw();
458
459         /*
460         if(have_handtracking) {
461                 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
462                 Mat4 head_dir_xform = head_xform.upper3x3();
463
464                 glUseProgram(0);
465                 glPushAttrib(GL_ENABLE_BIT);
466                 glDisable(GL_LIGHTING);
467                 glBegin(GL_LINES);
468                 for(int i=0; i<2; i++) {
469                         if(hand[i].valid) {
470                                 glColor3f(i, 1 - i, i);
471                         } else {
472                                 glColor3f(0.5, 0.5, 0.5);
473                         }
474                         Vec3 v = head_xform * hand[i].pos;
475                         Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), hand[i].rot) * 20.0f;
476                         Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), hand[i].rot) * 10.0f;
477                         Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), hand[i].rot) * 10.0f;
478
479                         glVertex3f(v.x, v.y, v.z);
480                         glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
481                         glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
482                         glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
483                         glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
484                         glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
485                 }
486                 glEnd();
487                 glPopAttrib();
488         }
489         */
490
491         if(debug_gui && dbg_sel_node) {
492                 AABox bvol = dbg_sel_node->get_bounds();
493                 draw_geom_object(&bvol);
494         }
495
496         if(show_walk_mesh && mscn->walk_mesh) {
497                 glPushAttrib(GL_ENABLE_BIT);
498                 glEnable(GL_BLEND);
499                 glBlendFunc(GL_ONE, GL_ONE);
500                 glEnable(GL_POLYGON_OFFSET_FILL);
501
502                 glUseProgram(0);
503
504                 glPolygonOffset(-1, 1);
505                 glDepthMask(0);
506
507                 glColor3f(0.3, 0.08, 0.01);
508                 mscn->walk_mesh->draw();
509
510                 glDepthMask(1);
511
512                 glPopAttrib();
513         }
514
515         exui_draw();
516
517         print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
518         draw_ui();
519 }
520
521
522 void app_reshape(int x, int y)
523 {
524         glViewport(0, 0, x, y);
525         goatvr_set_fb_size(x, y, 1.0f);
526         debug_gui_reshape(x, y);
527 }
528
529 void app_keyboard(int key, bool pressed)
530 {
531         unsigned int mod = app_get_modifiers();
532
533         if(debug_gui && !(pressed && (key == '`' || key == 27))) {
534                 debug_gui_key(key, pressed, mod);
535                 return; // ignore all keystrokes when GUI is visible
536         }
537
538         if(pressed) {
539                 switch(key) {
540                 case 27:
541                         app_quit();
542                         break;
543
544                 case '\n':
545                 case '\r':
546                         if(mod & MOD_ALT) {
547                                 app_toggle_fullscreen();
548                         }
549                         break;
550
551                 case '`':
552                         debug_gui = !debug_gui;
553                         show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
554                         break;
555
556                 case 'm':
557                         app_toggle_grab_mouse();
558                         show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
559                         break;
560
561                 case 'w':
562                         if(mod & MOD_CTRL) {
563                                 show_walk_mesh = !show_walk_mesh;
564                                 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
565                         }
566                         break;
567
568                 case 'c':
569                         if(mod & MOD_CTRL) {
570                                 noclip = !noclip;
571                                 show_message(noclip ? "no clip" : "clip");
572                         }
573                         break;
574
575                 case 'f':
576                         toggle_flight();
577                         break;
578
579                 case 'p':
580                         if(mod & MOD_CTRL) {
581                                 fb_srgb = !fb_srgb;
582                                 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
583                         }
584                         break;
585
586                 case '=':
587                         walk_speed *= 1.25;
588                         show_message("walk speed: %g", walk_speed);
589                         break;
590
591                 case '-':
592                         walk_speed *= 0.75;
593                         show_message("walk speed: %g", walk_speed);
594                         break;
595
596                 case ']':
597                         mouse_speed *= 1.2;
598                         show_message("mouse speed: %g", mouse_speed);
599                         break;
600
601                 case '[':
602                         mouse_speed *= 0.8;
603                         show_message("mouse speed: %g", mouse_speed);
604                         break;
605
606                 case 'b':
607                         show_blobs = !show_blobs;
608                         show_message("blobs: %s\n", show_blobs ? "on" : "off");
609                         break;
610
611                 case ' ':
612                         goatvr_recenter();
613                         show_message("VR recenter\n");
614                         break;
615
616                 case 'x':
617                         exman->load(mscn, "data/exhibits");
618                         break;
619                 }
620         }
621
622         if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
623                 keystate[key] = pressed;
624         }
625 }
626
627 void app_mouse_button(int bn, bool pressed, int x, int y)
628 {
629         static int press_x, press_y;
630
631         if(debug_gui) {
632                 debug_gui_mbutton(bn, pressed, x, y);
633                 return; // ignore mouse events while GUI is visible
634         }
635
636         prev_mx = x;
637         prev_my = y;
638         bnstate[bn] = pressed;
639
640         if(bn == 0) {
641                 ExSelection sel;
642                 Ray ray = calc_pick_ray(x, y);
643                 sel = exman->select(ray);
644
645                 if(pressed) {
646                         if(sel && (app_get_modifiers() & MOD_CTRL)) {
647                                 exsel_grab_mouse = sel;
648                                 Vec3 pos = sel.ex->node->get_position();
649                                 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
650                                 exslot_mouse.node.set_position(pos);
651                                 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
652                                 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
653                                 if(exsel_active) {
654                                         exsel_active = ExSelection::null;       // cancel active on grab
655                                 }
656                         }
657                         press_x = x;
658                         press_y = y;
659
660                 } else {
661                         if(exsel_grab_mouse) {
662                                 // cancel grab on mouse release
663                                 Exhibit *ex = exsel_grab_mouse.ex;
664                                 Vec3 pos = exslot_mouse.node.get_position();
665
666                                 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
667
668                                 exslot_mouse.detach_exhibit();
669
670                                 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
671                                 if(!slot) {
672                                         debug_log("no empty slot nearby\n");
673                                         if(ex->prev_slot && ex->prev_slot->empty()) {
674                                                 slot = ex->prev_slot;
675                                                 debug_log("using previous slot");
676                                         }
677                                 }
678
679                                 if(slot) {
680                                         slot->attach_exhibit(ex);
681                                 } else {
682                                         // nowhere to put it, so stash it for later
683                                         exslot_mouse.detach_exhibit();
684                                         exman->stash_exhibit(ex);
685                                         debug_log("no slots available, stashing\n");
686                                 }
687
688                                 exsel_grab_mouse = ExSelection::null;
689                         }
690
691                         if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
692                                 exsel_active = sel;     // select or deselect active exhibit
693                                 if(sel) {
694                                         debug_log("selecting...\n");
695                                 } else {
696                                         debug_log("deselecting...\n");
697                                 }
698                         }
699
700                         press_x = press_y = INT_MIN;
701                 }
702         }
703 }
704
705 static inline void mouse_look(float dx, float dy)
706 {
707         float scrsz = (float)win_height;
708         avatar.body_rot += dx * 512.0 / scrsz;
709         avatar.head_alt += dy * 512.0 / scrsz;
710
711         if(avatar.head_alt < -90) avatar.head_alt = -90;
712         if(avatar.head_alt > 90) avatar.head_alt = 90;
713 }
714
715 static void mouse_zoom(float dx, float dy)
716 {
717         cam_dist += dy * 0.1;
718         if(cam_dist < 0.0) cam_dist = 0.0;
719 }
720
721 void app_mouse_motion(int x, int y)
722 {
723         if(debug_gui) {
724                 debug_gui_mmotion(x, y);
725                 return; // ignore mouse events while GUI is visible
726         }
727
728         int dx = x - prev_mx;
729         int dy = y - prev_my;
730         prev_mx = x;
731         prev_my = y;
732
733         if(!dx && !dy) return;
734
735         if(exsel_grab_mouse) {
736                 Vec3 pos = exslot_mouse.node.get_node_position();
737                 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
738
739                 exslot_mouse.node.set_position(pos + dir);
740         }
741
742         if(bnstate[2]) {
743                 mouse_look(dx, dy);
744         }
745 }
746
747 void app_mouse_delta(int dx, int dy)
748 {
749         if(bnstate[2]) {
750                 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
751         } else {
752                 mouse_look(dx * mouse_speed, dy * mouse_speed);
753         }
754 }
755
756 void app_mouse_wheel(int dir)
757 {
758         if(debug_gui) {
759                 debug_gui_wheel(dir);
760         }
761 }
762
763 void app_gamepad_axis(int axis, float val)
764 {
765         switch(axis) {
766         case 0:
767                 joy_move.x = val;
768                 break;
769         case 1:
770                 joy_move.y = val;
771                 break;
772
773         case 2:
774                 joy_look.x = val;
775                 break;
776         case 3:
777                 joy_look.y = val;
778                 break;
779         }
780 }
781
782 void app_gamepad_button(int bn, bool pressed)
783 {
784         gpad_bnstate[bn] = pressed;
785
786         if(pressed) {
787                 switch(bn) {
788                 case GPAD_LSTICK:
789                         toggle_flight();
790                         break;
791
792                 case GPAD_X:
793                         show_blobs = !show_blobs;
794                         show_message("blobs: %s\n", show_blobs ? "on" : "off");
795                         break;
796
797                 case GPAD_START:
798                         goatvr_recenter();
799                         show_message("VR recenter\n");
800                         break;
801
802                 default:
803                         break;
804                 }
805         }
806 }
807
808 static void toggle_flight()
809 {
810         static float prev_walk_speed = -1.0;
811         if(prev_walk_speed < 0.0) {
812                 noclip = true;
813                 prev_walk_speed = walk_speed;
814                 walk_speed = 1000.0;
815                 show_message("fly mode\n");
816         } else {
817                 noclip = false;
818                 walk_speed = prev_walk_speed;
819                 prev_walk_speed = -1.0;
820                 show_message("walk mode\n");
821         }
822 }
823
824 static void calc_framerate()
825 {
826         //static int ncalc;
827         static int nframes;
828         static long prev_upd;
829
830         long elapsed = time_msec - prev_upd;
831         if(elapsed >= 1000) {
832                 framerate = (float)nframes / (float)(elapsed * 0.001);
833                 nframes = 1;
834                 prev_upd = time_msec;
835
836                 /*if(++ncalc >= 5) {
837                         printf("fps: %f\n", framerate);
838                         ncalc = 0;
839                 }*/
840         } else {
841                 ++nframes;
842         }
843 }
844
845 static Ray calc_pick_ray(int x, int y)
846 {
847         float nx = (float)x / (float)win_width;
848         float ny = (float)(win_height - y) / (float)win_height;
849
850         last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
851         return last_pick_ray;
852 }