11 #include "metascene.h"
18 #define FAR_CLIP 10000.0
20 static void draw_scene();
23 int win_width, win_height;
26 bool opt_gear_wireframe;
28 static float cam_dist = 0.0;
29 static float cam_theta, cam_phi = 20;
31 static float floor_y; // last floor height
32 static float user_eye_height = 165;
34 static float walk_speed = 300.0f;
35 static bool show_walk_mesh, noclip = false;
37 static bool have_headtracking, should_swap;
39 static int prev_mx, prev_my;
40 static bool bnstate[8];
41 static bool keystate[256];
43 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
44 static TextureSet texman;
46 static unsigned int sdr, sdr_post_gamma;
48 static long prev_msec;
51 bool app_init(int argc, char **argv)
53 if(!init_options(argc, argv, "demo.conf")) {
56 app_resize(opt.width, opt.height);
57 app_fullscreen(opt.fullscreen);
60 if(goatvr_init() == -1) {
63 goatvr_set_origin_mode(GOATVR_HEAD);
64 goatvr_set_units_scale(100.0f);
67 should_swap = goatvr_should_swap() != 0;
68 user_eye_height = goatvr_get_eye_height();
69 have_headtracking = goatvr_have_headtracking();
75 glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
76 printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
77 fb_srgb = srgb_capable != 0;
78 glEnable(GL_FRAMEBUFFER_SRGB);
80 glEnable(GL_MULTISAMPLE);
81 glEnable(GL_DEPTH_TEST);
82 glEnable(GL_CULL_FACE);
83 glEnable(GL_LIGHTING);
84 glEnable(GL_NORMALIZE);
86 Mesh::use_custom_sdr_attr = false;
88 float ambient[] = {0.0, 0.0, 0.0, 0.0};
89 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
91 glClearColor(0.2, 0.2, 0.2, 1.0);
93 scn = new Scene(&texman);
94 if(!load_scene(scn, "data/museum.scene")) {
98 // set initial cam_pos above the center of the walk mesh (if any)
102 scn->walk_mesh->get_bsphere(&bcent, &brad);
105 cam_pos = bcent + Vec3(0, user_eye_height, 0);
108 if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
109 fprintf(stderr, "failed to load test shaders\n");
112 set_uniform_int(sdr, "texmap", 0);
113 set_uniform_int(sdr, "lightmap", 1);
116 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
121 if(opt.vr || opt.fullscreen) {
122 app_grab_mouse(true);
129 app_grab_mouse(false);
136 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
138 Mesh *wm = scn->walk_mesh;
144 Ray downray = Ray(v, Vec3(0, -1, 0));
146 if(scn->walk_mesh->intersect(downray, &hit)) {
148 newv->y += user_eye_height;
154 static void update(float dt)
160 float speed = walk_speed * dt;
163 if(keystate[(int)'w']) {
166 if(keystate[(int)'s']) {
169 if(keystate[(int)'d']) {
172 if(keystate[(int)'a']) {
175 if(keystate[(int)'q']) {
178 if(keystate[(int)'z']) {
182 float theta = M_PI * cam_theta / 180.0f;
184 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
185 newpos.y = cam_pos.y;
186 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
191 if(!constrain_walk_mesh(newpos, &cam_pos)) {
192 float dtheta = M_PI / 32.0;
193 float theta = dtheta;
194 Vec2 dir2d = newpos.xz() - cam_pos.xz();
196 for(int i=0; i<16; i++) {
197 Vec2 dvec = rotate(dir2d, theta);
198 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
199 if(constrain_walk_mesh(pos, &cam_pos)) {
202 dvec = rotate(dir2d, -theta);
203 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
204 if(constrain_walk_mesh(pos, &cam_pos)) {
210 floor_y = cam_pos.y - user_eye_height;
213 // calculate mouselook view matrix
214 mouse_view_matrix = Mat4::identity;
215 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
216 if(!have_headtracking) {
217 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
219 mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
220 mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
223 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
225 unsigned int lt = GL_LIGHT0 + idx;
226 float posv[] = { pos.x, pos.y, pos.z, 1 };
227 float colv[] = { color.x, color.y, color.z, 1 };
230 glLightfv(lt, GL_POSITION, posv);
231 glLightfv(lt, GL_DIFFUSE, colv);
232 glLightfv(lt, GL_SPECULAR, colv);
237 float dt = (float)(time_msec - prev_msec) / 1000.0f;
238 prev_msec = time_msec;
245 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
247 for(int i=0; i<2; i++) {
251 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
252 glMatrixMode(GL_PROJECTION);
253 glLoadMatrixf(proj_matrix[0]);
255 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
256 glMatrixMode(GL_MODELVIEW);
257 glLoadMatrixf(view_matrix[0]);
268 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
270 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
271 glMatrixMode(GL_PROJECTION);
272 glLoadMatrixf(proj_matrix[0]);
274 view_matrix = mouse_view_matrix;
275 glMatrixMode(GL_MODELVIEW);
276 glLoadMatrixf(view_matrix[0]);
280 if(!fb_srgb && sdr_post_gamma) {
281 slow_post(sdr_post_gamma);
286 assert(glGetError() == GL_NO_ERROR);
290 static void draw_scene()
292 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
293 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
294 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
295 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
301 if(show_walk_mesh && scn->walk_mesh) {
302 glPushAttrib(GL_ENABLE_BIT);
304 glBlendFunc(GL_ONE, GL_ONE);
305 glDisable(GL_LIGHTING);
306 glEnable(GL_POLYGON_OFFSET_FILL);
308 glPolygonOffset(-1, 1);
311 glColor3f(0.3, 0.08, 0.01);
312 scn->walk_mesh->draw();
323 void app_reshape(int x, int y)
325 glViewport(0, 0, x, y);
326 goatvr_set_fb_size(x, y, 1.0f);
329 void app_keyboard(int key, bool pressed)
331 unsigned int mod = app_get_modifiers();
340 app_toggle_fullscreen();
344 app_toggle_grab_mouse();
345 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
350 show_walk_mesh = !show_walk_mesh;
351 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
358 show_message(noclip ? "no clip" : "clip");
365 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
371 show_message("walk speed: %g", walk_speed);
376 show_message("walk speed: %g", walk_speed);
381 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
382 keystate[key] = pressed;
386 void app_mouse_button(int bn, bool pressed, int x, int y)
390 bnstate[bn] = pressed;
393 static inline void mouse_look(int dx, int dy)
395 float scrsz = (float)win_height;
396 cam_theta += dx * 512.0 / scrsz;
397 cam_phi += dy * 512.0 / scrsz;
399 if(cam_phi < -90) cam_phi = -90;
400 if(cam_phi > 90) cam_phi = 90;
403 static void mouse_zoom(int dx, int dy)
405 cam_dist += dy * 0.1;
406 if(cam_dist < 0.0) cam_dist = 0.0;
409 void app_mouse_motion(int x, int y)
411 int dx = x - prev_mx;
412 int dy = y - prev_my;
416 if(!dx && !dy) return;
426 void app_mouse_delta(int dx, int dy)