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;
50 static int prev_mx, prev_my;
51 static bool bnstate[8];
52 static bool keystate[256];
53 static bool gpad_bnstate[64];
54 static Vec2 joy_move, joy_look;
55 static float joy_deadzone = 0.01;
57 static float framerate;
59 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
60 static MetaScene *mscn;
61 static unsigned int sdr_post_gamma;
63 static long prev_msec;
65 static ExhibitManager *exman;
66 static BlobExhibit *blobs;
67 static bool show_blobs;
69 static Renderer *rend;
72 bool app_init(int argc, char **argv)
74 if(!init_options(argc, argv, "demo.conf")) {
77 app_resize(opt.width, opt.height);
78 app_fullscreen(opt.fullscreen);
81 if(goatvr_init() == -1) {
84 goatvr_set_origin_mode(GOATVR_HEAD);
85 goatvr_set_units_scale(100.0f);
88 should_swap = goatvr_should_swap() != 0;
89 user_eye_height = goatvr_get_eye_height();
90 have_headtracking = goatvr_have_headtracking();
96 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
97 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
98 fb_srgb = srgb_capable != 0;
99 glEnable(GL_FRAMEBUFFER_SRGB);
101 glEnable(GL_MULTISAMPLE);
102 glEnable(GL_DEPTH_TEST);
103 glEnable(GL_CULL_FACE);
104 glEnable(GL_LIGHTING);
105 glEnable(GL_NORMALIZE);
107 Mesh::use_custom_sdr_attr = false;
109 float ambient[] = {0.0, 0.0, 0.0, 0.0};
110 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
112 glClearColor(1, 1, 1, 1);
114 mscn = new MetaScene;
115 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
119 cam_pos = mscn->start_pos;
120 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
122 cam_theta = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
124 exman = new ExhibitManager;
126 blobs = new BlobExhibit;
127 blobs->node = new SceneNode;
129 blobs->node->set_position(Vec3(-680, 160, -100));
130 blobs->node->set_scaling(Vec3(28, 28, 28));
131 blobs->node->update(0);
135 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
138 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
139 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
141 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
144 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
145 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
148 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
152 rend->set_scene(mscn);
156 if(opt.vr || opt.fullscreen) {
157 app_grab_mouse(true);
164 app_grab_mouse(false);
177 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
179 Mesh *wm = mscn->walk_mesh;
185 Ray downray = Ray(v, Vec3(0, -1, 0));
187 if(mscn->walk_mesh->intersect(downray, &hit)) {
189 newv->y += user_eye_height;
195 static void update(float dt)
203 float speed = walk_speed * dt;
207 float jdeadsq = joy_deadzone * joy_deadzone;
208 float jmove_lensq = length_sq(joy_move);
209 float jlook_lensq = length_sq(joy_look);
211 if(jmove_lensq > jdeadsq) {
212 float len = sqrt(jmove_lensq);
213 jmove_lensq -= jdeadsq;
215 float mag = len * len;
216 dir.x += mag * joy_move.x / len * 2.0 * speed;
217 dir.z += mag * joy_move.y / len * 2.0 * speed;
219 if(jlook_lensq > jdeadsq) {
220 float len = sqrt(jlook_lensq);
221 jlook_lensq -= jdeadsq;
223 float mag = len * len;
224 cam_theta += mag * joy_look.x / len * 200.0 * dt;
225 cam_phi += mag * joy_look.y / len * 100.0 * dt;
226 if(cam_phi < -90.0f) cam_phi = -90.0f;
227 if(cam_phi > 90.0f) cam_phi = 90.0f;
231 if(keystate[(int)'w']) {
234 if(keystate[(int)'s']) {
237 if(keystate[(int)'d']) {
240 if(keystate[(int)'a']) {
243 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
246 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
250 float theta = M_PI * cam_theta / 180.0f;
252 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
253 newpos.y = cam_pos.y;
254 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
259 if(!constrain_walk_mesh(newpos, &cam_pos)) {
260 float dtheta = M_PI / 32.0;
261 float theta = dtheta;
262 Vec2 dir2d = newpos.xz() - cam_pos.xz();
264 for(int i=0; i<16; i++) {
265 Vec2 dvec = rotate(dir2d, theta);
266 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
267 if(constrain_walk_mesh(pos, &cam_pos)) {
270 dvec = rotate(dir2d, -theta);
271 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
272 if(constrain_walk_mesh(pos, &cam_pos)) {
278 floor_y = cam_pos.y - user_eye_height;
281 // calculate mouselook view matrix
282 mouse_view_matrix = Mat4::identity;
283 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
284 if(!have_headtracking) {
285 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
287 mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
288 mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
291 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
293 unsigned int lt = GL_LIGHT0 + idx;
294 float posv[] = { pos.x, pos.y, pos.z, 1 };
295 float colv[] = { color.x, color.y, color.z, 1 };
298 glLightfv(lt, GL_POSITION, posv);
299 glLightfv(lt, GL_DIFFUSE, colv);
300 glLightfv(lt, GL_SPECULAR, colv);
305 float dt = (float)(time_msec - prev_msec) / 1000.0f;
306 prev_msec = time_msec;
313 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
315 for(int i=0; i<2; i++) {
319 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
320 glMatrixMode(GL_PROJECTION);
321 glLoadMatrixf(proj_matrix[0]);
323 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
324 glMatrixMode(GL_MODELVIEW);
325 glLoadMatrixf(view_matrix[0]);
336 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
338 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
339 glMatrixMode(GL_PROJECTION);
340 glLoadMatrixf(proj_matrix[0]);
342 view_matrix = mouse_view_matrix;
343 glMatrixMode(GL_MODELVIEW);
344 glLoadMatrixf(view_matrix[0]);
348 if(!fb_srgb && sdr_post_gamma) {
349 slow_post(sdr_post_gamma);
354 assert(glGetError() == GL_NO_ERROR);
360 static void draw_scene()
362 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
363 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
364 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
365 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
373 if(show_walk_mesh && mscn->walk_mesh) {
374 glPushAttrib(GL_ENABLE_BIT);
376 glBlendFunc(GL_ONE, GL_ONE);
377 glDisable(GL_LIGHTING);
378 glEnable(GL_POLYGON_OFFSET_FILL);
382 glPolygonOffset(-1, 1);
385 glColor3f(0.3, 0.08, 0.01);
386 mscn->walk_mesh->draw();
393 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
398 void app_reshape(int x, int y)
400 glViewport(0, 0, x, y);
401 goatvr_set_fb_size(x, y, 1.0f);
404 void app_keyboard(int key, bool pressed)
406 unsigned int mod = app_get_modifiers();
417 app_toggle_fullscreen();
422 app_toggle_grab_mouse();
423 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
428 show_walk_mesh = !show_walk_mesh;
429 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
436 show_message(noclip ? "no clip" : "clip");
447 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
453 show_message("walk speed: %g", walk_speed);
458 show_message("walk speed: %g", walk_speed);
463 show_message("mouse speed: %g", mouse_speed);
468 show_message("mouse speed: %g", mouse_speed);
472 show_blobs = !show_blobs;
473 show_message("blobs: %s\n", show_blobs ? "on" : "off");
478 show_message("VR recenter\n");
483 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
484 keystate[key] = pressed;
488 void app_mouse_button(int bn, bool pressed, int x, int y)
492 bnstate[bn] = pressed;
495 static inline void mouse_look(float dx, float dy)
497 float scrsz = (float)win_height;
498 cam_theta += dx * 512.0 / scrsz;
499 cam_phi += dy * 512.0 / scrsz;
501 if(cam_phi < -90) cam_phi = -90;
502 if(cam_phi > 90) cam_phi = 90;
505 static void mouse_zoom(float dx, float dy)
507 cam_dist += dy * 0.1;
508 if(cam_dist < 0.0) cam_dist = 0.0;
511 void app_mouse_motion(int x, int y)
513 int dx = x - prev_mx;
514 int dy = y - prev_my;
518 if(!dx && !dy) return;
528 void app_mouse_delta(int dx, int dy)
531 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
533 mouse_look(dx * mouse_speed, dy * mouse_speed);
537 void app_gamepad_axis(int axis, float val)
556 void app_gamepad_button(int bn, bool pressed)
558 gpad_bnstate[bn] = pressed;
567 show_blobs = !show_blobs;
568 show_message("blobs: %s\n", show_blobs ? "on" : "off");
573 show_message("VR recenter\n");
582 static void toggle_flight()
584 static float prev_walk_speed = -1.0;
585 if(prev_walk_speed < 0.0) {
587 prev_walk_speed = walk_speed;
589 show_message("fly mode\n");
592 walk_speed = prev_walk_speed;
593 prev_walk_speed = -1.0;
594 show_message("walk mode\n");
598 static void calc_framerate()
602 static long prev_upd;
604 long elapsed = time_msec - prev_upd;
605 if(elapsed >= 1000) {
606 framerate = (float)nframes / (float)(elapsed * 0.001);
608 prev_upd = time_msec;
611 printf("fps: %f\n", framerate);