11 static void draw_scene();
14 int win_width, win_height;
15 bool opt_gear_wireframe;
17 static float cam_dist = 10.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;
28 static long prev_msec;
33 glEnable(GL_MULTISAMPLE);
34 glEnable(GL_DEPTH_TEST);
35 glEnable(GL_CULL_FACE);
36 glEnable(GL_LIGHTING);
37 glEnable(GL_NORMALIZE);
39 Mesh::use_custom_sdr_attr = false;
41 float ambient[] = {0.1, 0.1, 0.1, 0.0};
42 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
44 unsigned int sflags = 0;
46 if(!(scn->load("data/testscene/patoma.fbx", sflags)) ||
47 !(scn->load("data/testscene/kolones.fbx", sflags))) {
48 fprintf(stderr, "failed to load test scene\n");
61 static void update(float dt)
67 float walk_speed = 10.0 * dt;
70 if(keystate[(int)'w']) {
73 if(keystate[(int)'s']) {
76 if(keystate[(int)'d']) {
79 if(keystate[(int)'a']) {
83 float theta = M_PI * cam_theta / 180.0f;
84 cam_pos.x += cos(theta) * dir.x - sin(theta) * dir.z;
85 cam_pos.z += sin(theta) * dir.x + cos(theta) * dir.z;
88 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
90 unsigned int lt = GL_LIGHT0 + idx;
91 float posv[] = { pos.x, pos.y, pos.z, 1 };
92 float colv[] = { color.x, color.y, color.z, 1 };
95 glLightfv(lt, GL_POSITION, posv);
96 glLightfv(lt, GL_DIFFUSE, colv);
97 glLightfv(lt, GL_SPECULAR, colv);
102 float dt = (float)(time_msec - prev_msec) / 1000.0f;
103 prev_msec = time_msec;
105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
107 view_matrix = Mat4::identity;
108 view_matrix.pre_translate(0, 0, -cam_dist);
109 view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
110 view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
111 view_matrix.pre_translate(-cam_pos.x, 0, -cam_pos.z);
113 glMatrixMode(GL_MODELVIEW);
114 glLoadMatrixf(view_matrix[0]);
116 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
117 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
118 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
119 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
126 assert(glGetError() == GL_NO_ERROR);
130 static void draw_scene()
135 glVertex3f(-30, -10, 30);
136 glVertex3f(30, -10, 30);
137 glVertex3f(30, -10, -30);
138 glVertex3f(-30, -10, -30);
145 void app_reshape(int x, int y)
147 glViewport(0, 0, x, y);
149 glMatrixMode(GL_PROJECTION);
151 gluPerspective(50.0, (float)x / (float)y, 0.5, 1000.0);
154 void app_keyboard(int key, bool pressed)
164 keystate[key] = pressed;
167 void app_mouse_button(int bn, bool pressed, int x, int y)
171 bnstate[bn] = pressed;
174 void app_mouse_motion(int x, int y)
176 int dx = x - prev_mx;
177 int dy = y - prev_my;
181 if(!dx && !dy) return;
184 cam_theta += dx * 0.5;
187 if(cam_phi < -90) cam_phi = -90;
188 if(cam_phi > 90) cam_phi = 90;
191 cam_dist += dy * 0.1;
192 if(cam_dist < 0.0) cam_dist = 0.0;