25 void Gear::set_teeth(int nt, float tooth_pitch)
27 float circ = tooth_pitch * nt;
28 radius = circ / (2.0 * M_PI);
30 init_angle = get_angular_pitch() * 3.0 / 8.0;
33 float Gear::get_rotation() const
35 return fmod(init_angle + angle, M_PI * 2.0);
38 float Gear::get_angular_pitch() const
40 return 2.0 * M_PI / (float)nteeth;
43 void Gear::draw() const
46 if(!((Gear*)this)->gen_mesh()) {
53 glMultMatrixf(xform[0]);
57 glPushAttrib(GL_ENABLE_BIT);
58 glDisable(GL_LIGHTING);
63 glVertex3f(0, 0, -10);
71 static Vec2 rev_pos(float u, float v, void *cls)
73 Gear *gear = (Gear*)cls;
75 float y = ((v - 1.0 / 3.0) / (1.0 / 3.0) - 0.5);
76 if(y < -0.5) y = -0.5;
80 if(v < 0.001 || v > 0.999) {
84 float nt = (float)gear->nteeth * 4.0;
86 int part = (int)(u * nt) % 4; /* 4 parts to a tooth /#\_ */
87 float t = fmod(u * nt * 4.0, 1.0);
104 float inner_rad = gear->radius - gear->teeth_length;
105 return Vec2(inner_rad + offs * gear->teeth_length, -y);
108 bool Gear::gen_mesh()
111 gen_revol(mesh, nteeth * 4, 3, rev_pos, 0, this);
113 mesh->calc_face_normals();
116 rot.rotation_x(M_PI / 2.0);
117 mesh->apply_xform(rot, rot);
119 mesh->set_vis_vecsize(6.0);
123 void Gear::calc_matrix() const
125 Vec3 up = Vec3(0, 1, 0);
126 if(1.0 - fabs(dot(up, axis)) < 1e-4) {
129 Vec3 right = normalize(cross(up, axis));
130 up = cross(axis, right);
132 xform = Mat4(right, up, axis);
133 xform.rotate_z(get_rotation());
134 xform.translate(pos);