13 #include "metascene.h"
23 #include "blob_exhibit.h"
26 #include "ui_exhibit.h"
29 #define FAR_CLIP 10000.0
31 static void draw_scene();
32 static void toggle_flight();
33 static void calc_framerate();
34 static Ray calc_pick_ray(int x, int y);
37 int win_width, win_height;
38 int vp_width, vp_height;
41 bool opt_gear_wireframe;
48 unsigned int dbg_key_pending;
52 static float cam_dist = 0.0;
53 static float floor_y; // last floor height
54 static float user_eye_height = 165;
56 static float walk_speed = 300.0f;
57 static float mouse_speed = 0.5f;
58 static bool show_walk_mesh, noclip = false;
60 static bool have_headtracking, have_handtracking, should_swap;
62 static int prev_mx, prev_my;
63 static bool bnstate[8];
64 static bool keystate[256];
65 static bool gpad_bnstate[64];
66 static Vec2 joy_move, joy_look;
67 static float joy_deadzone = 0.01;
69 static float framerate;
71 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
72 static MetaScene *mscn;
73 static unsigned int sdr_post_gamma;
75 static long prev_msec;
77 static ExhibitManager *exman;
78 static bool show_blobs;
80 ExSelection exsel_active, exsel_hover;
81 ExSelection exsel_grab_left, exsel_grab_right;
82 #define exsel_grab_mouse exsel_grab_right
83 static ExhibitSlot exslot_left, exslot_right;
84 #define exslot_mouse exslot_right
88 static Renderer *rend;
89 static RenderTarget *goatvr_rtarg;
91 static Ray last_pick_ray;
93 static bool post_scene_init_pending = true;
96 bool app_init(int argc, char **argv)
98 set_log_file("demo.log");
100 char *env = getenv("FPEXCEPT");
101 if(env && atoi(env)) {
102 info_log("enabling floating point exceptions\n");
103 fpexcept_enabled = 1;
107 if(init_opengl() == -1) {
111 if(!init_options(argc, argv, "demo.conf")) {
114 app_resize(opt.width, opt.height);
115 app_fullscreen(opt.fullscreen);
118 info_log("Adding URL asset source: %s\n", opt.data_url);
119 ass_add_url("data", opt.data_url);
123 if(goatvr_init() == -1) {
126 goatvr_set_origin_mode(GOATVR_HEAD);
127 goatvr_set_units_scale(100.0f);
130 should_swap = goatvr_should_swap() != 0;
131 user_eye_height = goatvr_get_eye_height();
132 have_headtracking = goatvr_have_headtracking();
133 have_handtracking = goatvr_have_handtracking();
137 goatvr_rtarg = new RenderTarget;
142 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
143 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
145 glEnable(GL_FRAMEBUFFER_SRGB);
151 glEnable(GL_MULTISAMPLE);
152 glEnable(GL_DEPTH_TEST);
153 glEnable(GL_CULL_FACE);
154 glEnable(GL_NORMALIZE);
156 if(!init_debug_gui()) {
160 Mesh::use_custom_sdr_attr = false;
162 float ambient[] = {0.0, 0.0, 0.0, 0.0};
163 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
167 if(!init_vrhands()) {
171 mscn = new MetaScene;
172 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
176 avatar.pos = mscn->start_pos;
177 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
179 avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
181 exman = new ExhibitManager;
182 // exhibits are loaded in post_scene_init, because they need access to the scene graph
185 error_log("failed to initialize exhibit ui system\n");
188 exui_setnode(&exslot_left.node);
189 if(have_handtracking) {
191 exui_rotation(Vec3(-deg_to_rad(35), 0, 0));
195 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
203 rend->ropt |= RENDER_MIRRORS;
205 rend->ropt &= ~RENDER_MIRRORS;
207 rend->set_scene(mscn);
211 if(opt.vr || opt.fullscreen) {
212 app_grab_mouse(true);
215 if(mscn->music && opt.music) {
216 mscn->music->play(AUDIO_PLAYMODE_LOOP);
221 // post_scene_init is called after the scene has completed loading
222 static void post_scene_init()
224 mscn->update(0); // update once to calculate node matrices
226 int num_mir = mscn->calc_mirror_planes();
227 info_log("found %d mirror planes\n", num_mir);
229 exman->load(mscn, "data/exhibits");
239 app_grab_mouse(false);
250 /* this must be destroyed before the scene graph to detach exhibit nodes
251 * before the scene tries to delete them recursively
261 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
263 Mesh *wm = mscn->walk_mesh;
269 Ray downray = Ray(v, Vec3(0, -1, 0));
271 if(mscn->walk_mesh->intersect(downray, &hit)) {
273 newv->y += user_eye_height;
279 static void update(float dt)
284 if(post_scene_init_pending && !sceneman.pending()) {
285 post_scene_init_pending = false;
292 // use goatvr sticks for joystick input
293 int num_vr_sticks = goatvr_num_sticks();
294 if(num_vr_sticks > 0) {
296 goatvr_stick_pos(0, p);
300 if(num_vr_sticks > 1) {
302 goatvr_stick_pos(1, p);
307 float speed = walk_speed * dt;
311 float jdeadsq = joy_deadzone * joy_deadzone;
312 float jmove_lensq = length_sq(joy_move);
313 float jlook_lensq = length_sq(joy_look);
315 if(jmove_lensq > jdeadsq) {
316 float len = sqrt(jmove_lensq);
317 jmove_lensq -= jdeadsq;
319 float mag = len * len;
320 dir.x += mag * joy_move.x / len * speed;
321 dir.z += mag * joy_move.y / len * speed;
323 if(jlook_lensq > jdeadsq) {
324 float len = sqrt(jlook_lensq);
325 jlook_lensq -= jdeadsq;
327 float mag = len * len;
328 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
329 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
330 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
331 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
335 if(keystate[(int)'w']) {
338 if(keystate[(int)'s']) {
341 if(keystate[(int)'d']) {
344 if(keystate[(int)'a']) {
347 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
348 avatar.pos.y += speed;
350 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
351 avatar.pos.y -= speed;
354 Vec3 walk_dir = avatar.calc_walk_dir(dir.z, dir.x);
355 Vec3 newpos = avatar.pos + walk_dir;
360 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
361 float dtheta = M_PI / 32.0;
362 float theta = dtheta;
363 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
365 for(int i=0; i<16; i++) {
366 Vec2 dvec = rotate(dir2d, theta);
367 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
368 if(constrain_walk_mesh(pos, &avatar.pos)) {
371 dvec = rotate(dir2d, -theta);
372 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
373 if(constrain_walk_mesh(pos, &avatar.pos)) {
379 floor_y = avatar.pos.y - user_eye_height;
382 if(have_headtracking) {
384 goatvr_head_orientation(&qhead.x);
385 avatar.tracked_head_rotation(qhead);
388 // TODO move to the avatar system
389 // calculate mouselook view matrix
390 mouse_view_matrix = Mat4::identity;
391 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
392 if(!have_headtracking) {
393 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
395 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
396 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
398 // update hand-tracking
399 if(have_handtracking) {
400 update_vrhands(&avatar);
402 ExSelection *exsel_grab[] = { &exsel_grab_left, &exsel_grab_right };
403 ExhibitSlot *exslot[] = { &exslot_left, &exslot_right };
405 for(int i=0; i<2; i++) {
406 if(vrhand[i].valid) {
407 exslot[i]->node.set_position(vrhand[i].pos);
408 exslot[i]->node.set_rotation(vrhand[i].rot * exslot[i]->grab_rot);
410 bool act_grab = goatvr_action(i, GOATVR_ACTION_GRAB) != 0;
413 sel = exman->select(Sphere(vrhand[i].pos, 10));
415 if(!*exsel_grab[i]) {
416 // we don't have an exhibit grabbed
419 *exsel_grab[i] = sel;
420 //SceneNode *objnode = sel.ex->node->find_object_node();
421 //exslot[i]->rotation = normalize(sel.ex->node->get_rotation());
422 exslot[i]->grab_rot = inverse(vrhand[i].rot);
423 exslot[i]->attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
425 exsel_active = ExSelection::null; // cancel active on grab
432 // we have an exhibit grabbed
435 Exhibit *ex = exsel_grab[i]->ex;
436 exslot[i]->detach_exhibit();
438 ExhibitSlot *slot = exman->nearest_empty_slot(vrhand[i].pos, 100);
440 debug_log("no empty slot nearby\n");
441 if(ex->prev_slot && ex->prev_slot->empty()) {
442 slot = ex->prev_slot;
443 debug_log("using previous slot\n");
448 Quat rot = normalize(exslot[i]->node.get_rotation());
449 ex->node->set_rotation(rot);
450 slot->attach_exhibit(ex);
452 // nowhere to put it, stash it for later
453 exman->stash_exhibit(ex);
454 debug_log("no slots available, stashing\n");
457 *exsel_grab[i] = ExSelection::null;
458 exslot[i]->grab_rot = Quat::identity;
464 // if there are no grabs, and we're pointing with the right finger, override active
465 if(!exsel_grab_left && !exsel_grab_right) {
466 if(goatvr_action(1, GOATVR_ACTION_POINT)) {
468 ray.origin = vrhand[1].pos;
469 ray.dir = rotate(Vec3(0, 0, -1), vrhand[1].rot);
470 exsel_active = exman->select(ray);
473 exsel_active = ExSelection::null;
479 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
480 if(!exsel_grab_mouse) {
481 Ray ray = calc_pick_ray(prev_mx, prev_my);
482 exsel_hover = exman->select(ray);
485 // TODO do this properly
486 // set the position of the left hand at a suitable position for the exhibit UI
487 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
488 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
490 rot.set_rotation(Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
491 exslot_left.node.set_rotation(rot);
494 if(!exslot_right.empty()) exslot_right.node.update(dt);
495 // always update the left slot, because it's the anchor point of the exhibit ui
496 exslot_left.node.update(dt);
498 // need to call this *after* we have updated the active exhibit (if any)
504 float dt = (float)(time_msec - prev_msec) / 1000.0f;
505 prev_msec = time_msec;
508 ImGui::GetIOPtr()->DeltaTime = dt;
511 //ImGui::ShowTestWindow();
514 glClearColor(1, 1, 1, 1);
519 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
521 unsigned int gfbo = goatvr_get_fbo();
525 for(int i=0; i<2; i++) {
529 vp_width = goatvr_get_fb_eye_width(i);
530 vp_height = goatvr_get_fb_eye_height(i);
532 // this is a lightweight operation
533 goatvr_rtarg->create_wrap_fbo(gfbo, vp_width, vp_height);
534 push_render_target(goatvr_rtarg, RT_FAKE);
536 vp_width = win_width / 2;
539 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
540 glMatrixMode(GL_PROJECTION);
541 glLoadMatrixf(proj_matrix[0]);
543 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
544 glMatrixMode(GL_MODELVIEW);
545 glLoadMatrixf(view_matrix[0]);
549 if(have_handtracking) {
559 pop_render_target(RT_FAKE);
565 vp_width = win_width;
566 vp_height = win_height;
568 if(!gfbo && !fb_srgb && sdr_post_gamma) {
569 glViewport(0, 0, win_width, win_height);
570 slow_post(sdr_post_gamma);
579 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
583 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
584 glMatrixMode(GL_PROJECTION);
585 glLoadMatrixf(proj_matrix[0]);
587 view_matrix = mouse_view_matrix;
588 glMatrixMode(GL_MODELVIEW);
589 glLoadMatrixf(view_matrix[0]);
593 if(!fb_srgb && sdr_post_gamma) {
594 slow_post(sdr_post_gamma);
603 assert(glGetError() == GL_NO_ERROR);
609 static void draw_scene()
614 if(have_handtracking) {
616 glPushAttrib(GL_ENABLE_BIT);
617 glDisable(GL_LIGHTING);
619 for(int i=0; i<2; i++) {
620 // skip drawing the left hand when we're showing the exhibit gui
621 if(exsel_active && i == 0) continue;
623 if(vrhand[i].valid) {
624 glColor3f(i, 1 - i, i);
626 glColor3f(0.5, 0.5, 0.5);
628 Vec3 v = vrhand[i].pos;
629 Vec3 dir = rotate(Vec3(0, 0, -1), vrhand[i].rot) * 10.0f;
630 Vec3 up = rotate(Vec3(0, 1, 0), vrhand[i].rot) * 5.0f;
631 Vec3 right = rotate(Vec3(1, 0, 0), vrhand[i].rot) * 5.0f;
633 if(i == 1 && pointing) {
637 glVertex3f(v.x, v.y, v.z);
638 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
639 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
640 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
641 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
642 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
648 if(debug_gui && dbg_sel_node) {
649 AABox bvol = dbg_sel_node->get_bounds();
650 draw_geom_object(&bvol);
653 if(show_walk_mesh && mscn->walk_mesh) {
654 glPushAttrib(GL_ENABLE_BIT);
656 glBlendFunc(GL_ONE, GL_ONE);
657 glEnable(GL_POLYGON_OFFSET_FILL);
661 glPolygonOffset(-1, 1);
664 glColor3f(0.3, 0.08, 0.01);
665 mscn->walk_mesh->draw();
674 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
679 void app_reshape(int x, int y)
681 glViewport(0, 0, x, y);
682 goatvr_set_fb_size(x, y, 1.0f);
683 debug_gui_reshape(x, y);
689 void app_keyboard(int key, bool pressed)
691 unsigned int mod = app_get_modifiers();
693 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
694 debug_gui_key(key, pressed, mod);
695 return; // ignore all keystrokes when GUI is visible
707 app_toggle_fullscreen();
712 debug_gui = !debug_gui;
713 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
717 app_toggle_grab_mouse();
718 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
723 show_walk_mesh = !show_walk_mesh;
724 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
731 show_message(noclip ? "no clip" : "clip");
742 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
748 show_message("walk speed: %g", walk_speed);
753 show_message("walk speed: %g", walk_speed);
758 show_message("mouse speed: %g", mouse_speed);
763 show_message("mouse speed: %g", mouse_speed);
767 show_blobs = !show_blobs;
768 show_message("blobs: %s\n", show_blobs ? "on" : "off");
773 show_message("VR recenter\n");
793 if(exsel_grab_mouse) {
794 Exhibit *ex = exsel_grab_mouse.ex;
795 exslot_mouse.detach_exhibit();
796 exman->stash_exhibit(ex);
797 exsel_grab_mouse = ExSelection::null;
799 Exhibit *ex = exman->unstash_exhibit();
801 exslot_mouse.attach_exhibit(ex, EXSLOT_ATTACH_TRANSIENT);
802 exsel_grab_mouse = ex;
804 Vec3 fwd = avatar.get_body_fwd();
805 exslot_mouse.node.set_position(avatar.pos + fwd * 100);
814 dbg_key_pending |= 1 << (key - KEY_F5);
819 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
820 keystate[key] = pressed;
824 void app_mouse_button(int bn, bool pressed, int x, int y)
826 static int press_x, press_y;
829 debug_gui_mbutton(bn, pressed, x, y);
830 return; // ignore mouse events while GUI is visible
835 bnstate[bn] = pressed;
839 Ray ray = calc_pick_ray(x, y);
840 sel = exman->select(ray);
843 if(sel && (app_get_modifiers() & MOD_CTRL)) {
844 exsel_grab_mouse = sel;
845 Vec3 pos = sel.ex->node->get_position();
846 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
847 exslot_mouse.node.set_position(pos);
848 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
849 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
851 exsel_active = ExSelection::null; // cancel active on grab
858 if(exsel_grab_mouse) {
859 // cancel grab on mouse release
860 Exhibit *ex = exsel_grab_mouse.ex;
861 Vec3 pos = exslot_mouse.node.get_position();
863 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
865 exslot_mouse.detach_exhibit();
867 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 300);
869 debug_log("no empty slot nearby\n");
870 if(ex->prev_slot && ex->prev_slot->empty()) {
871 slot = ex->prev_slot;
872 debug_log("using previous slot\n");
877 slot->attach_exhibit(ex);
879 // nowhere to put it, so stash it for later
880 exslot_mouse.detach_exhibit();
881 exman->stash_exhibit(ex);
882 debug_log("no slots available, stashing\n");
885 exsel_grab_mouse = ExSelection::null;
888 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
889 exsel_active = sel; // select or deselect active exhibit
891 debug_log("selecting...\n");
893 debug_log("deselecting...\n");
897 press_x = press_y = INT_MIN;
902 static inline void mouse_look(float dx, float dy)
904 float scrsz = (float)win_height;
905 avatar.set_body_rotation(avatar.body_rot + dx * 512.0 / scrsz);
906 avatar.head_alt += dy * 512.0 / scrsz;
908 if(avatar.head_alt < -90) avatar.head_alt = -90;
909 if(avatar.head_alt > 90) avatar.head_alt = 90;
912 static void mouse_zoom(float dx, float dy)
914 cam_dist += dy * 0.1;
915 if(cam_dist < 0.0) cam_dist = 0.0;
918 void app_mouse_motion(int x, int y)
921 debug_gui_mmotion(x, y);
922 return; // ignore mouse events while GUI is visible
925 int dx = x - prev_mx;
926 int dy = y - prev_my;
930 if(!dx && !dy) return;
932 if(exsel_grab_mouse) {
933 Vec3 pos = exslot_mouse.node.get_node_position();
934 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
936 exslot_mouse.node.set_position(pos + dir);
944 void app_mouse_delta(int dx, int dy)
947 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
949 mouse_look(dx * mouse_speed, dy * mouse_speed);
953 void app_mouse_wheel(int dir)
956 debug_gui_wheel(dir);
960 void app_gamepad_axis(int axis, float val)
979 void app_gamepad_button(int bn, bool pressed)
981 gpad_bnstate[bn] = pressed;
990 show_blobs = !show_blobs;
991 show_message("blobs: %s\n", show_blobs ? "on" : "off");
996 show_message("VR recenter\n");
1005 static void toggle_flight()
1007 static float prev_walk_speed = -1.0;
1008 if(prev_walk_speed < 0.0) {
1010 prev_walk_speed = walk_speed;
1011 walk_speed = 1000.0;
1012 show_message("fly mode\n");
1015 walk_speed = prev_walk_speed;
1016 prev_walk_speed = -1.0;
1017 show_message("walk mode\n");
1021 static void calc_framerate()
1025 static long prev_upd;
1027 long elapsed = time_msec - prev_upd;
1028 if(elapsed >= 1000) {
1029 framerate = (float)nframes / (float)(elapsed * 0.001);
1031 prev_upd = time_msec;
1033 /*if(++ncalc >= 5) {
1034 printf("fps: %f\n", framerate);
1042 static Ray calc_pick_ray(int x, int y)
1044 float nx = (float)x / (float)win_width;
1045 float ny = (float)(win_height - y) / (float)win_height;
1047 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
1048 return last_pick_ray;