4 #include <gmath/gmath.h>
19 static Vec3 calc_rand_point(const Triangle &tr, Vec3 *bary)
21 float u = (float)rand() / (float)RAND_MAX;
22 float v = (float)rand() / (float)RAND_MAX;
29 float c = 1 - (u + v);
31 Vec3 rp = u * tr.v[0] + v * tr.v[1] + c * tr.v[2];
37 // printf("u %f v %f c %f sum: %f\n", u, v, c, u+v+c);
41 static void get_spawn_triangles(const Mesh *m, float thresh, std::vector<Triangle> *faces)
44 fprintf(stderr, "Func: %s, invalid mesh.\n", __func__);
47 float min_y = FLT_MAX;
48 float max_y = -FLT_MAX;
50 for(size_t i=0; i<m->indices.size() / 3; i++) {
53 for(int j=0; j<3; j++) {
54 idx[j] = m->indices[i * 3 + j];
55 float c = (m->colors[idx[j]].x + m->colors[idx[j]].y + m->colors[idx[j]].z) / 3;
64 for(int j=0; j<3; j++) {
65 t.v[j] = m->vertices[idx[j]];
66 t.n[j] = m->normals[idx[j]];
75 printf("spawn tri AABB: min y: %f max y: %f\n", min_y, max_y);
78 bool Hair::init(const Mesh *m, int max_num_spawns, float thresh)
80 std::vector<Triangle> faces;
81 kdtree *kd = kd_create(3);
82 const float min_dist = 0.05;
85 fprintf(stderr, "Func %s: invalid mesh.\n", __func__);
89 get_spawn_triangles(m, thresh, &faces);
91 for(int i = 0; i < max_num_spawns; i++) {
93 int rnum = (float)((float)rand() / (float)RAND_MAX) * faces.size();
94 Triangle rtriangle = faces[rnum];
96 Vec3 rpoint = calc_rand_point(rtriangle, &bary);
98 kdres *res = kd_nearest3f(kd, rpoint.x, rpoint.y, rpoint.z);
100 if (res && !kd_res_end(res)) {
102 kd_res_item3f(res, &nearest.x, &nearest.y, &nearest.z);
103 if(distance_sq(rpoint, nearest) < min_dist * min_dist)
107 /* weighted sum of the triangle's vertex normals */
108 Vec3 spawn_dir = rtriangle.n[0] * bary.x + rtriangle.n[1] * bary.y + rtriangle.n[2] * bary.z;
109 spawn_directions.push_back(normalize(spawn_dir));
110 spawn_points.push_back(rpoint);
111 kd_insert3f(kd, rpoint.x, rpoint.y, rpoint.z, 0);
118 void Hair::draw() const
120 glPushAttrib(GL_ENABLE_BIT);
121 glDisable(GL_DEPTH_TEST);
122 glDisable(GL_LIGHTING);
125 for(size_t i = 0; i < spawn_points.size(); i++) {
127 glVertex3f(spawn_points[i].x, spawn_points[i].y, spawn_points[i].z);