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;
87 static bool post_scene_init_pending = true;
90 bool app_init(int argc, char **argv)
92 set_log_file("demo.log");
94 char *env = getenv("FPEXCEPT");
95 if(env && atoi(env)) {
96 info_log("enabling floating point exceptions\n");
101 if(init_opengl() == -1) {
105 if(!init_options(argc, argv, "demo.conf")) {
108 app_resize(opt.width, opt.height);
109 app_fullscreen(opt.fullscreen);
112 if(goatvr_init() == -1) {
115 goatvr_set_origin_mode(GOATVR_HEAD);
116 goatvr_set_units_scale(100.0f);
119 should_swap = goatvr_should_swap() != 0;
120 user_eye_height = goatvr_get_eye_height();
121 have_headtracking = goatvr_have_headtracking();
122 have_handtracking = goatvr_have_handtracking();
129 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
130 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
132 glEnable(GL_FRAMEBUFFER_SRGB);
138 glEnable(GL_MULTISAMPLE);
139 glEnable(GL_DEPTH_TEST);
140 glEnable(GL_CULL_FACE);
141 glEnable(GL_NORMALIZE);
143 if(!init_debug_gui()) {
147 Mesh::use_custom_sdr_attr = false;
149 float ambient[] = {0.0, 0.0, 0.0, 0.0};
150 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
154 if(!init_vrhands()) {
158 mscn = new MetaScene;
159 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
163 avatar.pos = mscn->start_pos;
164 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
166 avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
168 exman = new ExhibitManager;
170 if(!exman->load(mscn, "data/exhibits")) {
175 error_log("failed to initialize exhibit ui system\n");
178 exui_setnode(&exslot_left.node);
180 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
183 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
184 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
186 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
189 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
190 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
193 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
197 rend->set_scene(mscn);
201 if(opt.vr || opt.fullscreen) {
202 app_grab_mouse(true);
205 if(mscn->music && opt.music) {
206 mscn->music->play(AUDIO_PLAYMODE_LOOP);
211 static void post_scene_init()
213 exman->load(mscn, "data/exhibits");
223 app_grab_mouse(false);
233 /* this must be destroyed before the scene graph to detach exhibit nodes
234 * before the scene tries to delete them recursively
244 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
246 Mesh *wm = mscn->walk_mesh;
252 Ray downray = Ray(v, Vec3(0, -1, 0));
254 if(mscn->walk_mesh->intersect(downray, &hit)) {
256 newv->y += user_eye_height;
262 static void update(float dt)
267 if(post_scene_init_pending && !sceneman.pending()) {
268 post_scene_init_pending = false;
276 float speed = walk_speed * dt;
280 float jdeadsq = joy_deadzone * joy_deadzone;
281 float jmove_lensq = length_sq(joy_move);
282 float jlook_lensq = length_sq(joy_look);
284 if(jmove_lensq > jdeadsq) {
285 float len = sqrt(jmove_lensq);
286 jmove_lensq -= jdeadsq;
288 float mag = len * len;
289 dir.x += mag * joy_move.x / len * 2.0 * speed;
290 dir.z += mag * joy_move.y / len * 2.0 * speed;
292 if(jlook_lensq > jdeadsq) {
293 float len = sqrt(jlook_lensq);
294 jlook_lensq -= jdeadsq;
296 float mag = len * len;
297 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
298 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
299 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
300 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
304 if(keystate[(int)'w']) {
307 if(keystate[(int)'s']) {
310 if(keystate[(int)'d']) {
313 if(keystate[(int)'a']) {
316 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
317 avatar.pos.y += speed;
319 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
320 avatar.pos.y -= speed;
323 float theta = M_PI * avatar.body_rot / 180.0f;
325 newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
326 newpos.y = avatar.pos.y;
327 newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
332 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
333 float dtheta = M_PI / 32.0;
334 float theta = dtheta;
335 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
337 for(int i=0; i<16; i++) {
338 Vec2 dvec = rotate(dir2d, theta);
339 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
340 if(constrain_walk_mesh(pos, &avatar.pos)) {
343 dvec = rotate(dir2d, -theta);
344 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
345 if(constrain_walk_mesh(pos, &avatar.pos)) {
351 floor_y = avatar.pos.y - user_eye_height;
354 // TODO move to the avatar system
355 // calculate mouselook view matrix
356 mouse_view_matrix = Mat4::identity;
357 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
358 if(!have_headtracking) {
359 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
361 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
362 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
365 // update hand-tracking
366 if(have_handtracking) {
367 update_vrhands(&avatar);
369 if(vrhand[0].valid) {
370 exslot_left.node.set_position(vrhand[0].pos);
371 exslot_left.node.set_rotation(vrhand[0].rot);
373 // right hand takes precedence for hover
374 if(!exsel_grab_left && !exsel_hover) {
375 exsel_hover = exman->select(Sphere(vrhand[0].pos, 5));
378 if(vrhand[1].valid) {
379 exslot_right.node.set_position(vrhand[1].pos);
380 exslot_right.node.set_rotation(vrhand[1].rot);
382 if(!exsel_grab_right) {
383 exsel_hover = exman->select(Sphere(vrhand[1].pos, 5));
388 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
389 if(!exsel_grab_mouse) {
390 Ray ray = calc_pick_ray(prev_mx, prev_my);
391 exsel_hover = exman->select(ray);
394 // TODO do this properly
395 // set the position of the left hand at a suitable position for the exhibit UI
396 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
397 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
400 if(!exslot_right.empty()) exslot_right.node.update(dt);
401 // always update the left slot, because it's the anchor point of the exhibit ui
402 exslot_left.node.update(dt);
407 float dt = (float)(time_msec - prev_msec) / 1000.0f;
408 prev_msec = time_msec;
411 ImGui::GetIOPtr()->DeltaTime = dt;
414 ImGui::ShowTestWindow();
417 glClearColor(1, 1, 1, 1);
422 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
426 for(int i=0; i<2; i++) {
430 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
431 glMatrixMode(GL_PROJECTION);
432 glLoadMatrixf(proj_matrix[0]);
434 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
435 glMatrixMode(GL_MODELVIEW);
436 glLoadMatrixf(view_matrix[0]);
439 if(have_handtracking) {
454 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
458 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
459 glMatrixMode(GL_PROJECTION);
460 glLoadMatrixf(proj_matrix[0]);
462 view_matrix = mouse_view_matrix;
463 glMatrixMode(GL_MODELVIEW);
464 glLoadMatrixf(view_matrix[0]);
468 if(!fb_srgb && sdr_post_gamma) {
469 slow_post(sdr_post_gamma);
478 assert(glGetError() == GL_NO_ERROR);
484 static void draw_scene()
489 if(have_handtracking) {
490 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
491 Mat4 head_dir_xform = head_xform.upper3x3();
494 glPushAttrib(GL_ENABLE_BIT);
495 glDisable(GL_LIGHTING);
497 for(int i=0; i<2; i++) {
498 if(vrhand[i].valid) {
499 glColor3f(i, 1 - i, i);
501 glColor3f(0.5, 0.5, 0.5);
503 Vec3 v = head_xform * vrhand[i].pos;
504 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), vrhand[i].rot) * 20.0f;
505 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), vrhand[i].rot) * 10.0f;
506 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), vrhand[i].rot) * 10.0f;
508 glVertex3f(v.x, v.y, v.z);
509 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
510 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
511 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
512 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
513 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
519 if(debug_gui && dbg_sel_node) {
520 AABox bvol = dbg_sel_node->get_bounds();
521 draw_geom_object(&bvol);
524 if(show_walk_mesh && mscn->walk_mesh) {
525 glPushAttrib(GL_ENABLE_BIT);
527 glBlendFunc(GL_ONE, GL_ONE);
528 glEnable(GL_POLYGON_OFFSET_FILL);
532 glPolygonOffset(-1, 1);
535 glColor3f(0.3, 0.08, 0.01);
536 mscn->walk_mesh->draw();
545 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
550 void app_reshape(int x, int y)
552 glViewport(0, 0, x, y);
553 goatvr_set_fb_size(x, y, 1.0f);
554 debug_gui_reshape(x, y);
557 void app_keyboard(int key, bool pressed)
559 unsigned int mod = app_get_modifiers();
561 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
562 debug_gui_key(key, pressed, mod);
563 return; // ignore all keystrokes when GUI is visible
575 app_toggle_fullscreen();
580 debug_gui = !debug_gui;
581 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
585 app_toggle_grab_mouse();
586 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
591 show_walk_mesh = !show_walk_mesh;
592 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
599 show_message(noclip ? "no clip" : "clip");
610 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
616 show_message("walk speed: %g", walk_speed);
621 show_message("walk speed: %g", walk_speed);
626 show_message("mouse speed: %g", mouse_speed);
631 show_message("mouse speed: %g", mouse_speed);
635 show_blobs = !show_blobs;
636 show_message("blobs: %s\n", show_blobs ? "on" : "off");
641 show_message("VR recenter\n");
645 exman->load(mscn, "data/exhibits");
666 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
667 keystate[key] = pressed;
671 void app_mouse_button(int bn, bool pressed, int x, int y)
673 static int press_x, press_y;
676 debug_gui_mbutton(bn, pressed, x, y);
677 return; // ignore mouse events while GUI is visible
682 bnstate[bn] = pressed;
686 Ray ray = calc_pick_ray(x, y);
687 sel = exman->select(ray);
690 if(sel && (app_get_modifiers() & MOD_CTRL)) {
691 exsel_grab_mouse = sel;
692 Vec3 pos = sel.ex->node->get_position();
693 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
694 exslot_mouse.node.set_position(pos);
695 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
696 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
698 exsel_active = ExSelection::null; // cancel active on grab
705 if(exsel_grab_mouse) {
706 // cancel grab on mouse release
707 Exhibit *ex = exsel_grab_mouse.ex;
708 Vec3 pos = exslot_mouse.node.get_position();
710 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
712 exslot_mouse.detach_exhibit();
714 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
716 debug_log("no empty slot nearby\n");
717 if(ex->prev_slot && ex->prev_slot->empty()) {
718 slot = ex->prev_slot;
719 debug_log("using previous slot");
724 slot->attach_exhibit(ex);
726 // nowhere to put it, so stash it for later
727 exslot_mouse.detach_exhibit();
728 exman->stash_exhibit(ex);
729 debug_log("no slots available, stashing\n");
732 exsel_grab_mouse = ExSelection::null;
735 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
736 exsel_active = sel; // select or deselect active exhibit
738 debug_log("selecting...\n");
740 debug_log("deselecting...\n");
744 press_x = press_y = INT_MIN;
749 static inline void mouse_look(float dx, float dy)
751 float scrsz = (float)win_height;
752 avatar.body_rot += dx * 512.0 / scrsz;
753 avatar.head_alt += dy * 512.0 / scrsz;
755 if(avatar.head_alt < -90) avatar.head_alt = -90;
756 if(avatar.head_alt > 90) avatar.head_alt = 90;
759 static void mouse_zoom(float dx, float dy)
761 cam_dist += dy * 0.1;
762 if(cam_dist < 0.0) cam_dist = 0.0;
765 void app_mouse_motion(int x, int y)
768 debug_gui_mmotion(x, y);
769 return; // ignore mouse events while GUI is visible
772 int dx = x - prev_mx;
773 int dy = y - prev_my;
777 if(!dx && !dy) return;
779 if(exsel_grab_mouse) {
780 Vec3 pos = exslot_mouse.node.get_node_position();
781 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
783 exslot_mouse.node.set_position(pos + dir);
791 void app_mouse_delta(int dx, int dy)
794 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
796 mouse_look(dx * mouse_speed, dy * mouse_speed);
800 void app_mouse_wheel(int dir)
803 debug_gui_wheel(dir);
807 void app_gamepad_axis(int axis, float val)
826 void app_gamepad_button(int bn, bool pressed)
828 gpad_bnstate[bn] = pressed;
837 show_blobs = !show_blobs;
838 show_message("blobs: %s\n", show_blobs ? "on" : "off");
843 show_message("VR recenter\n");
852 static void toggle_flight()
854 static float prev_walk_speed = -1.0;
855 if(prev_walk_speed < 0.0) {
857 prev_walk_speed = walk_speed;
859 show_message("fly mode\n");
862 walk_speed = prev_walk_speed;
863 prev_walk_speed = -1.0;
864 show_message("walk mode\n");
868 static void calc_framerate()
872 static long prev_upd;
874 long elapsed = time_msec - prev_upd;
875 if(elapsed >= 1000) {
876 framerate = (float)nframes / (float)(elapsed * 0.001);
878 prev_upd = time_msec;
881 printf("fps: %f\n", framerate);
889 static Ray calc_pick_ray(int x, int y)
891 float nx = (float)x / (float)win_width;
892 float ny = (float)(win_height - y) / (float)win_height;
894 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
895 return last_pick_ray;