10 #include "metascene.h"
13 static void draw_scene();
16 int win_width, win_height;
17 bool opt_gear_wireframe;
19 static float cam_dist = 0.0;
20 static float cam_theta, cam_phi = 20;
22 static int prev_mx, prev_my;
23 static bool bnstate[8];
24 static bool keystate[256];
26 static Mat4 view_matrix;
27 static TextureSet texman;
29 static unsigned int sdr;
31 static long prev_msec;
36 glEnable(GL_FRAMEBUFFER_SRGB);
37 glEnable(GL_MULTISAMPLE);
38 glEnable(GL_DEPTH_TEST);
39 glEnable(GL_CULL_FACE);
40 glEnable(GL_LIGHTING);
41 glEnable(GL_NORMALIZE);
43 Mesh::use_custom_sdr_attr = false;
45 float ambient[] = {0.0, 0.0, 0.0, 0.0};
46 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
48 scn = new Scene(&texman);
49 if(!load_scene(scn, "data/museum.scene")) {
54 datamap_set_path("data");
55 if(!datamap_load_map("data.map")) {
56 fprintf(stderr, "failed to load datafile mappings\n");
59 unsigned int sflags = SCNLOAD_FLIPTEX;
60 if(!(scn->load("data/testscene/patoma.fbx", sflags)) ||
61 !(scn->load("data/testscene/kolones.fbx", sflags))) {
62 fprintf(stderr, "failed to load test scene\n");
67 if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
68 fprintf(stderr, "failed to load test shaders\n");
71 set_uniform_int(sdr, "texmap", 0);
72 set_uniform_int(sdr, "lightmap", 1);
83 static void update(float dt)
89 float walk_speed = 2000.0 * dt;
92 if(keystate[(int)'w']) {
95 if(keystate[(int)'s']) {
98 if(keystate[(int)'d']) {
101 if(keystate[(int)'a']) {
104 if(keystate[(int)'q']) {
105 cam_pos.y += walk_speed;
107 if(keystate[(int)'z']) {
108 cam_pos.y -= walk_speed;
111 float theta = M_PI * cam_theta / 180.0f;
112 cam_pos.x += cos(theta) * dir.x - sin(theta) * dir.z;
113 cam_pos.z += sin(theta) * dir.x + cos(theta) * dir.z;
116 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
118 unsigned int lt = GL_LIGHT0 + idx;
119 float posv[] = { pos.x, pos.y, pos.z, 1 };
120 float colv[] = { color.x, color.y, color.z, 1 };
123 glLightfv(lt, GL_POSITION, posv);
124 glLightfv(lt, GL_DIFFUSE, colv);
125 glLightfv(lt, GL_SPECULAR, colv);
130 float dt = (float)(time_msec - prev_msec) / 1000.0f;
131 prev_msec = time_msec;
133 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
135 view_matrix = Mat4::identity;
136 view_matrix.pre_translate(0, 0, -cam_dist);
137 view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
138 view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
139 view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
141 glMatrixMode(GL_MODELVIEW);
142 glLoadMatrixf(view_matrix[0]);
144 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
145 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
146 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
147 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
154 assert(glGetError() == GL_NO_ERROR);
158 static void draw_scene()
163 glVertex3f(-30, -10, 30);
164 glVertex3f(30, -10, 30);
165 glVertex3f(30, -10, -30);
166 glVertex3f(-30, -10, -30);
176 void app_reshape(int x, int y)
178 glViewport(0, 0, x, y);
180 glMatrixMode(GL_PROJECTION);
182 gluPerspective(50.0, (float)x / (float)y, 1.0, 10000.0);
185 void app_keyboard(int key, bool pressed)
195 keystate[key] = pressed;
198 void app_mouse_button(int bn, bool pressed, int x, int y)
202 bnstate[bn] = pressed;
205 void app_mouse_motion(int x, int y)
207 int dx = x - prev_mx;
208 int dy = y - prev_my;
212 if(!dx && !dy) return;
215 cam_theta += dx * 0.5;
218 if(cam_phi < -90) cam_phi = -90;
219 if(cam_phi > 90) cam_phi = 90;
222 cam_dist += dy * 0.1;
223 if(cam_dist < 0.0) cam_dist = 0.0;