27 void Gear::set_angular_offset(float offs)
33 float Gear::get_angular_offset() const
38 void Gear::set_teeth(int nt, float tooth_pitch)
40 float circ = tooth_pitch * nt;
41 radius = circ / (2.0 * M_PI);
45 void Gear::set_axis(const Vec3 &axis)
47 this->axis = normalize(axis);
51 const Vec3 &Gear::get_axis() const
56 void Gear::set_position(const Vec3 &pos)
62 const Vec3 &Gear::get_position() const
67 Vec3 Gear::get_global_position() const
72 Vec3 Gear::get_planar_position() const
74 Mat4 inv_xform = transpose(get_dir_matrix());
75 return inv_xform * pos;
78 void Gear::set_angle(float angle)
84 float Gear::get_angle() const
89 float Gear::get_vis_rotation() const
91 float fix_crooked_teeth = get_angular_pitch() * 3.0 / 8.0;
92 return fmod(init_angle + fix_crooked_teeth + angle, M_PI * 2.0);
95 const Mat4 &Gear::get_matrix() const
104 const Mat4 &Gear::get_dir_matrix() const
113 float Gear::get_angular_pitch() const
115 return 2.0 * M_PI / (float)nteeth;
118 void Gear::draw() const
121 if(!((Gear*)this)->gen_mesh()) {
129 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
132 glMultMatrixf(xform[0]);
134 if(opt_gear_wireframe) {
135 glPolygonOffset(1, 1);
136 glEnable(GL_POLYGON_OFFSET_FILL);
141 glDisable(GL_LIGHTING);
143 if(opt_gear_wireframe) {
144 glColor3f(0.2, 0.4, 1.0);
151 glVertex3f(0, 0, -10);
152 glVertex3f(0, 0, 10);
159 void Gear::draw_wire(float wire_width) const
162 if(!((Gear*)this)->gen_mesh()) {
170 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
171 glLineWidth(wire_width);
172 glDisable(GL_LIGHTING);
175 glMultMatrixf(xform[0]);
183 static Vec2 rev_pos(float u, float v, void *cls)
185 Gear *gear = (Gear*)cls;
187 float y = ((v - 1.0 / 3.0) / (1.0 / 3.0) - 0.5);
188 if(y < -0.5) y = -0.5;
190 y *= gear->thickness;
192 if(v < 0.001 || v > 0.999) {
196 float nt = (float)gear->nteeth * 4.0;
198 int part = (int)(u * nt) % 4; /* 4 parts to a tooth /#\_ */
199 float t = fmod(u * nt * 4.0, 1.0);
216 float inner_rad = gear->radius - gear->teeth_length;
217 return Vec2(inner_rad + offs * gear->teeth_length, -y);
220 bool Gear::gen_mesh()
223 gen_revol(mesh, nteeth * 4, 3, rev_pos, 0, this);
225 mesh->calc_face_normals();
228 rot.rotation_x(M_PI / 2.0);
229 mesh->apply_xform(rot, rot);
231 mesh->set_vis_vecsize(6.0);
235 void Gear::calc_matrix() const
237 Vec3 up = Vec3(0, 1, 0);
238 if(1.0 - fabs(dot(up, axis)) < 1e-4) {
241 Vec3 right = normalize(cross(up, axis));
242 up = cross(axis, right);
244 dir_xform = Mat4(right, up, axis);
247 xform.rotate_z(get_vis_rotation());
248 xform.translate(pos);