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;
131 /* orthonormal basis */
132 Vec3 vk = hair[i].spawn_dir;
133 Vec3 vi = Vec3(1, 0, 0);
134 if(fabs(vk.x > 0.99)) {
137 Vec3 vj = normalize(cross(vk, vi));
140 /* identity when the hair points to the z axis */
141 Mat4 basis = Mat4(vi, vj, vk);
143 for(int j=0; j<3; j++) {
144 float angle = (float)j / 3.0 * 2 * M_PI;
145 /* dir of each anchor relative to hair root */
146 Vec3 dir = Vec3(cos(angle), sin(angle), 0);
148 hair[i].anchor_dirs[j] = hair[i].pos + dir - hair[i].spawn_pt;
154 static Vec3 dbg_force;
155 void Hair::draw() const
157 glPushAttrib(GL_ENABLE_BIT);
158 // glDisable(GL_DEPTH_TEST);
159 glDisable(GL_LIGHTING);
164 for(size_t i=0; i<hair.size(); i++) {
166 Vec3 p = xform * hair[i].spawn_pt;
167 glVertex3f(p.x, p.y, p.z);
168 Vec3 dir = normalize(hair[i].pos - p) * hair_length;
170 glVertex3f(end.x, end.y, end.z);
173 glVertex3f(hair[i].pos.x, hair[i].pos.y, hair[i].pos.z);
174 Vec3 end = hair[i].pos + dbg_force * 2.0;
175 glVertex3f(end.x, end.y, end.z);
182 glColor3f(0.5, 1.0, 0.5);
183 for(size_t i = 0; i < hair.size(); i++) {
184 Vec3 p = xform * (hair[i].spawn_pt + hair[i].spawn_dir * hair_length);
185 glVertex3f(p.x, p.y, p.z);
192 void Hair::set_transform(Mat4 &xform)
197 void Hair::update(float dt)
199 for(size_t i = 0; i < hair.size(); i++) {
201 Vec3 hair_end = hair[i].spawn_pt + hair[i].spawn_dir * hair_length;
202 Vec3 anchor = xform * hair_end;
204 Vec3 force = (anchor - hair[i].pos) * K_ANC;
206 Vec3 accel = force; /* mass 1 */
207 hair[i].velocity += ((-hair[i].velocity * DAMPING) + accel) * dt;
208 hair[i].pos += hair[i].velocity * dt;