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);
91 machine->calc_meshing();
93 start_time = glutGet(GLUT_ELAPSED_TIME);
104 unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
105 float dt = (float)(msec - prev_msec) / 1000.0f;
110 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
112 glMatrixMode(GL_MODELVIEW);
114 glTranslatef(0, 0, -cam_dist);
115 glRotatef(cam_phi, 1, 0, 0);
116 glRotatef(cam_theta, 0, 1, 0);
121 assert(glGetError() == GL_NO_ERROR);
131 /* world scale is in meters, gears are in millimeters, scale by 1/1000 */
133 glScalef(0.001, 0.001, 0.001);
140 void reshape(int x, int y)
142 glViewport(0, 0, x, y);
144 glMatrixMode(GL_PROJECTION);
146 gluPerspective(50.0, (float)x / (float)y, 0.01, 100.0);
149 void keyb(unsigned char key, int x, int y)
156 opt_gear_wireframe = !opt_gear_wireframe;
162 void mouse(int bn, int st, int x, int y)
166 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
169 void motion(int x, int y)
171 int dx = x - prev_mx;
172 int dy = y - prev_my;
176 if(!dx && !dy) return;
179 cam_theta += dx * 0.5;
182 if(cam_phi < -90) cam_phi = -90;
183 if(cam_phi > 90) cam_phi = 90;
187 cam_dist += dy * 0.01;
188 if(cam_dist < 0.0) cam_dist = 0.0;