11 #include "metascene.h"
20 #include "blob_exhibit.h"
23 #define FAR_CLIP 10000.0
25 static void draw_scene();
26 static void toggle_flight();
27 static void calc_framerate();
30 int win_width, win_height;
33 bool opt_gear_wireframe;
38 unsigned int sdr_ltmap, sdr_ltmap_notex;
42 static float cam_dist = 0.0;
43 static float floor_y; // last floor height
44 static float user_eye_height = 165;
46 static float walk_speed = 300.0f;
47 static float mouse_speed = 0.5f;
48 static bool show_walk_mesh, noclip = false;
50 static bool have_headtracking, have_handtracking, should_swap;
52 static int prev_mx, prev_my;
53 static bool bnstate[8];
54 static bool keystate[256];
55 static bool gpad_bnstate[64];
56 static Vec2 joy_move, joy_look;
57 static float joy_deadzone = 0.01;
59 static float framerate;
61 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
62 static MetaScene *mscn;
63 static unsigned int sdr_post_gamma;
65 static long prev_msec;
67 static ExhibitManager *exman;
68 static BlobExhibit *blobs;
69 static bool show_blobs;
71 static Renderer *rend;
74 bool app_init(int argc, char **argv)
76 if(!init_options(argc, argv, "demo.conf")) {
79 app_resize(opt.width, opt.height);
80 app_fullscreen(opt.fullscreen);
83 if(goatvr_init() == -1) {
86 goatvr_set_origin_mode(GOATVR_HEAD);
87 goatvr_set_units_scale(100.0f);
90 should_swap = goatvr_should_swap() != 0;
91 user_eye_height = goatvr_get_eye_height();
92 have_headtracking = goatvr_have_headtracking();
93 have_handtracking = goatvr_have_handtracking();
100 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
101 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
103 glEnable(GL_FRAMEBUFFER_SRGB);
107 glEnable(GL_MULTISAMPLE);
108 glEnable(GL_DEPTH_TEST);
109 glEnable(GL_CULL_FACE);
110 glEnable(GL_LIGHTING);
111 glEnable(GL_NORMALIZE);
113 Mesh::use_custom_sdr_attr = false;
115 float ambient[] = {0.0, 0.0, 0.0, 0.0};
116 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
118 glClearColor(1, 1, 1, 1);
122 if(!init_vrhands()) {
126 mscn = new MetaScene;
127 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
131 avatar.pos = mscn->start_pos;
132 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
134 avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
136 exman = new ExhibitManager;
137 if(!exman->load(mscn, "data/exhibits")) {
141 blobs = new BlobExhibit;
142 blobs->node = new SceneNode;
144 blobs->node->set_position(Vec3(-680, 160, -100));
145 blobs->node->set_scaling(Vec3(28, 28, 28));
146 blobs->node->update(0);
150 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
153 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
154 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
156 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
159 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
160 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
163 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
167 rend->set_scene(mscn);
171 if(opt.vr || opt.fullscreen) {
172 app_grab_mouse(true);
175 if(mscn->music && opt.music) {
176 mscn->music->play(AUDIO_PLAYMODE_LOOP);
188 app_grab_mouse(false);
202 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
204 Mesh *wm = mscn->walk_mesh;
210 Ray downray = Ray(v, Vec3(0, -1, 0));
212 if(mscn->walk_mesh->intersect(downray, &hit)) {
214 newv->y += user_eye_height;
220 static void update(float dt)
228 float speed = walk_speed * dt;
232 float jdeadsq = joy_deadzone * joy_deadzone;
233 float jmove_lensq = length_sq(joy_move);
234 float jlook_lensq = length_sq(joy_look);
236 if(jmove_lensq > jdeadsq) {
237 float len = sqrt(jmove_lensq);
238 jmove_lensq -= jdeadsq;
240 float mag = len * len;
241 dir.x += mag * joy_move.x / len * 2.0 * speed;
242 dir.z += mag * joy_move.y / len * 2.0 * speed;
244 if(jlook_lensq > jdeadsq) {
245 float len = sqrt(jlook_lensq);
246 jlook_lensq -= jdeadsq;
248 float mag = len * len;
249 avatar.body_rot += mag * joy_look.x / len * 200.0 * dt;
250 avatar.head_alt += mag * joy_look.y / len * 100.0 * dt;
251 if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f;
252 if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f;
256 if(keystate[(int)'w']) {
259 if(keystate[(int)'s']) {
262 if(keystate[(int)'d']) {
265 if(keystate[(int)'a']) {
268 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
269 avatar.pos.y += speed;
271 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
272 avatar.pos.y -= speed;
275 float theta = M_PI * avatar.body_rot / 180.0f;
277 newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
278 newpos.y = avatar.pos.y;
279 newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
284 if(!constrain_walk_mesh(newpos, &avatar.pos)) {
285 float dtheta = M_PI / 32.0;
286 float theta = dtheta;
287 Vec2 dir2d = newpos.xz() - avatar.pos.xz();
289 for(int i=0; i<16; i++) {
290 Vec2 dvec = rotate(dir2d, theta);
291 Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
292 if(constrain_walk_mesh(pos, &avatar.pos)) {
295 dvec = rotate(dir2d, -theta);
296 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y);
297 if(constrain_walk_mesh(pos, &avatar.pos)) {
303 floor_y = avatar.pos.y - user_eye_height;
306 // TODO move to avatar
307 // calculate mouselook view matrix
308 mouse_view_matrix = Mat4::identity;
309 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
310 if(!have_headtracking) {
311 mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt));
313 mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot));
314 mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z);
316 // update hand-tracking
317 if(have_handtracking) {
318 update_vrhands(&avatar);
322 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
324 unsigned int lt = GL_LIGHT0 + idx;
325 float posv[] = { pos.x, pos.y, pos.z, 1 };
326 float colv[] = { color.x, color.y, color.z, 1 };
329 glLightfv(lt, GL_POSITION, posv);
330 glLightfv(lt, GL_DIFFUSE, colv);
331 glLightfv(lt, GL_SPECULAR, colv);
336 float dt = (float)(time_msec - prev_msec) / 1000.0f;
337 prev_msec = time_msec;
342 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
346 for(int i=0; i<2; i++) {
350 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
351 glMatrixMode(GL_PROJECTION);
352 glLoadMatrixf(proj_matrix[0]);
354 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
355 glMatrixMode(GL_MODELVIEW);
356 glLoadMatrixf(view_matrix[0]);
359 if(have_handtracking) {
370 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
374 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
375 glMatrixMode(GL_PROJECTION);
376 glLoadMatrixf(proj_matrix[0]);
378 view_matrix = mouse_view_matrix;
379 glMatrixMode(GL_MODELVIEW);
380 glLoadMatrixf(view_matrix[0]);
384 if(!fb_srgb && sdr_post_gamma) {
385 slow_post(sdr_post_gamma);
390 assert(glGetError() == GL_NO_ERROR);
396 static void draw_scene()
398 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
399 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
400 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
401 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
410 if(have_handtracking) {
411 Mat4 head_xform = inverse(mouse_view_matrix);//goatvr_head_matrix();
412 Mat4 head_dir_xform = head_xform.upper3x3();
415 glPushAttrib(GL_ENABLE_BIT);
416 glDisable(GL_LIGHTING);
418 for(int i=0; i<2; i++) {
420 glColor3f(i, 1 - i, i);
422 glColor3f(0.5, 0.5, 0.5);
424 Vec3 v = head_xform * hand[i].pos;
425 Vec3 dir = head_dir_xform * rotate(Vec3(0, 0, -1), hand[i].rot) * 20.0f;
426 Vec3 up = head_dir_xform * rotate(Vec3(0, 1, 0), hand[i].rot) * 10.0f;
427 Vec3 right = head_dir_xform * rotate(Vec3(1, 0, 0), hand[i].rot) * 10.0f;
429 glVertex3f(v.x, v.y, v.z);
430 glVertex3f(v.x + dir.x, v.y + dir.y, v.z + dir.z);
431 glVertex3f(v.x - right.x, v.y - right.y, v.z - right.z);
432 glVertex3f(v.x + right.x, v.y + right.y, v.z + right.z);
433 glVertex3f(v.x - up.x, v.y - up.y, v.z - up.z);
434 glVertex3f(v.x + up.x, v.y + up.y, v.z + up.z);
441 if(show_walk_mesh && mscn->walk_mesh) {
442 glPushAttrib(GL_ENABLE_BIT);
444 glBlendFunc(GL_ONE, GL_ONE);
445 glDisable(GL_LIGHTING);
446 glEnable(GL_POLYGON_OFFSET_FILL);
450 glPolygonOffset(-1, 1);
453 glColor3f(0.3, 0.08, 0.01);
454 mscn->walk_mesh->draw();
461 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
466 void app_reshape(int x, int y)
468 glViewport(0, 0, x, y);
469 goatvr_set_fb_size(x, y, 1.0f);
472 void app_keyboard(int key, bool pressed)
474 unsigned int mod = app_get_modifiers();
485 app_toggle_fullscreen();
490 app_toggle_grab_mouse();
491 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
496 show_walk_mesh = !show_walk_mesh;
497 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
504 show_message(noclip ? "no clip" : "clip");
515 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
521 show_message("walk speed: %g", walk_speed);
526 show_message("walk speed: %g", walk_speed);
531 show_message("mouse speed: %g", mouse_speed);
536 show_message("mouse speed: %g", mouse_speed);
540 show_blobs = !show_blobs;
541 show_message("blobs: %s\n", show_blobs ? "on" : "off");
546 show_message("VR recenter\n");
551 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
552 keystate[key] = pressed;
556 void app_mouse_button(int bn, bool pressed, int x, int y)
560 bnstate[bn] = pressed;
563 static inline void mouse_look(float dx, float dy)
565 float scrsz = (float)win_height;
566 avatar.body_rot += dx * 512.0 / scrsz;
567 avatar.head_alt += dy * 512.0 / scrsz;
569 if(avatar.head_alt < -90) avatar.head_alt = -90;
570 if(avatar.head_alt > 90) avatar.head_alt = 90;
573 static void mouse_zoom(float dx, float dy)
575 cam_dist += dy * 0.1;
576 if(cam_dist < 0.0) cam_dist = 0.0;
579 void app_mouse_motion(int x, int y)
581 int dx = x - prev_mx;
582 int dy = y - prev_my;
586 if(!dx && !dy) return;
596 void app_mouse_delta(int dx, int dy)
599 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
601 mouse_look(dx * mouse_speed, dy * mouse_speed);
605 void app_gamepad_axis(int axis, float val)
624 void app_gamepad_button(int bn, bool pressed)
626 gpad_bnstate[bn] = pressed;
635 show_blobs = !show_blobs;
636 show_message("blobs: %s\n", show_blobs ? "on" : "off");
641 show_message("VR recenter\n");
650 static void toggle_flight()
652 static float prev_walk_speed = -1.0;
653 if(prev_walk_speed < 0.0) {
655 prev_walk_speed = walk_speed;
657 show_message("fly mode\n");
660 walk_speed = prev_walk_speed;
661 prev_walk_speed = -1.0;
662 show_message("walk mode\n");
666 static void calc_framerate()
670 static long prev_upd;
672 long elapsed = time_msec - prev_upd;
673 if(elapsed >= 1000) {
674 framerate = (float)nframes / (float)(elapsed * 0.001);
676 prev_upd = time_msec;
679 printf("fps: %f\n", framerate);