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);
352 // check if an exhibit is hovered-over by mouse or 6dof (only if we don't have one grabbed)
353 if(!exsel_grab_mouse) {
354 // XXX note: using previous view/proj matrix lattency shouldn't be an issue but
355 // make sure state-creep doesn't get us
356 // XXX also this mouse-picking probably should only be active in non-VR mode
357 Ray ray = calc_pick_ray(prev_mx, prev_my);
358 exsel_hover = exman->select(ray);
361 // update hand-tracking
362 if(have_handtracking) {
363 update_vrhands(&avatar);
365 // TODO do this properly
366 // set the position of the left hand at a suitable position for the exhibit UI
367 dir = rotate(Vec3(-0.46, -0.1, -1), Vec3(0, 1, 0), deg_to_rad(-avatar.body_rot));
368 exslot_left.node.set_position(avatar.pos + dir * 30); // magic: distance in front
371 if(!exslot_right.empty()) exslot_right.node.update(dt);
372 // always update the left slot, because it's the anchor point of the exhibit ui
373 exslot_left.node.update(dt);
378 float dt = (float)(time_msec - prev_msec) / 1000.0f;
379 prev_msec = time_msec;
382 ImGui::GetIOPtr()->DeltaTime = dt;
385 ImGui::ShowTestWindow();
388 glClearColor(1, 1, 1, 1);
393 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
397 for(int i=0; i<2; i++) {
401 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
402 glMatrixMode(GL_PROJECTION);
403 glLoadMatrixf(proj_matrix[0]);
405 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
406 glMatrixMode(GL_MODELVIEW);
407 glLoadMatrixf(view_matrix[0]);
410 if(have_handtracking) {
425 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
429 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
430 glMatrixMode(GL_PROJECTION);
431 glLoadMatrixf(proj_matrix[0]);
433 view_matrix = mouse_view_matrix;
434 glMatrixMode(GL_MODELVIEW);
435 glLoadMatrixf(view_matrix[0]);
439 if(!fb_srgb && sdr_post_gamma) {
440 slow_post(sdr_post_gamma);
449 assert(glGetError() == GL_NO_ERROR);
455 static void draw_scene()
461 if(have_handtracking) {
462 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
463 Mat4 head_dir_xform = head_xform.upper3x3();
466 glPushAttrib(GL_ENABLE_BIT);
467 glDisable(GL_LIGHTING);
469 for(int i=0; i<2; i++) {
471 glColor3f(i, 1 - i, i);
473 glColor3f(0.5, 0.5, 0.5);
475 Vec3 v = head_xform * hand[i].pos;
476 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), hand[i].rot) * 20.0f;
477 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), hand[i].rot) * 10.0f;
478 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), hand[i].rot) * 10.0f;
480 glVertex3f(v.x, v.y, v.z);
481 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
482 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
483 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
484 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
485 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
492 if(debug_gui && dbg_sel_node) {
493 AABox bvol = dbg_sel_node->get_bounds();
494 draw_geom_object(&bvol);
497 if(show_walk_mesh && mscn->walk_mesh) {
498 glPushAttrib(GL_ENABLE_BIT);
500 glBlendFunc(GL_ONE, GL_ONE);
501 glEnable(GL_POLYGON_OFFSET_FILL);
505 glPolygonOffset(-1, 1);
508 glColor3f(0.3, 0.08, 0.01);
509 mscn->walk_mesh->draw();
518 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
523 void app_reshape(int x, int y)
525 glViewport(0, 0, x, y);
526 goatvr_set_fb_size(x, y, 1.0f);
527 debug_gui_reshape(x, y);
530 void app_keyboard(int key, bool pressed)
532 unsigned int mod = app_get_modifiers();
534 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
535 debug_gui_key(key, pressed, mod);
536 return; // ignore all keystrokes when GUI is visible
548 app_toggle_fullscreen();
553 debug_gui = !debug_gui;
554 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
558 app_toggle_grab_mouse();
559 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
564 show_walk_mesh = !show_walk_mesh;
565 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
572 show_message(noclip ? "no clip" : "clip");
583 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
589 show_message("walk speed: %g", walk_speed);
594 show_message("walk speed: %g", walk_speed);
599 show_message("mouse speed: %g", mouse_speed);
604 show_message("mouse speed: %g", mouse_speed);
608 show_blobs = !show_blobs;
609 show_message("blobs: %s\n", show_blobs ? "on" : "off");
614 show_message("VR recenter\n");
618 exman->load(mscn, "data/exhibits");
623 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
624 keystate[key] = pressed;
628 void app_mouse_button(int bn, bool pressed, int x, int y)
630 static int press_x, press_y;
633 debug_gui_mbutton(bn, pressed, x, y);
634 return; // ignore mouse events while GUI is visible
639 bnstate[bn] = pressed;
643 Ray ray = calc_pick_ray(x, y);
644 sel = exman->select(ray);
647 if(sel && (app_get_modifiers() & MOD_CTRL)) {
648 exsel_grab_mouse = sel;
649 Vec3 pos = sel.ex->node->get_position();
650 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
651 exslot_mouse.node.set_position(pos);
652 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
653 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
655 exsel_active = ExSelection::null; // cancel active on grab
662 if(exsel_grab_mouse) {
663 // cancel grab on mouse release
664 Exhibit *ex = exsel_grab_mouse.ex;
665 Vec3 pos = exslot_mouse.node.get_position();
667 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
669 exslot_mouse.detach_exhibit();
671 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
673 debug_log("no empty slot nearby\n");
674 if(ex->prev_slot && ex->prev_slot->empty()) {
675 slot = ex->prev_slot;
676 debug_log("using previous slot");
681 slot->attach_exhibit(ex);
683 // nowhere to put it, so stash it for later
684 exslot_mouse.detach_exhibit();
685 exman->stash_exhibit(ex);
686 debug_log("no slots available, stashing\n");
689 exsel_grab_mouse = ExSelection::null;
692 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
693 exsel_active = sel; // select or deselect active exhibit
695 debug_log("selecting...\n");
697 debug_log("deselecting...\n");
701 press_x = press_y = INT_MIN;
706 static inline void mouse_look(float dx, float dy)
708 float scrsz = (float)win_height;
709 avatar.body_rot += dx * 512.0 / scrsz;
710 avatar.head_alt += dy * 512.0 / scrsz;
712 if(avatar.head_alt < -90) avatar.head_alt = -90;
713 if(avatar.head_alt > 90) avatar.head_alt = 90;
716 static void mouse_zoom(float dx, float dy)
718 cam_dist += dy * 0.1;
719 if(cam_dist < 0.0) cam_dist = 0.0;
722 void app_mouse_motion(int x, int y)
725 debug_gui_mmotion(x, y);
726 return; // ignore mouse events while GUI is visible
729 int dx = x - prev_mx;
730 int dy = y - prev_my;
734 if(!dx && !dy) return;
736 if(exsel_grab_mouse) {
737 Vec3 pos = exslot_mouse.node.get_node_position();
738 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
740 exslot_mouse.node.set_position(pos + dir);
748 void app_mouse_delta(int dx, int dy)
751 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
753 mouse_look(dx * mouse_speed, dy * mouse_speed);
757 void app_mouse_wheel(int dir)
760 debug_gui_wheel(dir);
764 void app_gamepad_axis(int axis, float val)
783 void app_gamepad_button(int bn, bool pressed)
785 gpad_bnstate[bn] = pressed;
794 show_blobs = !show_blobs;
795 show_message("blobs: %s\n", show_blobs ? "on" : "off");
800 show_message("VR recenter\n");
809 static void toggle_flight()
811 static float prev_walk_speed = -1.0;
812 if(prev_walk_speed < 0.0) {
814 prev_walk_speed = walk_speed;
816 show_message("fly mode\n");
819 walk_speed = prev_walk_speed;
820 prev_walk_speed = -1.0;
821 show_message("walk mode\n");
825 static void calc_framerate()
829 static long prev_upd;
831 long elapsed = time_msec - prev_upd;
832 if(elapsed >= 1000) {
833 framerate = (float)nframes / (float)(elapsed * 0.001);
835 prev_upd = time_msec;
838 printf("fps: %f\n", framerate);
846 static Ray calc_pick_ray(int x, int y)
848 float nx = (float)x / (float)win_width;
849 float ny = (float)(win_height - y) / (float)win_height;
851 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
852 return last_pick_ray;