27 void Gear::set_teeth(int nt, float tooth_pitch)
29 float circ = tooth_pitch * nt;
30 radius = circ / (2.0 * M_PI);
34 void Gear::set_axis(const Vec3 &axis)
36 this->axis = normalize(axis);
40 void Gear::set_position(const Vec3 &pos)
46 const Vec3 &Gear::get_position() const
51 Vec3 Gear::get_global_position() const
56 Vec3 Gear::get_planar_position() const
58 Mat4 inv_xform = transpose(get_dir_matrix());
59 return inv_xform * pos;
62 void Gear::set_angle(float angle)
68 float Gear::get_angle() const
73 float Gear::get_vis_rotation() const
75 float fix_crooked_teeth = get_angular_pitch() * 3.0 / 8.0;
76 return fmod(init_angle + fix_crooked_teeth + angle, M_PI * 2.0);
79 const Mat4 &Gear::get_matrix() const
88 const Mat4 &Gear::get_dir_matrix() const
97 float Gear::get_angular_pitch() const
99 return 2.0 * M_PI / (float)nteeth;
102 void Gear::draw() const
105 if(!((Gear*)this)->gen_mesh()) {
113 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
116 glMultMatrixf(xform[0]);
118 if(opt_gear_wireframe) {
119 glPolygonOffset(1, 1);
120 glEnable(GL_POLYGON_OFFSET_FILL);
125 glDisable(GL_LIGHTING);
127 if(opt_gear_wireframe) {
128 glColor3f(0.2, 0.4, 1.0);
135 glVertex3f(0, 0, -10);
136 glVertex3f(0, 0, 10);
143 void Gear::draw_wire(float wire_width) const
146 if(!((Gear*)this)->gen_mesh()) {
154 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
155 glLineWidth(wire_width);
156 glDisable(GL_LIGHTING);
159 glMultMatrixf(xform[0]);
167 static Vec2 rev_pos(float u, float v, void *cls)
169 Gear *gear = (Gear*)cls;
171 float y = ((v - 1.0 / 3.0) / (1.0 / 3.0) - 0.5);
172 if(y < -0.5) y = -0.5;
174 y *= gear->thickness;
176 if(v < 0.001 || v > 0.999) {
180 float nt = (float)gear->nteeth * 4.0;
182 int part = (int)(u * nt) % 4; /* 4 parts to a tooth /#\_ */
183 float t = fmod(u * nt * 4.0, 1.0);
200 float inner_rad = gear->radius - gear->teeth_length;
201 return Vec2(inner_rad + offs * gear->teeth_length, -y);
204 bool Gear::gen_mesh()
207 gen_revol(mesh, nteeth * 4, 3, rev_pos, 0, this);
209 mesh->calc_face_normals();
212 rot.rotation_x(M_PI / 2.0);
213 mesh->apply_xform(rot, rot);
215 mesh->set_vis_vecsize(6.0);
219 void Gear::calc_matrix() const
221 Vec3 up = Vec3(0, 1, 0);
222 if(1.0 - fabs(dot(up, axis)) < 1e-4) {
225 Vec3 right = normalize(cross(up, axis));
226 up = cross(axis, right);
228 dir_xform = Mat4(right, up, axis);
231 xform.rotate_z(get_vis_rotation());
232 xform.translate(pos);