18 void reshape(int x, int y);
19 void keyb(unsigned char key, int x, int y);
20 void mouse(int bn, int st, int x, int y);
21 void motion(int x, int y);
23 static float cam_dist = 0.5;
24 static float cam_theta, cam_phi;
25 static int prev_mx, prev_my;
26 static bool bnstate[8];
28 static unsigned int start_time, prev_msec;
29 static Machine *machine;
31 int main(int argc, char **argv)
33 glutInitWindowSize(1024, 768);
34 glutInit(&argc, argv);
35 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
36 glutCreateWindow("Antikythera");
38 glutDisplayFunc(display);
40 glutReshapeFunc(reshape);
41 glutKeyboardFunc(keyb);
43 glutMotionFunc(motion);
58 glEnable(GL_MULTISAMPLE);
59 glEnable(GL_DEPTH_TEST);
60 glEnable(GL_CULL_FACE);
61 glEnable(GL_LIGHTING);
63 glEnable(GL_NORMALIZE);
65 Mesh::use_custom_sdr_attr = false;
67 machine = new Machine;
69 const float pitch = 10.0f;
71 Gear *gear1 = new Gear;
72 gear1->pos = Vec3(-50, 0, 0);
73 gear1->set_teeth(16, pitch);
75 machine->add_gear(gear1);
77 Gear *gear2 = new Gear;
78 gear2->set_teeth(32, pitch);
79 gear2->pos = gear1->pos + Vec3(gear1->radius + gear2->radius - gear1->teeth_length * 0.75, 0, 0);
82 machine->add_gear(gear2);
84 Gear *gear3 = new Gear;
85 gear3->set_teeth(8, pitch);
86 gear3->pos = gear2->pos + Vec3(0, gear2->radius + gear3->radius - gear2->teeth_length * 0.75, 0);
88 machine->add_gear(gear3);
90 machine->add_motor(0, 1.0);
92 start_time = glutGet(GLUT_ELAPSED_TIME);
103 unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
104 float dt = (float)(msec - prev_msec) / 1000.0f;
109 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
111 glMatrixMode(GL_MODELVIEW);
113 glTranslatef(0, 0, -cam_dist);
114 glRotatef(cam_phi, 1, 0, 0);
115 glRotatef(cam_theta, 0, 1, 0);
120 assert(glGetError() == GL_NO_ERROR);
130 /* world scale is in meters, gears are in millimeters, scale by 1/1000 */
132 glScalef(0.001, 0.001, 0.001);
139 void reshape(int x, int y)
141 glViewport(0, 0, x, y);
143 glMatrixMode(GL_PROJECTION);
145 gluPerspective(50.0, (float)x / (float)y, 0.01, 100.0);
148 void keyb(unsigned char key, int x, int y)
155 opt_gear_wireframe = !opt_gear_wireframe;
161 void mouse(int bn, int st, int x, int y)
165 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
168 void motion(int x, int y)
170 int dx = x - prev_mx;
171 int dy = y - prev_my;
175 if(!dx && !dy) return;
178 cam_theta += dx * 0.5;
181 if(cam_phi < -90) cam_phi = -90;
182 if(cam_phi > 90) cam_phi = 90;
186 cam_dist += dy * 0.01;
187 if(cam_dist < 0.0) cam_dist = 0.0;