12 #include "metascene.h"
22 #include "blob_exhibit.h"
25 #include "ui_exhibit.h"
28 #define FAR_CLIP 10000.0
30 static void draw_scene();
31 static void toggle_flight();
32 static void calc_framerate();
33 static Ray calc_pick_ray(int x, int y);
36 int win_width, win_height;
37 int vp_width, vp_height;
40 bool opt_gear_wireframe;
47 unsigned int dbg_key_pending;
51 static float cam_dist = 0.0;
52 static float floor_y; // last floor height
53 static float user_eye_height = 165;
55 static float walk_speed = 300.0f;
56 static float mouse_speed = 0.5f;
57 static bool show_walk_mesh, noclip = false;
59 static bool have_headtracking, have_handtracking, should_swap;
61 static int prev_mx, prev_my;
62 static bool bnstate[8];
63 static bool keystate[256];
64 static bool gpad_bnstate[64];
65 static Vec2 joy_move, joy_look;
66 static float joy_deadzone = 0.01;
68 static float framerate;
70 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
71 static MetaScene *mscn;
72 static unsigned int sdr_post_gamma;
74 static long prev_msec;
76 static ExhibitManager *exman;
77 static bool show_blobs;
79 ExSelection exsel_active, exsel_hover;
80 ExSelection exsel_grab_left, exsel_grab_right;
81 #define exsel_grab_mouse exsel_grab_right
82 static ExhibitSlot exslot_left, exslot_right;
83 #define exslot_mouse exslot_right
87 static Renderer *rend;
88 static RenderTarget *goatvr_rtarg;
90 static Ray last_pick_ray;
92 static bool post_scene_init_pending = true;
95 bool app_init(int argc, char **argv)
97 set_log_file("demo.log");
99 char *env = getenv("FPEXCEPT");
100 if(env && atoi(env)) {
101 info_log("enabling floating point exceptions\n");
102 fpexcept_enabled = 1;
106 if(init_opengl() == -1) {
110 if(!init_options(argc, argv, "demo.conf")) {
113 app_resize(opt.width, opt.height);
114 app_fullscreen(opt.fullscreen);
117 if(goatvr_init() == -1) {
120 goatvr_set_origin_mode(GOATVR_HEAD);
121 goatvr_set_units_scale(100.0f);
124 should_swap = goatvr_should_swap() != 0;
125 user_eye_height = goatvr_get_eye_height();
126 have_headtracking = goatvr_have_headtracking();
127 have_handtracking = goatvr_have_handtracking();
131 goatvr_rtarg = new RenderTarget;
136 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
137 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
139 glEnable(GL_FRAMEBUFFER_SRGB);
145 glEnable(GL_MULTISAMPLE);
146 glEnable(GL_DEPTH_TEST);
147 glEnable(GL_CULL_FACE);
148 glEnable(GL_NORMALIZE);
150 if(!init_debug_gui()) {
154 Mesh::use_custom_sdr_attr = false;
156 float ambient[] = {0.0, 0.0, 0.0, 0.0};
157 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
161 if(!init_vrhands()) {
165 mscn = new MetaScene;
166 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
170 avatar.pos = mscn->start_pos;
171 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
173 avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
175 exman = new ExhibitManager;
176 // exhibits are loaded in post_scene_init, because they need access to the scene graph
179 error_log("failed to initialize exhibit ui system\n");
182 exui_setnode(&exslot_left.node);
183 if(have_handtracking) {
185 exui_rotation(Vec3(-deg_to_rad(35), 0, 0));
189 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
197 rend->ropt |= RENDER_MIRRORS;
199 rend->ropt &= ~RENDER_MIRRORS;
201 rend->set_scene(mscn);
205 if(opt.vr || opt.fullscreen) {
206 app_grab_mouse(true);
209 if(mscn->music && opt.music) {
210 mscn->music->play(AUDIO_PLAYMODE_LOOP);
215 // post_scene_init is called after the scene has completed loading
216 static void post_scene_init()
218 mscn->update(0); // update once to calculate node matrices
220 int num_mir = mscn->calc_mirror_planes();
221 info_log("found %d mirror planes\n", num_mir);
223 exman->load(mscn, "data/exhibits");
233 app_grab_mouse(false);
244 /* this must be destroyed before the scene graph to detach exhibit nodes
245 * before the scene tries to delete them recursively
255 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
257 Mesh *wm = mscn->walk_mesh;
263 Ray downray = Ray(v, Vec3(0, -1, 0));
265 if(mscn->walk_mesh->intersect(downray, &hit)) {
267 newv->y += user_eye_height;
273 static void update(float dt)
278 if(post_scene_init_pending && !sceneman.pending()) {
279 post_scene_init_pending = false;
286 // use goatvr sticks for joystick input
287 int num_vr_sticks = goatvr_num_sticks();
288 if(num_vr_sticks > 0) {
290 goatvr_stick_pos(0, p);
294 if(num_vr_sticks > 1) {
296 goatvr_stick_pos(1, p);
301 float speed = walk_speed * dt;
305 float jdeadsq = joy_deadzone * joy_deadzone;
306 float jmove_lensq = length_sq(joy_move);
307 float jlook_lensq = length_sq(joy_look);
309 if(jmove_lensq > jdeadsq) {
310 float len = sqrt(jmove_lensq);
311 jmove_lensq -= jdeadsq;
313 float mag = len * len;
314 dir.x += mag * joy_move.x / len * 2.0 * speed;
315 dir.z += mag * joy_move.y / len * 2.0 * speed;
317 if(jlook_lensq > jdeadsq) {
318 float len = sqrt(jlook_lensq);
319 jlook_lensq -= jdeadsq;
321 float mag = len * len;
322 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
323 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
324 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
325 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
329 if(keystate[(int)'w']) {
332 if(keystate[(int)'s']) {
335 if(keystate[(int)'d']) {
338 if(keystate[(int)'a']) {
341 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
342 avatar.pos.y += speed;
344 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
345 avatar.pos.y -= speed;
348 float theta = M_PI * avatar.body_rot / 180.0f;
350 newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
351 newpos.y = avatar.pos.y;
352 newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
357 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
358 float dtheta = M_PI / 32.0;
359 float theta = dtheta;
360 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
362 for(int i=0; i<16; i++) {
363 Vec2 dvec = rotate(dir2d, theta);
364 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
365 if(constrain_walk_mesh(pos, &avatar.pos)) {
368 dvec = rotate(dir2d, -theta);
369 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
370 if(constrain_walk_mesh(pos, &avatar.pos)) {
376 floor_y = avatar.pos.y - user_eye_height;
379 if(have_headtracking) {
381 goatvr_head_orientation(&qhead.x);
382 avatar.tracked_head_rotation(qhead);
385 // TODO move to the avatar system
386 // calculate mouselook view matrix
387 mouse_view_matrix = Mat4::identity;
388 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
389 if(!have_headtracking) {
390 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
392 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
393 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
395 // update hand-tracking
396 if(have_handtracking) {
397 update_vrhands(&avatar);
399 ExSelection *exsel_grab[] = { &exsel_grab_left, &exsel_grab_right };
400 ExhibitSlot *exslot[] = { &exslot_left, &exslot_right };
402 for(int i=0; i<2; i++) {
403 if(vrhand[i].valid) {
404 exslot[i]->node.set_position(vrhand[i].pos);
405 exslot[i]->node.set_rotation(vrhand[i].rot * exslot[i]->grab_rot);
407 bool act_grab = goatvr_action(i, GOATVR_ACTION_GRAB) != 0;
410 sel = exman->select(Sphere(vrhand[i].pos, 10));
412 if(!*exsel_grab[i]) {
413 // we don't have an exhibit grabbed
416 *exsel_grab[i] = sel;
417 //SceneNode *objnode = sel.ex->node->find_object_node();
418 //exslot[i]->rotation = normalize(sel.ex->node->get_rotation());
419 exslot[i]->grab_rot = inverse(vrhand[i].rot);
420 exslot[i]->attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
422 exsel_active = ExSelection::null; // cancel active on grab
429 // we have an exhibit grabbed
432 Exhibit *ex = exsel_grab[i]->ex;
433 exslot[i]->detach_exhibit();
435 ExhibitSlot *slot = exman->nearest_empty_slot(vrhand[i].pos, 100);
437 debug_log("no empty slot nearby\n");
438 if(ex->prev_slot && ex->prev_slot->empty()) {
439 slot = ex->prev_slot;
440 debug_log("using previous slot\n");
445 Quat rot = normalize(exslot[i]->node.get_rotation());
446 ex->node->set_rotation(rot);
447 slot->attach_exhibit(ex);
449 // nowhere to put it, stash it for later
450 exman->stash_exhibit(ex);
451 debug_log("no slots available, stashing\n");
454 *exsel_grab[i] = ExSelection::null;
455 exslot[i]->grab_rot = Quat::identity;
461 // if there are no grabs, and we're pointing with the right finger, override active
462 if(!exsel_grab_left && !exsel_grab_right) {
463 if(goatvr_action(1, GOATVR_ACTION_POINT)) {
465 ray.origin = vrhand[1].pos;
466 ray.dir = rotate(Vec3(0, 0, -1), vrhand[1].rot);
467 exsel_active = exman->select(ray);
470 exsel_active = ExSelection::null;
476 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
477 if(!exsel_grab_mouse) {
478 Ray ray = calc_pick_ray(prev_mx, prev_my);
479 exsel_hover = exman->select(ray);
482 // TODO do this properly
483 // set the position of the left hand at a suitable position for the exhibit UI
484 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
485 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
487 rot.set_rotation(Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
488 exslot_left.node.set_rotation(rot);
491 if(!exslot_right.empty()) exslot_right.node.update(dt);
492 // always update the left slot, because it's the anchor point of the exhibit ui
493 exslot_left.node.update(dt);
495 // need to call this *after* we have updated the active exhibit (if any)
501 float dt = (float)(time_msec - prev_msec) / 1000.0f;
502 prev_msec = time_msec;
505 ImGui::GetIOPtr()->DeltaTime = dt;
508 //ImGui::ShowTestWindow();
511 glClearColor(1, 1, 1, 1);
516 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
518 unsigned int gfbo = goatvr_get_fbo();
522 for(int i=0; i<2; i++) {
526 vp_width = goatvr_get_fb_eye_width(i);
527 vp_height = goatvr_get_fb_eye_height(i);
529 // this is a lightweight operation
530 goatvr_rtarg->create_wrap_fbo(gfbo, vp_width, vp_height);
531 push_render_target(goatvr_rtarg, RT_FAKE);
533 vp_width = win_width / 2;
536 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
537 glMatrixMode(GL_PROJECTION);
538 glLoadMatrixf(proj_matrix[0]);
540 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
541 glMatrixMode(GL_MODELVIEW);
542 glLoadMatrixf(view_matrix[0]);
546 if(have_handtracking) {
556 pop_render_target(RT_FAKE);
562 vp_width = win_width;
563 vp_height = win_height;
570 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
574 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
575 glMatrixMode(GL_PROJECTION);
576 glLoadMatrixf(proj_matrix[0]);
578 view_matrix = mouse_view_matrix;
579 glMatrixMode(GL_MODELVIEW);
580 glLoadMatrixf(view_matrix[0]);
584 if(!fb_srgb && sdr_post_gamma) {
585 slow_post(sdr_post_gamma);
594 assert(glGetError() == GL_NO_ERROR);
600 static void draw_scene()
605 if(have_handtracking) {
607 glPushAttrib(GL_ENABLE_BIT);
608 glDisable(GL_LIGHTING);
610 for(int i=0; i<2; i++) {
611 // skip drawing the left hand when we're showing the exhibit gui
612 if(exsel_active && i == 0) continue;
614 if(vrhand[i].valid) {
615 glColor3f(i, 1 - i, i);
617 glColor3f(0.5, 0.5, 0.5);
619 Vec3 v = vrhand[i].pos;
620 Vec3 dir = rotate(Vec3(0, 0, -1), vrhand[i].rot) * 10.0f;
621 Vec3 up = rotate(Vec3(0, 1, 0), vrhand[i].rot) * 5.0f;
622 Vec3 right = rotate(Vec3(1, 0, 0), vrhand[i].rot) * 5.0f;
624 if(i == 1 && pointing) {
628 glVertex3f(v.x, v.y, v.z);
629 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
630 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
631 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
632 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
633 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
639 if(debug_gui && dbg_sel_node) {
640 AABox bvol = dbg_sel_node->get_bounds();
641 draw_geom_object(&bvol);
644 if(show_walk_mesh && mscn->walk_mesh) {
645 glPushAttrib(GL_ENABLE_BIT);
647 glBlendFunc(GL_ONE, GL_ONE);
648 glEnable(GL_POLYGON_OFFSET_FILL);
652 glPolygonOffset(-1, 1);
655 glColor3f(0.3, 0.08, 0.01);
656 mscn->walk_mesh->draw();
665 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
670 void app_reshape(int x, int y)
672 glViewport(0, 0, x, y);
673 goatvr_set_fb_size(x, y, 1.0f);
674 debug_gui_reshape(x, y);
680 void app_keyboard(int key, bool pressed)
682 unsigned int mod = app_get_modifiers();
684 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
685 debug_gui_key(key, pressed, mod);
686 return; // ignore all keystrokes when GUI is visible
698 app_toggle_fullscreen();
703 debug_gui = !debug_gui;
704 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
708 app_toggle_grab_mouse();
709 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
714 show_walk_mesh = !show_walk_mesh;
715 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
722 show_message(noclip ? "no clip" : "clip");
733 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
739 show_message("walk speed: %g", walk_speed);
744 show_message("walk speed: %g", walk_speed);
749 show_message("mouse speed: %g", mouse_speed);
754 show_message("mouse speed: %g", mouse_speed);
758 show_blobs = !show_blobs;
759 show_message("blobs: %s\n", show_blobs ? "on" : "off");
764 show_message("VR recenter\n");
787 dbg_key_pending |= 1 << (key - KEY_F5);
792 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
793 keystate[key] = pressed;
797 void app_mouse_button(int bn, bool pressed, int x, int y)
799 static int press_x, press_y;
802 debug_gui_mbutton(bn, pressed, x, y);
803 return; // ignore mouse events while GUI is visible
808 bnstate[bn] = pressed;
812 Ray ray = calc_pick_ray(x, y);
813 sel = exman->select(ray);
816 if(sel && (app_get_modifiers() & MOD_CTRL)) {
817 exsel_grab_mouse = sel;
818 Vec3 pos = sel.ex->node->get_position();
819 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
820 exslot_mouse.node.set_position(pos);
821 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
822 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
824 exsel_active = ExSelection::null; // cancel active on grab
831 if(exsel_grab_mouse) {
832 // cancel grab on mouse release
833 Exhibit *ex = exsel_grab_mouse.ex;
834 Vec3 pos = exslot_mouse.node.get_position();
836 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
838 exslot_mouse.detach_exhibit();
840 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
842 debug_log("no empty slot nearby\n");
843 if(ex->prev_slot && ex->prev_slot->empty()) {
844 slot = ex->prev_slot;
845 debug_log("using previous slot\n");
850 slot->attach_exhibit(ex);
852 // nowhere to put it, so stash it for later
853 exslot_mouse.detach_exhibit();
854 exman->stash_exhibit(ex);
855 debug_log("no slots available, stashing\n");
858 exsel_grab_mouse = ExSelection::null;
861 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
862 exsel_active = sel; // select or deselect active exhibit
864 debug_log("selecting...\n");
866 debug_log("deselecting...\n");
870 press_x = press_y = INT_MIN;
875 static inline void mouse_look(float dx, float dy)
877 float scrsz = (float)win_height;
878 avatar.body_rot += dx * 512.0 / scrsz;
879 avatar.head_alt += dy * 512.0 / scrsz;
881 if(avatar.head_alt < -90) avatar.head_alt = -90;
882 if(avatar.head_alt > 90) avatar.head_alt = 90;
885 static void mouse_zoom(float dx, float dy)
887 cam_dist += dy * 0.1;
888 if(cam_dist < 0.0) cam_dist = 0.0;
891 void app_mouse_motion(int x, int y)
894 debug_gui_mmotion(x, y);
895 return; // ignore mouse events while GUI is visible
898 int dx = x - prev_mx;
899 int dy = y - prev_my;
903 if(!dx && !dy) return;
905 if(exsel_grab_mouse) {
906 Vec3 pos = exslot_mouse.node.get_node_position();
907 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
909 exslot_mouse.node.set_position(pos + dir);
917 void app_mouse_delta(int dx, int dy)
920 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
922 mouse_look(dx * mouse_speed, dy * mouse_speed);
926 void app_mouse_wheel(int dir)
929 debug_gui_wheel(dir);
933 void app_gamepad_axis(int axis, float val)
952 void app_gamepad_button(int bn, bool pressed)
954 gpad_bnstate[bn] = pressed;
963 show_blobs = !show_blobs;
964 show_message("blobs: %s\n", show_blobs ? "on" : "off");
969 show_message("VR recenter\n");
978 static void toggle_flight()
980 static float prev_walk_speed = -1.0;
981 if(prev_walk_speed < 0.0) {
983 prev_walk_speed = walk_speed;
985 show_message("fly mode\n");
988 walk_speed = prev_walk_speed;
989 prev_walk_speed = -1.0;
990 show_message("walk mode\n");
994 static void calc_framerate()
998 static long prev_upd;
1000 long elapsed = time_msec - prev_upd;
1001 if(elapsed >= 1000) {
1002 framerate = (float)nframes / (float)(elapsed * 0.001);
1004 prev_upd = time_msec;
1006 /*if(++ncalc >= 5) {
1007 printf("fps: %f\n", framerate);
1015 static Ray calc_pick_ray(int x, int y)
1017 float nx = (float)x / (float)win_width;
1018 float ny = (float)(win_height - y) / (float)win_height;
1020 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
1021 return last_pick_ray;