27 void Gear::set_teeth(int nt, float tooth_pitch)
29 float circ = tooth_pitch * nt;
30 radius = circ / (2.0 * M_PI);
32 init_angle = get_angular_pitch() * 3.0 / 8.0;
35 void Gear::set_axis(const Vec3 &axis)
37 this->axis = normalize(axis);
41 void Gear::set_position(const Vec3 &pos)
47 const Vec3 &Gear::get_position() const
52 Vec3 Gear::get_global_position() const
57 void Gear::set_angle(float angle)
63 float Gear::get_angle() const
68 float Gear::get_vis_rotation() const
70 return fmod(init_angle + angle, M_PI * 2.0);
73 const Mat4 &Gear::get_matrix() const
82 const Mat4 &Gear::get_dir_matrix() const
91 float Gear::get_angular_pitch() const
93 return 2.0 * M_PI / (float)nteeth;
96 void Gear::draw() const
99 if(!((Gear*)this)->gen_mesh()) {
105 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
108 glMultMatrixf(xform[0]);
110 if(opt_gear_wireframe) {
111 glPolygonOffset(1, 1);
112 glEnable(GL_POLYGON_OFFSET_FILL);
117 glDisable(GL_LIGHTING);
119 if(opt_gear_wireframe) {
120 glColor3f(0.2, 0.4, 1.0);
127 glVertex3f(0, 0, -10);
128 glVertex3f(0, 0, 10);
135 static Vec2 rev_pos(float u, float v, void *cls)
137 Gear *gear = (Gear*)cls;
139 float y = ((v - 1.0 / 3.0) / (1.0 / 3.0) - 0.5);
140 if(y < -0.5) y = -0.5;
142 y *= gear->thickness;
144 if(v < 0.001 || v > 0.999) {
148 float nt = (float)gear->nteeth * 4.0;
150 int part = (int)(u * nt) % 4; /* 4 parts to a tooth /#\_ */
151 float t = fmod(u * nt * 4.0, 1.0);
168 float inner_rad = gear->radius - gear->teeth_length;
169 return Vec2(inner_rad + offs * gear->teeth_length, -y);
172 bool Gear::gen_mesh()
175 gen_revol(mesh, nteeth * 4, 3, rev_pos, 0, this);
177 mesh->calc_face_normals();
180 rot.rotation_x(M_PI / 2.0);
181 mesh->apply_xform(rot, rot);
183 mesh->set_vis_vecsize(6.0);
187 void Gear::calc_matrix() const
189 Vec3 up = Vec3(0, 1, 0);
190 if(1.0 - fabs(dot(up, axis)) < 1e-4) {
193 Vec3 right = normalize(cross(up, axis));
194 up = cross(axis, right);
196 dir_xform = Mat4(right, up, axis);
199 xform.rotate_z(get_vis_rotation());
200 xform.translate(pos);