24 color = Vec3(0.6, 0.6, 0.6);
34 void Gear::attach(Gear *g)
37 if(g->supergear == this) {
40 g->supergear->detach(g);
43 subgears.push_back(g);
51 bool Gear::detach(Gear *g)
53 int nsubgears = (int)subgears.size();
54 for(int i=0; i<nsubgears; i++) {
55 if(subgears[i] == g) {
56 subgears.erase(subgears.begin() + i);
64 Gear *Gear::get_super() const
69 void Gear::set_angular_offset(float offs)
75 float Gear::get_angular_offset() const
80 void Gear::set_teeth(int nt, float tooth_pitch)
82 if(tooth_pitch <= 0) {
83 tooth_pitch = this->tooth_pitch;
85 this->tooth_pitch = tooth_pitch;
87 float circ = tooth_pitch * nt;
88 radius = circ / (2.0 * M_PI);
92 void Gear::set_axis(const Vec3 &axis)
94 this->axis = normalize(axis);
98 const Vec3 &Gear::get_axis() const
103 void Gear::set_position(const Vec3 &pos)
108 this->pos = supergear->pos;
113 for(int i=0; i<(int)subgears.size(); i++) {
114 Vec3 subpos = this->pos;
115 subpos.z = subgears[i]->pos.z;
116 subgears[i]->set_position(subpos);
120 const Vec3 &Gear::get_position() const
125 Vec3 Gear::get_global_position() const
127 const Mat4 &m = get_matrix();
128 return m * Vec3(0, 0, 0);
131 void Gear::set_angle(float angle)
137 float Gear::get_angle() const
142 float Gear::get_vis_rotation() const
144 return fmod(init_angle + angle, M_PI * 2.0);
147 const Mat4 &Gear::get_matrix() const
156 const Mat4 &Gear::get_dir_matrix() const
165 float Gear::get_angular_pitch() const
167 return 2.0 * M_PI / (float)nteeth;
170 void Gear::draw() const
173 if(!((Gear*)this)->gen_mesh()) {
181 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_LIGHTING_BIT);
184 glMultMatrixf(xform[0]);
186 if(opt_gear_wireframe) {
187 glPolygonOffset(1, 1);
188 glEnable(GL_POLYGON_OFFSET_FILL);
191 Vec3 diffuse = metallic ? Vec3(0, 0, 0) : color;
192 float col[] = {diffuse.x, diffuse.y, diffuse.z, 1.0};
193 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
195 Vec3 specular = (metallic ? color : Vec3(1, 1, 1)) * (1.0 - roughness);
196 float scol[] = {specular.x, specular.y, specular.z, 1.0};
197 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
198 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 1.0 + (1.0 - roughness) * 60.0);
202 glDisable(GL_LIGHTING);
204 if(opt_gear_wireframe) {
205 glColor3f(0.2, 0.4, 1.0);
213 void Gear::draw_wire(float wire_width) const
216 if(!((Gear*)this)->gen_mesh()) {
224 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
225 glLineWidth(wire_width);
226 glDisable(GL_LIGHTING);
229 glMultMatrixf(xform[0]);
237 static Vec2 rev_pos(float u, float v, void *cls)
239 Gear *gear = (Gear*)cls;
241 float y = ((v - 1.0 / 3.0) / (1.0 / 3.0) - 0.5);
242 if(y < -0.5) y = -0.5;
244 y *= gear->thickness;
246 if(v < 0.001 || v > 0.999) {
250 float nt = (float)gear->nteeth * 4.0;
252 int part = (int)(u * nt) % 4; /* 4 parts to a tooth /#\_ */
253 float t = fmod(u * nt * 4.0, 1.0);
270 float inner_rad = gear->radius - gear->teeth_length;
271 return Vec2(inner_rad + offs * gear->teeth_length, -y);
274 bool Gear::gen_mesh()
277 gen_revol(mesh, nteeth * 4, 3, rev_pos, 0, this);
279 mesh->calc_face_normals();
281 float fix_tooth_up = get_angular_pitch() * 3.0 / 8.0;
284 rot.rotation_x(M_PI / 2.0);
285 rot.rotate_z(fix_tooth_up);
286 mesh->apply_xform(rot, rot);
288 mesh->set_vis_vecsize(6.0);
292 void Gear::calc_matrix() const
294 Vec3 up = Vec3(0, 1, 0);
295 if(1.0 - fabs(dot(up, axis)) < 1e-4) {
298 Vec3 right = normalize(cross(up, axis));
299 up = cross(axis, right);
301 dir_xform = Mat4(right, up, axis);
304 xform.rotate_z(get_vis_rotation());
305 xform.translate(pos);
307 axel_xform = dir_xform;
308 axel_xform.translate(pos);