4 #include <gmath/gmath.h>
30 static Vec3 calc_rand_point(const Triangle &tr, Vec3 *bary)
32 float u = (float)rand() / (float)RAND_MAX;
33 float v = (float)rand() / (float)RAND_MAX;
40 float c = 1 - (u + v);
42 Vec3 rp = u * tr.v[0] + v * tr.v[1] + c * tr.v[2];
51 static void get_spawn_triangles(const Mesh *m, float thresh, std::vector<Triangle> *faces)
54 fprintf(stderr, "Func: %s, invalid mesh.\n", __func__);
57 float min_y = FLT_MAX;
58 float max_y = -FLT_MAX;
60 for(size_t i=0; i<m->indices.size() / 3; i++) {
63 for(int j=0; j<3; j++) {
64 idx[j] = m->indices[i * 3 + j];
65 float c = (m->colors[idx[j]].x + m->colors[idx[j]].y + m->colors[idx[j]].z) / 3;
74 for(int j=0; j<3; j++) {
75 t.v[j] = m->vertices[idx[j]];
76 t.n[j] = m->normals[idx[j]];
85 /* printf("spawn tri AABB: min y: %f max y: %f\n", min_y, max_y);*/
88 bool Hair::init(const Mesh *m, int max_num_spawns, float thresh)
90 std::vector<Triangle> faces;
91 kdtree *kd = kd_create(3);
92 const float min_dist = 0.05;
95 fprintf(stderr, "Func %s: invalid mesh.\n", __func__);
99 get_spawn_triangles(m, thresh, &faces);
101 for(int i = 0; i < max_num_spawns; i++) {
103 int rnum = (float)((float)rand() / (float)RAND_MAX) * faces.size();
104 Triangle rtriangle = faces[rnum];
106 Vec3 rpoint = calc_rand_point(rtriangle, &bary);
108 kdres *res = kd_nearest3f(kd, rpoint.x, rpoint.y, rpoint.z);
110 if (res && !kd_res_end(res)) {
112 kd_res_item3f(res, &nearest.x, &nearest.y, &nearest.z);
113 if(distance_sq(rpoint, nearest) < min_dist * min_dist)
118 /* weighted sum of the triangle's vertex normals */
119 strand.spawn_dir = normalize(rtriangle.n[0] * bary.x + rtriangle.n[1] * bary.y + rtriangle.n[2] * bary.z);
120 strand.spawn_pt = rpoint;
121 hair.push_back(strand);
123 kd_insert3f(kd, rpoint.x, rpoint.y, rpoint.z, 0);
128 for(size_t i=0; i<hair.size(); i++) {
129 hair[i].pos = hair[i].spawn_pt + hair[i].spawn_dir * hair_length;
134 static Vec3 dbg_force;
135 void Hair::draw() const
137 glPushAttrib(GL_ENABLE_BIT);
138 // glDisable(GL_DEPTH_TEST);
139 glDisable(GL_LIGHTING);
144 for(size_t i=0; i<hair.size(); i++) {
146 Vec3 p = xform * hair[i].spawn_pt;
147 glVertex3f(p.x, p.y, p.z);
148 Vec3 dir = normalize(hair[i].pos - p) * hair_length;
150 glVertex3f(end.x, end.y, end.z);
153 glVertex3f(hair[i].pos.x, hair[i].pos.y, hair[i].pos.z);
154 Vec3 fend = hair[i].pos + dbg_force * 2.0;
155 glVertex3f(fend.x, fend.y, fend.z);
162 glColor3f(0.5, 1.0, 0.5);
163 for(size_t i = 0; i < hair.size(); i++) {
164 Vec3 p = xform * (hair[i].spawn_pt + hair[i].spawn_dir * hair_length);
165 glVertex3f(p.x, p.y, p.z);
172 void Hair::set_transform(Mat4 &xform)
177 void Hair::update(float dt)
179 for(size_t i = 0; i < hair.size(); i++) {
181 Vec3 hair_end = hair[i].spawn_pt + hair[i].spawn_dir * hair_length;
182 Vec3 anchor = xform * hair_end;
184 Vec3 force = (anchor - hair[i].pos) * K_ANC;
186 Vec3 accel = force; /* mass 1 */
187 hair[i].velocity += ((-hair[i].velocity * DAMPING) + accel) * dt;
188 Vec3 new_pos = hair[i].pos + hair[i].velocity * dt;
190 hair[i].pos = new_pos; //= handle_collision(new_pos);
196 void Hair::add_collider(CollSphere *cobj) {
197 colliders.push_back(cobj);
200 Vec3 Hair::handle_collision(const Vec3 &v) const
202 /* if we transform the center and the radius of the collider sphere
203 * we might end up with a spheroid, so better just multiply the
204 * position with the inverse transform before check for collisions :*/
206 Vec3 new_v = inverse(xform) * v;
208 for(size_t i=0; i<colliders.size(); i++) {
209 if(colliders[i]->contains(new_v)) {
210 new_v = colliders[i]->project_surf(new_v);
213 return xform * new_v;