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 // use goatvr sticks for joystick input
282 int num_vr_sticks = goatvr_num_sticks();
283 if(num_vr_sticks > 0) {
285 goatvr_stick_pos(0, p);
289 if(num_vr_sticks > 1) {
291 goatvr_stick_pos(1, p);
296 float speed = walk_speed * dt;
300 float jdeadsq = joy_deadzone * joy_deadzone;
301 float jmove_lensq = length_sq(joy_move);
302 float jlook_lensq = length_sq(joy_look);
304 if(jmove_lensq > jdeadsq) {
305 float len = sqrt(jmove_lensq);
306 jmove_lensq -= jdeadsq;
308 float mag = len * len;
309 dir.x += mag * joy_move.x / len * 2.0 * speed;
310 dir.z += mag * joy_move.y / len * 2.0 * speed;
312 if(jlook_lensq > jdeadsq) {
313 float len = sqrt(jlook_lensq);
314 jlook_lensq -= jdeadsq;
316 float mag = len * len;
317 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
318 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
319 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
320 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
324 if(keystate[(int)'w']) {
327 if(keystate[(int)'s']) {
330 if(keystate[(int)'d']) {
333 if(keystate[(int)'a']) {
336 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
337 avatar.pos.y += speed;
339 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
340 avatar.pos.y -= speed;
343 float theta = M_PI * avatar.body_rot / 180.0f;
345 newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
346 newpos.y = avatar.pos.y;
347 newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
352 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
353 float dtheta = M_PI / 32.0;
354 float theta = dtheta;
355 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
357 for(int i=0; i<16; i++) {
358 Vec2 dvec = rotate(dir2d, theta);
359 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
360 if(constrain_walk_mesh(pos, &avatar.pos)) {
363 dvec = rotate(dir2d, -theta);
364 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
365 if(constrain_walk_mesh(pos, &avatar.pos)) {
371 floor_y = avatar.pos.y - user_eye_height;
374 // TODO move to the avatar system
375 // calculate mouselook view matrix
376 mouse_view_matrix = Mat4::identity;
377 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
378 if(!have_headtracking) {
379 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
381 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
382 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
385 // update hand-tracking
386 if(have_handtracking) {
387 update_vrhands(&avatar);
389 if(vrhand[0].valid) {
390 exslot_left.node.set_position(vrhand[0].pos);
391 exslot_left.node.set_rotation(vrhand[0].rot);
393 // right hand takes precedence for hover
394 if(!exsel_grab_left && !exsel_hover) {
395 exsel_hover = exman->select(Sphere(vrhand[0].pos, 5));
398 if(vrhand[1].valid) {
399 exslot_right.node.set_position(vrhand[1].pos);
400 exslot_right.node.set_rotation(vrhand[1].rot);
402 if(!exsel_grab_right) {
403 exsel_hover = exman->select(Sphere(vrhand[1].pos, 5));
408 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
409 if(!exsel_grab_mouse) {
410 Ray ray = calc_pick_ray(prev_mx, prev_my);
411 exsel_hover = exman->select(ray);
414 // TODO do this properly
415 // set the position of the left hand at a suitable position for the exhibit UI
416 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
417 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
420 if(!exslot_right.empty()) exslot_right.node.update(dt);
421 // always update the left slot, because it's the anchor point of the exhibit ui
422 exslot_left.node.update(dt);
427 float dt = (float)(time_msec - prev_msec) / 1000.0f;
428 prev_msec = time_msec;
431 ImGui::GetIOPtr()->DeltaTime = dt;
434 //ImGui::ShowTestWindow();
437 glClearColor(1, 1, 1, 1);
442 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
444 unsigned int gfbo = goatvr_get_fbo();
448 for(int i=0; i<2; i++) {
452 vp_width = goatvr_get_fb_eye_width(i);
453 vp_height = goatvr_get_fb_eye_height(i);
455 // this is a lightweight operation
456 goatvr_rtarg->create_wrap_fbo(gfbo, vp_width, vp_height);
457 push_render_target(goatvr_rtarg, RT_FAKE);
459 vp_width = win_width / 2;
462 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
463 glMatrixMode(GL_PROJECTION);
464 glLoadMatrixf(proj_matrix[0]);
466 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
467 glMatrixMode(GL_MODELVIEW);
468 glLoadMatrixf(view_matrix[0]);
471 if(have_handtracking) {
480 pop_render_target(RT_FAKE);
486 vp_width = win_width;
487 vp_height = win_height;
494 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
498 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
499 glMatrixMode(GL_PROJECTION);
500 glLoadMatrixf(proj_matrix[0]);
502 view_matrix = mouse_view_matrix;
503 glMatrixMode(GL_MODELVIEW);
504 glLoadMatrixf(view_matrix[0]);
508 if(!fb_srgb && sdr_post_gamma) {
509 slow_post(sdr_post_gamma);
518 assert(glGetError() == GL_NO_ERROR);
524 static void draw_scene()
529 if(have_handtracking) {
530 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
531 Mat4 head_dir_xform = head_xform.upper3x3();
534 glPushAttrib(GL_ENABLE_BIT);
535 glDisable(GL_LIGHTING);
537 for(int i=0; i<2; i++) {
538 if(vrhand[i].valid) {
539 glColor3f(i, 1 - i, i);
541 glColor3f(0.5, 0.5, 0.5);
543 Vec3 v = head_xform * vrhand[i].pos;
544 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), vrhand[i].rot) * 20.0f;
545 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), vrhand[i].rot) * 10.0f;
546 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), vrhand[i].rot) * 10.0f;
548 glVertex3f(v.x, v.y, v.z);
549 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
550 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
551 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
552 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
553 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
559 if(debug_gui && dbg_sel_node) {
560 AABox bvol = dbg_sel_node->get_bounds();
561 draw_geom_object(&bvol);
564 if(show_walk_mesh && mscn->walk_mesh) {
565 glPushAttrib(GL_ENABLE_BIT);
567 glBlendFunc(GL_ONE, GL_ONE);
568 glEnable(GL_POLYGON_OFFSET_FILL);
572 glPolygonOffset(-1, 1);
575 glColor3f(0.3, 0.08, 0.01);
576 mscn->walk_mesh->draw();
585 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
590 void app_reshape(int x, int y)
592 glViewport(0, 0, x, y);
593 goatvr_set_fb_size(x, y, 1.0f);
594 debug_gui_reshape(x, y);
600 void app_keyboard(int key, bool pressed)
602 unsigned int mod = app_get_modifiers();
604 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
605 debug_gui_key(key, pressed, mod);
606 return; // ignore all keystrokes when GUI is visible
618 app_toggle_fullscreen();
623 debug_gui = !debug_gui;
624 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
628 app_toggle_grab_mouse();
629 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
634 show_walk_mesh = !show_walk_mesh;
635 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
642 show_message(noclip ? "no clip" : "clip");
653 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
659 show_message("walk speed: %g", walk_speed);
664 show_message("walk speed: %g", walk_speed);
669 show_message("mouse speed: %g", mouse_speed);
674 show_message("mouse speed: %g", mouse_speed);
678 show_blobs = !show_blobs;
679 show_message("blobs: %s\n", show_blobs ? "on" : "off");
684 show_message("VR recenter\n");
707 dbg_key_pending |= 1 << (key - KEY_F5);
712 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
713 keystate[key] = pressed;
717 void app_mouse_button(int bn, bool pressed, int x, int y)
719 static int press_x, press_y;
722 debug_gui_mbutton(bn, pressed, x, y);
723 return; // ignore mouse events while GUI is visible
728 bnstate[bn] = pressed;
732 Ray ray = calc_pick_ray(x, y);
733 sel = exman->select(ray);
736 if(sel && (app_get_modifiers() & MOD_CTRL)) {
737 exsel_grab_mouse = sel;
738 Vec3 pos = sel.ex->node->get_position();
739 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
740 exslot_mouse.node.set_position(pos);
741 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
742 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
744 exsel_active = ExSelection::null; // cancel active on grab
751 if(exsel_grab_mouse) {
752 // cancel grab on mouse release
753 Exhibit *ex = exsel_grab_mouse.ex;
754 Vec3 pos = exslot_mouse.node.get_position();
756 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
758 exslot_mouse.detach_exhibit();
760 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
762 debug_log("no empty slot nearby\n");
763 if(ex->prev_slot && ex->prev_slot->empty()) {
764 slot = ex->prev_slot;
765 debug_log("using previous slot");
770 slot->attach_exhibit(ex);
772 // nowhere to put it, so stash it for later
773 exslot_mouse.detach_exhibit();
774 exman->stash_exhibit(ex);
775 debug_log("no slots available, stashing\n");
778 exsel_grab_mouse = ExSelection::null;
781 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
782 exsel_active = sel; // select or deselect active exhibit
784 debug_log("selecting...\n");
786 debug_log("deselecting...\n");
790 press_x = press_y = INT_MIN;
795 static inline void mouse_look(float dx, float dy)
797 float scrsz = (float)win_height;
798 avatar.body_rot += dx * 512.0 / scrsz;
799 avatar.head_alt += dy * 512.0 / scrsz;
801 if(avatar.head_alt < -90) avatar.head_alt = -90;
802 if(avatar.head_alt > 90) avatar.head_alt = 90;
805 static void mouse_zoom(float dx, float dy)
807 cam_dist += dy * 0.1;
808 if(cam_dist < 0.0) cam_dist = 0.0;
811 void app_mouse_motion(int x, int y)
814 debug_gui_mmotion(x, y);
815 return; // ignore mouse events while GUI is visible
818 int dx = x - prev_mx;
819 int dy = y - prev_my;
823 if(!dx && !dy) return;
825 if(exsel_grab_mouse) {
826 Vec3 pos = exslot_mouse.node.get_node_position();
827 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
829 exslot_mouse.node.set_position(pos + dir);
837 void app_mouse_delta(int dx, int dy)
840 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
842 mouse_look(dx * mouse_speed, dy * mouse_speed);
846 void app_mouse_wheel(int dir)
849 debug_gui_wheel(dir);
853 void app_gamepad_axis(int axis, float val)
872 void app_gamepad_button(int bn, bool pressed)
874 gpad_bnstate[bn] = pressed;
883 show_blobs = !show_blobs;
884 show_message("blobs: %s\n", show_blobs ? "on" : "off");
889 show_message("VR recenter\n");
898 static void toggle_flight()
900 static float prev_walk_speed = -1.0;
901 if(prev_walk_speed < 0.0) {
903 prev_walk_speed = walk_speed;
905 show_message("fly mode\n");
908 walk_speed = prev_walk_speed;
909 prev_walk_speed = -1.0;
910 show_message("walk mode\n");
914 static void calc_framerate()
918 static long prev_upd;
920 long elapsed = time_msec - prev_upd;
921 if(elapsed >= 1000) {
922 framerate = (float)nframes / (float)(elapsed * 0.001);
924 prev_upd = time_msec;
927 printf("fps: %f\n", framerate);
935 static Ray calc_pick_ray(int x, int y)
937 float nx = (float)x / (float)win_width;
938 float ny = (float)(win_height - y) / (float)win_height;
940 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
941 return last_pick_ray;