12 #include "metascene.h"
21 #include "blob_exhibit.h"
25 #define FAR_CLIP 10000.0
27 static void draw_scene();
28 static void toggle_flight();
29 static void calc_framerate();
30 static Ray calc_pick_ray(int x, int y);
33 int win_width, win_height;
36 bool opt_gear_wireframe;
41 unsigned int sdr_ltmap, sdr_ltmap_notex;
47 static float cam_dist = 0.0;
48 static float floor_y; // last floor height
49 static float user_eye_height = 165;
51 static float walk_speed = 300.0f;
52 static float mouse_speed = 0.5f;
53 static bool show_walk_mesh, noclip = false;
55 static bool have_headtracking, have_handtracking, should_swap;
57 static int prev_mx, prev_my;
58 static bool bnstate[8];
59 static bool keystate[256];
60 static bool gpad_bnstate[64];
61 static Vec2 joy_move, joy_look;
62 static float joy_deadzone = 0.01;
64 static float framerate;
66 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
67 static MetaScene *mscn;
68 static unsigned int sdr_post_gamma;
70 static long prev_msec;
72 static ExhibitManager *exman;
73 static BlobExhibit *blobs;
74 static bool show_blobs;
76 ExSelection exsel_active, exsel_hover;
77 ExSelection exsel_grab_left, exsel_grab_right;
78 #define exsel_grab_mouse exsel_grab_right
79 static ExhibitSlot exslot_left, exslot_right;
80 #define exslot_mouse exslot_right
82 static Renderer *rend;
84 static Ray last_pick_ray;
87 bool app_init(int argc, char **argv)
89 set_log_file("demo.log");
91 char *env = getenv("FPEXCEPT");
92 if(env && atoi(env)) {
93 info_log("enabling floating point exceptions\n");
98 if(init_opengl() == -1) {
102 if(!init_options(argc, argv, "demo.conf")) {
105 app_resize(opt.width, opt.height);
106 app_fullscreen(opt.fullscreen);
109 if(goatvr_init() == -1) {
112 goatvr_set_origin_mode(GOATVR_HEAD);
113 goatvr_set_units_scale(100.0f);
116 should_swap = goatvr_should_swap() != 0;
117 user_eye_height = goatvr_get_eye_height();
118 have_headtracking = goatvr_have_headtracking();
119 have_handtracking = goatvr_have_handtracking();
126 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
127 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
129 glEnable(GL_FRAMEBUFFER_SRGB);
135 glEnable(GL_MULTISAMPLE);
136 glEnable(GL_DEPTH_TEST);
137 glEnable(GL_CULL_FACE);
138 glEnable(GL_NORMALIZE);
140 if(!init_debug_gui()) {
144 Mesh::use_custom_sdr_attr = false;
146 float ambient[] = {0.0, 0.0, 0.0, 0.0};
147 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
149 glClearColor(1, 1, 1, 1);
153 if(!init_vrhands()) {
157 mscn = new MetaScene;
158 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
162 avatar.pos = mscn->start_pos;
163 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
165 avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
167 exman = new ExhibitManager;
169 if(!exman->load(mscn, "data/exhibits")) {
174 blobs = new BlobExhibit;
175 blobs->node = new SceneNode;
177 blobs->node->set_position(Vec3(-680, 160, -100));
178 blobs->node->set_scaling(Vec3(28, 28, 28));
179 blobs->node->update(0);
183 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
186 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
187 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
189 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
192 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
193 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
196 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
200 rend->set_scene(mscn);
204 if(opt.vr || opt.fullscreen) {
205 app_grab_mouse(true);
208 if(mscn->music && opt.music) {
209 mscn->music->play(AUDIO_PLAYMODE_LOOP);
221 app_grab_mouse(false);
229 /* this must be destroyed before the scene graph to detach exhibit nodes
230 * before the scene tries to delete them recursively
240 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
242 Mesh *wm = mscn->walk_mesh;
248 Ray downray = Ray(v, Vec3(0, -1, 0));
250 if(mscn->walk_mesh->intersect(downray, &hit)) {
252 newv->y += user_eye_height;
258 static void update(float dt)
266 float speed = walk_speed * dt;
270 float jdeadsq = joy_deadzone * joy_deadzone;
271 float jmove_lensq = length_sq(joy_move);
272 float jlook_lensq = length_sq(joy_look);
274 if(jmove_lensq > jdeadsq) {
275 float len = sqrt(jmove_lensq);
276 jmove_lensq -= jdeadsq;
278 float mag = len * len;
279 dir.x += mag * joy_move.x / len * 2.0 * speed;
280 dir.z += mag * joy_move.y / len * 2.0 * speed;
282 if(jlook_lensq > jdeadsq) {
283 float len = sqrt(jlook_lensq);
284 jlook_lensq -= jdeadsq;
286 float mag = len * len;
287 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
288 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
289 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
290 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
294 if(keystate[(int)'w']) {
297 if(keystate[(int)'s']) {
300 if(keystate[(int)'d']) {
303 if(keystate[(int)'a']) {
306 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
307 avatar.pos.y += speed;
309 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
310 avatar.pos.y -= speed;
313 float theta = M_PI * avatar.body_rot / 180.0f;
315 newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
316 newpos.y = avatar.pos.y;
317 newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
322 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
323 float dtheta = M_PI / 32.0;
324 float theta = dtheta;
325 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
327 for(int i=0; i<16; i++) {
328 Vec2 dvec = rotate(dir2d, theta);
329 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
330 if(constrain_walk_mesh(pos, &avatar.pos)) {
333 dvec = rotate(dir2d, -theta);
334 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
335 if(constrain_walk_mesh(pos, &avatar.pos)) {
341 floor_y = avatar.pos.y - user_eye_height;
344 // TODO move to the avatar system
345 // calculate mouselook view matrix
346 mouse_view_matrix = Mat4::identity;
347 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
348 if(!have_headtracking) {
349 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
351 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
352 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
354 // check if an exhibit is hovered-over by mouse or 6dof (only if we don't have one grabbed)
355 if(!exsel_grab_mouse) {
356 // XXX note: using previous view/proj matrix lattency shouldn't be an issue but
357 // make sure state-creep doesn't get us
358 // XXX also this mouse-picking probably should only be active in non-VR mode
359 Ray ray = calc_pick_ray(prev_mx, prev_my);
360 exsel_hover = exman->select(ray);
363 if(!exslot_left.empty()) exslot_left.node.update(dt);
364 if(!exslot_right.empty()) exslot_right.node.update(dt);
366 // update hand-tracking
367 if(have_handtracking) {
368 update_vrhands(&avatar);
374 float dt = (float)(time_msec - prev_msec) / 1000.0f;
375 prev_msec = time_msec;
378 ImGui::GetIOPtr()->DeltaTime = dt;
381 ImGui::ShowTestWindow();
387 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
391 for(int i=0; i<2; i++) {
395 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
396 glMatrixMode(GL_PROJECTION);
397 glLoadMatrixf(proj_matrix[0]);
399 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
400 glMatrixMode(GL_MODELVIEW);
401 glLoadMatrixf(view_matrix[0]);
404 if(have_handtracking) {
419 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
423 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
424 glMatrixMode(GL_PROJECTION);
425 glLoadMatrixf(proj_matrix[0]);
427 view_matrix = mouse_view_matrix;
428 glMatrixMode(GL_MODELVIEW);
429 glLoadMatrixf(view_matrix[0]);
433 if(!fb_srgb && sdr_post_gamma) {
434 slow_post(sdr_post_gamma);
443 assert(glGetError() == GL_NO_ERROR);
449 static void draw_scene()
455 if(have_handtracking) {
456 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
457 Mat4 head_dir_xform = head_xform.upper3x3();
460 glPushAttrib(GL_ENABLE_BIT);
461 glDisable(GL_LIGHTING);
463 for(int i=0; i<2; i++) {
465 glColor3f(i, 1 - i, i);
467 glColor3f(0.5, 0.5, 0.5);
469 Vec3 v = head_xform * hand[i].pos;
470 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), hand[i].rot) * 20.0f;
471 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), hand[i].rot) * 10.0f;
472 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), hand[i].rot) * 10.0f;
474 glVertex3f(v.x, v.y, v.z);
475 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
476 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
477 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
478 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
479 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
486 if(show_walk_mesh && mscn->walk_mesh) {
487 glPushAttrib(GL_ENABLE_BIT);
489 glBlendFunc(GL_ONE, GL_ONE);
490 glEnable(GL_POLYGON_OFFSET_FILL);
494 glPolygonOffset(-1, 1);
497 glColor3f(0.3, 0.08, 0.01);
498 mscn->walk_mesh->draw();
505 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
510 void app_reshape(int x, int y)
512 glViewport(0, 0, x, y);
513 goatvr_set_fb_size(x, y, 1.0f);
514 debug_gui_reshape(x, y);
517 void app_keyboard(int key, bool pressed)
519 unsigned int mod = app_get_modifiers();
521 if(debug_gui && !(pressed && (key == '`' || key == 27))) {
522 debug_gui_key(key, pressed, mod);
523 return; // ignore all keystrokes when GUI is visible
535 app_toggle_fullscreen();
540 debug_gui = !debug_gui;
541 show_message("debug gui %s", debug_gui ? "enabled" : "disabled");
545 app_toggle_grab_mouse();
546 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
551 show_walk_mesh = !show_walk_mesh;
552 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
559 show_message(noclip ? "no clip" : "clip");
570 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
576 show_message("walk speed: %g", walk_speed);
581 show_message("walk speed: %g", walk_speed);
586 show_message("mouse speed: %g", mouse_speed);
591 show_message("mouse speed: %g", mouse_speed);
595 show_blobs = !show_blobs;
596 show_message("blobs: %s\n", show_blobs ? "on" : "off");
601 show_message("VR recenter\n");
605 exman->load(mscn, "data/exhibits");
610 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
611 keystate[key] = pressed;
615 void app_mouse_button(int bn, bool pressed, int x, int y)
617 static int press_x, press_y;
620 debug_gui_mbutton(bn, pressed, x, y);
621 return; // ignore mouse events while GUI is visible
626 bnstate[bn] = pressed;
630 Ray ray = calc_pick_ray(x, y);
631 sel = exman->select(ray);
634 if(sel && (app_get_modifiers() & MOD_CTRL)) {
635 exsel_grab_mouse = sel;
636 Vec3 pos = sel.ex->node->get_position();
637 debug_log("grabbing... (%g %g %g)\n", pos.x, pos.y, pos.z);
638 exslot_mouse.node.set_position(pos);
639 exslot_mouse.node.set_rotation(sel.ex->node->get_rotation());
640 exslot_mouse.attach_exhibit(sel.ex, EXSLOT_ATTACH_TRANSIENT);
642 exsel_active = ExSelection::null; // cancel active on grab
649 if(exsel_grab_mouse) {
650 // cancel grab on mouse release
651 Exhibit *ex = exsel_grab_mouse.ex;
652 Vec3 pos = exslot_mouse.node.get_position();
654 debug_log("releasing at %g %g %g ...\n", pos.x, pos.y, pos.z);
656 exslot_mouse.detach_exhibit();
658 ExhibitSlot *slot = exman->nearest_empty_slot(pos, 100);
660 debug_log("no empty slot nearby\n");
661 if(ex->prev_slot && ex->prev_slot->empty()) {
662 slot = ex->prev_slot;
663 debug_log("using previous slot");
668 slot->attach_exhibit(ex);
670 // nowhere to put it, so stash it for later
671 exslot_mouse.detach_exhibit();
672 exman->stash_exhibit(ex);
673 debug_log("no slots available, stashing\n");
676 exsel_grab_mouse = ExSelection::null;
679 if(abs(press_x - x) < 5 && abs(press_y - y) < 5) {
680 exsel_active = sel; // select or deselect active exhibit
682 debug_log("selecting...\n");
684 debug_log("deselecting...\n");
688 press_x = press_y = INT_MIN;
693 static inline void mouse_look(float dx, float dy)
695 float scrsz = (float)win_height;
696 avatar.body_rot += dx * 512.0 / scrsz;
697 avatar.head_alt += dy * 512.0 / scrsz;
699 if(avatar.head_alt < -90) avatar.head_alt = -90;
700 if(avatar.head_alt > 90) avatar.head_alt = 90;
703 static void mouse_zoom(float dx, float dy)
705 cam_dist += dy * 0.1;
706 if(cam_dist < 0.0) cam_dist = 0.0;
709 void app_mouse_motion(int x, int y)
712 debug_gui_mmotion(x, y);
713 return; // ignore mouse events while GUI is visible
716 int dx = x - prev_mx;
717 int dy = y - prev_my;
721 if(!dx && !dy) return;
723 if(exsel_grab_mouse) {
724 Vec3 pos = exslot_mouse.node.get_node_position();
725 Vec3 dir = transpose(view_matrix.upper3x3()) * Vec3(dx * 1.0, dy * -1.0, 0);
727 exslot_mouse.node.set_position(pos + dir);
735 void app_mouse_delta(int dx, int dy)
738 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
740 mouse_look(dx * mouse_speed, dy * mouse_speed);
744 void app_mouse_wheel(int dir)
747 debug_gui_wheel(dir);
751 void app_gamepad_axis(int axis, float val)
770 void app_gamepad_button(int bn, bool pressed)
772 gpad_bnstate[bn] = pressed;
781 show_blobs = !show_blobs;
782 show_message("blobs: %s\n", show_blobs ? "on" : "off");
787 show_message("VR recenter\n");
796 static void toggle_flight()
798 static float prev_walk_speed = -1.0;
799 if(prev_walk_speed < 0.0) {
801 prev_walk_speed = walk_speed;
803 show_message("fly mode\n");
806 walk_speed = prev_walk_speed;
807 prev_walk_speed = -1.0;
808 show_message("walk mode\n");
812 static void calc_framerate()
816 static long prev_upd;
818 long elapsed = time_msec - prev_upd;
819 if(elapsed >= 1000) {
820 framerate = (float)nframes / (float)(elapsed * 0.001);
822 prev_upd = time_msec;
825 printf("fps: %f\n", framerate);
833 static Ray calc_pick_ray(int x, int y)
835 float nx = (float)x / (float)win_width;
836 float ny = (float)(win_height - y) / (float)win_height;
838 last_pick_ray = mouse_pick_ray(nx, ny, view_matrix, proj_matrix);
839 return last_pick_ray;