12 static void draw_scene();
15 int win_width, win_height;
16 bool opt_gear_wireframe;
18 static float cam_dist = 0.0;
19 static float cam_theta, cam_phi = 20;
21 static int prev_mx, prev_my;
22 static bool bnstate[8];
23 static bool keystate[256];
25 static Mat4 view_matrix;
26 static TextureSet texman;
28 static unsigned int sdr;
30 static long prev_msec;
35 glEnable(GL_FRAMEBUFFER_SRGB);
36 glEnable(GL_MULTISAMPLE);
37 glEnable(GL_DEPTH_TEST);
38 glEnable(GL_CULL_FACE);
39 glEnable(GL_LIGHTING);
40 glEnable(GL_NORMALIZE);
42 Mesh::use_custom_sdr_attr = false;
44 float ambient[] = {0.0, 0.0, 0.0, 0.0};
45 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
47 datamap_set_path("data");
48 if(!datamap_load_map("data.map")) {
49 fprintf(stderr, "failed to load datafile mappings\n");
52 unsigned int sflags = SCNLOAD_FLIPTEX;
53 scn = new Scene(&texman);
54 if(!(scn->load("data/testscene/patoma.fbx", sflags)) ||
55 !(scn->load("data/testscene/kolones.fbx", sflags))) {
56 fprintf(stderr, "failed to load test scene\n");
60 // hardcoded texture assignment hack
61 Texture *tex_kolones_lightmap = texman.get_texture("data/testscene/kolones_lighmap.jpg", TEX_2D);
62 Texture *tex_patoma_lightmap = texman.get_texture("data/testscene/patomacorona_lightmap.jpg", TEX_2D);
65 for(int i=0; i<(int)scn->objects.size(); i++) {
66 Object *obj = scn->objects[i];
67 if(obj->mtl.name == "WiteMarble") {
68 obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
69 } else if(obj->mtl.name == "BrownMarble") {
70 obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
71 } else if(obj->mtl.name == "GiroGiroMarmaro") {
72 obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
73 } else if(obj->mtl.name == "KentrikoKafeMarmaro") {
74 obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
76 } else if(obj->mtl.name == "SkouroGrizoMarmaro") {
77 obj->mtl.add_texture(tex_kolones_lightmap, MTL_TEX_LIGHTMAP);
78 } else if(obj->mtl.name == "PalioMarmaro") {
79 obj->mtl.add_texture(tex_kolones_lightmap, MTL_TEX_LIGHTMAP);
84 if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
85 fprintf(stderr, "failed to load test shaders\n");
88 set_uniform_int(sdr, "texmap", 0);
89 set_uniform_int(sdr, "lightmap", 1);
100 static void update(float dt)
106 float walk_speed = 2000.0 * dt;
109 if(keystate[(int)'w']) {
112 if(keystate[(int)'s']) {
115 if(keystate[(int)'d']) {
118 if(keystate[(int)'a']) {
121 if(keystate[(int)'q']) {
122 cam_pos.y += walk_speed;
124 if(keystate[(int)'z']) {
125 cam_pos.y -= walk_speed;
128 float theta = M_PI * cam_theta / 180.0f;
129 cam_pos.x += cos(theta) * dir.x - sin(theta) * dir.z;
130 cam_pos.z += sin(theta) * dir.x + cos(theta) * dir.z;
133 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
135 unsigned int lt = GL_LIGHT0 + idx;
136 float posv[] = { pos.x, pos.y, pos.z, 1 };
137 float colv[] = { color.x, color.y, color.z, 1 };
140 glLightfv(lt, GL_POSITION, posv);
141 glLightfv(lt, GL_DIFFUSE, colv);
142 glLightfv(lt, GL_SPECULAR, colv);
147 float dt = (float)(time_msec - prev_msec) / 1000.0f;
148 prev_msec = time_msec;
150 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
152 view_matrix = Mat4::identity;
153 view_matrix.pre_translate(0, 0, -cam_dist);
154 view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
155 view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
156 view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
158 glMatrixMode(GL_MODELVIEW);
159 glLoadMatrixf(view_matrix[0]);
161 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
162 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
163 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
164 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
171 assert(glGetError() == GL_NO_ERROR);
175 static void draw_scene()
180 glVertex3f(-30, -10, 30);
181 glVertex3f(30, -10, 30);
182 glVertex3f(30, -10, -30);
183 glVertex3f(-30, -10, -30);
193 void app_reshape(int x, int y)
195 glViewport(0, 0, x, y);
197 glMatrixMode(GL_PROJECTION);
199 gluPerspective(50.0, (float)x / (float)y, 1.0, 10000.0);
202 void app_keyboard(int key, bool pressed)
212 keystate[key] = pressed;
215 void app_mouse_button(int bn, bool pressed, int x, int y)
219 bnstate[bn] = pressed;
222 void app_mouse_motion(int x, int y)
224 int dx = x - prev_mx;
225 int dy = y - prev_my;
229 if(!dx && !dy) return;
232 cam_theta += dx * 0.5;
235 if(cam_phi < -90) cam_phi = -90;
236 if(cam_phi > 90) cam_phi = 90;
239 cam_dist += dy * 0.1;
240 if(cam_dist < 0.0) cam_dist = 0.0;