Skip to content
Snippets Groups Projects
Commit ba4f1e5a authored by bobarna's avatar bobarna
Browse files

work on collision constraint

parent 90a1d1b8
Branches
Tags
1 merge request!2Head propagate
......@@ -16,7 +16,9 @@ public:
virtual void Draw() = 0;
virtual VertexData GetVertexDataByUV(float u, float v) { throw std::bad_exception(); };
virtual VertexData GetVertexDataByUV(float u, float v) {
return vtxData[std::rand()%vtxData.size()];
};
~Geometry();
};
......
......@@ -15,16 +15,18 @@ PBDSimulation::PBDSimulation(HeadObject *_head, size_t _nr_sims, size_t _nr_segm
// placing hair on the head
propagateHead();
collisionTriangles.emplace_back(0.5, -1, 1);
collisionTriangles.emplace_back(0.5, 1, 0);
collisionTriangles.emplace_back(0.5, -1, -1);
//TODO optimize this, it's smelly code right now
std::vector<VertexData> vtxData = head->getGeometry()->vtxData;
// %4 == 0, these should be guaranteed to be triangles + the normal vector of the face
for (size_t i = 0; i < vtxData.size(); i += 3) {
collisionTriangles.emplace_back(vtxData[i].position);
collisionTriangles.emplace_back(vtxData[i + 1].position);
collisionTriangles.emplace_back(vtxData[i + 2].position);
}
void PBDSimulation::propagateHead() {
}
// for(float u = .0f; u <= 1.f; u+=.05f)
// for(float v = .0f; v <= 1.f; v+=.05f)
// std::cout << head->GetVertexDataByUV(u, v) << std::endl;
void PBDSimulation::propagateHead() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> dis(.3, .95);
......@@ -33,16 +35,12 @@ void PBDSimulation::propagateHead() {
float currU = dis(gen);
float currV = dis(gen);
VertexData currPos = head->GetVertexDataByUV(currU, currV);
while (currPos.normal.y < .0f) {
while (currPos.normal.y > .0f) {
currU = dis(gen);
currV = dis(gen);
currPos = head->GetVertexDataByUV(currU, currV);
}
#ifdef DEBUG_UV
std::cout << vec2(currU, currV) << " -> "<< currPos <<std::endl;
#endif
vec3 color = util::getRandomRGBColorAround(vec3(222.0f, 101.0f, 32.0f), vec3(40.0f, 20.0f, 20.0f));
strands.emplace_back(CreateStrand(nrSegments, lSeg, currPos.position * vec3(1, -1, 1), color));
}
......@@ -76,8 +74,12 @@ void PBDSimulation::update(float dt) {
if (i < strand.size() - 1) solve_bending_constraint(strand[i - 1], strand[i + 1], lSeg * 0.9f);
if (i < strand.size() - 2 && i > 1)
solve_bending_constraint(strand[i - 2], strand[i + 2], lSeg * 1.9f);
solve_collision_constraint(strand[i],
collisionTriangles[0], collisionTriangles[1], collisionTriangles[2]);
// for (size_t currTriangle = 0; currTriangle < collisionTriangles.size(); currTriangle += 3)
// solve_collision_constraint(strand[i],
// collisionTriangles[currTriangle],
// collisionTriangles[currTriangle + 1],
// collisionTriangles[currTriangle + 2]
// );
}
}
}
......@@ -118,7 +120,7 @@ void PBDSimulation::solve_bending_constraint(Particle *p1, Particle *p2, float d
}
void PBDSimulation::solve_collision_constraint(Particle *p, vec3 &q1, vec3 &q2, vec3 &q3) {
vec3 n = normalize(cross(q1 - q3, q1 - q2));
vec3 n = normalize(cross(q1 - q2, q3 - q2));
if (dot(p->tmp_pos - q1, n) < 0) return;
......
......@@ -13,11 +13,11 @@ VertexData HeadObject::GetVertexDataByUV(float u, float v) {
vD.position = vec3(WP.x, WP.y, WP.z);
vD.normal = vec3(WN.x, WN.y, WN.z);
#ifdef DEBUG_UV
std::cout << vD << std::endl;
#endif
return vD;
}
Geometry *HeadObject::getGeometry() {
return geometry;
}
......@@ -8,6 +8,8 @@ public:
HeadObject(Shader *_shader, Geometry *_geometry, Material *_material, Texture *_texture);
VertexData GetVertexDataByUV(float u, float v);
Geometry* getGeometry();
};
......
......@@ -24,44 +24,45 @@ void Scene::Build() {
auto sphere = new Sphere();
Shader *phongShader = new PhongShader();
Material *headMaterial = new Material;
headMaterial->kd = vec3(0.5f, 0.3f, .3f);
headMaterial->ks = vec3(.1, .05, .05);
headMaterial->ka = vec3(.2f, .2f, .2f);
headMaterial->shininess = 20;
headMaterial->kd = vec3(0.5f, 0.5f, 0.5f);
headMaterial->ks = vec3(.7f, .7f, .7f);
headMaterial->ka = vec3(.15f, .15f, .15f);
headMaterial->shininess = 10;
Texture *headTexture = new UniformColorTexture(.6f, .2f, 0);
Texture *headTexture = new UniformColorTexture(.25f, .25f, .35f);
// Texture *headTexture = new CheckerBoardTexture(1, 0);
auto headObject = new HeadObject(phongShader, sphere, headMaterial, headTexture);
// auto headObject = new HeadObject(phongShader, sphere, headMaterial, headTexture);
auto headObject = new HeadObject(phongShader, new ObjGeometry("../data/sphere.obj"), headMaterial, headTexture);
headObject->Scale(vec3(.15, .1, .1));
headObject->Scale(vec3(.35, .35, .35));
objects.push_back(headObject);
auto PBDSim = new PBDSimulation(headObject, nrSims, nrSegments, lSeg);
auto simulationObject = new HairSimObject(headObject, basicShader, PBDSim);
sims.push_back(simulationObject);
auto testObject =
new Object(phongShader,
new ObjGeometry("../data/susanne.obj"),
headMaterial,
headTexture);
objects.push_back(testObject);
// auto testObject =
// new Object(phongShader,
// new ObjGeometry("../data/sphere.obj"),
// headMaterial,
// headTexture);
//
// objects.push_back(testObject);
// Lights
lights.resize(3);
lights[0].wLightPos = vec4(0, 1, 0, 0); // ideal point -> directional light source
lights[0].La = vec3(0.1f, 0.1f, 1);
lights.resize(1);
lights[0].wLightPos = vec4(0.0f, 3.0f, 1.5f, 0); // ideal point -> directional light source
lights[0].La = vec3(3.0f, 3.0f, 3.0f);
lights[0].Le = vec3(1, 1, 1);
lights[1].wLightPos = vec4(-1, .2, 1, 0); // ideal point -> directional light source
lights[1].La = vec3(0.2f, 0.2f, 0.2f);
lights[1].Le = vec3(5, 3, 2);
// lights[1].wLightPos = vec4(2.0f, 3.0f, 1.0f, 0); // ideal point -> directional light source
// lights[1].La = vec3(0.2f, 0.2f, 0.2f);
// lights[1].Le = vec3(1, 1, 1);
lights[2].wLightPos = vec4(0, 0, 1, 0); // ideal point -> directional light source
lights[2].La = vec3(0.1f, 0.1f, 0.1f);
lights[2].Le = vec3(5, 3, 2);
// lights[2].wLightPos = vec4(0, 0, 1, 0); // ideal point -> directional light source
// lights[2].La = vec3(0.1f, 0.1f, 0.1f);
// lights[2].Le = vec3(1, 1, 1);
}
void Scene::Render() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment