11 #include "metascene.h"
16 #include "blob_exhibit.h"
19 #define FAR_CLIP 1000.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_shadow, sdr_shadow_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 = 1.65;
42 static float walk_speed = 3.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 = 1;
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);
82 should_swap = goatvr_should_swap() != 0;
83 user_eye_height = goatvr_get_eye_height();
84 have_headtracking = goatvr_have_headtracking();
90 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
91 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
92 fb_srgb = srgb_capable != 0;
93 glEnable(GL_FRAMEBUFFER_SRGB);
95 glEnable(GL_MULTISAMPLE);
96 glEnable(GL_DEPTH_TEST);
97 glEnable(GL_CULL_FACE);
98 glEnable(GL_LIGHTING);
99 glEnable(GL_NORMALIZE);
101 Mesh::use_custom_sdr_attr = false;
103 float ambient[] = {0.0, 0.0, 0.0, 0.0};
104 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
106 glClearColor(0.1, 0.1, 0.1, 1.0);
108 mscn = new MetaScene;
109 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/room.scene")) {
113 cam_pos = mscn->start_pos;
114 debug_log("start_pos: [%g %g %g]\n", cam_pos.x, cam_pos.y, cam_pos.z);
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(0, 1, 0));
123 blobs->node->update(0);
125 if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
128 set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
129 set_uniform_int(sdr_shadow_notex, "envmap", 2);
132 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
137 if(opt.vr || opt.fullscreen) {
138 app_grab_mouse(true);
145 app_grab_mouse(false);
158 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
160 Mesh *wm = mscn->walk_mesh;
166 Ray downray = Ray(v, Vec3(0, -1, 0));
168 if(mscn->walk_mesh->intersect(downray, &hit)) {
170 newv->y += user_eye_height;
176 static void update(float dt)
186 float speed = walk_speed * dt;
190 float jdeadsq = joy_deadzone * joy_deadzone;
191 float jmove_lensq = length_sq(joy_move);
192 float jlook_lensq = length_sq(joy_look);
194 if(jmove_lensq > jdeadsq) {
195 float len = sqrt(jmove_lensq);
196 jmove_lensq -= jdeadsq;
198 float mag = len * len;
199 dir.x += mag * joy_move.x / len * 2.0 * speed;
200 dir.z += mag * joy_move.y / len * 2.0 * speed;
202 if(jlook_lensq > jdeadsq) {
203 float len = sqrt(jlook_lensq);
204 jlook_lensq -= jdeadsq;
206 float mag = len * len;
207 cam_theta += mag * joy_look.x / len * 200.0 * dt;
208 cam_phi += mag * joy_look.y / len * 100.0 * dt;
209 if(cam_phi < -90.0f) cam_phi = -90.0f;
210 if(cam_phi > 90.0f) cam_phi = 90.0f;
214 if(keystate[(int)'w']) {
217 if(keystate[(int)'s']) {
220 if(keystate[(int)'d']) {
223 if(keystate[(int)'a']) {
226 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
229 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
233 float theta = M_PI * cam_theta / 180.0f;
235 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
236 newpos.y = cam_pos.y;
237 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
242 if(!constrain_walk_mesh(newpos, &cam_pos)) {
243 float dtheta = M_PI / 32.0;
244 float theta = dtheta;
245 Vec2 dir2d = newpos.xz() - cam_pos.xz();
247 for(int i=0; i<16; i++) {
248 Vec2 dvec = rotate(dir2d, theta);
249 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
250 if(constrain_walk_mesh(pos, &cam_pos)) {
253 dvec = rotate(dir2d, -theta);
254 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
255 if(constrain_walk_mesh(pos, &cam_pos)) {
261 floor_y = cam_pos.y - user_eye_height;
264 // calculate mouselook view matrix
265 mouse_view_matrix = Mat4::identity;
266 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
267 if(!have_headtracking) {
268 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
270 mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
271 mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
274 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
276 unsigned int lt = GL_LIGHT0 + idx;
277 float posv[] = { pos.x, pos.y, pos.z, 1 };
278 float colv[] = { color.x, color.y, color.z, 1 };
281 glLightfv(lt, GL_POSITION, posv);
282 glLightfv(lt, GL_DIFFUSE, colv);
283 glLightfv(lt, GL_SPECULAR, colv);
288 float dt = (float)(time_msec - prev_msec) / 1000.0f;
289 prev_msec = time_msec;
296 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
298 for(int i=0; i<2; i++) {
302 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
303 glMatrixMode(GL_PROJECTION);
304 glLoadMatrixf(proj_matrix[0]);
306 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
307 glMatrixMode(GL_MODELVIEW);
308 glLoadMatrixf(view_matrix[0]);
319 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
321 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
322 glMatrixMode(GL_PROJECTION);
323 glLoadMatrixf(proj_matrix[0]);
325 view_matrix = mouse_view_matrix;
326 glMatrixMode(GL_MODELVIEW);
327 glLoadMatrixf(view_matrix[0]);
331 if(!fb_srgb && sdr_post_gamma) {
332 slow_post(sdr_post_gamma);
337 assert(glGetError() == GL_NO_ERROR);
343 static void draw_scene()
345 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
346 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
347 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
348 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
355 if(show_walk_mesh && mscn->walk_mesh) {
356 glPushAttrib(GL_ENABLE_BIT);
358 glBlendFunc(GL_ONE, GL_ONE);
359 glDisable(GL_LIGHTING);
360 glEnable(GL_POLYGON_OFFSET_FILL);
364 glPolygonOffset(-1, 1);
367 glColor3f(0.3, 0.08, 0.01);
368 mscn->walk_mesh->draw();
375 print_text(Vec2(9 * win_width / 10, 20), Vec3(1, 1, 0), "fps: %.1f", framerate);
380 void app_reshape(int x, int y)
382 glViewport(0, 0, x, y);
383 goatvr_set_fb_size(x, y, 1.0f);
386 void app_keyboard(int key, bool pressed)
388 unsigned int mod = app_get_modifiers();
399 app_toggle_fullscreen();
404 app_toggle_grab_mouse();
405 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
410 show_walk_mesh = !show_walk_mesh;
411 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
418 show_message(noclip ? "no clip" : "clip");
429 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
435 show_message("walk speed: %g", walk_speed);
440 show_message("walk speed: %g", walk_speed);
445 show_message("mouse speed: %g", mouse_speed);
450 show_message("mouse speed: %g", mouse_speed);
454 show_blobs = !show_blobs;
455 show_message("blobs: %s\n", show_blobs ? "on" : "off");
460 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
461 keystate[key] = pressed;
465 void app_mouse_button(int bn, bool pressed, int x, int y)
469 bnstate[bn] = pressed;
472 static inline void mouse_look(float dx, float dy)
474 float scrsz = (float)win_height;
475 cam_theta += dx * 512.0 / scrsz;
476 cam_phi += dy * 512.0 / scrsz;
478 if(cam_phi < -90) cam_phi = -90;
479 if(cam_phi > 90) cam_phi = 90;
482 static void mouse_zoom(float dx, float dy)
484 cam_dist += dy * 0.1;
485 if(cam_dist < 0.0) cam_dist = 0.0;
488 void app_mouse_motion(int x, int y)
490 int dx = x - prev_mx;
491 int dy = y - prev_my;
495 if(!dx && !dy) return;
505 void app_mouse_delta(int dx, int dy)
508 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
510 mouse_look(dx * mouse_speed, dy * mouse_speed);
514 void app_gamepad_axis(int axis, float val)
533 void app_gamepad_button(int bn, bool pressed)
535 gpad_bnstate[bn] = pressed;
544 show_blobs = !show_blobs;
545 show_message("blobs: %s\n", show_blobs ? "on" : "off");
554 static void toggle_flight()
556 static float prev_walk_speed = -1.0;
557 if(prev_walk_speed < 0.0) {
559 prev_walk_speed = walk_speed;
561 show_message("fly mode\n");
564 walk_speed = prev_walk_speed;
565 prev_walk_speed = -1.0;
566 show_message("walk mode\n");
570 static void calc_framerate()
574 static long prev_upd;
576 long elapsed = time_msec - prev_upd;
577 if(elapsed >= 1000) {
578 framerate = (float)nframes / (float)(elapsed * 0.001);
580 prev_upd = time_msec;
583 printf("fps: %f\n", framerate);