11 #include "metascene.h"
18 #include "blob_exhibit.h"
21 #define FAR_CLIP 10000.0
23 static void draw_scene();
24 static void toggle_flight();
25 static void calc_framerate();
28 int win_width, win_height;
31 bool opt_gear_wireframe;
36 unsigned int sdr_ltmap, sdr_ltmap_notex;
38 static float cam_dist = 0.0;
39 static float cam_theta, cam_phi;
41 static float floor_y; // last floor height
42 static float user_eye_height = 165;
44 static float walk_speed = 300.0f;
45 static float mouse_speed = 0.5f;
46 static bool show_walk_mesh, noclip = false;
48 static bool have_headtracking, should_swap;
49 static bool have_handtracking;
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 static Renderer *rend;
79 bool app_init(int argc, char **argv)
81 if(!init_options(argc, argv, "demo.conf")) {
84 app_resize(opt.width, opt.height);
85 app_fullscreen(opt.fullscreen);
88 if(goatvr_init() == -1) {
91 goatvr_set_origin_mode(GOATVR_HEAD);
92 goatvr_set_units_scale(100.0f);
95 should_swap = goatvr_should_swap() != 0;
96 user_eye_height = goatvr_get_eye_height();
97 have_headtracking = goatvr_have_headtracking();
98 have_handtracking = goatvr_have_handtracking();
104 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
105 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
106 fb_srgb = srgb_capable != 0;
107 glEnable(GL_FRAMEBUFFER_SRGB);
109 glEnable(GL_MULTISAMPLE);
110 glEnable(GL_DEPTH_TEST);
111 glEnable(GL_CULL_FACE);
112 glEnable(GL_LIGHTING);
113 glEnable(GL_NORMALIZE);
115 Mesh::use_custom_sdr_attr = false;
117 float ambient[] = {0.0, 0.0, 0.0, 0.0};
118 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
120 glClearColor(1, 1, 1, 1);
122 mscn = new MetaScene;
123 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
127 cam_pos = mscn->start_pos;
128 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
130 cam_theta = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
132 exman = new ExhibitManager;
133 if(!exman->load(mscn, "data/exhibits")) {
137 blobs = new BlobExhibit;
138 blobs->node = new SceneNode;
140 blobs->node->set_position(Vec3(-680, 160, -100));
141 blobs->node->set_scaling(Vec3(28, 28, 28));
142 blobs->node->update(0);
146 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
149 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
150 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
152 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
155 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
156 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
159 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
163 rend->set_scene(mscn);
167 if(opt.vr || opt.fullscreen) {
168 app_grab_mouse(true);
175 app_grab_mouse(false);
188 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
190 Mesh *wm = mscn->walk_mesh;
196 Ray downray = Ray(v, Vec3(0, -1, 0));
198 if(mscn->walk_mesh->intersect(downray, &hit)) {
200 newv->y += user_eye_height;
206 static void update(float dt)
214 float speed = walk_speed * dt;
218 float jdeadsq = joy_deadzone * joy_deadzone;
219 float jmove_lensq = length_sq(joy_move);
220 float jlook_lensq = length_sq(joy_look);
222 if(jmove_lensq > jdeadsq) {
223 float len = sqrt(jmove_lensq);
224 jmove_lensq -= jdeadsq;
226 float mag = len * len;
227 dir.x += mag * joy_move.x / len * 2.0 * speed;
228 dir.z += mag * joy_move.y / len * 2.0 * speed;
230 if(jlook_lensq > jdeadsq) {
231 float len = sqrt(jlook_lensq);
232 jlook_lensq -= jdeadsq;
234 float mag = len * len;
235 cam_theta += mag * joy_look.x / len * 200.0 * dt;
236 cam_phi += mag * joy_look.y / len * 100.0 * dt;
237 if(cam_phi < -90.0f) cam_phi = -90.0f;
238 if(cam_phi > 90.0f) cam_phi = 90.0f;
242 if(keystate[(int)'w']) {
245 if(keystate[(int)'s']) {
248 if(keystate[(int)'d']) {
251 if(keystate[(int)'a']) {
254 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
257 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
261 float theta = M_PI * cam_theta / 180.0f;
263 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
264 newpos.y = cam_pos.y;
265 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
270 if(!constrain_walk_mesh(newpos, &cam_pos)) {
271 float dtheta = M_PI / 32.0;
272 float theta = dtheta;
273 Vec2 dir2d = newpos.xz() - cam_pos.xz();
275 for(int i=0; i<16; i++) {
276 Vec2 dvec = rotate(dir2d, theta);
277 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
278 if(constrain_walk_mesh(pos, &cam_pos)) {
281 dvec = rotate(dir2d, -theta);
282 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
283 if(constrain_walk_mesh(pos, &cam_pos)) {
289 floor_y = cam_pos.y - user_eye_height;
292 // calculate mouselook view matrix
293 mouse_view_matrix = Mat4::identity;
294 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
295 if(!have_headtracking) {
296 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
298 mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
299 mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
301 if(have_handtracking) {
302 for(int i=0; i<2; i++) {
303 if(goatvr_hand_active(i)) {
304 goatvr_hand_position(i, &hand[i].pos.x);
305 goatvr_hand_orientation(i, &hand[i].rot.x);
306 hand[i].valid = true;
308 hand[i].valid = false;
314 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
316 unsigned int lt = GL_LIGHT0 + idx;
317 float posv[] = { pos.x, pos.y, pos.z, 1 };
318 float colv[] = { color.x, color.y, color.z, 1 };
321 glLightfv(lt, GL_POSITION, posv);
322 glLightfv(lt, GL_DIFFUSE, colv);
323 glLightfv(lt, GL_SPECULAR, colv);
328 float dt = (float)(time_msec - prev_msec) / 1000.0f;
329 prev_msec = time_msec;
334 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
338 for(int i=0; i<2; i++) {
342 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
343 glMatrixMode(GL_PROJECTION);
344 glLoadMatrixf(proj_matrix[0]);
346 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
347 glMatrixMode(GL_MODELVIEW);
348 glLoadMatrixf(view_matrix[0]);
359 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
363 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
364 glMatrixMode(GL_PROJECTION);
365 glLoadMatrixf(proj_matrix[0]);
367 view_matrix = mouse_view_matrix;
368 glMatrixMode(GL_MODELVIEW);
369 glLoadMatrixf(view_matrix[0]);
373 if(!fb_srgb && sdr_post_gamma) {
374 slow_post(sdr_post_gamma);
379 assert(glGetError() == GL_NO_ERROR);
385 static void draw_scene()
387 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
388 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
389 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
390 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
398 if(have_handtracking) {
399 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
400 Mat4 head_dir_xform = head_xform.upper3x3();
403 glPushAttrib(GL_ENABLE_BIT);
404 glDisable(GL_LIGHTING);
406 for(int i=0; i<2; i++) {
408 glColor3f(i, 1 - i, i);
410 glColor3f(0.5, 0.5, 0.5);
412 Vec3 v = head_xform * hand[i].pos;
413 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), hand[i].rot) * 20.0f;
414 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), hand[i].rot) * 10.0f;
415 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), hand[i].rot) * 10.0f;
417 glVertex3f(v.x, v.y, v.z);
418 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
419 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
420 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
421 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
422 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
428 if(show_walk_mesh && mscn->walk_mesh) {
429 glPushAttrib(GL_ENABLE_BIT);
431 glBlendFunc(GL_ONE, GL_ONE);
432 glDisable(GL_LIGHTING);
433 glEnable(GL_POLYGON_OFFSET_FILL);
437 glPolygonOffset(-1, 1);
440 glColor3f(0.3, 0.08, 0.01);
441 mscn->walk_mesh->draw();
448 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
453 void app_reshape(int x, int y)
455 glViewport(0, 0, x, y);
456 goatvr_set_fb_size(x, y, 1.0f);
459 void app_keyboard(int key, bool pressed)
461 unsigned int mod = app_get_modifiers();
472 app_toggle_fullscreen();
477 app_toggle_grab_mouse();
478 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
483 show_walk_mesh = !show_walk_mesh;
484 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
491 show_message(noclip ? "no clip" : "clip");
502 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
508 show_message("walk speed: %g", walk_speed);
513 show_message("walk speed: %g", walk_speed);
518 show_message("mouse speed: %g", mouse_speed);
523 show_message("mouse speed: %g", mouse_speed);
527 show_blobs = !show_blobs;
528 show_message("blobs: %s\n", show_blobs ? "on" : "off");
533 show_message("VR recenter\n");
538 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
539 keystate[key] = pressed;
543 void app_mouse_button(int bn, bool pressed, int x, int y)
547 bnstate[bn] = pressed;
550 static inline void mouse_look(float dx, float dy)
552 float scrsz = (float)win_height;
553 cam_theta += dx * 512.0 / scrsz;
554 cam_phi += dy * 512.0 / scrsz;
556 if(cam_phi < -90) cam_phi = -90;
557 if(cam_phi > 90) cam_phi = 90;
560 static void mouse_zoom(float dx, float dy)
562 cam_dist += dy * 0.1;
563 if(cam_dist < 0.0) cam_dist = 0.0;
566 void app_mouse_motion(int x, int y)
568 int dx = x - prev_mx;
569 int dy = y - prev_my;
573 if(!dx && !dy) return;
583 void app_mouse_delta(int dx, int dy)
586 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
588 mouse_look(dx * mouse_speed, dy * mouse_speed);
592 void app_gamepad_axis(int axis, float val)
611 void app_gamepad_button(int bn, bool pressed)
613 gpad_bnstate[bn] = pressed;
622 show_blobs = !show_blobs;
623 show_message("blobs: %s\n", show_blobs ? "on" : "off");
628 show_message("VR recenter\n");
637 static void toggle_flight()
639 static float prev_walk_speed = -1.0;
640 if(prev_walk_speed < 0.0) {
642 prev_walk_speed = walk_speed;
644 show_message("fly mode\n");
647 walk_speed = prev_walk_speed;
648 prev_walk_speed = -1.0;
649 show_message("walk mode\n");
653 static void calc_framerate()
657 static long prev_upd;
659 long elapsed = time_msec - prev_upd;
660 if(elapsed >= 1000) {
661 framerate = (float)nframes / (float)(elapsed * 0.001);
663 prev_upd = time_msec;
666 printf("fps: %f\n", framerate);