11 #include "metascene.h"
19 #include "blob_exhibit.h"
22 #define FAR_CLIP 10000.0
24 static void draw_scene();
25 static void toggle_flight();
26 static void calc_framerate();
29 int win_width, win_height;
32 bool opt_gear_wireframe;
37 unsigned int sdr_ltmap, sdr_ltmap_notex;
39 static float cam_dist = 0.0;
40 static float cam_theta, cam_phi;
42 static float floor_y; // last floor height
43 static float user_eye_height = 165;
45 static float walk_speed = 300.0f;
46 static float mouse_speed = 0.5f;
47 static bool show_walk_mesh, noclip = false;
49 static bool have_headtracking, have_handtracking, should_swap;
51 static int prev_mx, prev_my;
52 static bool bnstate[8];
53 static bool keystate[256];
54 static bool gpad_bnstate[64];
55 static Vec2 joy_move, joy_look;
56 static float joy_deadzone = 0.01;
58 static float framerate;
60 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
61 static MetaScene *mscn;
62 static unsigned int sdr_post_gamma;
64 static long prev_msec;
66 static ExhibitManager *exman;
67 static BlobExhibit *blobs;
68 static bool show_blobs;
70 static Renderer *rend;
73 bool app_init(int argc, char **argv)
75 if(!init_options(argc, argv, "demo.conf")) {
78 app_resize(opt.width, opt.height);
79 app_fullscreen(opt.fullscreen);
82 if(goatvr_init() == -1) {
85 goatvr_set_origin_mode(GOATVR_HEAD);
86 goatvr_set_units_scale(100.0f);
89 should_swap = goatvr_should_swap() != 0;
90 user_eye_height = goatvr_get_eye_height();
91 have_headtracking = goatvr_have_headtracking();
92 have_handtracking = goatvr_have_handtracking();
98 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
99 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
100 fb_srgb = srgb_capable != 0;
101 glEnable(GL_FRAMEBUFFER_SRGB);
103 glEnable(GL_MULTISAMPLE);
104 glEnable(GL_DEPTH_TEST);
105 glEnable(GL_CULL_FACE);
106 glEnable(GL_LIGHTING);
107 glEnable(GL_NORMALIZE);
109 Mesh::use_custom_sdr_attr = false;
111 float ambient[] = {0.0, 0.0, 0.0, 0.0};
112 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
114 glClearColor(1, 1, 1, 1);
116 if(!init_vrhands()) {
120 mscn = new MetaScene;
121 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
125 cam_pos = mscn->start_pos;
126 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
128 cam_theta = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
130 exman = new ExhibitManager;
131 if(!exman->load(mscn, "data/exhibits")) {
135 blobs = new BlobExhibit;
136 blobs->node = new SceneNode;
138 blobs->node->set_position(Vec3(-680, 160, -100));
139 blobs->node->set_scaling(Vec3(28, 28, 28));
140 blobs->node->update(0);
144 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
147 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
148 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
150 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
153 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
154 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
157 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
161 rend->set_scene(mscn);
165 if(opt.vr || opt.fullscreen) {
166 app_grab_mouse(true);
173 app_grab_mouse(false);
187 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
189 Mesh *wm = mscn->walk_mesh;
195 Ray downray = Ray(v, Vec3(0, -1, 0));
197 if(mscn->walk_mesh->intersect(downray, &hit)) {
199 newv->y += user_eye_height;
205 static void update(float dt)
213 float speed = walk_speed * dt;
217 float jdeadsq = joy_deadzone * joy_deadzone;
218 float jmove_lensq = length_sq(joy_move);
219 float jlook_lensq = length_sq(joy_look);
221 if(jmove_lensq > jdeadsq) {
222 float len = sqrt(jmove_lensq);
223 jmove_lensq -= jdeadsq;
225 float mag = len * len;
226 dir.x += mag * joy_move.x / len * 2.0 * speed;
227 dir.z += mag * joy_move.y / len * 2.0 * speed;
229 if(jlook_lensq > jdeadsq) {
230 float len = sqrt(jlook_lensq);
231 jlook_lensq -= jdeadsq;
233 float mag = len * len;
234 cam_theta += mag * joy_look.x / len * 200.0 * dt;
235 cam_phi += mag * joy_look.y / len * 100.0 * dt;
236 if(cam_phi < -90.0f) cam_phi = -90.0f;
237 if(cam_phi > 90.0f) cam_phi = 90.0f;
241 if(keystate[(int)'w']) {
244 if(keystate[(int)'s']) {
247 if(keystate[(int)'d']) {
250 if(keystate[(int)'a']) {
253 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
256 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
260 float theta = M_PI * cam_theta / 180.0f;
262 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
263 newpos.y = cam_pos.y;
264 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
269 if(!constrain_walk_mesh(newpos, &cam_pos)) {
270 float dtheta = M_PI / 32.0;
271 float theta = dtheta;
272 Vec2 dir2d = newpos.xz() - cam_pos.xz();
274 for(int i=0; i<16; i++) {
275 Vec2 dvec = rotate(dir2d, theta);
276 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
277 if(constrain_walk_mesh(pos, &cam_pos)) {
280 dvec = rotate(dir2d, -theta);
281 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
282 if(constrain_walk_mesh(pos, &cam_pos)) {
288 floor_y = cam_pos.y - user_eye_height;
291 // calculate mouselook view matrix
292 mouse_view_matrix = Mat4::identity;
293 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
294 if(!have_headtracking) {
295 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
297 mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
298 mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
300 // update hand-tracking
301 if(have_handtracking) {
306 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
308 unsigned int lt = GL_LIGHT0 + idx;
309 float posv[] = { pos.x, pos.y, pos.z, 1 };
310 float colv[] = { color.x, color.y, color.z, 1 };
313 glLightfv(lt, GL_POSITION, posv);
314 glLightfv(lt, GL_DIFFUSE, colv);
315 glLightfv(lt, GL_SPECULAR, colv);
320 float dt = (float)(time_msec - prev_msec) / 1000.0f;
321 prev_msec = time_msec;
328 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
330 for(int i=0; i<2; i++) {
334 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
335 glMatrixMode(GL_PROJECTION);
336 glLoadMatrixf(proj_matrix[0]);
338 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
339 glMatrixMode(GL_MODELVIEW);
340 glLoadMatrixf(view_matrix[0]);
352 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
354 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
355 glMatrixMode(GL_PROJECTION);
356 glLoadMatrixf(proj_matrix[0]);
358 view_matrix = mouse_view_matrix;
359 glMatrixMode(GL_MODELVIEW);
360 glLoadMatrixf(view_matrix[0]);
364 if(!fb_srgb && sdr_post_gamma) {
365 slow_post(sdr_post_gamma);
370 assert(glGetError() == GL_NO_ERROR);
376 static void draw_scene()
378 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
379 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
380 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
381 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
389 if(show_walk_mesh && mscn->walk_mesh) {
390 glPushAttrib(GL_ENABLE_BIT);
392 glBlendFunc(GL_ONE, GL_ONE);
393 glDisable(GL_LIGHTING);
394 glEnable(GL_POLYGON_OFFSET_FILL);
398 glPolygonOffset(-1, 1);
401 glColor3f(0.3, 0.08, 0.01);
402 mscn->walk_mesh->draw();
409 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
414 void app_reshape(int x, int y)
416 glViewport(0, 0, x, y);
417 goatvr_set_fb_size(x, y, 1.0f);
420 void app_keyboard(int key, bool pressed)
422 unsigned int mod = app_get_modifiers();
433 app_toggle_fullscreen();
438 app_toggle_grab_mouse();
439 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
444 show_walk_mesh = !show_walk_mesh;
445 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
452 show_message(noclip ? "no clip" : "clip");
463 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
469 show_message("walk speed: %g", walk_speed);
474 show_message("walk speed: %g", walk_speed);
479 show_message("mouse speed: %g", mouse_speed);
484 show_message("mouse speed: %g", mouse_speed);
488 show_blobs = !show_blobs;
489 show_message("blobs: %s\n", show_blobs ? "on" : "off");
494 show_message("VR recenter\n");
499 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
500 keystate[key] = pressed;
504 void app_mouse_button(int bn, bool pressed, int x, int y)
508 bnstate[bn] = pressed;
511 static inline void mouse_look(float dx, float dy)
513 float scrsz = (float)win_height;
514 cam_theta += dx * 512.0 / scrsz;
515 cam_phi += dy * 512.0 / scrsz;
517 if(cam_phi < -90) cam_phi = -90;
518 if(cam_phi > 90) cam_phi = 90;
521 static void mouse_zoom(float dx, float dy)
523 cam_dist += dy * 0.1;
524 if(cam_dist < 0.0) cam_dist = 0.0;
527 void app_mouse_motion(int x, int y)
529 int dx = x - prev_mx;
530 int dy = y - prev_my;
534 if(!dx && !dy) return;
544 void app_mouse_delta(int dx, int dy)
547 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
549 mouse_look(dx * mouse_speed, dy * mouse_speed);
553 void app_gamepad_axis(int axis, float val)
572 void app_gamepad_button(int bn, bool pressed)
574 gpad_bnstate[bn] = pressed;
583 show_blobs = !show_blobs;
584 show_message("blobs: %s\n", show_blobs ? "on" : "off");
589 show_message("VR recenter\n");
598 static void toggle_flight()
600 static float prev_walk_speed = -1.0;
601 if(prev_walk_speed < 0.0) {
603 prev_walk_speed = walk_speed;
605 show_message("fly mode\n");
608 walk_speed = prev_walk_speed;
609 prev_walk_speed = -1.0;
610 show_message("walk mode\n");
614 static void calc_framerate()
618 static long prev_upd;
620 long elapsed = time_msec - prev_upd;
621 if(elapsed >= 1000) {
622 framerate = (float)nframes / (float)(elapsed * 0.001);
624 prev_upd = time_msec;
627 printf("fps: %f\n", framerate);