10 #include "metascene.h"
13 static void draw_scene();
16 int win_width, win_height;
17 bool opt_gear_wireframe;
20 static float cam_dist = 0.0;
21 static float cam_theta, cam_phi = 20;
23 static int prev_mx, prev_my;
24 static bool bnstate[8];
25 static bool keystate[256];
27 static Mat4 view_matrix;
28 static TextureSet texman;
30 static unsigned int sdr;
32 static long prev_msec;
37 glEnable(GL_FRAMEBUFFER_SRGB);
38 glEnable(GL_MULTISAMPLE);
39 glEnable(GL_DEPTH_TEST);
40 glEnable(GL_CULL_FACE);
41 glEnable(GL_LIGHTING);
42 glEnable(GL_NORMALIZE);
44 Mesh::use_custom_sdr_attr = false;
46 float ambient[] = {0.0, 0.0, 0.0, 0.0};
47 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
49 scn = new Scene(&texman);
50 if(!load_scene(scn, "data/museum.scene")) {
54 if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
55 fprintf(stderr, "failed to load test shaders\n");
58 set_uniform_int(sdr, "texmap", 0);
59 set_uniform_int(sdr, "lightmap", 1);
70 static void update(float dt)
76 float walk_speed = 2000.0 * dt;
79 if(keystate[(int)'w']) {
82 if(keystate[(int)'s']) {
85 if(keystate[(int)'d']) {
88 if(keystate[(int)'a']) {
91 if(keystate[(int)'q']) {
92 cam_pos.y += walk_speed;
94 if(keystate[(int)'z']) {
95 cam_pos.y -= walk_speed;
98 float theta = M_PI * cam_theta / 180.0f;
99 cam_pos.x += cos(theta) * dir.x - sin(theta) * dir.z;
100 cam_pos.z += sin(theta) * dir.x + cos(theta) * dir.z;
103 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
105 unsigned int lt = GL_LIGHT0 + idx;
106 float posv[] = { pos.x, pos.y, pos.z, 1 };
107 float colv[] = { color.x, color.y, color.z, 1 };
110 glLightfv(lt, GL_POSITION, posv);
111 glLightfv(lt, GL_DIFFUSE, colv);
112 glLightfv(lt, GL_SPECULAR, colv);
117 float dt = (float)(time_msec - prev_msec) / 1000.0f;
118 prev_msec = time_msec;
120 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
122 view_matrix = Mat4::identity;
123 view_matrix.pre_translate(0, 0, -cam_dist);
124 view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
125 view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
126 view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
128 glMatrixMode(GL_MODELVIEW);
129 glLoadMatrixf(view_matrix[0]);
131 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
132 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
133 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
134 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
141 assert(glGetError() == GL_NO_ERROR);
145 static void draw_scene()
150 glVertex3f(-30, -10, 30);
151 glVertex3f(30, -10, 30);
152 glVertex3f(30, -10, -30);
153 glVertex3f(-30, -10, -30);
161 if(show_walk_mesh && scn->walk_mesh) {
162 glPushAttrib(GL_ENABLE_BIT);
164 glBlendFunc(GL_ONE, GL_ONE);
165 glDisable(GL_LIGHTING);
167 glColor3f(0.5, 0.4, 0.05);
168 scn->walk_mesh->draw();
175 void app_reshape(int x, int y)
177 glViewport(0, 0, x, y);
179 glMatrixMode(GL_PROJECTION);
181 gluPerspective(50.0, (float)x / (float)y, 1.0, 10000.0);
184 void app_keyboard(int key, bool pressed)
187 printf("key: %d (mod: %x)\n", key, app_get_modifiers());
194 if(app_get_modifiers() & MOD_CTRL) {
195 show_walk_mesh = !show_walk_mesh;
201 keystate[key] = pressed;
204 void app_mouse_button(int bn, bool pressed, int x, int y)
208 bnstate[bn] = pressed;
211 void app_mouse_motion(int x, int y)
213 int dx = x - prev_mx;
214 int dy = y - prev_my;
218 if(!dx && !dy) return;
221 cam_theta += dx * 0.5;
224 if(cam_phi < -90) cam_phi = -90;
225 if(cam_phi > 90) cam_phi = 90;
228 cam_dist += dy * 0.1;
229 if(cam_dist < 0.0) cam_dist = 0.0;