14 static void cleanup();
15 static void display();
17 static void draw_gears();
18 static void reshape(int x, int y);
19 static void keyb(unsigned char key, int x, int y);
20 static void mouse(int bn, int st, int x, int y);
21 static void motion(int x, int y);
22 static void passive_motion(int x, int y);
23 static Gear *pick_gear(int x, int y);
25 static int win_width, win_height;
27 static float cam_dist = 0.5;
28 static float cam_theta, cam_phi;
29 static int prev_mx, prev_my;
30 static bool bnstate[8];
32 static unsigned int start_time, prev_msec;
33 static Machine *machine;
34 static Gear *hover_gear, *sel_gear;
35 static HitPoint pick_hit;
36 static Vec3 sel_hit_pos;
38 int main(int argc, char **argv)
40 glutInitWindowSize(1024, 768);
41 glutInit(&argc, argv);
42 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
43 glutCreateWindow("Antikythera");
45 glutDisplayFunc(display);
47 glutReshapeFunc(reshape);
48 glutKeyboardFunc(keyb);
50 glutMotionFunc(motion);
51 glutPassiveMotionFunc(passive_motion);
66 glEnable(GL_MULTISAMPLE);
67 glEnable(GL_DEPTH_TEST);
68 glEnable(GL_CULL_FACE);
69 glEnable(GL_LIGHTING);
71 glEnable(GL_NORMALIZE);
73 Mesh::use_custom_sdr_attr = false;
75 machine = new Machine;
77 const float pitch = 10.0f;
79 Gear *gear1 = new Gear;
80 gear1->pos = Vec3(-50, 0, 0);
81 gear1->set_teeth(16, pitch);
83 machine->add_gear(gear1);
85 Gear *gear2 = new Gear;
86 gear2->set_teeth(32, pitch);
87 gear2->pos = gear1->pos + Vec3(gear1->radius + gear2->radius - gear1->teeth_length * 0.75, 0, 0);
90 machine->add_gear(gear2);
92 Gear *gear3 = new Gear;
93 gear3->set_teeth(8, pitch);
94 gear3->pos = gear2->pos + Vec3(0, gear2->radius + gear3->radius - gear2->teeth_length * 0.75, 0);
96 machine->add_gear(gear3);
98 machine->add_motor(0, 1.0);
100 start_time = glutGet(GLUT_ELAPSED_TIME);
104 static void cleanup()
109 static void update(float dt)
116 hover_gear = pick_gear(prev_mx, prev_my);
119 static void display()
121 unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
122 float dt = (float)(msec - prev_msec) / 1000.0f;
125 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
127 glMatrixMode(GL_MODELVIEW);
129 glTranslatef(0, 0, -cam_dist);
130 glRotatef(cam_phi, 1, 0, 0);
131 glRotatef(cam_theta, 0, 1, 0);
138 assert(glGetError() == GL_NO_ERROR);
146 static void draw_gears()
148 /* world scale is in meters, gears are in millimeters, scale by 1/1000 */
150 glScalef(0.001, 0.001, 0.001);
152 if(sel_gear || hover_gear) {
153 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POLYGON_BIT);
155 glDisable(GL_LIGHTING);
157 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
161 glColor3f(0.2, 1.0, 0.3);
164 glColor3f(1.0, 0.75, 0.2);
176 static void reshape(int x, int y)
181 glViewport(0, 0, x, y);
183 glMatrixMode(GL_PROJECTION);
185 gluPerspective(50.0, (float)x / (float)y, 0.01, 100.0);
188 static void keyb(unsigned char key, int x, int y)
195 opt_gear_wireframe = !opt_gear_wireframe;
201 static void mouse(int bn, int st, int x, int y)
203 int bidx = bn - GLUT_LEFT_BUTTON;
204 bool down = st == GLUT_DOWN;
208 bnstate[bidx] = down;
212 sel_gear = pick_gear(x, y);
213 sel_hit_pos = pick_hit.pos;
220 static void motion(int x, int y)
222 int dx = x - prev_mx;
223 int dy = y - prev_my;
227 if(!dx && !dy) return;
231 Vec3 offs = Vec3(dx * speed, -dy * speed, 0.0);
232 offs = sel_gear->get_dir_matrix() * offs;
234 sel_gear->set_position(sel_gear->get_position() + offs);
235 machine->invalidate_meshing();
239 cam_theta += dx * 0.5;
242 if(cam_phi < -90) cam_phi = -90;
243 if(cam_phi > 90) cam_phi = 90;
247 cam_dist += dy * 0.01;
248 if(cam_dist < 0.0) cam_dist = 0.0;
254 static void passive_motion(int x, int y)
260 static Gear *pick_gear(int x, int y)
263 double viewmat[16], projmat[16];
269 glGetDoublev(GL_MODELVIEW_MATRIX, viewmat);
270 glGetDoublev(GL_PROJECTION_MATRIX, projmat);
271 glGetIntegerv(GL_VIEWPORT, vp);
273 gluUnProject(x, y, 0, viewmat, projmat, vp, pt, pt + 1, pt + 2);
274 ray.origin = Vec3(pt[0], pt[1], pt[2]) * 1000.0f;
276 gluUnProject(x, y, 1, viewmat, projmat, vp, pt, pt + 1, pt + 2);
277 ray.dir = Vec3(pt[0], pt[1], pt[2]) * 1000.0f - ray.origin;
279 return machine->intersect_gear(ray, &pick_hit);