11 static void draw_scene();
14 int win_width, win_height;
15 bool opt_gear_wireframe;
17 static float cam_dist = 0.0;
18 static float cam_theta, cam_phi = 20;
20 static int prev_mx, prev_my;
21 static bool bnstate[8];
22 static bool keystate[256];
24 static Mat4 view_matrix;
25 static TextureSet texman;
27 static unsigned int sdr;
29 static long prev_msec;
34 glEnable(GL_FRAMEBUFFER_SRGB);
35 glEnable(GL_MULTISAMPLE);
36 glEnable(GL_DEPTH_TEST);
37 glEnable(GL_CULL_FACE);
38 glEnable(GL_LIGHTING);
39 glEnable(GL_NORMALIZE);
41 Mesh::use_custom_sdr_attr = false;
43 float ambient[] = {0.0, 0.0, 0.0, 0.0};
44 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
46 unsigned int sflags = SCNLOAD_FLIPTEX;
47 scn = new Scene(&texman);
48 if(!(scn->load("data/testscene/patoma.fbx", sflags)) ||
49 !(scn->load("data/testscene/kolones.fbx", sflags))) {
50 fprintf(stderr, "failed to load test scene\n");
54 // hardcoded texture assignment hack
55 Texture *tex_girogiromarmaro = texman.get("data/testscene/girogiromarmarodiffuse.jpg");
56 Texture *tex_kafemarble = texman.get("data/testscene/kafemarblediffuse.jpg");
57 Texture *tex_kentrikokafemarble = texman.get("data/testscene/kentrikokafemarblediffuse.jpg");
58 Texture *tex_paliomarmaro = texman.get("data/testscene/paliomarmarodiffuse.jpg");
59 Texture *tex_steelgreygranite = texman.get("data/testscene/steel-grey-granitediffuse.jpg");
60 Texture *tex_whitemarble = texman.get("data/testscene/whitemarblediffuse.jpg");
61 Texture *tex_kolones_lightmap = texman.get("data/testscene/kolones_lighmap.jpg");
62 Texture *tex_patoma_lightmap = texman.get("data/testscene/patomacorona_lightmap.jpg");
64 for(int i=0; i<(int)scn->objects.size(); i++) {
65 Object *obj = scn->objects[i];
66 if(obj->mtl.name == "WiteMarble") {
67 obj->mtl.add_texture(tex_whitemarble, MTL_TEX_DIFFUSE);
68 obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
69 } else if(obj->mtl.name == "BrownMarble") {
70 obj->mtl.add_texture(tex_kafemarble, MTL_TEX_DIFFUSE);
71 obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
72 } else if(obj->mtl.name == "GiroGiroMarmaro") {
73 obj->mtl.add_texture(tex_girogiromarmaro, MTL_TEX_DIFFUSE);
74 obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
75 } else if(obj->mtl.name == "KentrikoKafeMarmaro") {
76 obj->mtl.add_texture(tex_kentrikokafemarble, MTL_TEX_DIFFUSE);
77 obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
78 } else if(obj->mtl.name == "SkouroGrizoMarmaro") {
79 obj->mtl.add_texture(tex_steelgreygranite, MTL_TEX_DIFFUSE);
80 obj->mtl.add_texture(tex_kolones_lightmap, MTL_TEX_LIGHTMAP);
81 } else if(obj->mtl.name == "PalioMarmaro") {
82 obj->mtl.add_texture(tex_paliomarmaro, MTL_TEX_DIFFUSE);
83 obj->mtl.add_texture(tex_kolones_lightmap, MTL_TEX_LIGHTMAP);
87 if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
88 fprintf(stderr, "failed to load test shaders\n");
91 set_uniform_int(sdr, "texmap", 0);
92 set_uniform_int(sdr, "lightmap", 1);
103 static void update(float dt)
109 float walk_speed = 10.0 * dt;
112 if(keystate[(int)'w']) {
115 if(keystate[(int)'s']) {
118 if(keystate[(int)'d']) {
121 if(keystate[(int)'a']) {
124 if(keystate[(int)'q']) {
125 cam_pos.y += walk_speed;
127 if(keystate[(int)'z']) {
128 cam_pos.y -= walk_speed;
131 float theta = M_PI * cam_theta / 180.0f;
132 cam_pos.x += cos(theta) * dir.x - sin(theta) * dir.z;
133 cam_pos.z += sin(theta) * dir.x + cos(theta) * dir.z;
136 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
138 unsigned int lt = GL_LIGHT0 + idx;
139 float posv[] = { pos.x, pos.y, pos.z, 1 };
140 float colv[] = { color.x, color.y, color.z, 1 };
143 glLightfv(lt, GL_POSITION, posv);
144 glLightfv(lt, GL_DIFFUSE, colv);
145 glLightfv(lt, GL_SPECULAR, colv);
150 float dt = (float)(time_msec - prev_msec) / 1000.0f;
151 prev_msec = time_msec;
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155 view_matrix = Mat4::identity;
156 view_matrix.pre_translate(0, 0, -cam_dist);
157 view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
158 view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
159 view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
161 glMatrixMode(GL_MODELVIEW);
162 glLoadMatrixf(view_matrix[0]);
164 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
165 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
166 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
167 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
174 assert(glGetError() == GL_NO_ERROR);
178 static void draw_scene()
183 glVertex3f(-30, -10, 30);
184 glVertex3f(30, -10, 30);
185 glVertex3f(30, -10, -30);
186 glVertex3f(-30, -10, -30);
196 void app_reshape(int x, int y)
198 glViewport(0, 0, x, y);
200 glMatrixMode(GL_PROJECTION);
202 gluPerspective(50.0, (float)x / (float)y, 0.5, 1000.0);
205 void app_keyboard(int key, bool pressed)
215 keystate[key] = pressed;
218 void app_mouse_button(int bn, bool pressed, int x, int y)
222 bnstate[bn] = pressed;
225 void app_mouse_motion(int x, int y)
227 int dx = x - prev_mx;
228 int dy = y - prev_my;
232 if(!dx && !dy) return;
235 cam_theta += dx * 0.5;
238 if(cam_phi < -90) cam_phi = -90;
239 if(cam_phi > 90) cam_phi = 90;
242 cam_dist += dy * 0.1;
243 if(cam_dist < 0.0) cam_dist = 0.0;