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 void Gear::set_position(const Vec3 &pos)
57 const Vec3 &Gear::get_position() const
62 Vec3 Gear::get_global_position() const
67 Vec3 Gear::get_planar_position() const
69 Mat4 inv_xform = transpose(get_dir_matrix());
70 return inv_xform * pos;
73 void Gear::set_angle(float angle)
79 float Gear::get_angle() const
84 float Gear::get_vis_rotation() const
86 float fix_crooked_teeth = get_angular_pitch() * 3.0 / 8.0;
87 return fmod(init_angle + fix_crooked_teeth + angle, M_PI * 2.0);
90 const Mat4 &Gear::get_matrix() const
99 const Mat4 &Gear::get_dir_matrix() const
108 float Gear::get_angular_pitch() const
110 return 2.0 * M_PI / (float)nteeth;
113 void Gear::draw() const
116 if(!((Gear*)this)->gen_mesh()) {
124 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
127 glMultMatrixf(xform[0]);
129 if(opt_gear_wireframe) {
130 glPolygonOffset(1, 1);
131 glEnable(GL_POLYGON_OFFSET_FILL);
136 glDisable(GL_LIGHTING);
138 if(opt_gear_wireframe) {
139 glColor3f(0.2, 0.4, 1.0);
146 glVertex3f(0, 0, -10);
147 glVertex3f(0, 0, 10);
154 void Gear::draw_wire(float wire_width) const
157 if(!((Gear*)this)->gen_mesh()) {
165 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
166 glLineWidth(wire_width);
167 glDisable(GL_LIGHTING);
170 glMultMatrixf(xform[0]);
178 static Vec2 rev_pos(float u, float v, void *cls)
180 Gear *gear = (Gear*)cls;
182 float y = ((v - 1.0 / 3.0) / (1.0 / 3.0) - 0.5);
183 if(y < -0.5) y = -0.5;
185 y *= gear->thickness;
187 if(v < 0.001 || v > 0.999) {
191 float nt = (float)gear->nteeth * 4.0;
193 int part = (int)(u * nt) % 4; /* 4 parts to a tooth /#\_ */
194 float t = fmod(u * nt * 4.0, 1.0);
211 float inner_rad = gear->radius - gear->teeth_length;
212 return Vec2(inner_rad + offs * gear->teeth_length, -y);
215 bool Gear::gen_mesh()
218 gen_revol(mesh, nteeth * 4, 3, rev_pos, 0, this);
220 mesh->calc_face_normals();
223 rot.rotation_x(M_PI / 2.0);
224 mesh->apply_xform(rot, rot);
226 mesh->set_vis_vecsize(6.0);
230 void Gear::calc_matrix() const
232 Vec3 up = Vec3(0, 1, 0);
233 if(1.0 - fabs(dot(up, axis)) < 1e-4) {
236 Vec3 right = normalize(cross(up, axis));
237 up = cross(axis, right);
239 dir_xform = Mat4(right, up, axis);
242 xform.rotate_z(get_vis_rotation());
243 xform.translate(pos);