12 #include "metascene.h"
21 #include "blob_exhibit.h"
26 #define FAR_CLIP 10000.0
28 static void draw_scene();
29 static void toggle_flight();
30 static void calc_framerate();
31 static Ray calc_pick_ray(int x, int y);
34 int win_width, win_height;
37 bool opt_gear_wireframe;
42 unsigned int sdr_ltmap, sdr_ltmap_notex;
48 static float cam_dist = 0.0;
49 static float floor_y; // last floor height
50 static float user_eye_height = 165;
52 static float walk_speed = 300.0f;
53 static float mouse_speed = 0.5f;
54 static bool show_walk_mesh, noclip = false;
56 static bool have_headtracking, have_handtracking, should_swap;
58 static int prev_mx, prev_my;
59 static bool bnstate[8];
60 static bool keystate[256];
61 static bool gpad_bnstate[64];
62 static Vec2 joy_move, joy_look;
63 static float joy_deadzone = 0.01;
65 static float framerate;
67 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
68 static MetaScene *mscn;
69 static unsigned int sdr_post_gamma;
71 static long prev_msec;
73 static ExhibitManager *exman;
74 static BlobExhibit *blobs;
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);
150 glClearColor(1, 1, 1, 1);
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 blobs = new BlobExhibit;
176 blobs->node = new SceneNode;
178 blobs->node->set_position(Vec3(-680, 160, -100));
179 blobs->node->set_scaling(Vec3(28, 28, 28));
180 blobs->node->update(0);
184 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
187 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
188 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
190 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
193 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
194 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
197 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
201 rend->set_scene(mscn);
205 if(opt.vr || opt.fullscreen) {
206 app_grab_mouse(true);
209 if(mscn->music && opt.music) {
210 mscn->music->play(AUDIO_PLAYMODE_LOOP);
222 app_grab_mouse(false);
230 /* this must be destroyed before the scene graph to detach exhibit nodes
231 * before the scene tries to delete them recursively
241 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
243 Mesh *wm = mscn->walk_mesh;
249 Ray downray = Ray(v, Vec3(0, -1, 0));
251 if(mscn->walk_mesh->intersect(downray, &hit)) {
253 newv->y += user_eye_height;
259 static void update(float dt)
267 float speed = walk_speed * dt;
271 float jdeadsq = joy_deadzone * joy_deadzone;
272 float jmove_lensq = length_sq(joy_move);
273 float jlook_lensq = length_sq(joy_look);
275 if(jmove_lensq > jdeadsq) {
276 float len = sqrt(jmove_lensq);
277 jmove_lensq -= jdeadsq;
279 float mag = len * len;
280 dir.x += mag * joy_move.x / len * 2.0 * speed;
281 dir.z += mag * joy_move.y / len * 2.0 * speed;
283 if(jlook_lensq > jdeadsq) {
284 float len = sqrt(jlook_lensq);
285 jlook_lensq -= jdeadsq;
287 float mag = len * len;
288 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
289 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
290 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
291 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
295 if(keystate[(int)'w']) {
298 if(keystate[(int)'s']) {
301 if(keystate[(int)'d']) {
304 if(keystate[(int)'a']) {
307 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
308 avatar.pos.y += speed;
310 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
311 avatar.pos.y -= speed;
314 float theta = M_PI * avatar.body_rot / 180.0f;
316 newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
317 newpos.y = avatar.pos.y;
318 newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
323 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
324 float dtheta = M_PI / 32.0;
325 float theta = dtheta;
326 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
328 for(int i=0; i<16; i++) {
329 Vec2 dvec = rotate(dir2d, theta);
330 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
331 if(constrain_walk_mesh(pos, &avatar.pos)) {
334 dvec = rotate(dir2d, -theta);
335 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
336 if(constrain_walk_mesh(pos, &avatar.pos)) {
342 floor_y = avatar.pos.y - user_eye_height;
345 // TODO move to the avatar system
346 // calculate mouselook view matrix
347 mouse_view_matrix = Mat4::identity;
348 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
349 if(!have_headtracking) {
350 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
352 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
353 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
355 // check if an exhibit is hovered-over by mouse or 6dof (only if we don't have one grabbed)
356 if(!exsel_grab_mouse) {
357 // XXX note: using previous view/proj matrix lattency shouldn't be an issue but
358 // make sure state-creep doesn't get us
359 // XXX also this mouse-picking probably should only be active in non-VR mode
360 Ray ray = calc_pick_ray(prev_mx, prev_my);
361 exsel_hover = exman->select(ray);
364 if(!exslot_left.empty()) exslot_left.node.update(dt);
365 if(!exslot_right.empty()) exslot_right.node.update(dt);
367 // update hand-tracking
368 if(have_handtracking) {
369 update_vrhands(&avatar);
375 float dt = (float)(time_msec - prev_msec) / 1000.0f;
376 prev_msec = time_msec;
379 ImGui::GetIOPtr()->DeltaTime = dt;
382 ImGui::ShowTestWindow();
388 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
392 for(int i=0; i<2; i++) {
396 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
397 glMatrixMode(GL_PROJECTION);
398 glLoadMatrixf(proj_matrix[0]);
400 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
401 glMatrixMode(GL_MODELVIEW);
402 glLoadMatrixf(view_matrix[0]);
405 if(have_handtracking) {
420 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
424 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
425 glMatrixMode(GL_PROJECTION);
426 glLoadMatrixf(proj_matrix[0]);
428 view_matrix = mouse_view_matrix;
429 glMatrixMode(GL_MODELVIEW);
430 glLoadMatrixf(view_matrix[0]);
434 if(!fb_srgb && sdr_post_gamma) {
435 slow_post(sdr_post_gamma);
444 assert(glGetError() == GL_NO_ERROR);
450 static void draw_scene()
456 if(have_handtracking) {
457 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
458 Mat4 head_dir_xform = head_xform.upper3x3();
461 glPushAttrib(GL_ENABLE_BIT);
462 glDisable(GL_LIGHTING);
464 for(int i=0; i<2; i++) {
466 glColor3f(i, 1 - i, i);
468 glColor3f(0.5, 0.5, 0.5);
470 Vec3 v = head_xform * hand[i].pos;
471 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), hand[i].rot) * 20.0f;
472 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), hand[i].rot) * 10.0f;
473 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), hand[i].rot) * 10.0f;
475 glVertex3f(v.x, v.y, v.z);
476 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
477 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
478 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
479 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
480 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
487 if(debug_gui && dbg_sel_node) {
488 AABox bvol = dbg_sel_node->get_bounds();
489 draw_geom_object(&bvol);
492 if(show_walk_mesh && mscn->walk_mesh) {
493 glPushAttrib(GL_ENABLE_BIT);
495 glBlendFunc(GL_ONE, GL_ONE);
496 glEnable(GL_POLYGON_OFFSET_FILL);
500 glPolygonOffset(-1, 1);
503 glColor3f(0.3, 0.08, 0.01);
504 mscn->walk_mesh->draw();
511 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
516 void app_reshape(int x, int y)
518 glViewport(0, 0, x, y);
519 goatvr_set_fb_size(x, y, 1.0f);
520 debug_gui_reshape(x, y);
523 void app_keyboard(int key, bool pressed)
525 unsigned int mod = app_get_modifiers();
527 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
528 debug_gui_key(key, pressed, mod);
529 return; // ignore all keystrokes when GUI is visible
541 app_toggle_fullscreen();
546 debug_gui = !debug_gui;
547 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
551 app_toggle_grab_mouse();
552 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
557 show_walk_mesh = !show_walk_mesh;
558 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
565 show_message(noclip ? "no clip" : "clip");
576 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
582 show_message("walk speed: %g", walk_speed);
587 show_message("walk speed: %g", walk_speed);
592 show_message("mouse speed: %g", mouse_speed);
597 show_message("mouse speed: %g", mouse_speed);
601 show_blobs = !show_blobs;
602 show_message("blobs: %s\n", show_blobs ? "on" : "off");
607 show_message("VR recenter\n");
611 exman->load(mscn, "data/exhibits");
616 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
617 keystate[key] = pressed;
621 void app_mouse_button(int bn, bool pressed, int x, int y)
623 static int press_x, press_y;
626 debug_gui_mbutton(bn, pressed, x, y);
627 return; // ignore mouse events while GUI is visible
632 bnstate[bn] = pressed;
636 Ray ray = calc_pick_ray(x, y);
637 sel = exman->select(ray);
640 if(sel && (app_get_modifiers() & MOD_CTRL)) {
641 exsel_grab_mouse = sel;
642 Vec3 pos = sel.ex->node->get_position();
643 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
644 exslot_mouse.node.set_position(pos);
645 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
646 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
648 exsel_active = ExSelection::null; // cancel active on grab
655 if(exsel_grab_mouse) {
656 // cancel grab on mouse release
657 Exhibit *ex = exsel_grab_mouse.ex;
658 Vec3 pos = exslot_mouse.node.get_position();
660 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
662 exslot_mouse.detach_exhibit();
664 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
666 debug_log("no empty slot nearby\n");
667 if(ex->prev_slot && ex->prev_slot->empty()) {
668 slot = ex->prev_slot;
669 debug_log("using previous slot");
674 slot->attach_exhibit(ex);
676 // nowhere to put it, so stash it for later
677 exslot_mouse.detach_exhibit();
678 exman->stash_exhibit(ex);
679 debug_log("no slots available, stashing\n");
682 exsel_grab_mouse = ExSelection::null;
685 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
686 exsel_active = sel; // select or deselect active exhibit
688 debug_log("selecting...\n");
690 debug_log("deselecting...\n");
694 press_x = press_y = INT_MIN;
699 static inline void mouse_look(float dx, float dy)
701 float scrsz = (float)win_height;
702 avatar.body_rot += dx * 512.0 / scrsz;
703 avatar.head_alt += dy * 512.0 / scrsz;
705 if(avatar.head_alt < -90) avatar.head_alt = -90;
706 if(avatar.head_alt > 90) avatar.head_alt = 90;
709 static void mouse_zoom(float dx, float dy)
711 cam_dist += dy * 0.1;
712 if(cam_dist < 0.0) cam_dist = 0.0;
715 void app_mouse_motion(int x, int y)
718 debug_gui_mmotion(x, y);
719 return; // ignore mouse events while GUI is visible
722 int dx = x - prev_mx;
723 int dy = y - prev_my;
727 if(!dx && !dy) return;
729 if(exsel_grab_mouse) {
730 Vec3 pos = exslot_mouse.node.get_node_position();
731 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
733 exslot_mouse.node.set_position(pos + dir);
741 void app_mouse_delta(int dx, int dy)
744 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
746 mouse_look(dx * mouse_speed, dy * mouse_speed);
750 void app_mouse_wheel(int dir)
753 debug_gui_wheel(dir);
757 void app_gamepad_axis(int axis, float val)
776 void app_gamepad_button(int bn, bool pressed)
778 gpad_bnstate[bn] = pressed;
787 show_blobs = !show_blobs;
788 show_message("blobs: %s\n", show_blobs ? "on" : "off");
793 show_message("VR recenter\n");
802 static void toggle_flight()
804 static float prev_walk_speed = -1.0;
805 if(prev_walk_speed < 0.0) {
807 prev_walk_speed = walk_speed;
809 show_message("fly mode\n");
812 walk_speed = prev_walk_speed;
813 prev_walk_speed = -1.0;
814 show_message("walk mode\n");
818 static void calc_framerate()
822 static long prev_upd;
824 long elapsed = time_msec - prev_upd;
825 if(elapsed >= 1000) {
826 framerate = (float)nframes / (float)(elapsed * 0.001);
828 prev_upd = time_msec;
831 printf("fps: %f\n", framerate);
839 static Ray calc_pick_ray(int x, int y)
841 float nx = (float)x / (float)win_width;
842 float ny = (float)(win_height - y) / (float)win_height;
844 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
845 return last_pick_ray;