X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=antikythera;a=blobdiff_plain;f=src%2Fgear.cc;h=c916e765c389519e8405ce3059f220ef3ed0406e;hp=865679c155c0aab3d9234ec1f0ebe168f64cc5fd;hb=b29d7193d03833109cfdda3ab576fe183efd8acf;hpb=a88a9ac53952e1bb3768b147a043c19392e3d5d1 diff --git a/src/gear.cc b/src/gear.cc index 865679c..c916e76 100644 --- a/src/gear.cc +++ b/src/gear.cc @@ -1,6 +1,7 @@ #include #include "gear.h" #include "meshgen.h" +#include "app.h" Gear::Gear() : axis(0, 0, 1) @@ -13,6 +14,7 @@ Gear::Gear() thickness = 5; bevel = 1.5; init_angle = 0; + xform_valid = false; mesh = 0; } @@ -22,17 +24,90 @@ Gear::~Gear() delete mesh; } +void Gear::set_angular_offset(float offs) +{ + init_angle = offs; + xform_valid = false; +} + +float Gear::get_angular_offset() const +{ + return init_angle; +} + void Gear::set_teeth(int nt, float tooth_pitch) { float circ = tooth_pitch * nt; radius = circ / (2.0 * M_PI); nteeth = nt; - init_angle = get_angular_pitch() * 3.0 / 8.0; } -float Gear::get_rotation() const +void Gear::set_axis(const Vec3 &axis) +{ + this->axis = normalize(axis); + xform_valid = false; +} + +const Vec3 &Gear::get_axis() const +{ + return axis; +} + +void Gear::set_position(const Vec3 &pos) +{ + this->pos = pos; + xform_valid = false; +} + +const Vec3 &Gear::get_position() const +{ + return pos; +} + +Vec3 Gear::get_global_position() const +{ + return pos; // TODO +} + +Vec3 Gear::get_planar_position() const +{ + Mat4 inv_xform = transpose(get_dir_matrix()); + return inv_xform * pos; +} + +void Gear::set_angle(float angle) { - return fmod(init_angle + angle, M_PI * 2.0); + this->angle = angle; + xform_valid = false; +} + +float Gear::get_angle() const +{ + return angle; +} + +float Gear::get_vis_rotation() const +{ + float fix_crooked_teeth = get_angular_pitch() * 3.0 / 8.0; + return fmod(init_angle + fix_crooked_teeth + angle, M_PI * 2.0); +} + +const Mat4 &Gear::get_matrix() const +{ + if(!xform_valid) { + calc_matrix(); + xform_valid = true; + } + return xform; +} + +const Mat4 &Gear::get_dir_matrix() const +{ + if(!xform_valid) { + calc_matrix(); + xform_valid = true; + } + return dir_xform; } float Gear::get_angular_pitch() const @@ -47,25 +122,62 @@ void Gear::draw() const abort(); } } - calc_matrix(); + if(!xform_valid) { + calc_matrix(); + } + + glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT); glPushMatrix(); glMultMatrixf(xform[0]); + if(opt_gear_wireframe) { + glPolygonOffset(1, 1); + glEnable(GL_POLYGON_OFFSET_FILL); + } + mesh->draw(); - glPushAttrib(GL_ENABLE_BIT); glDisable(GL_LIGHTING); + if(opt_gear_wireframe) { + glColor3f(0.2, 0.4, 1.0); + mesh->draw_wire(); + } + glLineWidth(2.0); glBegin(GL_LINES); glColor3f(0, 0, 1); glVertex3f(0, 0, -10); glVertex3f(0, 0, 10); glEnd(); + + glPopMatrix(); glPopAttrib(); +} + +void Gear::draw_wire(float wire_width) const +{ + if(!mesh) { + if(!((Gear*)this)->gen_mesh()) { + abort(); + } + } + if(!xform_valid) { + calc_matrix(); + } + + glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT); + glLineWidth(wire_width); + glDisable(GL_LIGHTING); + + glPushMatrix(); + glMultMatrixf(xform[0]); + + mesh->draw_wire(); glPopMatrix(); + glPopAttrib(); } static Vec2 rev_pos(float u, float v, void *cls) @@ -129,7 +241,9 @@ void Gear::calc_matrix() const Vec3 right = normalize(cross(up, axis)); up = cross(axis, right); - xform = Mat4(right, up, axis); - xform.rotate_z(get_rotation()); + dir_xform = Mat4(right, up, axis); + + xform = dir_xform; + xform.rotate_z(get_vis_rotation()); xform.translate(pos); }