11 #include "metascene.h"
16 #include "blob_exhibit.h"
19 #define FAR_CLIP 10000.0
21 static void draw_scene();
22 static void toggle_flight();
23 static void calc_framerate();
26 int win_width, win_height;
29 bool opt_gear_wireframe;
34 unsigned int sdr_ltmap, sdr_ltmap_notex;
36 static float cam_dist = 0.0;
37 static float cam_theta, cam_phi;
39 static float floor_y; // last floor height
40 static float user_eye_height = 165;
42 static float walk_speed = 300.0f;
43 static float mouse_speed = 0.5f;
44 static bool show_walk_mesh, noclip = false;
46 static bool have_headtracking, should_swap;
48 static int prev_mx, prev_my;
49 static bool bnstate[8];
50 static bool keystate[256];
51 static bool gpad_bnstate[64];
52 static Vec2 joy_move, joy_look;
53 static float joy_deadzone = 0.01;
55 static float framerate;
57 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
58 static MetaScene *mscn;
59 static unsigned int sdr_post_gamma;
61 static long prev_msec;
63 static BlobExhibit *blobs;
64 static bool show_blobs;
67 bool app_init(int argc, char **argv)
69 if(!init_options(argc, argv, "demo.conf")) {
72 app_resize(opt.width, opt.height);
73 app_fullscreen(opt.fullscreen);
76 if(goatvr_init() == -1) {
79 goatvr_set_origin_mode(GOATVR_HEAD);
80 goatvr_set_units_scale(100.0f);
83 should_swap = goatvr_should_swap() != 0;
84 user_eye_height = goatvr_get_eye_height();
85 have_headtracking = goatvr_have_headtracking();
91 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
92 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
93 fb_srgb = srgb_capable != 0;
94 glEnable(GL_FRAMEBUFFER_SRGB);
96 glEnable(GL_MULTISAMPLE);
97 glEnable(GL_DEPTH_TEST);
98 glEnable(GL_CULL_FACE);
99 glEnable(GL_LIGHTING);
100 glEnable(GL_NORMALIZE);
102 Mesh::use_custom_sdr_attr = false;
104 float ambient[] = {0.0, 0.0, 0.0, 0.0};
105 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
107 glClearColor(0.2, 0.2, 0.2, 1.0);
109 mscn = new MetaScene;
110 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
114 cam_pos = mscn->start_pos;
115 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
117 cam_theta = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
119 blobs = new BlobExhibit;
120 blobs->node = new SceneNode;
122 blobs->node->set_position(Vec3(-680, 160, -100));
123 blobs->node->set_scaling(Vec3(28, 28, 28));
124 blobs->node->update(0);
126 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
129 set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
130 set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
132 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
135 set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
136 set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
139 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
144 if(opt.vr || opt.fullscreen) {
145 app_grab_mouse(true);
152 app_grab_mouse(false);
165 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
167 Mesh *wm = mscn->walk_mesh;
173 Ray downray = Ray(v, Vec3(0, -1, 0));
175 if(mscn->walk_mesh->intersect(downray, &hit)) {
177 newv->y += user_eye_height;
183 static void update(float dt)
193 float speed = walk_speed * dt;
197 float jdeadsq = joy_deadzone * joy_deadzone;
198 float jmove_lensq = length_sq(joy_move);
199 float jlook_lensq = length_sq(joy_look);
201 if(jmove_lensq > jdeadsq) {
202 float len = sqrt(jmove_lensq);
203 jmove_lensq -= jdeadsq;
205 float mag = len * len;
206 dir.x += mag * joy_move.x / len * 2.0 * speed;
207 dir.z += mag * joy_move.y / len * 2.0 * speed;
209 if(jlook_lensq > jdeadsq) {
210 float len = sqrt(jlook_lensq);
211 jlook_lensq -= jdeadsq;
213 float mag = len * len;
214 cam_theta += mag * joy_look.x / len * 200.0 * dt;
215 cam_phi += mag * joy_look.y / len * 100.0 * dt;
216 if(cam_phi < -90.0f) cam_phi = -90.0f;
217 if(cam_phi > 90.0f) cam_phi = 90.0f;
221 if(keystate[(int)'w']) {
224 if(keystate[(int)'s']) {
227 if(keystate[(int)'d']) {
230 if(keystate[(int)'a']) {
233 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
236 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
240 float theta = M_PI * cam_theta / 180.0f;
242 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
243 newpos.y = cam_pos.y;
244 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
249 if(!constrain_walk_mesh(newpos, &cam_pos)) {
250 float dtheta = M_PI / 32.0;
251 float theta = dtheta;
252 Vec2 dir2d = newpos.xz() - cam_pos.xz();
254 for(int i=0; i<16; i++) {
255 Vec2 dvec = rotate(dir2d, theta);
256 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
257 if(constrain_walk_mesh(pos, &cam_pos)) {
260 dvec = rotate(dir2d, -theta);
261 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
262 if(constrain_walk_mesh(pos, &cam_pos)) {
268 floor_y = cam_pos.y - user_eye_height;
271 // calculate mouselook view matrix
272 mouse_view_matrix = Mat4::identity;
273 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
274 if(!have_headtracking) {
275 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
277 mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
278 mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
281 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
283 unsigned int lt = GL_LIGHT0 + idx;
284 float posv[] = { pos.x, pos.y, pos.z, 1 };
285 float colv[] = { color.x, color.y, color.z, 1 };
288 glLightfv(lt, GL_POSITION, posv);
289 glLightfv(lt, GL_DIFFUSE, colv);
290 glLightfv(lt, GL_SPECULAR, colv);
295 float dt = (float)(time_msec - prev_msec) / 1000.0f;
296 prev_msec = time_msec;
303 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
305 for(int i=0; i<2; i++) {
309 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
310 glMatrixMode(GL_PROJECTION);
311 glLoadMatrixf(proj_matrix[0]);
313 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
314 glMatrixMode(GL_MODELVIEW);
315 glLoadMatrixf(view_matrix[0]);
326 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
328 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
329 glMatrixMode(GL_PROJECTION);
330 glLoadMatrixf(proj_matrix[0]);
332 view_matrix = mouse_view_matrix;
333 glMatrixMode(GL_MODELVIEW);
334 glLoadMatrixf(view_matrix[0]);
338 if(!fb_srgb && sdr_post_gamma) {
339 slow_post(sdr_post_gamma);
344 assert(glGetError() == GL_NO_ERROR);
350 static void draw_scene()
352 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
353 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
354 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
355 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
362 if(show_walk_mesh && mscn->walk_mesh) {
363 glPushAttrib(GL_ENABLE_BIT);
365 glBlendFunc(GL_ONE, GL_ONE);
366 glDisable(GL_LIGHTING);
367 glEnable(GL_POLYGON_OFFSET_FILL);
371 glPolygonOffset(-1, 1);
374 glColor3f(0.3, 0.08, 0.01);
375 mscn->walk_mesh->draw();
382 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
387 void app_reshape(int x, int y)
389 glViewport(0, 0, x, y);
390 goatvr_set_fb_size(x, y, 1.0f);
393 void app_keyboard(int key, bool pressed)
395 unsigned int mod = app_get_modifiers();
406 app_toggle_fullscreen();
411 app_toggle_grab_mouse();
412 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
417 show_walk_mesh = !show_walk_mesh;
418 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
425 show_message(noclip ? "no clip" : "clip");
436 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
442 show_message("walk speed: %g", walk_speed);
447 show_message("walk speed: %g", walk_speed);
452 show_message("mouse speed: %g", mouse_speed);
457 show_message("mouse speed: %g", mouse_speed);
461 show_blobs = !show_blobs;
462 show_message("blobs: %s\n", show_blobs ? "on" : "off");
467 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
468 keystate[key] = pressed;
472 void app_mouse_button(int bn, bool pressed, int x, int y)
476 bnstate[bn] = pressed;
479 static inline void mouse_look(float dx, float dy)
481 float scrsz = (float)win_height;
482 cam_theta += dx * 512.0 / scrsz;
483 cam_phi += dy * 512.0 / scrsz;
485 if(cam_phi < -90) cam_phi = -90;
486 if(cam_phi > 90) cam_phi = 90;
489 static void mouse_zoom(float dx, float dy)
491 cam_dist += dy * 0.1;
492 if(cam_dist < 0.0) cam_dist = 0.0;
495 void app_mouse_motion(int x, int y)
497 int dx = x - prev_mx;
498 int dy = y - prev_my;
502 if(!dx && !dy) return;
512 void app_mouse_delta(int dx, int dy)
515 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
517 mouse_look(dx * mouse_speed, dy * mouse_speed);
521 void app_gamepad_axis(int axis, float val)
540 void app_gamepad_button(int bn, bool pressed)
542 gpad_bnstate[bn] = pressed;
551 show_blobs = !show_blobs;
552 show_message("blobs: %s\n", show_blobs ? "on" : "off");
561 static void toggle_flight()
563 static float prev_walk_speed = -1.0;
564 if(prev_walk_speed < 0.0) {
566 prev_walk_speed = walk_speed;
568 show_message("fly mode\n");
571 walk_speed = prev_walk_speed;
572 prev_walk_speed = -1.0;
573 show_message("walk mode\n");
577 static void calc_framerate()
581 static long prev_upd;
583 long elapsed = time_msec - prev_upd;
584 if(elapsed >= 1000) {
585 framerate = (float)nframes / (float)(elapsed * 0.001);
587 prev_upd = time_msec;
590 printf("fps: %f\n", framerate);