From ed91d376224c1efe305d483c05f837820df75b71 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Wed, 21 Nov 2018 16:42:53 +0200 Subject: [PATCH] Added anchor points and hair strands. --- Makefile | 2 +- src/hair.cc | 68 +++++++++++++++++++++++++++++++++++++++++++++++++---------- src/hair.h | 13 ++++++++++-- src/main.cc | 5 +++-- 4 files changed, 72 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 517443a..660400a 100755 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ inc = -Isrc -Isrc/shaders -Isrc/math CXX = g++ CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(inc) -LDFLAGS = -lGL -lGLU -lglut -lGLEW -limago -lassimp +LDFLAGS = -lGL -lGLU -lglut -lGLEW -limago -lassimp -lgmath $(bin): $(obj) $(CXX) -o $@ $(obj) $(LDFLAGS) diff --git a/src/hair.cc b/src/hair.cc index 33bdd0d..171c7e8 100644 --- a/src/hair.cc +++ b/src/hair.cc @@ -13,8 +13,14 @@ struct Triangle { 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 +40,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; } @@ -104,28 +109,69 @@ 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; i 0.99)) { + vi = Vec3(0, -1, 0); + } + Vec3 vj = normalize(cross(vk, vi)); + vi = cross(vj, vk); + + /* identity when the hair points to the z axis */ + Mat4 basis = Mat4(vi, vj, vk); + + for(int j=0; j<3; j++) { + float angle = (float)j / 3.0 * 2 * M_PI; + /* dir of each anchor relative to hair end */ + Vec3 dir = Vec3(cos(angle), sin(angle), 0); + dir = basis * dir; + hair[i].anchor_dirs[j] = hair[i].pos + dir - hair[i].spawn_pt; + } + } return true; } void Hair::draw() const { glPushAttrib(GL_ENABLE_BIT); - glDisable(GL_DEPTH_TEST); +// glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); - glPointSize(2); + glPointSize(5); + glLineWidth(2); + + glBegin(GL_LINES); + for(size_t i=0; i spawn_points; - std::vector spawn_directions; + float hair_length; + std::vector hair; public: Hair(); diff --git a/src/main.cc b/src/main.cc index 750a2d8..2bdbad9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -9,7 +9,8 @@ #include "mesh.h" #include "hair.h" -#define MAX_NUM_SPAWNS 500 +#define MAX_NUM_SPAWNS 4 +#define THRESH 0.5 static bool init(); static void cleanup(); @@ -90,7 +91,7 @@ static bool init() return false; } - if(!hair.init(mesh_head, MAX_NUM_SPAWNS, 0.1)) { + if(!hair.init(mesh_head, MAX_NUM_SPAWNS, THRESH)) { fprintf(stderr, "Failed to initialize hair\n"); return false; } -- 1.7.10.4