10 #include "metascene.h"
14 static void draw_scene();
17 int win_width, win_height;
19 bool opt_gear_wireframe;
21 static float cam_dist = 0.0;
22 static float cam_theta, cam_phi = 20;
24 static float floor_y; // last floor height
25 static float user_eye_height = 165;
27 static float walk_speed = 300.0f;
28 static bool show_walk_mesh, noclip = false;
30 static int prev_mx, prev_my;
31 static bool bnstate[8];
32 static bool keystate[256];
34 static Mat4 view_matrix;
35 static TextureSet texman;
37 static unsigned int sdr;
39 static long prev_msec;
44 glEnable(GL_FRAMEBUFFER_SRGB);
45 glEnable(GL_MULTISAMPLE);
46 glEnable(GL_DEPTH_TEST);
47 glEnable(GL_CULL_FACE);
48 glEnable(GL_LIGHTING);
49 glEnable(GL_NORMALIZE);
51 Mesh::use_custom_sdr_attr = false;
53 float ambient[] = {0.0, 0.0, 0.0, 0.0};
54 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
56 scn = new Scene(&texman);
57 if(!load_scene(scn, "data/museum.scene")) {
61 // set initial cam_pos above the center of the walk mesh (if any)
65 scn->walk_mesh->get_bsphere(&bcent, &brad);
68 cam_pos = bcent + Vec3(0, user_eye_height, 0);
71 if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
72 fprintf(stderr, "failed to load test shaders\n");
75 set_uniform_int(sdr, "texmap", 0);
76 set_uniform_int(sdr, "lightmap", 1);
87 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
89 Mesh *wm = scn->walk_mesh;
95 Ray downray = Ray(v, Vec3(0, -1, 0));
97 if(scn->walk_mesh->intersect(downray, &hit)) {
99 newv->y += user_eye_height;
105 static void update(float dt)
111 float speed = walk_speed * dt;
114 if(keystate[(int)'w']) {
117 if(keystate[(int)'s']) {
120 if(keystate[(int)'d']) {
123 if(keystate[(int)'a']) {
126 if(keystate[(int)'q']) {
129 if(keystate[(int)'z']) {
133 float theta = M_PI * cam_theta / 180.0f;
135 newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
136 newpos.y = cam_pos.y;
137 newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
142 if(!constrain_walk_mesh(newpos, &cam_pos)) {
143 float dtheta = M_PI / 32.0;
144 float theta = dtheta;
145 Vec2 dir2d = newpos.xz() - cam_pos.xz();
147 for(int i=0; i<16; i++) {
148 Vec2 dvec = rotate(dir2d, theta);
149 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
150 if(constrain_walk_mesh(pos, &cam_pos)) {
153 dvec = rotate(dir2d, -theta);
154 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
155 if(constrain_walk_mesh(pos, &cam_pos)) {
161 floor_y = cam_pos.y - user_eye_height;
165 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
167 unsigned int lt = GL_LIGHT0 + idx;
168 float posv[] = { pos.x, pos.y, pos.z, 1 };
169 float colv[] = { color.x, color.y, color.z, 1 };
172 glLightfv(lt, GL_POSITION, posv);
173 glLightfv(lt, GL_DIFFUSE, colv);
174 glLightfv(lt, GL_SPECULAR, colv);
179 float dt = (float)(time_msec - prev_msec) / 1000.0f;
180 prev_msec = time_msec;
182 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
184 view_matrix = Mat4::identity;
185 view_matrix.pre_translate(0, 0, -cam_dist);
186 view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
187 view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
188 view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
190 glMatrixMode(GL_MODELVIEW);
191 glLoadMatrixf(view_matrix[0]);
193 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
194 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
195 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
196 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
204 assert(glGetError() == GL_NO_ERROR);
208 static void draw_scene()
214 if(show_walk_mesh && scn->walk_mesh) {
215 glPushAttrib(GL_ENABLE_BIT);
217 glBlendFunc(GL_ONE, GL_ONE);
218 glDisable(GL_LIGHTING);
219 glEnable(GL_POLYGON_OFFSET_FILL);
221 glPolygonOffset(-1, 1);
224 glColor3f(0.3, 0.08, 0.01);
225 scn->walk_mesh->draw();
234 void app_reshape(int x, int y)
236 glViewport(0, 0, x, y);
238 glMatrixMode(GL_PROJECTION);
240 gluPerspective(50.0, (float)x / (float)y, 1.0, 10000.0);
243 void app_keyboard(int key, bool pressed)
245 unsigned int mod = app_get_modifiers();
254 app_toggle_fullscreen();
258 app_toggle_grab_mouse();
259 show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
264 show_walk_mesh = !show_walk_mesh;
265 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
272 show_message(noclip ? "no clip" : "clip");
278 show_message("walk speed: %g", walk_speed);
283 show_message("walk speed: %g", walk_speed);
288 if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
289 keystate[key] = pressed;
293 void app_mouse_button(int bn, bool pressed, int x, int y)
297 bnstate[bn] = pressed;
300 static inline void mouse_look(int dx, int dy)
302 cam_theta += dx * 0.5;
305 if(cam_phi < -90) cam_phi = -90;
306 if(cam_phi > 90) cam_phi = 90;
309 static void mouse_zoom(int dx, int dy)
311 cam_dist += dy * 0.1;
312 if(cam_dist < 0.0) cam_dist = 0.0;
315 void app_mouse_motion(int x, int y)
317 int dx = x - prev_mx;
318 int dy = y - prev_my;
322 if(!dx && !dy) return;
332 void app_mouse_delta(int dx, int dy)