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 int num_mir = mscn->calc_mirror_planes();
214 info_log("found %d mirror planes\n", num_mir);
216 exman->load(mscn, "data/exhibits");
226 app_grab_mouse(false);
236 /* this must be destroyed before the scene graph to detach exhibit nodes
237 * before the scene tries to delete them recursively
247 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
249 Mesh *wm = mscn->walk_mesh;
255 Ray downray = Ray(v, Vec3(0, -1, 0));
257 if(mscn->walk_mesh->intersect(downray, &hit)) {
259 newv->y += user_eye_height;
265 static void update(float dt)
270 if(post_scene_init_pending && !sceneman.pending()) {
271 post_scene_init_pending = false;
279 float speed = walk_speed * dt;
283 float jdeadsq = joy_deadzone * joy_deadzone;
284 float jmove_lensq = length_sq(joy_move);
285 float jlook_lensq = length_sq(joy_look);
287 if(jmove_lensq > jdeadsq) {
288 float len = sqrt(jmove_lensq);
289 jmove_lensq -= jdeadsq;
291 float mag = len * len;
292 dir.x += mag * joy_move.x / len * 2.0 * speed;
293 dir.z += mag * joy_move.y / len * 2.0 * speed;
295 if(jlook_lensq > jdeadsq) {
296 float len = sqrt(jlook_lensq);
297 jlook_lensq -= jdeadsq;
299 float mag = len * len;
300 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
301 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
302 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
303 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
307 if(keystate[(int)'w']) {
310 if(keystate[(int)'s']) {
313 if(keystate[(int)'d']) {
316 if(keystate[(int)'a']) {
319 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
320 avatar.pos.y += speed;
322 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
323 avatar.pos.y -= speed;
326 float theta = M_PI * avatar.body_rot / 180.0f;
328 newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
329 newpos.y = avatar.pos.y;
330 newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
335 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
336 float dtheta = M_PI / 32.0;
337 float theta = dtheta;
338 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
340 for(int i=0; i<16; i++) {
341 Vec2 dvec = rotate(dir2d, theta);
342 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
343 if(constrain_walk_mesh(pos, &avatar.pos)) {
346 dvec = rotate(dir2d, -theta);
347 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
348 if(constrain_walk_mesh(pos, &avatar.pos)) {
354 floor_y = avatar.pos.y - user_eye_height;
357 // TODO move to the avatar system
358 // calculate mouselook view matrix
359 mouse_view_matrix = Mat4::identity;
360 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
361 if(!have_headtracking) {
362 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
364 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
365 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
368 // update hand-tracking
369 if(have_handtracking) {
370 update_vrhands(&avatar);
372 if(vrhand[0].valid) {
373 exslot_left.node.set_position(vrhand[0].pos);
374 exslot_left.node.set_rotation(vrhand[0].rot);
376 // right hand takes precedence for hover
377 if(!exsel_grab_left && !exsel_hover) {
378 exsel_hover = exman->select(Sphere(vrhand[0].pos, 5));
381 if(vrhand[1].valid) {
382 exslot_right.node.set_position(vrhand[1].pos);
383 exslot_right.node.set_rotation(vrhand[1].rot);
385 if(!exsel_grab_right) {
386 exsel_hover = exman->select(Sphere(vrhand[1].pos, 5));
391 // check if an exhibit is hovered-over by mouse (only if we don't have one grabbed)
392 if(!exsel_grab_mouse) {
393 Ray ray = calc_pick_ray(prev_mx, prev_my);
394 exsel_hover = exman->select(ray);
397 // TODO do this properly
398 // set the position of the left hand at a suitable position for the exhibit UI
399 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
400 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
403 if(!exslot_right.empty()) exslot_right.node.update(dt);
404 // always update the left slot, because it's the anchor point of the exhibit ui
405 exslot_left.node.update(dt);
410 float dt = (float)(time_msec - prev_msec) / 1000.0f;
411 prev_msec = time_msec;
414 ImGui::GetIOPtr()->DeltaTime = dt;
417 ImGui::ShowTestWindow();
420 glClearColor(1, 1, 1, 1);
425 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
429 for(int i=0; i<2; i++) {
433 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
434 glMatrixMode(GL_PROJECTION);
435 glLoadMatrixf(proj_matrix[0]);
437 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
438 glMatrixMode(GL_MODELVIEW);
439 glLoadMatrixf(view_matrix[0]);
442 if(have_handtracking) {
457 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
461 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
462 glMatrixMode(GL_PROJECTION);
463 glLoadMatrixf(proj_matrix[0]);
465 view_matrix = mouse_view_matrix;
466 glMatrixMode(GL_MODELVIEW);
467 glLoadMatrixf(view_matrix[0]);
471 if(!fb_srgb && sdr_post_gamma) {
472 slow_post(sdr_post_gamma);
481 assert(glGetError() == GL_NO_ERROR);
487 static void draw_scene()
492 if(have_handtracking) {
493 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
494 Mat4 head_dir_xform = head_xform.upper3x3();
497 glPushAttrib(GL_ENABLE_BIT);
498 glDisable(GL_LIGHTING);
500 for(int i=0; i<2; i++) {
501 if(vrhand[i].valid) {
502 glColor3f(i, 1 - i, i);
504 glColor3f(0.5, 0.5, 0.5);
506 Vec3 v = head_xform * vrhand[i].pos;
507 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), vrhand[i].rot) * 20.0f;
508 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), vrhand[i].rot) * 10.0f;
509 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), vrhand[i].rot) * 10.0f;
511 glVertex3f(v.x, v.y, v.z);
512 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
513 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
514 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
515 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
516 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
522 if(debug_gui && dbg_sel_node) {
523 AABox bvol = dbg_sel_node->get_bounds();
524 draw_geom_object(&bvol);
527 if(show_walk_mesh && mscn->walk_mesh) {
528 glPushAttrib(GL_ENABLE_BIT);
530 glBlendFunc(GL_ONE, GL_ONE);
531 glEnable(GL_POLYGON_OFFSET_FILL);
535 glPolygonOffset(-1, 1);
538 glColor3f(0.3, 0.08, 0.01);
539 mscn->walk_mesh->draw();
548 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
553 void app_reshape(int x, int y)
555 glViewport(0, 0, x, y);
556 goatvr_set_fb_size(x, y, 1.0f);
557 debug_gui_reshape(x, y);
560 void app_keyboard(int key, bool pressed)
562 unsigned int mod = app_get_modifiers();
564 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
565 debug_gui_key(key, pressed, mod);
566 return; // ignore all keystrokes when GUI is visible
578 app_toggle_fullscreen();
583 debug_gui = !debug_gui;
584 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
588 app_toggle_grab_mouse();
589 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
594 show_walk_mesh = !show_walk_mesh;
595 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
602 show_message(noclip ? "no clip" : "clip");
613 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
619 show_message("walk speed: %g", walk_speed);
624 show_message("walk speed: %g", walk_speed);
629 show_message("mouse speed: %g", mouse_speed);
634 show_message("mouse speed: %g", mouse_speed);
638 show_blobs = !show_blobs;
639 show_message("blobs: %s\n", show_blobs ? "on" : "off");
644 show_message("VR recenter\n");
665 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
666 keystate[key] = pressed;
670 void app_mouse_button(int bn, bool pressed, int x, int y)
672 static int press_x, press_y;
675 debug_gui_mbutton(bn, pressed, x, y);
676 return; // ignore mouse events while GUI is visible
681 bnstate[bn] = pressed;
685 Ray ray = calc_pick_ray(x, y);
686 sel = exman->select(ray);
689 if(sel && (app_get_modifiers() & MOD_CTRL)) {
690 exsel_grab_mouse = sel;
691 Vec3 pos = sel.ex->node->get_position();
692 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
693 exslot_mouse.node.set_position(pos);
694 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
695 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
697 exsel_active = ExSelection::null; // cancel active on grab
704 if(exsel_grab_mouse) {
705 // cancel grab on mouse release
706 Exhibit *ex = exsel_grab_mouse.ex;
707 Vec3 pos = exslot_mouse.node.get_position();
709 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
711 exslot_mouse.detach_exhibit();
713 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
715 debug_log("no empty slot nearby\n");
716 if(ex->prev_slot && ex->prev_slot->empty()) {
717 slot = ex->prev_slot;
718 debug_log("using previous slot");
723 slot->attach_exhibit(ex);
725 // nowhere to put it, so stash it for later
726 exslot_mouse.detach_exhibit();
727 exman->stash_exhibit(ex);
728 debug_log("no slots available, stashing\n");
731 exsel_grab_mouse = ExSelection::null;
734 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
735 exsel_active = sel; // select or deselect active exhibit
737 debug_log("selecting...\n");
739 debug_log("deselecting...\n");
743 press_x = press_y = INT_MIN;
748 static inline void mouse_look(float dx, float dy)
750 float scrsz = (float)win_height;
751 avatar.body_rot += dx * 512.0 / scrsz;
752 avatar.head_alt += dy * 512.0 / scrsz;
754 if(avatar.head_alt < -90) avatar.head_alt = -90;
755 if(avatar.head_alt > 90) avatar.head_alt = 90;
758 static void mouse_zoom(float dx, float dy)
760 cam_dist += dy * 0.1;
761 if(cam_dist < 0.0) cam_dist = 0.0;
764 void app_mouse_motion(int x, int y)
767 debug_gui_mmotion(x, y);
768 return; // ignore mouse events while GUI is visible
771 int dx = x - prev_mx;
772 int dy = y - prev_my;
776 if(!dx && !dy) return;
778 if(exsel_grab_mouse) {
779 Vec3 pos = exslot_mouse.node.get_node_position();
780 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
782 exslot_mouse.node.set_position(pos + dir);
790 void app_mouse_delta(int dx, int dy)
793 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
795 mouse_look(dx * mouse_speed, dy * mouse_speed);
799 void app_mouse_wheel(int dir)
802 debug_gui_wheel(dir);
806 void app_gamepad_axis(int axis, float val)
825 void app_gamepad_button(int bn, bool pressed)
827 gpad_bnstate[bn] = pressed;
836 show_blobs = !show_blobs;
837 show_message("blobs: %s\n", show_blobs ? "on" : "off");
842 show_message("VR recenter\n");
851 static void toggle_flight()
853 static float prev_walk_speed = -1.0;
854 if(prev_walk_speed < 0.0) {
856 prev_walk_speed = walk_speed;
858 show_message("fly mode\n");
861 walk_speed = prev_walk_speed;
862 prev_walk_speed = -1.0;
863 show_message("walk mode\n");
867 static void calc_framerate()
871 static long prev_upd;
873 long elapsed = time_msec - prev_upd;
874 if(elapsed >= 1000) {
875 framerate = (float)nframes / (float)(elapsed * 0.001);
877 prev_upd = time_msec;
880 printf("fps: %f\n", framerate);
888 static Ray calc_pick_ray(int x, int y)
890 float nx = (float)x / (float)win_width;
891 float ny = (float)(win_height - y) / (float)win_height;
893 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
894 return last_pick_ray;