12 #include "metascene.h"
21 #include "blob_exhibit.h"
24 #include "ui_exhibit.h"
27 #define FAR_CLIP 10000.0
29 static void draw_scene();
30 static void toggle_flight();
31 static void calc_framerate();
32 static Ray calc_pick_ray(int x, int y);
35 int win_width, win_height;
38 bool opt_gear_wireframe;
43 unsigned int sdr_ltmap, sdr_ltmap_notex;
49 static float cam_dist = 0.0;
50 static float floor_y; // last floor height
51 static float user_eye_height = 165;
53 static float walk_speed = 300.0f;
54 static float mouse_speed = 0.5f;
55 static bool show_walk_mesh, noclip = false;
57 static bool have_headtracking, have_handtracking, should_swap;
59 static int prev_mx, prev_my;
60 static bool bnstate[8];
61 static bool keystate[256];
62 static bool gpad_bnstate[64];
63 static Vec2 joy_move, joy_look;
64 static float joy_deadzone = 0.01;
66 static float framerate;
68 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
69 static MetaScene *mscn;
70 static unsigned int sdr_post_gamma;
72 static long prev_msec;
74 static ExhibitManager *exman;
75 static bool show_blobs;
77 ExSelection exsel_active, exsel_hover;
78 ExSelection exsel_grab_left, exsel_grab_right;
79 #define exsel_grab_mouse exsel_grab_right
80 static ExhibitSlot exslot_left, exslot_right;
81 #define exslot_mouse exslot_right
83 static Renderer *rend;
85 static Ray last_pick_ray;
88 bool app_init(int argc, char **argv)
90 set_log_file("demo.log");
92 char *env = getenv("FPEXCEPT");
93 if(env && atoi(env)) {
94 info_log("enabling floating point exceptions\n");
99 if(init_opengl() == -1) {
103 if(!init_options(argc, argv, "demo.conf")) {
106 app_resize(opt.width, opt.height);
107 app_fullscreen(opt.fullscreen);
110 if(goatvr_init() == -1) {
113 goatvr_set_origin_mode(GOATVR_HEAD);
114 goatvr_set_units_scale(100.0f);
117 should_swap = goatvr_should_swap() != 0;
118 user_eye_height = goatvr_get_eye_height();
119 have_headtracking = goatvr_have_headtracking();
120 have_handtracking = goatvr_have_handtracking();
127 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
128 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
130 glEnable(GL_FRAMEBUFFER_SRGB);
136 glEnable(GL_MULTISAMPLE);
137 glEnable(GL_DEPTH_TEST);
138 glEnable(GL_CULL_FACE);
139 glEnable(GL_NORMALIZE);
141 if(!init_debug_gui()) {
145 Mesh::use_custom_sdr_attr = false;
147 float ambient[] = {0.0, 0.0, 0.0, 0.0};
148 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
152 if(!init_vrhands()) {
156 mscn = new MetaScene;
157 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
161 avatar.pos = mscn->start_pos;
162 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
164 avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
166 exman = new ExhibitManager;
168 if(!exman->load(mscn, "data/exhibits")) {
173 error_log("failed to initialize exhibit ui system\n");
176 exui_setnode(&exslot_left.node);
178 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
181 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
182 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
184 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
187 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
188 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
191 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
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);
216 app_grab_mouse(false);
226 /* this must be destroyed before the scene graph to detach exhibit nodes
227 * before the scene tries to delete them recursively
237 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
239 Mesh *wm = mscn->walk_mesh;
245 Ray downray = Ray(v, Vec3(0, -1, 0));
247 if(mscn->walk_mesh->intersect(downray, &hit)) {
249 newv->y += user_eye_height;
255 static void update(float dt)
264 float speed = walk_speed * dt;
268 float jdeadsq = joy_deadzone * joy_deadzone;
269 float jmove_lensq = length_sq(joy_move);
270 float jlook_lensq = length_sq(joy_look);
272 if(jmove_lensq > jdeadsq) {
273 float len = sqrt(jmove_lensq);
274 jmove_lensq -= jdeadsq;
276 float mag = len * len;
277 dir.x += mag * joy_move.x / len * 2.0 * speed;
278 dir.z += mag * joy_move.y / len * 2.0 * speed;
280 if(jlook_lensq > jdeadsq) {
281 float len = sqrt(jlook_lensq);
282 jlook_lensq -= jdeadsq;
284 float mag = len * len;
285 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
286 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
287 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
288 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
292 if(keystate[(int)'w']) {
295 if(keystate[(int)'s']) {
298 if(keystate[(int)'d']) {
301 if(keystate[(int)'a']) {
304 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
305 avatar.pos.y += speed;
307 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
308 avatar.pos.y -= speed;
311 float theta = M_PI * avatar.body_rot / 180.0f;
313 newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
314 newpos.y = avatar.pos.y;
315 newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
320 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
321 float dtheta = M_PI / 32.0;
322 float theta = dtheta;
323 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
325 for(int i=0; i<16; i++) {
326 Vec2 dvec = rotate(dir2d, theta);
327 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
328 if(constrain_walk_mesh(pos, &avatar.pos)) {
331 dvec = rotate(dir2d, -theta);
332 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
333 if(constrain_walk_mesh(pos, &avatar.pos)) {
339 floor_y = avatar.pos.y - user_eye_height;
342 // TODO move to the avatar system
343 // calculate mouselook view matrix
344 mouse_view_matrix = Mat4::identity;
345 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
346 if(!have_headtracking) {
347 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
349 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
350 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
353 // update hand-tracking
354 if(have_handtracking) {
355 update_vrhands(&avatar);
357 if(vrhand[0].valid) {
358 exslot_left.node.set_position(vrhand[0].pos);
359 exslot_left.node.set_rotation(vrhand[0].rot);
361 // right hand takes precedence for hover
362 if(!exsel_grab_left && !exsel_hover) {
363 exsel_hover = exman->select(Sphere(vrhand[0].pos, 5));
366 if(vrhand[1].valid) {
367 exslot_right.node.set_position(vrhand[1].pos);
368 exslot_right.node.set_rotation(vrhand[1].rot);
370 if(!exsel_grab_right) {
371 exsel_hover = exman->select(Sphere(vrhand[1].pos, 5));
376 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
377 if(!exsel_grab_mouse) {
378 Ray ray = calc_pick_ray(prev_mx, prev_my);
379 exsel_hover = exman->select(ray);
382 // TODO do this properly
383 // set the position of the left hand at a suitable position for the exhibit UI
384 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
385 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
388 if(!exslot_right.empty()) exslot_right.node.update(dt);
389 // always update the left slot, because it's the anchor point of the exhibit ui
390 exslot_left.node.update(dt);
395 float dt = (float)(time_msec - prev_msec) / 1000.0f;
396 prev_msec = time_msec;
399 ImGui::GetIOPtr()->DeltaTime = dt;
402 ImGui::ShowTestWindow();
405 glClearColor(1, 1, 1, 1);
410 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
414 for(int i=0; i<2; i++) {
418 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
419 glMatrixMode(GL_PROJECTION);
420 glLoadMatrixf(proj_matrix[0]);
422 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
423 glMatrixMode(GL_MODELVIEW);
424 glLoadMatrixf(view_matrix[0]);
427 if(have_handtracking) {
442 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
446 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
447 glMatrixMode(GL_PROJECTION);
448 glLoadMatrixf(proj_matrix[0]);
450 view_matrix = mouse_view_matrix;
451 glMatrixMode(GL_MODELVIEW);
452 glLoadMatrixf(view_matrix[0]);
456 if(!fb_srgb && sdr_post_gamma) {
457 slow_post(sdr_post_gamma);
466 assert(glGetError() == GL_NO_ERROR);
472 static void draw_scene()
477 if(have_handtracking) {
478 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
479 Mat4 head_dir_xform = head_xform.upper3x3();
482 glPushAttrib(GL_ENABLE_BIT);
483 glDisable(GL_LIGHTING);
485 for(int i=0; i<2; i++) {
486 if(vrhand[i].valid) {
487 glColor3f(i, 1 - i, i);
489 glColor3f(0.5, 0.5, 0.5);
491 Vec3 v = head_xform * vrhand[i].pos;
492 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), vrhand[i].rot) * 20.0f;
493 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), vrhand[i].rot) * 10.0f;
494 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), vrhand[i].rot) * 10.0f;
496 glVertex3f(v.x, v.y, v.z);
497 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
498 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
499 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
500 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
501 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
507 if(debug_gui && dbg_sel_node) {
508 AABox bvol = dbg_sel_node->get_bounds();
509 draw_geom_object(&bvol);
512 if(show_walk_mesh && mscn->walk_mesh) {
513 glPushAttrib(GL_ENABLE_BIT);
515 glBlendFunc(GL_ONE, GL_ONE);
516 glEnable(GL_POLYGON_OFFSET_FILL);
520 glPolygonOffset(-1, 1);
523 glColor3f(0.3, 0.08, 0.01);
524 mscn->walk_mesh->draw();
533 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
538 void app_reshape(int x, int y)
540 glViewport(0, 0, x, y);
541 goatvr_set_fb_size(x, y, 1.0f);
542 debug_gui_reshape(x, y);
545 void app_keyboard(int key, bool pressed)
547 unsigned int mod = app_get_modifiers();
549 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
550 debug_gui_key(key, pressed, mod);
551 return; // ignore all keystrokes when GUI is visible
563 app_toggle_fullscreen();
568 debug_gui = !debug_gui;
569 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
573 app_toggle_grab_mouse();
574 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
579 show_walk_mesh = !show_walk_mesh;
580 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
587 show_message(noclip ? "no clip" : "clip");
598 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
604 show_message("walk speed: %g", walk_speed);
609 show_message("walk speed: %g", walk_speed);
614 show_message("mouse speed: %g", mouse_speed);
619 show_message("mouse speed: %g", mouse_speed);
623 show_blobs = !show_blobs;
624 show_message("blobs: %s\n", show_blobs ? "on" : "off");
629 show_message("VR recenter\n");
633 exman->load(mscn, "data/exhibits");
654 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
655 keystate[key] = pressed;
659 void app_mouse_button(int bn, bool pressed, int x, int y)
661 static int press_x, press_y;
664 debug_gui_mbutton(bn, pressed, x, y);
665 return; // ignore mouse events while GUI is visible
670 bnstate[bn] = pressed;
674 Ray ray = calc_pick_ray(x, y);
675 sel = exman->select(ray);
678 if(sel && (app_get_modifiers() & MOD_CTRL)) {
679 exsel_grab_mouse = sel;
680 Vec3 pos = sel.ex->node->get_position();
681 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
682 exslot_mouse.node.set_position(pos);
683 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
684 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
686 exsel_active = ExSelection::null; // cancel active on grab
693 if(exsel_grab_mouse) {
694 // cancel grab on mouse release
695 Exhibit *ex = exsel_grab_mouse.ex;
696 Vec3 pos = exslot_mouse.node.get_position();
698 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
700 exslot_mouse.detach_exhibit();
702 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
704 debug_log("no empty slot nearby\n");
705 if(ex->prev_slot && ex->prev_slot->empty()) {
706 slot = ex->prev_slot;
707 debug_log("using previous slot");
712 slot->attach_exhibit(ex);
714 // nowhere to put it, so stash it for later
715 exslot_mouse.detach_exhibit();
716 exman->stash_exhibit(ex);
717 debug_log("no slots available, stashing\n");
720 exsel_grab_mouse = ExSelection::null;
723 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
724 exsel_active = sel; // select or deselect active exhibit
726 debug_log("selecting...\n");
728 debug_log("deselecting...\n");
732 press_x = press_y = INT_MIN;
737 static inline void mouse_look(float dx, float dy)
739 float scrsz = (float)win_height;
740 avatar.body_rot += dx * 512.0 / scrsz;
741 avatar.head_alt += dy * 512.0 / scrsz;
743 if(avatar.head_alt < -90) avatar.head_alt = -90;
744 if(avatar.head_alt > 90) avatar.head_alt = 90;
747 static void mouse_zoom(float dx, float dy)
749 cam_dist += dy * 0.1;
750 if(cam_dist < 0.0) cam_dist = 0.0;
753 void app_mouse_motion(int x, int y)
756 debug_gui_mmotion(x, y);
757 return; // ignore mouse events while GUI is visible
760 int dx = x - prev_mx;
761 int dy = y - prev_my;
765 if(!dx && !dy) return;
767 if(exsel_grab_mouse) {
768 Vec3 pos = exslot_mouse.node.get_node_position();
769 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
771 exslot_mouse.node.set_position(pos + dir);
779 void app_mouse_delta(int dx, int dy)
782 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
784 mouse_look(dx * mouse_speed, dy * mouse_speed);
788 void app_mouse_wheel(int dir)
791 debug_gui_wheel(dir);
795 void app_gamepad_axis(int axis, float val)
814 void app_gamepad_button(int bn, bool pressed)
816 gpad_bnstate[bn] = pressed;
825 show_blobs = !show_blobs;
826 show_message("blobs: %s\n", show_blobs ? "on" : "off");
831 show_message("VR recenter\n");
840 static void toggle_flight()
842 static float prev_walk_speed = -1.0;
843 if(prev_walk_speed < 0.0) {
845 prev_walk_speed = walk_speed;
847 show_message("fly mode\n");
850 walk_speed = prev_walk_speed;
851 prev_walk_speed = -1.0;
852 show_message("walk mode\n");
856 static void calc_framerate()
860 static long prev_upd;
862 long elapsed = time_msec - prev_upd;
863 if(elapsed >= 1000) {
864 framerate = (float)nframes / (float)(elapsed * 0.001);
866 prev_upd = time_msec;
869 printf("fps: %f\n", framerate);
877 static Ray calc_pick_ray(int x, int y)
879 float nx = (float)x / (float)win_width;
880 float ny = (float)(win_height - y) / (float)win_height;
882 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
883 return last_pick_ray;