11 #include "metascene.h"
17 #define FAR_CLIP 10000.0
19 static void draw_scene();
22 int win_width, win_height;
24 bool opt_gear_wireframe;
26 static float cam_dist = 0.0;
27 static float cam_theta, cam_phi = 20;
29 static float floor_y; // last floor height
30 static float user_eye_height = 165;
32 static float walk_speed = 300.0f;
33 static bool show_walk_mesh, noclip = false;
35 static bool have_headtracking, should_swap;
37 static int prev_mx, prev_my;
38 static bool bnstate[8];
39 static bool keystate[256];
41 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
42 static TextureSet texman;
44 static unsigned int sdr;
46 static long prev_msec;
49 bool app_init(int argc, char **argv)
51 if(!init_options(argc, argv, "demo.conf")) {
54 app_resize(opt.width, opt.height);
55 app_fullscreen(opt.fullscreen);
58 if(goatvr_init() == -1) {
61 goatvr_set_origin_mode(GOATVR_HEAD);
62 goatvr_set_units_scale(100.0f);
65 should_swap = goatvr_should_swap() != 0;
66 user_eye_height = goatvr_get_eye_height();
67 have_headtracking = goatvr_have_headtracking();
72 glEnable(GL_FRAMEBUFFER_SRGB);
73 glEnable(GL_MULTISAMPLE);
74 glEnable(GL_DEPTH_TEST);
75 glEnable(GL_CULL_FACE);
76 glEnable(GL_LIGHTING);
77 glEnable(GL_NORMALIZE);
79 Mesh::use_custom_sdr_attr = false;
81 float ambient[] = {0.0, 0.0, 0.0, 0.0};
82 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
84 glClearColor(0.2, 0.2, 0.2, 1.0);
86 scn = new Scene(&texman);
87 if(!load_scene(scn, "data/museum.scene")) {
91 // set initial cam_pos above the center of the walk mesh (if any)
95 scn->walk_mesh->get_bsphere(&bcent, &brad);
98 cam_pos = bcent + Vec3(0, user_eye_height, 0);
101 if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
102 fprintf(stderr, "failed to load test shaders\n");
105 set_uniform_int(sdr, "texmap", 0);
106 set_uniform_int(sdr, "lightmap", 1);
110 if(opt.vr || opt.fullscreen) {
111 app_grab_mouse(true);
118 app_grab_mouse(false);
125 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
127 Mesh *wm = scn->walk_mesh;
133 Ray downray = Ray(v, Vec3(0, -1, 0));
135 if(scn->walk_mesh->intersect(downray, &hit)) {
137 newv->y += user_eye_height;
143 static void update(float dt)
149 float speed = walk_speed * dt;
152 if(keystate[(int)'w']) {
155 if(keystate[(int)'s']) {
158 if(keystate[(int)'d']) {
161 if(keystate[(int)'a']) {
164 if(keystate[(int)'q']) {
167 if(keystate[(int)'z']) {
171 float theta = M_PI * cam_theta / 180.0f;
173 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
174 newpos.y = cam_pos.y;
175 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
180 if(!constrain_walk_mesh(newpos, &cam_pos)) {
181 float dtheta = M_PI / 32.0;
182 float theta = dtheta;
183 Vec2 dir2d = newpos.xz() - cam_pos.xz();
185 for(int i=0; i<16; i++) {
186 Vec2 dvec = rotate(dir2d, theta);
187 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
188 if(constrain_walk_mesh(pos, &cam_pos)) {
191 dvec = rotate(dir2d, -theta);
192 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
193 if(constrain_walk_mesh(pos, &cam_pos)) {
199 floor_y = cam_pos.y - user_eye_height;
202 // calculate mouselook view matrix
203 mouse_view_matrix = Mat4::identity;
204 mouse_view_matrix.pre_translate(0, 0, -cam_dist);
205 if(!have_headtracking) {
206 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
208 mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
209 mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
212 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
214 unsigned int lt = GL_LIGHT0 + idx;
215 float posv[] = { pos.x, pos.y, pos.z, 1 };
216 float colv[] = { color.x, color.y, color.z, 1 };
219 glLightfv(lt, GL_POSITION, posv);
220 glLightfv(lt, GL_DIFFUSE, colv);
221 glLightfv(lt, GL_SPECULAR, colv);
226 float dt = (float)(time_msec - prev_msec) / 1000.0f;
227 prev_msec = time_msec;
234 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
236 for(int i=0; i<2; i++) {
240 proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
241 glMatrixMode(GL_PROJECTION);
242 glLoadMatrixf(proj_matrix[0]);
244 view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
245 glMatrixMode(GL_MODELVIEW);
246 glLoadMatrixf(view_matrix[0]);
257 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
259 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
260 glMatrixMode(GL_PROJECTION);
261 glLoadMatrixf(proj_matrix[0]);
263 view_matrix = mouse_view_matrix;
264 glMatrixMode(GL_MODELVIEW);
265 glLoadMatrixf(view_matrix[0]);
271 assert(glGetError() == GL_NO_ERROR);
275 static void draw_scene()
277 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
278 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
279 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
280 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
286 if(show_walk_mesh && scn->walk_mesh) {
287 glPushAttrib(GL_ENABLE_BIT);
289 glBlendFunc(GL_ONE, GL_ONE);
290 glDisable(GL_LIGHTING);
291 glEnable(GL_POLYGON_OFFSET_FILL);
293 glPolygonOffset(-1, 1);
296 glColor3f(0.3, 0.08, 0.01);
297 scn->walk_mesh->draw();
308 void app_reshape(int x, int y)
310 glViewport(0, 0, x, y);
311 goatvr_set_fb_size(x, y, 1.0f);
314 void app_keyboard(int key, bool pressed)
316 unsigned int mod = app_get_modifiers();
325 app_toggle_fullscreen();
329 app_toggle_grab_mouse();
330 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
335 show_walk_mesh = !show_walk_mesh;
336 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
343 show_message(noclip ? "no clip" : "clip");
349 show_message("walk speed: %g", walk_speed);
354 show_message("walk speed: %g", walk_speed);
359 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
360 keystate[key] = pressed;
364 void app_mouse_button(int bn, bool pressed, int x, int y)
368 bnstate[bn] = pressed;
371 static inline void mouse_look(int dx, int dy)
373 cam_theta += dx * 0.5;
376 if(cam_phi < -90) cam_phi = -90;
377 if(cam_phi > 90) cam_phi = 90;
380 static void mouse_zoom(int dx, int dy)
382 cam_dist += dy * 0.1;
383 if(cam_dist < 0.0) cam_dist = 0.0;
386 void app_mouse_motion(int x, int y)
388 int dx = x - prev_mx;
389 int dy = y - prev_my;
393 if(!dx && !dy) return;
403 void app_mouse_delta(int dx, int dy)