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.1;
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;
329 if(opt.min_turn > 0.0f) {
330 static long last_turn;
331 if(len > 0.5 && time_msec - last_turn > 350) {
332 float sign = joy_look.x > 0.0f ? 1.0f : -1.0f;
333 avatar.body_rot += opt.min_turn * sign;
334 last_turn = time_msec;
337 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
340 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
341 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
342 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
346 if(keystate[(int)'w']) {
349 if(keystate[(int)'s']) {
352 if(keystate[(int)'d']) {
355 if(keystate[(int)'a']) {
358 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
359 avatar.pos.y += speed;
361 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
362 avatar.pos.y -= speed;
365 Vec3 walk_dir = avatar.calc_walk_dir(dir.z, dir.x);
366 Vec3 newpos = avatar.pos + walk_dir;
371 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
372 float dtheta = M_PI / 32.0;
373 float theta = dtheta;
374 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
376 for(int i=0; i<16; i++) {
377 Vec2 dvec = rotate(dir2d, theta);
378 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
379 if(constrain_walk_mesh(pos, &avatar.pos)) {
382 dvec = rotate(dir2d, -theta);
383 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
384 if(constrain_walk_mesh(pos, &avatar.pos)) {
390 floor_y = avatar.pos.y - user_eye_height;
393 if(have_headtracking) {
395 goatvr_head_orientation(&qhead.x);
396 avatar.tracked_head_rotation(qhead);
399 // TODO move to the avatar system
400 // calculate mouselook view matrix
401 mouse_view_matrix = Mat4::identity;
402 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
403 if(!have_headtracking) {
404 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
406 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
407 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
409 // update hand-tracking
410 if(have_handtracking) {
411 update_vrhands(&avatar);
413 ExSelection *exsel_grab[] = { &exsel_grab_left, &exsel_grab_right };
414 ExhibitSlot *exslot[] = { &exslot_left, &exslot_right };
416 for(int i=0; i<2; i++) {
417 if(vrhand[i].valid) {
418 exslot[i]->node.set_position(vrhand[i].pos);
419 exslot[i]->node.set_rotation(vrhand[i].rot * exslot[i]->grab_rot);
421 bool act_grab = goatvr_action(i, GOATVR_ACTION_GRAB) != 0;
424 sel = exman->select(Sphere(vrhand[i].pos, 10));
426 if(!*exsel_grab[i]) {
427 // we don't have an exhibit grabbed
430 *exsel_grab[i] = sel;
431 //SceneNode *objnode = sel.ex->node->find_object_node();
432 //exslot[i]->rotation = normalize(sel.ex->node->get_rotation());
433 exslot[i]->grab_rot = inverse(vrhand[i].rot);
434 exslot[i]->attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
436 exsel_active = ExSelection::null; // cancel active on grab
443 // we have an exhibit grabbed
446 Exhibit *ex = exsel_grab[i]->ex;
447 exslot[i]->detach_exhibit();
449 ExhibitSlot *slot = exman->nearest_empty_slot(vrhand[i].pos, 100);
451 debug_log("no empty slot nearby\n");
452 if(ex->prev_slot && ex->prev_slot->empty()) {
453 slot = ex->prev_slot;
454 debug_log("using previous slot\n");
459 Quat rot = normalize(exslot[i]->node.get_rotation());
460 ex->node->set_rotation(rot);
461 slot->attach_exhibit(ex);
463 // nowhere to put it, stash it for later
464 exman->stash_exhibit(ex);
465 debug_log("no slots available, stashing\n");
468 *exsel_grab[i] = ExSelection::null;
469 exslot[i]->grab_rot = Quat::identity;
475 // if there are no grabs, and we're pointing with the right finger, override active
476 if(!exsel_grab_left && !exsel_grab_right) {
477 if(goatvr_action(1, GOATVR_ACTION_POINT)) {
479 ray.origin = vrhand[1].pos;
480 ray.dir = rotate(Vec3(0, 0, -1), vrhand[1].rot);
481 exsel_active = exman->select(ray);
484 exsel_active = ExSelection::null;
490 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
491 if(!exsel_grab_mouse) {
492 Ray ray = calc_pick_ray(prev_mx, prev_my);
493 exsel_hover = exman->select(ray);
496 // TODO do this properly
497 // set the position of the left hand at a suitable position for the exhibit UI
498 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
499 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
501 rot.set_rotation(Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
502 exslot_left.node.set_rotation(rot);
505 if(!exslot_right.empty()) exslot_right.node.update(dt);
506 // always update the left slot, because it's the anchor point of the exhibit ui
507 exslot_left.node.update(dt);
509 // need to call this *after* we have updated the active exhibit (if any)
515 float dt = (float)(time_msec - prev_msec) / 1000.0f;
516 prev_msec = time_msec;
519 ImGui::GetIOPtr()->DeltaTime = dt;
522 //ImGui::ShowTestWindow();
525 glClearColor(1, 1, 1, 1);
530 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
532 unsigned int gfbo = goatvr_get_fbo();
536 for(int i=0; i<2; i++) {
540 vp_width = goatvr_get_fb_eye_width(i);
541 vp_height = goatvr_get_fb_eye_height(i);
543 // this is a lightweight operation
544 goatvr_rtarg->create_wrap_fbo(gfbo, vp_width, vp_height);
545 push_render_target(goatvr_rtarg, RT_FAKE);
547 vp_width = win_width / 2;
550 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
551 glMatrixMode(GL_PROJECTION);
552 glLoadMatrixf(proj_matrix[0]);
554 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
555 glMatrixMode(GL_MODELVIEW);
556 glLoadMatrixf(view_matrix[0]);
560 if(have_handtracking) {
570 pop_render_target(RT_FAKE);
576 vp_width = win_width;
577 vp_height = win_height;
579 if(!gfbo && !fb_srgb && sdr_post_gamma) {
580 glViewport(0, 0, win_width, win_height);
581 slow_post(sdr_post_gamma);
590 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
594 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
595 glMatrixMode(GL_PROJECTION);
596 glLoadMatrixf(proj_matrix[0]);
598 view_matrix = mouse_view_matrix;
599 glMatrixMode(GL_MODELVIEW);
600 glLoadMatrixf(view_matrix[0]);
604 if(!fb_srgb && sdr_post_gamma) {
605 slow_post(sdr_post_gamma);
614 assert(glGetError() == GL_NO_ERROR);
620 static void draw_scene()
625 if(have_handtracking) {
627 glPushAttrib(GL_ENABLE_BIT);
628 glDisable(GL_LIGHTING);
630 for(int i=0; i<2; i++) {
631 // skip drawing the left hand when we're showing the exhibit gui
632 if(exsel_active && i == 0) continue;
634 if(vrhand[i].valid) {
635 glColor3f(i, 1 - i, i);
637 glColor3f(0.5, 0.5, 0.5);
639 Vec3 v = vrhand[i].pos;
640 Vec3 dir = rotate(Vec3(0, 0, -1), vrhand[i].rot) * 10.0f;
641 Vec3 up = rotate(Vec3(0, 1, 0), vrhand[i].rot) * 5.0f;
642 Vec3 right = rotate(Vec3(1, 0, 0), vrhand[i].rot) * 5.0f;
644 if(i == 1 && pointing) {
648 glVertex3f(v.x, v.y, v.z);
649 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
650 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
651 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
652 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
653 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
659 if(debug_gui && dbg_sel_node) {
660 AABox bvol = dbg_sel_node->get_bounds();
661 draw_geom_object(&bvol);
664 if(show_walk_mesh && mscn->walk_mesh) {
665 glPushAttrib(GL_ENABLE_BIT);
667 glBlendFunc(GL_ONE, GL_ONE);
668 glEnable(GL_POLYGON_OFFSET_FILL);
672 glPolygonOffset(-1, 1);
675 glColor3f(0.3, 0.08, 0.01);
676 mscn->walk_mesh->draw();
685 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
690 void app_reshape(int x, int y)
692 glViewport(0, 0, x, y);
693 goatvr_set_fb_size(x, y, 1.0f);
694 debug_gui_reshape(x, y);
700 void app_keyboard(int key, bool pressed)
702 unsigned int mod = app_get_modifiers();
704 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
705 debug_gui_key(key, pressed, mod);
706 return; // ignore all keystrokes when GUI is visible
718 app_toggle_fullscreen();
723 debug_gui = !debug_gui;
724 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
728 app_toggle_grab_mouse();
729 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
734 show_walk_mesh = !show_walk_mesh;
735 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
742 show_message(noclip ? "no clip" : "clip");
753 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
759 show_message("walk speed: %g", walk_speed);
764 show_message("walk speed: %g", walk_speed);
769 show_message("mouse speed: %g", mouse_speed);
774 show_message("mouse speed: %g", mouse_speed);
778 show_blobs = !show_blobs;
779 show_message("blobs: %s\n", show_blobs ? "on" : "off");
784 show_message("VR recenter\n");
804 if(exsel_grab_mouse) {
805 Exhibit *ex = exsel_grab_mouse.ex;
806 exslot_mouse.detach_exhibit();
807 exman->stash_exhibit(ex);
808 exsel_grab_mouse = ExSelection::null;
810 Exhibit *ex = exman->unstash_exhibit();
812 exslot_mouse.attach_exhibit(ex, EXSLOT_ATTACH_TRANSIENT);
813 exsel_grab_mouse = ex;
815 Vec3 fwd = avatar.get_body_fwd();
816 exslot_mouse.node.set_position(avatar.pos + fwd * 100);
825 dbg_key_pending |= 1 << (key - KEY_F5);
830 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
831 keystate[key] = pressed;
835 void app_mouse_button(int bn, bool pressed, int x, int y)
837 static int press_x, press_y;
840 debug_gui_mbutton(bn, pressed, x, y);
841 return; // ignore mouse events while GUI is visible
846 bnstate[bn] = pressed;
850 Ray ray = calc_pick_ray(x, y);
851 sel = exman->select(ray);
854 if(sel && (app_get_modifiers() & MOD_CTRL)) {
855 exsel_grab_mouse = sel;
856 Vec3 pos = sel.ex->node->get_position();
857 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
858 exslot_mouse.node.set_position(pos);
859 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
860 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
862 exsel_active = ExSelection::null; // cancel active on grab
869 if(exsel_grab_mouse) {
870 // cancel grab on mouse release
871 Exhibit *ex = exsel_grab_mouse.ex;
872 Vec3 pos = exslot_mouse.node.get_position();
874 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
876 exslot_mouse.detach_exhibit();
878 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 300);
880 debug_log("no empty slot nearby\n");
881 if(ex->prev_slot && ex->prev_slot->empty()) {
882 slot = ex->prev_slot;
883 debug_log("using previous slot\n");
888 slot->attach_exhibit(ex);
890 // nowhere to put it, so stash it for later
891 exslot_mouse.detach_exhibit();
892 exman->stash_exhibit(ex);
893 debug_log("no slots available, stashing\n");
896 exsel_grab_mouse = ExSelection::null;
899 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
900 exsel_active = sel; // select or deselect active exhibit
902 debug_log("selecting...\n");
904 debug_log("deselecting...\n");
908 press_x = press_y = INT_MIN;
913 static inline void mouse_look(float dx, float dy)
915 float scrsz = (float)win_height;
916 avatar.set_body_rotation(avatar.body_rot + dx * 512.0 / scrsz);
917 avatar.head_alt += dy * 512.0 / scrsz;
919 if(avatar.head_alt < -90) avatar.head_alt = -90;
920 if(avatar.head_alt > 90) avatar.head_alt = 90;
923 static void mouse_zoom(float dx, float dy)
925 cam_dist += dy * 0.1;
926 if(cam_dist < 0.0) cam_dist = 0.0;
929 void app_mouse_motion(int x, int y)
932 debug_gui_mmotion(x, y);
933 return; // ignore mouse events while GUI is visible
936 int dx = x - prev_mx;
937 int dy = y - prev_my;
941 if(!dx && !dy) return;
943 if(exsel_grab_mouse) {
944 Vec3 pos = exslot_mouse.node.get_node_position();
945 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
947 exslot_mouse.node.set_position(pos + dir);
955 void app_mouse_delta(int dx, int dy)
958 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
960 mouse_look(dx * mouse_speed, dy * mouse_speed);
964 void app_mouse_wheel(int dir)
967 debug_gui_wheel(dir);
971 void app_gamepad_axis(int axis, float val)
990 void app_gamepad_button(int bn, bool pressed)
992 gpad_bnstate[bn] = pressed;
1001 show_blobs = !show_blobs;
1002 show_message("blobs: %s\n", show_blobs ? "on" : "off");
1007 show_message("VR recenter\n");
1016 static void toggle_flight()
1018 static float prev_walk_speed = -1.0;
1019 if(prev_walk_speed < 0.0) {
1021 prev_walk_speed = walk_speed;
1022 walk_speed = 1000.0;
1023 show_message("fly mode\n");
1026 walk_speed = prev_walk_speed;
1027 prev_walk_speed = -1.0;
1028 show_message("walk mode\n");
1032 static void calc_framerate()
1036 static long prev_upd;
1038 long elapsed = time_msec - prev_upd;
1039 if(elapsed >= 1000) {
1040 framerate = (float)nframes / (float)(elapsed * 0.001);
1042 prev_upd = time_msec;
1044 /*if(++ncalc >= 5) {
1045 printf("fps: %f\n", framerate);
1053 static Ray calc_pick_ray(int x, int y)
1055 float nx = (float)x / (float)win_width;
1056 float ny = (float)(win_height - y) / (float)win_height;
1058 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
1059 return last_pick_ray;