23 color = Vec3(0.6, 0.6, 0.6);
33 void Gear::attach(Gear *g)
36 if(g->supergear == this) {
39 g->supergear->detach(g);
42 subgears.push_back(g);
50 bool Gear::detach(Gear *g)
52 int nsubgears = (int)subgears.size();
53 for(int i=0; i<nsubgears; i++) {
54 if(subgears[i] == g) {
55 subgears.erase(subgears.begin() + i);
63 Gear *Gear::get_super() const
68 void Gear::set_angular_offset(float offs)
74 float Gear::get_angular_offset() const
79 void Gear::set_teeth(int nt, float tooth_pitch)
81 float circ = tooth_pitch * nt;
82 radius = circ / (2.0 * M_PI);
86 void Gear::set_axis(const Vec3 &axis)
88 this->axis = normalize(axis);
92 const Vec3 &Gear::get_axis() const
97 void Gear::set_position(const Vec3 &pos)
102 this->pos = supergear->pos;
107 for(int i=0; i<(int)subgears.size(); i++) {
108 Vec3 subpos = this->pos;
109 subpos.z = subgears[i]->pos.z;
110 subgears[i]->set_position(subpos);
114 const Vec3 &Gear::get_position() const
119 Vec3 Gear::get_global_position() const
121 const Mat4 &m = get_matrix();
122 return m * Vec3(0, 0, 0);
125 void Gear::set_angle(float angle)
131 float Gear::get_angle() const
136 float Gear::get_vis_rotation() const
138 return fmod(init_angle + angle, M_PI * 2.0);
141 const Mat4 &Gear::get_matrix() const
150 const Mat4 &Gear::get_dir_matrix() const
159 float Gear::get_angular_pitch() const
161 return 2.0 * M_PI / (float)nteeth;
164 void Gear::draw() const
167 if(!((Gear*)this)->gen_mesh()) {
175 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_LIGHTING_BIT);
178 glMultMatrixf(xform[0]);
180 if(opt_gear_wireframe) {
181 glPolygonOffset(1, 1);
182 glEnable(GL_POLYGON_OFFSET_FILL);
185 Vec3 diffuse = metallic ? Vec3(0, 0, 0) : color;
186 float col[] = {diffuse.x, diffuse.y, diffuse.z, 1.0};
187 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
189 Vec3 specular = (metallic ? color : Vec3(1, 1, 1)) * (1.0 - roughness);
190 float scol[] = {specular.x, specular.y, specular.z, 1.0};
191 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
192 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 1.0 + (1.0 - roughness) * 60.0);
196 glDisable(GL_LIGHTING);
198 if(opt_gear_wireframe) {
199 glColor3f(0.2, 0.4, 1.0);
207 void Gear::draw_wire(float wire_width) const
210 if(!((Gear*)this)->gen_mesh()) {
218 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
219 glLineWidth(wire_width);
220 glDisable(GL_LIGHTING);
223 glMultMatrixf(xform[0]);
231 static Vec2 rev_pos(float u, float v, void *cls)
233 Gear *gear = (Gear*)cls;
235 float y = ((v - 1.0 / 3.0) / (1.0 / 3.0) - 0.5);
236 if(y < -0.5) y = -0.5;
238 y *= gear->thickness;
240 if(v < 0.001 || v > 0.999) {
244 float nt = (float)gear->nteeth * 4.0;
246 int part = (int)(u * nt) % 4; /* 4 parts to a tooth /#\_ */
247 float t = fmod(u * nt * 4.0, 1.0);
264 float inner_rad = gear->radius - gear->teeth_length;
265 return Vec2(inner_rad + offs * gear->teeth_length, -y);
268 bool Gear::gen_mesh()
271 gen_revol(mesh, nteeth * 4, 3, rev_pos, 0, this);
273 mesh->calc_face_normals();
275 float fix_tooth_up = get_angular_pitch() * 3.0 / 8.0;
278 rot.rotation_x(M_PI / 2.0);
279 rot.rotate_z(fix_tooth_up);
280 mesh->apply_xform(rot, rot);
282 mesh->set_vis_vecsize(6.0);
286 void Gear::calc_matrix() const
288 Vec3 up = Vec3(0, 1, 0);
289 if(1.0 - fabs(dot(up, axis)) < 1e-4) {
292 Vec3 right = normalize(cross(up, axis));
293 up = cross(axis, right);
295 dir_xform = Mat4(right, up, axis);
298 xform.rotate_z(get_vis_rotation());
299 xform.translate(pos);
301 axel_xform = dir_xform;
302 axel_xform.translate(pos);