11 #include "metascene.h"
17 #include "blob_exhibit.h"
20 #define FAR_CLIP 10000.0
22 static void draw_scene();
23 static void toggle_flight();
24 static void calc_framerate();
27 int win_width, win_height;
30 bool opt_gear_wireframe;
35 unsigned int sdr_ltmap, sdr_ltmap_notex;
37 static float cam_dist = 0.0;
38 static float cam_theta, cam_phi;
40 static float floor_y; // last floor height
41 static float user_eye_height = 165;
43 static float walk_speed = 300.0f;
44 static float mouse_speed = 0.5f;
45 static bool show_walk_mesh, noclip = false;
47 static bool have_headtracking, should_swap;
49 static int prev_mx, prev_my;
50 static bool bnstate[8];
51 static bool keystate[256];
52 static bool gpad_bnstate[64];
53 static Vec2 joy_move, joy_look;
54 static float joy_deadzone = 0.01;
56 static float framerate;
58 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
59 static MetaScene *mscn;
60 static unsigned int sdr_post_gamma;
62 static long prev_msec;
64 static BlobExhibit *blobs;
65 static bool show_blobs;
67 static Renderer *rend;
70 bool app_init(int argc, char **argv)
72 if(!init_options(argc, argv, "demo.conf")) {
75 app_resize(opt.width, opt.height);
76 app_fullscreen(opt.fullscreen);
79 if(goatvr_init() == -1) {
82 goatvr_set_origin_mode(GOATVR_HEAD);
83 goatvr_set_units_scale(100.0f);
86 should_swap = goatvr_should_swap() != 0;
87 user_eye_height = goatvr_get_eye_height();
88 have_headtracking = goatvr_have_headtracking();
94 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
95 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
96 fb_srgb = srgb_capable != 0;
97 glEnable(GL_FRAMEBUFFER_SRGB);
99 glEnable(GL_MULTISAMPLE);
100 glEnable(GL_DEPTH_TEST);
101 glEnable(GL_CULL_FACE);
102 glEnable(GL_LIGHTING);
103 glEnable(GL_NORMALIZE);
105 Mesh::use_custom_sdr_attr = false;
107 float ambient[] = {0.0, 0.0, 0.0, 0.0};
108 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
110 glClearColor(1, 1, 1, 1);
112 mscn = new MetaScene;
113 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
117 cam_pos = mscn->start_pos;
118 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
120 cam_theta = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
122 blobs = new BlobExhibit;
123 blobs->node = new SceneNode;
125 blobs->node->set_position(Vec3(-680, 160, -100));
126 blobs->node->set_scaling(Vec3(28, 28, 28));
127 blobs->node->update(0);
129 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
132 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
133 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
135 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
138 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
139 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
142 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
146 rend->set_scene(mscn);
150 if(opt.vr || opt.fullscreen) {
151 app_grab_mouse(true);
158 app_grab_mouse(false);
173 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
175 Mesh *wm = mscn->walk_mesh;
181 Ray downray = Ray(v, Vec3(0, -1, 0));
183 if(mscn->walk_mesh->intersect(downray, &hit)) {
185 newv->y += user_eye_height;
191 static void update(float dt)
201 float speed = walk_speed * dt;
205 float jdeadsq = joy_deadzone * joy_deadzone;
206 float jmove_lensq = length_sq(joy_move);
207 float jlook_lensq = length_sq(joy_look);
209 if(jmove_lensq > jdeadsq) {
210 float len = sqrt(jmove_lensq);
211 jmove_lensq -= jdeadsq;
213 float mag = len * len;
214 dir.x += mag * joy_move.x / len * 2.0 * speed;
215 dir.z += mag * joy_move.y / len * 2.0 * speed;
217 if(jlook_lensq > jdeadsq) {
218 float len = sqrt(jlook_lensq);
219 jlook_lensq -= jdeadsq;
221 float mag = len * len;
222 cam_theta += mag * joy_look.x / len * 200.0 * dt;
223 cam_phi += mag * joy_look.y / len * 100.0 * dt;
224 if(cam_phi < -90.0f) cam_phi = -90.0f;
225 if(cam_phi > 90.0f) cam_phi = 90.0f;
229 if(keystate[(int)'w']) {
232 if(keystate[(int)'s']) {
235 if(keystate[(int)'d']) {
238 if(keystate[(int)'a']) {
241 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
244 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
248 float theta = M_PI * cam_theta / 180.0f;
250 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
251 newpos.y = cam_pos.y;
252 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
257 if(!constrain_walk_mesh(newpos, &cam_pos)) {
258 float dtheta = M_PI / 32.0;
259 float theta = dtheta;
260 Vec2 dir2d = newpos.xz() - cam_pos.xz();
262 for(int i=0; i<16; i++) {
263 Vec2 dvec = rotate(dir2d, theta);
264 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
265 if(constrain_walk_mesh(pos, &cam_pos)) {
268 dvec = rotate(dir2d, -theta);
269 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
270 if(constrain_walk_mesh(pos, &cam_pos)) {
276 floor_y = cam_pos.y - user_eye_height;
279 // calculate mouselook view matrix
280 mouse_view_matrix = Mat4::identity;
281 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
282 if(!have_headtracking) {
283 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
285 mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
286 mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
289 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
291 unsigned int lt = GL_LIGHT0 + idx;
292 float posv[] = { pos.x, pos.y, pos.z, 1 };
293 float colv[] = { color.x, color.y, color.z, 1 };
296 glLightfv(lt, GL_POSITION, posv);
297 glLightfv(lt, GL_DIFFUSE, colv);
298 glLightfv(lt, GL_SPECULAR, colv);
303 float dt = (float)(time_msec - prev_msec) / 1000.0f;
304 prev_msec = time_msec;
311 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
313 for(int i=0; i<2; i++) {
317 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
318 glMatrixMode(GL_PROJECTION);
319 glLoadMatrixf(proj_matrix[0]);
321 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
322 glMatrixMode(GL_MODELVIEW);
323 glLoadMatrixf(view_matrix[0]);
334 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
336 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
337 glMatrixMode(GL_PROJECTION);
338 glLoadMatrixf(proj_matrix[0]);
340 view_matrix = mouse_view_matrix;
341 glMatrixMode(GL_MODELVIEW);
342 glLoadMatrixf(view_matrix[0]);
346 if(!fb_srgb && sdr_post_gamma) {
347 slow_post(sdr_post_gamma);
352 assert(glGetError() == GL_NO_ERROR);
358 static void draw_scene()
360 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
361 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
362 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
363 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
371 if(show_walk_mesh && mscn->walk_mesh) {
372 glPushAttrib(GL_ENABLE_BIT);
374 glBlendFunc(GL_ONE, GL_ONE);
375 glDisable(GL_LIGHTING);
376 glEnable(GL_POLYGON_OFFSET_FILL);
380 glPolygonOffset(-1, 1);
383 glColor3f(0.3, 0.08, 0.01);
384 mscn->walk_mesh->draw();
391 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
396 void app_reshape(int x, int y)
398 glViewport(0, 0, x, y);
399 goatvr_set_fb_size(x, y, 1.0f);
402 void app_keyboard(int key, bool pressed)
404 unsigned int mod = app_get_modifiers();
415 app_toggle_fullscreen();
420 app_toggle_grab_mouse();
421 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
426 show_walk_mesh = !show_walk_mesh;
427 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
434 show_message(noclip ? "no clip" : "clip");
445 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
451 show_message("walk speed: %g", walk_speed);
456 show_message("walk speed: %g", walk_speed);
461 show_message("mouse speed: %g", mouse_speed);
466 show_message("mouse speed: %g", mouse_speed);
470 show_blobs = !show_blobs;
471 show_message("blobs: %s\n", show_blobs ? "on" : "off");
476 show_message("VR recenter\n");
481 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
482 keystate[key] = pressed;
486 void app_mouse_button(int bn, bool pressed, int x, int y)
490 bnstate[bn] = pressed;
493 static inline void mouse_look(float dx, float dy)
495 float scrsz = (float)win_height;
496 cam_theta += dx * 512.0 / scrsz;
497 cam_phi += dy * 512.0 / scrsz;
499 if(cam_phi < -90) cam_phi = -90;
500 if(cam_phi > 90) cam_phi = 90;
503 static void mouse_zoom(float dx, float dy)
505 cam_dist += dy * 0.1;
506 if(cam_dist < 0.0) cam_dist = 0.0;
509 void app_mouse_motion(int x, int y)
511 int dx = x - prev_mx;
512 int dy = y - prev_my;
516 if(!dx && !dy) return;
526 void app_mouse_delta(int dx, int dy)
529 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
531 mouse_look(dx * mouse_speed, dy * mouse_speed);
535 void app_gamepad_axis(int axis, float val)
554 void app_gamepad_button(int bn, bool pressed)
556 gpad_bnstate[bn] = pressed;
565 show_blobs = !show_blobs;
566 show_message("blobs: %s\n", show_blobs ? "on" : "off");
571 show_message("VR recenter\n");
580 static void toggle_flight()
582 static float prev_walk_speed = -1.0;
583 if(prev_walk_speed < 0.0) {
585 prev_walk_speed = walk_speed;
587 show_message("fly mode\n");
590 walk_speed = prev_walk_speed;
591 prev_walk_speed = -1.0;
592 show_message("walk mode\n");
596 static void calc_framerate()
600 static long prev_upd;
602 long elapsed = time_msec - prev_upd;
603 if(elapsed >= 1000) {
604 framerate = (float)nframes / (float)(elapsed * 0.001);
606 prev_upd = time_msec;
609 printf("fps: %f\n", framerate);