From: John Tsiombikas Date: Sat, 17 Sep 2016 05:39:00 +0000 (+0300) Subject: meshing recaclulation on the fly, picking, whatever X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=antikythera;a=commitdiff_plain;h=014fccdee30291407985658e8cefd9be7dc9e9fa meshing recaclulation on the fly, picking, whatever --- diff --git a/src/gear.cc b/src/gear.cc index 6ce9d8a..10e8bc8 100644 --- a/src/gear.cc +++ b/src/gear.cc @@ -29,7 +29,6 @@ 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; } void Gear::set_axis(const Vec3 &axis) @@ -54,6 +53,12 @@ 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) { this->angle = angle; @@ -67,7 +72,8 @@ float Gear::get_angle() const float Gear::get_vis_rotation() const { - return fmod(init_angle + angle, M_PI * 2.0); + 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 @@ -85,7 +91,7 @@ const Mat4 &Gear::get_dir_matrix() const calc_matrix(); xform_valid = true; } - return xform; + return dir_xform; } float Gear::get_angular_pitch() const @@ -100,7 +106,9 @@ void Gear::draw() const abort(); } } - calc_matrix(); + if(!xform_valid) { + calc_matrix(); + } glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT); @@ -132,6 +140,30 @@ void Gear::draw() const 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) { Gear *gear = (Gear*)cls; diff --git a/src/gear.h b/src/gear.h index 281e77f..45a3d13 100644 --- a/src/gear.h +++ b/src/gear.h @@ -79,6 +79,8 @@ public: const Vec3 &get_position() const; Vec3 get_global_position() const; // taking parent gear into account + Vec3 get_planar_position() const; // 2D pos of gear on its plane + void set_angle(float angle); float get_angle() const; @@ -90,6 +92,7 @@ public: float get_angular_pitch() const; void draw() const; + void draw_wire(float line_width = 1.0f) const; bool gen_mesh(); }; diff --git a/src/machine.cc b/src/machine.cc index 274362a..913b0b0 100644 --- a/src/machine.cc +++ b/src/machine.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "machine.h" @@ -63,10 +64,10 @@ void Machine::calc_meshing() visited = new bool[ngears]; } - // we're going to need the inverse of each gear's matrix, so let's cache it here - Mat4 *inv_xform = (Mat4*)alloca(ngears * sizeof *inv_xform); + // we're going to need the planar position of each gear on its plane, so let's cache it + Vec3 *ppos = (Vec3*)alloca(ngears * sizeof *ppos); for(int i=0; iget_dir_matrix()); + ppos[i] = gears[i]->get_planar_position(); } for(int i=0; iaxis, gears[j]->axis)) < 1e-5) { // co-planar, just check Z range after inverse-transforming to the XY plane - Vec3 pos_i = inv_xform[i] * gears[i]->get_position(); - Vec3 pos_j = inv_xform[j] * gears[j]->get_position(); - - if(fabs(pos_i.z - pos_j.z) > (gears[i]->thickness + gears[j]->thickness) / 2.0) { + if(fabs(ppos[i].z - ppos[j].z) > (gears[i]->thickness + gears[j]->thickness) / 2.0) { continue; } // Z interval match, check distance - float dsq = length_sq(pos_i.xy() - pos_j.xy()); + float dsq = length_sq(ppos[i].xy() - ppos[j].xy()); float outer_rad_sum = gears[i]->radius + gears[j]->radius; float inner_rad_sum = outer_rad_sum - gears[i]->teeth_length - gears[j]->teeth_length; @@ -107,12 +105,19 @@ void Machine::calc_meshing() // fix the initial angles so that teeth mesh as best as possible // should work in one pass as long as the gear train is not impossible for(int i=0; iinit_angle = 0; + } + + for(int i=0; iinit_angle / gears[i]->get_angular_pitch() + 1.0, 1.0); - float frac_j = fmod(gears[j]->init_angle / gears[j]->get_angular_pitch() + 1.0, 1.0); + float frac_j = fmod((gears[j]->init_angle + rel_angle) / gears[j]->get_angular_pitch() + 1.0, 1.0); float delta = frac_j - frac_i; float correction = 0.5 - delta; @@ -120,6 +125,12 @@ void Machine::calc_meshing() } } } + + /* + for(int i=0; iinit_angle); + } + */ } void Machine::update_gear(int idx, float angle) @@ -128,6 +139,7 @@ void Machine::update_gear(int idx, float angle) if(delta_angle(angle, gears[idx]->angle) > 0.25 / gears[idx]->nteeth) { fprintf(stderr, "warning: trying to transmit different values to gear %s (%d)\n", gears[idx]->name.c_str(), idx); + gears[idx]->angle = 0; } return; } @@ -170,6 +182,29 @@ void Machine::draw() const } } +Gear *Machine::intersect_gear(const Ray &ray, HitPoint *hitp) const +{ + Gear *res = 0; + HitPoint nearest; + nearest.dist = FLT_MAX; + + for(size_t i=0; iget_global_position(); + float rad = gears[i]->radius; + + Plane plane = Plane(pos, gears[i]->axis); + + HitPoint hit; + if(plane.intersect(ray, &hit) && hit.dist < nearest.dist && + length_sq(hit.pos - pos) <= rad * rad) { + nearest = hit; + res = gears[i]; + } + } + + if(hitp) *hitp = nearest; + return res; +} static float delta_angle(float a, float b) { diff --git a/src/machine.h b/src/machine.h index 5513e02..86caa48 100644 --- a/src/machine.h +++ b/src/machine.h @@ -32,6 +32,8 @@ public: void update(float dt); void draw() const; + + Gear *intersect_gear(const Ray &ray, HitPoint *hitp = 0) const; }; #endif // MACHINE_H_ diff --git a/src/main.cc b/src/main.cc index 6fc85ba..f98f190 100644 --- a/src/main.cc +++ b/src/main.cc @@ -10,15 +10,19 @@ #include "app.h" #include "machine.h" -bool init(); -void cleanup(); -void display(); -void idle(); -void draw_gears(); -void reshape(int x, int y); -void keyb(unsigned char key, int x, int y); -void mouse(int bn, int st, int x, int y); -void motion(int x, int y); +static bool init(); +static void cleanup(); +static void display(); +static void idle(); +static void draw_gears(); +static void reshape(int x, int y); +static void keyb(unsigned char key, int x, int y); +static void mouse(int bn, int st, int x, int y); +static void motion(int x, int y); +static void passive_motion(int x, int y); +static Gear *pick_gear(int x, int y); + +static int win_width, win_height; static float cam_dist = 0.5; static float cam_theta, cam_phi; @@ -27,6 +31,9 @@ static bool bnstate[8]; static unsigned int start_time, prev_msec; static Machine *machine; +static Gear *hover_gear, *sel_gear; +static HitPoint pick_hit; +static Vec3 sel_hit_pos; int main(int argc, char **argv) { @@ -41,6 +48,7 @@ int main(int argc, char **argv) glutKeyboardFunc(keyb); glutMouseFunc(mouse); glutMotionFunc(motion); + glutPassiveMotionFunc(passive_motion); if(!init()) { return 1; @@ -51,7 +59,7 @@ int main(int argc, char **argv) return 0; } -bool init() +static bool init() { glewInit(); @@ -93,19 +101,27 @@ bool init() return true; } -void cleanup() +static void cleanup() { delete machine; } -void display() +static void update(float dt) +{ + machine->update(dt); + + if(sel_gear) { + } + + hover_gear = pick_gear(prev_mx, prev_my); +} + +static void display() { unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time; float dt = (float)(msec - prev_msec) / 1000.0f; prev_msec = msec; - machine->update(dt); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); @@ -114,30 +130,54 @@ void display() glRotatef(cam_phi, 1, 0, 0); glRotatef(cam_theta, 0, 1, 0); + update(dt); + draw_gears(); glutSwapBuffers(); assert(glGetError() == GL_NO_ERROR); } -void idle() +static void idle() { glutPostRedisplay(); } -void draw_gears() +static void draw_gears() { /* world scale is in meters, gears are in millimeters, scale by 1/1000 */ glPushMatrix(); glScalef(0.001, 0.001, 0.001); + if(sel_gear || hover_gear) { + glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POLYGON_BIT); + + glDisable(GL_LIGHTING); + glFrontFace(GL_CW); + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glLineWidth(3.0); + + if(sel_gear) { + glColor3f(0.2, 1.0, 0.3); + sel_gear->draw(); + } else { + glColor3f(1.0, 0.75, 0.2); + hover_gear->draw(); + } + + glPopAttrib(); + } + machine->draw(); glPopMatrix(); } -void reshape(int x, int y) +static void reshape(int x, int y) { + win_width = x; + win_height = y; + glViewport(0, 0, x, y); glMatrixMode(GL_PROJECTION); @@ -145,7 +185,7 @@ void reshape(int x, int y) gluPerspective(50.0, (float)x / (float)y, 0.01, 100.0); } -void keyb(unsigned char key, int x, int y) +static void keyb(unsigned char key, int x, int y) { switch(key) { case 27: @@ -158,14 +198,26 @@ void keyb(unsigned char key, int x, int y) } } -void mouse(int bn, int st, int x, int y) +static void mouse(int bn, int st, int x, int y) { + int bidx = bn - GLUT_LEFT_BUTTON; + bool down = st == GLUT_DOWN; + prev_mx = x; prev_my = y; - bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; + bnstate[bidx] = down; + + if(bidx == 0) { + if(down) { + sel_gear = pick_gear(x, y); + sel_hit_pos = pick_hit.pos; + } else { + sel_gear = 0; + } + } } -void motion(int x, int y) +static void motion(int x, int y) { int dx = x - prev_mx; int dy = y - prev_my; @@ -174,17 +226,55 @@ void motion(int x, int y) if(!dx && !dy) return; - if(bnstate[0]) { - cam_theta += dx * 0.5; - cam_phi += dy * 0.5; - - if(cam_phi < -90) cam_phi = -90; - if(cam_phi > 90) cam_phi = 90; - glutPostRedisplay(); - } - if(bnstate[2]) { - cam_dist += dy * 0.01; - if(cam_dist < 0.0) cam_dist = 0.0; - glutPostRedisplay(); + if(sel_gear) { + float speed = 0.5; + Vec3 offs = Vec3(dx * speed, -dy * speed, 0.0); + offs = sel_gear->get_dir_matrix() * offs; + + sel_gear->set_position(sel_gear->get_position() + offs); + machine->invalidate_meshing(); + + } else { + if(bnstate[0]) { + cam_theta += dx * 0.5; + cam_phi += dy * 0.5; + + if(cam_phi < -90) cam_phi = -90; + if(cam_phi > 90) cam_phi = 90; + glutPostRedisplay(); + } + if(bnstate[2]) { + cam_dist += dy * 0.01; + if(cam_dist < 0.0) cam_dist = 0.0; + glutPostRedisplay(); + } } } + +static void passive_motion(int x, int y) +{ + prev_mx = x; + prev_my = y; +} + +static Gear *pick_gear(int x, int y) +{ + double pt[3]; + double viewmat[16], projmat[16]; + int vp[4]; + Ray ray; + + y = win_height - y; + + glGetDoublev(GL_MODELVIEW_MATRIX, viewmat); + glGetDoublev(GL_PROJECTION_MATRIX, projmat); + glGetIntegerv(GL_VIEWPORT, vp); + + gluUnProject(x, y, 0, viewmat, projmat, vp, pt, pt + 1, pt + 2); + ray.origin = Vec3(pt[0], pt[1], pt[2]) * 1000.0f; + + gluUnProject(x, y, 1, viewmat, projmat, vp, pt, pt + 1, pt + 2); + ray.dir = Vec3(pt[0], pt[1], pt[2]) * 1000.0f - ray.origin; + + return machine->intersect_gear(ray, &pick_hit); +}