From: Eleni Maria Stea Date: Wed, 21 Nov 2018 10:24:17 +0000 (+0200) Subject: Fixed bug in points calculation, fixed the model X-Git-Url: http://git.mutantstargoat.com?p=hair;a=commitdiff_plain;h=43078ddde36cc81375cddfe45f47f806b9d19307 Fixed bug in points calculation, fixed the model --- diff --git a/data/head.fbx b/data/head.fbx index c4dd1ff..d37b042 100644 Binary files a/data/head.fbx and b/data/head.fbx differ diff --git a/src/hair.cc b/src/hair.cc index e4843fb..33bdd0d 100644 --- a/src/hair.cc +++ b/src/hair.cc @@ -1,3 +1,6 @@ +#include + +#include #include #include #include @@ -25,22 +28,30 @@ static Vec3 calc_rand_point(const Triangle &tr, Vec3 *bary) float c = 1 - (u + v); - Vec3 rp = u * tr.v[0] + v * tr.v[1] + c * tr.v[3]; + Vec3 rp = u * tr.v[0] + v * tr.v[1] + c * tr.v[2]; bary->x = u; bary->y = v; bary->z = c; +// printf("u %f v %f c %f sum: %f\n", u, v, c, u+v+c); return rp; } static void get_spawn_triangles(const Mesh *m, float thresh, std::vector *faces) { + if (!m) { + fprintf(stderr, "Func: %s, invalid mesh.\n", __func__); + exit(1); + } + float min_y = FLT_MAX; + float max_y = -FLT_MAX; + for(size_t i=0; iindices.size() / 3; i++) { bool is_spawn = true; int idx[3]; for(int j=0; j<3; j++) { - idx[j] = i * 3 + j; + idx[j] = m->indices[i * 3 + j]; float c = (m->colors[idx[j]].x + m->colors[idx[j]].y + m->colors[idx[j]].z) / 3; if (c >= thresh) { is_spawn = false; @@ -53,10 +64,15 @@ static void get_spawn_triangles(const Mesh *m, float thresh, std::vectorvertices[idx[j]]; t.n[j] = m->normals[idx[j]]; + if(t.v[j].y < min_y) + min_y = t.v[j].y; + if(t.v[j].y > max_y) + max_y = t.v[j].y; } faces->push_back(t); } } + 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) @@ -65,6 +81,11 @@ bool Hair::init(const Mesh *m, int max_num_spawns, float thresh) kdtree *kd = kd_create(3); const float min_dist = 0.05; + if(!m) { + fprintf(stderr, "Func %s: invalid mesh.\n", __func__); + return false; + } + get_spawn_triangles(m, thresh, &faces); for(int i = 0; i < max_num_spawns; i++) { @@ -75,7 +96,8 @@ bool Hair::init(const Mesh *m, int max_num_spawns, float thresh) Vec3 rpoint = calc_rand_point(rtriangle, &bary); kdres *res = kd_nearest3f(kd, rpoint.x, rpoint.y, rpoint.z); - if (!kd_res_end(res)) { + + if (res && !kd_res_end(res)) { Vec3 nearest; kd_res_item3f(res, &nearest.x, &nearest.y, &nearest.z); if(distance_sq(rpoint, nearest) < min_dist * min_dist) @@ -95,4 +117,15 @@ bool Hair::init(const Mesh *m, int max_num_spawns, float thresh) void Hair::draw() const { + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glPointSize(2); + glBegin(GL_POINTS); + for(size_t i = 0; i < spawn_points.size(); i++) { + glColor3f(1, 1, 0); + glVertex3f(spawn_points[i].x, spawn_points[i].y, spawn_points[i].z); + } + glEnd(); + glPopAttrib(); } diff --git a/src/main.cc b/src/main.cc index 8b0fe13..750a2d8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,6 +7,9 @@ #include #include "mesh.h" +#include "hair.h" + +#define MAX_NUM_SPAWNS 500 static bool init(); static void cleanup(); @@ -18,6 +21,7 @@ static void motion(int x, int y); static std::vector meshes; static Mesh *mesh_head; +static Hair hair; int win_width, win_height; float cam_theta, cam_phi = 25, cam_dist = 8; @@ -55,7 +59,7 @@ static bool init() glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); - glClearColor(1, 0.5, 0.5, 1); + glClearColor(0.5, 0.5, 0.5, 1); meshes = load_meshes("data/head.fbx"); if (meshes.empty()) { fprintf(stderr, "Failed to load mesh.\n"); @@ -81,12 +85,24 @@ static bool init() mesh_head = meshes[i]; } } + if(!mesh_head) { + fprintf(stderr, "Failed to find the head mesh.\n"); + return false; + } + + if(!hair.init(mesh_head, MAX_NUM_SPAWNS, 0.1)) { + fprintf(stderr, "Failed to initialize hair\n"); + return false; + } return true; } static void cleanup() { + for(size_t i=0; idraw(); } + hair.draw(); + glutSwapBuffers(); assert(glGetError() == GL_NO_ERROR); }