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
85 static Renderer *rend;
86 static RenderTarget *goatvr_rtarg;
88 static Ray last_pick_ray;
90 static bool post_scene_init_pending = true;
93 bool app_init(int argc, char **argv)
95 set_log_file("demo.log");
97 char *env = getenv("FPEXCEPT");
98 if(env && atoi(env)) {
99 info_log("enabling floating point exceptions\n");
100 fpexcept_enabled = 1;
104 if(init_opengl() == -1) {
108 if(!init_options(argc, argv, "demo.conf")) {
111 app_resize(opt.width, opt.height);
112 app_fullscreen(opt.fullscreen);
115 if(goatvr_init() == -1) {
118 goatvr_set_origin_mode(GOATVR_HEAD);
119 goatvr_set_units_scale(100.0f);
122 should_swap = goatvr_should_swap() != 0;
123 user_eye_height = goatvr_get_eye_height();
124 have_headtracking = goatvr_have_headtracking();
125 have_handtracking = goatvr_have_handtracking();
129 goatvr_rtarg = new RenderTarget;
134 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
135 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
137 glEnable(GL_FRAMEBUFFER_SRGB);
143 glEnable(GL_MULTISAMPLE);
144 glEnable(GL_DEPTH_TEST);
145 glEnable(GL_CULL_FACE);
146 glEnable(GL_NORMALIZE);
148 if(!init_debug_gui()) {
152 Mesh::use_custom_sdr_attr = false;
154 float ambient[] = {0.0, 0.0, 0.0, 0.0};
155 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
159 if(!init_vrhands()) {
163 mscn = new MetaScene;
164 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
168 avatar.pos = mscn->start_pos;
169 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
171 avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
173 exman = new ExhibitManager;
174 // exhibits are loaded in post_scene_init, because they need access to the scene graph
177 error_log("failed to initialize exhibit ui system\n");
180 exui_setnode(&exslot_left.node);
183 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
191 rend->ropt |= RENDER_MIRRORS;
193 rend->ropt &= ~RENDER_MIRRORS;
195 rend->set_scene(mscn);
199 if(opt.vr || opt.fullscreen) {
200 app_grab_mouse(true);
203 if(mscn->music && opt.music) {
204 mscn->music->play(AUDIO_PLAYMODE_LOOP);
209 // post_scene_init is called after the scene has completed loading
210 static void post_scene_init()
212 mscn->update(0); // update once to calculate node matrices
214 int num_mir = mscn->calc_mirror_planes();
215 info_log("found %d mirror planes\n", num_mir);
217 exman->load(mscn, "data/exhibits");
227 app_grab_mouse(false);
238 /* this must be destroyed before the scene graph to detach exhibit nodes
239 * before the scene tries to delete them recursively
249 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
251 Mesh *wm = mscn->walk_mesh;
257 Ray downray = Ray(v, Vec3(0, -1, 0));
259 if(mscn->walk_mesh->intersect(downray, &hit)) {
261 newv->y += user_eye_height;
267 static void update(float dt)
272 if(post_scene_init_pending && !sceneman.pending()) {
273 post_scene_init_pending = false;
281 float speed = walk_speed * dt;
285 float jdeadsq = joy_deadzone * joy_deadzone;
286 float jmove_lensq = length_sq(joy_move);
287 float jlook_lensq = length_sq(joy_look);
289 if(jmove_lensq > jdeadsq) {
290 float len = sqrt(jmove_lensq);
291 jmove_lensq -= jdeadsq;
293 float mag = len * len;
294 dir.x += mag * joy_move.x / len * 2.0 * speed;
295 dir.z += mag * joy_move.y / len * 2.0 * speed;
297 if(jlook_lensq > jdeadsq) {
298 float len = sqrt(jlook_lensq);
299 jlook_lensq -= jdeadsq;
301 float mag = len * len;
302 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
303 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
304 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
305 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
309 if(keystate[(int)'w']) {
312 if(keystate[(int)'s']) {
315 if(keystate[(int)'d']) {
318 if(keystate[(int)'a']) {
321 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
322 avatar.pos.y += speed;
324 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
325 avatar.pos.y -= speed;
328 float theta = M_PI * avatar.body_rot / 180.0f;
330 newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
331 newpos.y = avatar.pos.y;
332 newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
337 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
338 float dtheta = M_PI / 32.0;
339 float theta = dtheta;
340 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
342 for(int i=0; i<16; i++) {
343 Vec2 dvec = rotate(dir2d, theta);
344 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
345 if(constrain_walk_mesh(pos, &avatar.pos)) {
348 dvec = rotate(dir2d, -theta);
349 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
350 if(constrain_walk_mesh(pos, &avatar.pos)) {
356 floor_y = avatar.pos.y - user_eye_height;
359 // TODO move to the avatar system
360 // calculate mouselook view matrix
361 mouse_view_matrix = Mat4::identity;
362 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
363 if(!have_headtracking) {
364 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
366 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
367 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
370 // update hand-tracking
371 if(have_handtracking) {
372 update_vrhands(&avatar);
374 if(vrhand[0].valid) {
375 exslot_left.node.set_position(vrhand[0].pos);
376 exslot_left.node.set_rotation(vrhand[0].rot);
378 // right hand takes precedence for hover
379 if(!exsel_grab_left && !exsel_hover) {
380 exsel_hover = exman->select(Sphere(vrhand[0].pos, 5));
383 if(vrhand[1].valid) {
384 exslot_right.node.set_position(vrhand[1].pos);
385 exslot_right.node.set_rotation(vrhand[1].rot);
387 if(!exsel_grab_right) {
388 exsel_hover = exman->select(Sphere(vrhand[1].pos, 5));
393 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
394 if(!exsel_grab_mouse) {
395 Ray ray = calc_pick_ray(prev_mx, prev_my);
396 exsel_hover = exman->select(ray);
399 // TODO do this properly
400 // set the position of the left hand at a suitable position for the exhibit UI
401 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
402 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
405 if(!exslot_right.empty()) exslot_right.node.update(dt);
406 // always update the left slot, because it's the anchor point of the exhibit ui
407 exslot_left.node.update(dt);
412 float dt = (float)(time_msec - prev_msec) / 1000.0f;
413 prev_msec = time_msec;
416 ImGui::GetIOPtr()->DeltaTime = dt;
419 //ImGui::ShowTestWindow();
422 glClearColor(1, 1, 1, 1);
427 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
429 unsigned int gfbo = goatvr_get_fbo();
433 for(int i=0; i<2; i++) {
437 vp_width = goatvr_get_fb_eye_width(i);
438 vp_height = goatvr_get_fb_eye_height(i);
440 // this is a lightweight operation
441 goatvr_rtarg->create_wrap_fbo(gfbo, vp_width, vp_height);
442 push_render_target(goatvr_rtarg, RT_FAKE);
444 vp_width = win_width / 2;
447 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
448 glMatrixMode(GL_PROJECTION);
449 glLoadMatrixf(proj_matrix[0]);
451 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
452 glMatrixMode(GL_MODELVIEW);
453 glLoadMatrixf(view_matrix[0]);
456 if(have_handtracking) {
465 pop_render_target(RT_FAKE);
471 vp_width = win_width;
472 vp_height = win_height;
479 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
483 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
484 glMatrixMode(GL_PROJECTION);
485 glLoadMatrixf(proj_matrix[0]);
487 view_matrix = mouse_view_matrix;
488 glMatrixMode(GL_MODELVIEW);
489 glLoadMatrixf(view_matrix[0]);
493 if(!fb_srgb && sdr_post_gamma) {
494 slow_post(sdr_post_gamma);
503 assert(glGetError() == GL_NO_ERROR);
509 static void draw_scene()
514 if(have_handtracking) {
515 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
516 Mat4 head_dir_xform = head_xform.upper3x3();
519 glPushAttrib(GL_ENABLE_BIT);
520 glDisable(GL_LIGHTING);
522 for(int i=0; i<2; i++) {
523 if(vrhand[i].valid) {
524 glColor3f(i, 1 - i, i);
526 glColor3f(0.5, 0.5, 0.5);
528 Vec3 v = head_xform * vrhand[i].pos;
529 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), vrhand[i].rot) * 20.0f;
530 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), vrhand[i].rot) * 10.0f;
531 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), vrhand[i].rot) * 10.0f;
533 glVertex3f(v.x, v.y, v.z);
534 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
535 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
536 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
537 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
538 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
544 if(debug_gui && dbg_sel_node) {
545 AABox bvol = dbg_sel_node->get_bounds();
546 draw_geom_object(&bvol);
549 if(show_walk_mesh && mscn->walk_mesh) {
550 glPushAttrib(GL_ENABLE_BIT);
552 glBlendFunc(GL_ONE, GL_ONE);
553 glEnable(GL_POLYGON_OFFSET_FILL);
557 glPolygonOffset(-1, 1);
560 glColor3f(0.3, 0.08, 0.01);
561 mscn->walk_mesh->draw();
570 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
575 void app_reshape(int x, int y)
577 glViewport(0, 0, x, y);
578 goatvr_set_fb_size(x, y, 1.0f);
579 debug_gui_reshape(x, y);
585 void app_keyboard(int key, bool pressed)
587 unsigned int mod = app_get_modifiers();
589 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
590 debug_gui_key(key, pressed, mod);
591 return; // ignore all keystrokes when GUI is visible
603 app_toggle_fullscreen();
608 debug_gui = !debug_gui;
609 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
613 app_toggle_grab_mouse();
614 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
619 show_walk_mesh = !show_walk_mesh;
620 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
627 show_message(noclip ? "no clip" : "clip");
638 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
644 show_message("walk speed: %g", walk_speed);
649 show_message("walk speed: %g", walk_speed);
654 show_message("mouse speed: %g", mouse_speed);
659 show_message("mouse speed: %g", mouse_speed);
663 show_blobs = !show_blobs;
664 show_message("blobs: %s\n", show_blobs ? "on" : "off");
669 show_message("VR recenter\n");
692 dbg_key_pending |= 1 << (key - KEY_F5);
697 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
698 keystate[key] = pressed;
702 void app_mouse_button(int bn, bool pressed, int x, int y)
704 static int press_x, press_y;
707 debug_gui_mbutton(bn, pressed, x, y);
708 return; // ignore mouse events while GUI is visible
713 bnstate[bn] = pressed;
717 Ray ray = calc_pick_ray(x, y);
718 sel = exman->select(ray);
721 if(sel && (app_get_modifiers() & MOD_CTRL)) {
722 exsel_grab_mouse = sel;
723 Vec3 pos = sel.ex->node->get_position();
724 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
725 exslot_mouse.node.set_position(pos);
726 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
727 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
729 exsel_active = ExSelection::null; // cancel active on grab
736 if(exsel_grab_mouse) {
737 // cancel grab on mouse release
738 Exhibit *ex = exsel_grab_mouse.ex;
739 Vec3 pos = exslot_mouse.node.get_position();
741 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
743 exslot_mouse.detach_exhibit();
745 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
747 debug_log("no empty slot nearby\n");
748 if(ex->prev_slot && ex->prev_slot->empty()) {
749 slot = ex->prev_slot;
750 debug_log("using previous slot");
755 slot->attach_exhibit(ex);
757 // nowhere to put it, so stash it for later
758 exslot_mouse.detach_exhibit();
759 exman->stash_exhibit(ex);
760 debug_log("no slots available, stashing\n");
763 exsel_grab_mouse = ExSelection::null;
766 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
767 exsel_active = sel; // select or deselect active exhibit
769 debug_log("selecting...\n");
771 debug_log("deselecting...\n");
775 press_x = press_y = INT_MIN;
780 static inline void mouse_look(float dx, float dy)
782 float scrsz = (float)win_height;
783 avatar.body_rot += dx * 512.0 / scrsz;
784 avatar.head_alt += dy * 512.0 / scrsz;
786 if(avatar.head_alt < -90) avatar.head_alt = -90;
787 if(avatar.head_alt > 90) avatar.head_alt = 90;
790 static void mouse_zoom(float dx, float dy)
792 cam_dist += dy * 0.1;
793 if(cam_dist < 0.0) cam_dist = 0.0;
796 void app_mouse_motion(int x, int y)
799 debug_gui_mmotion(x, y);
800 return; // ignore mouse events while GUI is visible
803 int dx = x - prev_mx;
804 int dy = y - prev_my;
808 if(!dx && !dy) return;
810 if(exsel_grab_mouse) {
811 Vec3 pos = exslot_mouse.node.get_node_position();
812 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
814 exslot_mouse.node.set_position(pos + dir);
822 void app_mouse_delta(int dx, int dy)
825 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
827 mouse_look(dx * mouse_speed, dy * mouse_speed);
831 void app_mouse_wheel(int dir)
834 debug_gui_wheel(dir);
838 void app_gamepad_axis(int axis, float val)
857 void app_gamepad_button(int bn, bool pressed)
859 gpad_bnstate[bn] = pressed;
868 show_blobs = !show_blobs;
869 show_message("blobs: %s\n", show_blobs ? "on" : "off");
874 show_message("VR recenter\n");
883 static void toggle_flight()
885 static float prev_walk_speed = -1.0;
886 if(prev_walk_speed < 0.0) {
888 prev_walk_speed = walk_speed;
890 show_message("fly mode\n");
893 walk_speed = prev_walk_speed;
894 prev_walk_speed = -1.0;
895 show_message("walk mode\n");
899 static void calc_framerate()
903 static long prev_upd;
905 long elapsed = time_msec - prev_upd;
906 if(elapsed >= 1000) {
907 framerate = (float)nframes / (float)(elapsed * 0.001);
909 prev_upd = time_msec;
912 printf("fps: %f\n", framerate);
920 static Ray calc_pick_ray(int x, int y)
922 float nx = (float)x / (float)win_width;
923 float ny = (float)(win_height - y) / (float)win_height;
925 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
926 return last_pick_ray;