11 #include "metascene.h"
18 #define FAR_CLIP 10000.0
20 static void draw_scene();
21 static void toggle_flight();
24 int win_width, win_height;
27 bool opt_gear_wireframe;
32 unsigned int sdr_ltmap, sdr_ltmap_notex;
34 static float cam_dist = 0.0;
35 static float cam_theta, cam_phi;
37 static float floor_y; // last floor height
38 static float user_eye_height = 165;
40 static float walk_speed = 300.0f;
41 static float mouse_speed = 0.5f;
42 static bool show_walk_mesh, noclip = false;
44 static bool have_headtracking, should_swap;
46 static int prev_mx, prev_my;
47 static bool bnstate[8];
48 static bool keystate[256];
49 static bool gpad_bnstate[64];
50 static Vec2 joy_move, joy_look;
51 static float joy_deadzone = 0.01;
53 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
54 static MetaScene *mscn;
55 static unsigned int sdr_post_gamma;
57 static long prev_msec;
60 bool app_init(int argc, char **argv)
62 if(!init_options(argc, argv, "demo.conf")) {
65 app_resize(opt.width, opt.height);
66 app_fullscreen(opt.fullscreen);
69 if(goatvr_init() == -1) {
72 goatvr_set_origin_mode(GOATVR_HEAD);
73 goatvr_set_units_scale(100.0f);
76 should_swap = goatvr_should_swap() != 0;
77 user_eye_height = goatvr_get_eye_height();
78 have_headtracking = goatvr_have_headtracking();
84 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
85 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
86 fb_srgb = srgb_capable != 0;
87 glEnable(GL_FRAMEBUFFER_SRGB);
89 glEnable(GL_MULTISAMPLE);
90 glEnable(GL_DEPTH_TEST);
91 glEnable(GL_CULL_FACE);
92 glEnable(GL_LIGHTING);
93 glEnable(GL_NORMALIZE);
95 Mesh::use_custom_sdr_attr = false;
97 float ambient[] = {0.0, 0.0, 0.0, 0.0};
98 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
100 glClearColor(0.2, 0.2, 0.2, 1.0);
102 mscn = new MetaScene;
103 if(!mscn->load(opt.scenefile ? opt.scenefile : "data/museum.scene")) {
107 cam_pos = mscn->start_pos;
108 Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot);
110 cam_theta = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
112 if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
116 if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
119 set_uniform_int(sdr_ltmap, "texmap", 0);
120 set_uniform_int(sdr_ltmap, "lightmap", 1);
123 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
128 if(opt.vr || opt.fullscreen) {
129 app_grab_mouse(true);
136 app_grab_mouse(false);
145 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
147 Mesh *wm = mscn->walk_mesh;
153 Ray downray = Ray(v, Vec3(0, -1, 0));
155 if(mscn->walk_mesh->intersect(downray, &hit)) {
157 newv->y += user_eye_height;
163 static void update(float dt)
170 float speed = walk_speed * dt;
174 float jdeadsq = joy_deadzone * joy_deadzone;
175 float jmove_lensq = length_sq(joy_move);
176 float jlook_lensq = length_sq(joy_look);
178 if(jmove_lensq > jdeadsq) {
179 float len = sqrt(jmove_lensq);
180 jmove_lensq -= jdeadsq;
182 float mag = len * len;
183 dir.x += mag * joy_move.x / len * 2.0 * speed;
184 dir.z += mag * joy_move.y / len * 2.0 * speed;
186 if(jlook_lensq > jdeadsq) {
187 float len = sqrt(jlook_lensq);
188 jlook_lensq -= jdeadsq;
190 float mag = len * len;
191 cam_theta += mag * joy_look.x / len * 200.0 * dt;
192 cam_phi += mag * joy_look.y / len * 100.0 * dt;
193 if(cam_phi < -90.0f) cam_phi = -90.0f;
194 if(cam_phi > 90.0f) cam_phi = 90.0f;
198 if(keystate[(int)'w']) {
201 if(keystate[(int)'s']) {
204 if(keystate[(int)'d']) {
207 if(keystate[(int)'a']) {
210 if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
213 if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
217 float theta = M_PI * cam_theta / 180.0f;
219 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
220 newpos.y = cam_pos.y;
221 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
226 if(!constrain_walk_mesh(newpos, &cam_pos)) {
227 float dtheta = M_PI / 32.0;
228 float theta = dtheta;
229 Vec2 dir2d = newpos.xz() - cam_pos.xz();
231 for(int i=0; i<16; i++) {
232 Vec2 dvec = rotate(dir2d, theta);
233 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
234 if(constrain_walk_mesh(pos, &cam_pos)) {
237 dvec = rotate(dir2d, -theta);
238 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
239 if(constrain_walk_mesh(pos, &cam_pos)) {
245 floor_y = cam_pos.y - user_eye_height;
248 // calculate mouselook view matrix
249 mouse_view_matrix = Mat4::identity;
250 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
251 if(!have_headtracking) {
252 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
254 mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
255 mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
258 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
260 unsigned int lt = GL_LIGHT0 + idx;
261 float posv[] = { pos.x, pos.y, pos.z, 1 };
262 float colv[] = { color.x, color.y, color.z, 1 };
265 glLightfv(lt, GL_POSITION, posv);
266 glLightfv(lt, GL_DIFFUSE, colv);
267 glLightfv(lt, GL_SPECULAR, colv);
272 float dt = (float)(time_msec - prev_msec) / 1000.0f;
273 prev_msec = time_msec;
280 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
282 for(int i=0; i<2; i++) {
286 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
287 glMatrixMode(GL_PROJECTION);
288 glLoadMatrixf(proj_matrix[0]);
290 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
291 glMatrixMode(GL_MODELVIEW);
292 glLoadMatrixf(view_matrix[0]);
303 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
305 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
306 glMatrixMode(GL_PROJECTION);
307 glLoadMatrixf(proj_matrix[0]);
309 view_matrix = mouse_view_matrix;
310 glMatrixMode(GL_MODELVIEW);
311 glLoadMatrixf(view_matrix[0]);
315 if(!fb_srgb && sdr_post_gamma) {
316 slow_post(sdr_post_gamma);
321 assert(glGetError() == GL_NO_ERROR);
325 static void draw_scene()
327 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
328 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
329 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
330 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
334 if(show_walk_mesh && mscn->walk_mesh) {
335 glPushAttrib(GL_ENABLE_BIT);
337 glBlendFunc(GL_ONE, GL_ONE);
338 glDisable(GL_LIGHTING);
339 glEnable(GL_POLYGON_OFFSET_FILL);
343 glPolygonOffset(-1, 1);
346 glColor3f(0.3, 0.08, 0.01);
347 mscn->walk_mesh->draw();
358 void app_reshape(int x, int y)
360 glViewport(0, 0, x, y);
361 goatvr_set_fb_size(x, y, 1.0f);
364 void app_keyboard(int key, bool pressed)
366 unsigned int mod = app_get_modifiers();
377 app_toggle_fullscreen();
382 app_toggle_grab_mouse();
383 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
388 show_walk_mesh = !show_walk_mesh;
389 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
396 show_message(noclip ? "no clip" : "clip");
407 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
413 show_message("walk speed: %g", walk_speed);
418 show_message("walk speed: %g", walk_speed);
423 show_message("mouse speed: %g", mouse_speed);
428 show_message("mouse speed: %g", mouse_speed);
433 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
434 keystate[key] = pressed;
438 void app_mouse_button(int bn, bool pressed, int x, int y)
442 bnstate[bn] = pressed;
445 static inline void mouse_look(float dx, float dy)
447 float scrsz = (float)win_height;
448 cam_theta += dx * 512.0 / scrsz;
449 cam_phi += dy * 512.0 / scrsz;
451 if(cam_phi < -90) cam_phi = -90;
452 if(cam_phi > 90) cam_phi = 90;
455 static void mouse_zoom(float dx, float dy)
457 cam_dist += dy * 0.1;
458 if(cam_dist < 0.0) cam_dist = 0.0;
461 void app_mouse_motion(int x, int y)
463 int dx = x - prev_mx;
464 int dy = y - prev_my;
468 if(!dx && !dy) return;
478 void app_mouse_delta(int dx, int dy)
481 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
483 mouse_look(dx * mouse_speed, dy * mouse_speed);
487 void app_gamepad_axis(int axis, float val)
506 void app_gamepad_button(int bn, bool pressed)
508 gpad_bnstate[bn] = pressed;
522 static void toggle_flight()
524 static float prev_walk_speed = -1.0;
525 if(prev_walk_speed < 0.0) {
527 prev_walk_speed = walk_speed;
529 show_message("fly mode\n");
532 walk_speed = prev_walk_speed;
533 prev_walk_speed = -1.0;
534 show_message("walk mode\n");