gears meshing, machines of gears, machines of MADNESS
[antikythera] / src / gear.cc
index 6450c65..865679c 100644 (file)
@@ -12,6 +12,7 @@ Gear::Gear()
        teeth_length = 5;
        thickness = 5;
        bevel = 1.5;
+       init_angle = 0;
 
        mesh = 0;
 }
@@ -21,6 +22,24 @@ Gear::~Gear()
        delete mesh;
 }
 
+void Gear::set_teeth(int nt, float tooth_pitch)
+{
+       float circ = tooth_pitch * nt;
+       radius = circ / (2.0 * M_PI);
+       nteeth = nt;
+       init_angle = get_angular_pitch() * 3.0 / 8.0;
+}
+
+float Gear::get_rotation() const
+{
+       return fmod(init_angle + angle, M_PI * 2.0);
+}
+
+float Gear::get_angular_pitch() const
+{
+       return 2.0 * M_PI / (float)nteeth;
+}
+
 void Gear::draw() const
 {
        if(!mesh) {
@@ -35,13 +54,16 @@ void Gear::draw() const
 
        mesh->draw();
 
-       /*
        glPushAttrib(GL_ENABLE_BIT);
        glDisable(GL_LIGHTING);
-       glColor3f(0, 1, 0);
-       mesh->draw_normals();
+
+       glLineWidth(2.0);
+       glBegin(GL_LINES);
+       glColor3f(0, 0, 1);
+       glVertex3f(0, 0, -10);
+       glVertex3f(0, 0, 10);
+       glEnd();
        glPopAttrib();
-       */
 
        glPopMatrix();
 }
@@ -108,5 +130,6 @@ void Gear::calc_matrix() const
        up = cross(axis, right);
 
        xform = Mat4(right, up, axis);
+       xform.rotate_z(get_rotation());
        xform.translate(pos);
 }