X-Git-Url: https://eleni.mutantstargoat.com/git/?p=hair;a=blobdiff_plain;f=src%2Fhair.cc;h=b2eb4a058f2731d3ffa216ec7dbc1f3d7000a6cb;hp=33bdd0d43f2f41ac91c4d773032ce6d15c619ed2;hb=8efb44efc8830ffc5ae8eb387926f8b4fd00fb6e;hpb=43078ddde36cc81375cddfe45f47f806b9d19307 diff --git a/src/hair.cc b/src/hair.cc index 33bdd0d..b2eb4a0 100644 --- a/src/hair.cc +++ b/src/hair.cc @@ -8,13 +8,24 @@ #include "kdtree.h" #include "hair.h" +/* spring constant */ + +#define K_ANC 4.0 +#define DAMPING 1.5 + struct Triangle { Vec3 v[3]; Vec3 n[3]; }; -Hair::Hair() {} -Hair::~Hair() {} +Hair::Hair() +{ + hair_length = 0.5; +} + +Hair::~Hair() +{ +} static Vec3 calc_rand_point(const Triangle &tr, Vec3 *bary) { @@ -34,7 +45,6 @@ static Vec3 calc_rand_point(const Triangle &tr, Vec3 *bary) bary->y = v; bary->z = c; -// printf("u %f v %f c %f sum: %f\n", u, v, c, u+v+c); return rp; } @@ -72,7 +82,7 @@ static void get_spawn_triangles(const Mesh *m, float thresh, std::vectorpush_back(t); } } - printf("spawn tri AABB: min y: %f max y: %f\n", min_y, max_y); +/* printf("spawn tri AABB: min y: %f max y: %f\n", min_y, max_y);*/ } bool Hair::init(const Mesh *m, int max_num_spawns, float thresh) @@ -104,28 +114,114 @@ bool Hair::init(const Mesh *m, int max_num_spawns, float thresh) continue; } + HairStrand strand; /* weighted sum of the triangle's vertex normals */ - Vec3 spawn_dir = rtriangle.n[0] * bary.x + rtriangle.n[1] * bary.y + rtriangle.n[2] * bary.z; - spawn_directions.push_back(normalize(spawn_dir)); - spawn_points.push_back(rpoint); + strand.spawn_dir = normalize(rtriangle.n[0] * bary.x + rtriangle.n[1] * bary.y + rtriangle.n[2] * bary.z); + strand.spawn_pt = rpoint; + hair.push_back(strand); + kd_insert3f(kd, rpoint.x, rpoint.y, rpoint.z, 0); } kd_free(kd); + + for(size_t i=0; ixform = xform; +} + +void Hair::update(float dt) +{ + for(size_t i = 0; i < hair.size(); i++) { + /* in local space */ + Vec3 hair_end = hair[i].spawn_pt + hair[i].spawn_dir * hair_length; + Vec3 anchor = xform * hair_end; + + Vec3 force = (anchor - hair[i].pos) * K_ANC; + + Vec3 accel = force; /* mass 1 */ + hair[i].velocity += ((-hair[i].velocity * DAMPING) + accel) * dt; + Vec3 new_pos = hair[i].pos + hair[i].velocity * dt; + + /* collision detection with the head */ + Vec3 normal = xform.upper3x3() * hair[i].spawn_dir; + Vec3 root = xform * hair[i].spawn_pt; + Vec3 dir = new_pos - root; + + normal.normalize(); + + /* angle that will cause the hair to be rendered inside the head */ + float d = dot(dir, normal); + if(d < 0) { + new_pos += -d * normal; + } + + hair[i].pos = handle_collision(new_pos); + + dbg_force = force; + } +} + +void Hair::add_collider(CollSphere *cobj) { + colliders.push_back(cobj); +} + +Vec3 Hair::handle_collision(const Vec3 &v) const +{ + /* if we transform the center and the radius of the collider sphere + * we might end up with a spheroid, so better just multiply the + * position with the inverse transform before check for collisions :*/ + + Vec3 new_v = inverse(xform) * v; + + for(size_t i=0; icontains(new_v)) { + new_v = colliders[i]->project_surf(new_v); + } + } + return xform * new_v; +}