+#include <GL/glew.h>
+
+#include <float.h>
#include <gmath/gmath.h>
#include <stdlib.h>
#include <string>
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<Triangle> *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; i<m->indices.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;
for(int j=0; j<3; j++) {
t.v[j] = m->vertices[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)
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++) {
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)
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();
}
#include <string>
#include "mesh.h"
+#include "hair.h"
+
+#define MAX_NUM_SPAWNS 500
static bool init();
static void cleanup();
static std::vector<Mesh*> meshes;
static Mesh *mesh_head;
+static Hair hair;
int win_width, win_height;
float cam_theta, cam_phi = 25, cam_dist = 8;
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");
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; i<meshes.size(); i++) {
+ delete meshes[i];
+ }
}
static void display()
glRotatef(cam_phi, 1, 0, 0);
glRotatef(cam_theta, 0, 1, 0);
- glTranslatef(0, -16, 0);
-
for(size_t i=0; i<meshes.size(); i++) {
meshes[i]->draw();
}
+ hair.draw();
+
glutSwapBuffers();
assert(glGetError() == GL_NO_ERROR);
}