add a small framework of garments, update the related tests

This commit is contained in:
Irlan
2018-05-23 03:54:02 -03:00
parent aebe39143d
commit 109f23da49
16 changed files with 1037 additions and 145 deletions

View File

@ -19,36 +19,16 @@
#ifndef MASS_TYPES_H
#define MASS_TYPES_H
class MassTypes : public SpringClothTest
class MassTypes : public PinnedCloth
{
public:
MassTypes()
{
b3SpringClothDef def;
def.allocator = &m_clothAllocator;
def.mesh = &m_clothMesh;
def.density = 0.2f;
def.ks = 100000.0f;
def.gravity.Set(0.0f, -10.0f, 0.0f);
m_cloth.Initialize(def);
b3AABB3 aabb;
aabb.m_lower.Set(-5.0f, -1.0f, -6.0f);
aabb.m_upper.Set(5.0f, 1.0f, -4.0f);
for (u32 i = 0; i < def.mesh->vertexCount; ++i)
{
if (aabb.Contains(def.mesh->vertices[i]))
{
m_cloth.SetType(i, b3MassType::e_staticMass);
}
}
}
void Step()
{
SpringClothTest::Step();
PinnedCloth::Step();
g_draw->DrawString(b3Color_white, "S - Static");
g_draw->DrawString(b3Color_white, "D - Dynamic");
@ -56,30 +36,29 @@ public:
g_draw->DrawString(b3Color_white, "Arrows - Apply Force/Velocity/Position");
}
void SetClothType(b3MassType type)
{
for (u32 i = 0; i < m_cloth.GetMassCount(); ++i)
{
m_cloth.SetType(i, type);
}
}
void KeyDown(int button)
{
if (button == GLFW_KEY_S)
{
for (u32 i = 0; i < m_cloth.GetMassCount(); ++i)
{
m_cloth.SetType(i, b3MassType::e_staticMass);
}
SetClothType(b3MassType::e_staticMass);
}
if (button == GLFW_KEY_K)
{
for (u32 i = 0; i < m_cloth.GetMassCount(); ++i)
{
m_cloth.SetType(i, b3MassType::e_kinematicMass);
}
SetClothType(b3MassType::e_kinematicMass);
}
if (button == GLFW_KEY_D)
{
for (u32 i = 0; i < m_cloth.GetMassCount(); ++i)
{
m_cloth.SetType(i, b3MassType::e_dynamicMass);
}
SetClothType(b3MassType::e_dynamicMass);
}
for (u32 i = 0; i < m_cloth.GetMassCount(); ++i)
@ -191,8 +170,6 @@ public:
{
return new MassTypes();
}
b3GridMesh<10, 10> m_clothMesh;
};
#endif

View File

@ -16,19 +16,39 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SPRING_CLOTH_H
#define SPRING_CLOTH_H
#ifndef PINNED_CLOTH_H
#define PINNED_CLOTH_H
class SpringCloth : public SpringClothTest
class PinnedCloth : public SpringClothTest
{
public:
SpringCloth()
PinnedCloth()
{
m_gridClothMesh.vertexCount = m_gridMesh.vertexCount;
m_gridClothMesh.vertices = m_gridMesh.vertices;
m_gridClothMesh.triangleCount = m_gridMesh.triangleCount;
m_gridClothMesh.triangles = (b3ClothMeshTriangle*)m_gridMesh.triangles;
m_gridClothMeshMesh.vertexCount = m_gridClothMesh.vertexCount;
m_gridClothMeshMesh.startVertex = 0;
m_gridClothMeshMesh.triangleCount = m_gridClothMesh.triangleCount;
m_gridClothMeshMesh.startTriangle = 0;
m_gridClothMesh.meshCount = 1;
m_gridClothMesh.meshes = &m_gridClothMeshMesh;
m_gridClothMesh.sewingLineCount = 0;
m_gridClothMesh.sewingLines = nullptr;
b3SpringClothDef def;
def.allocator = &m_clothAllocator;
def.mesh = &m_clothMesh;
def.mesh = &m_gridClothMesh;
def.density = 0.2f;
def.ks = 100000.0f;
def.ks = 10000.0f;
def.kd = 0.0f;
def.r = 0.05f;
def.gravity.Set(0.0f, -10.0f, 0.0f);
m_cloth.Initialize(def);
@ -37,21 +57,23 @@ public:
aabb.m_lower.Set(-5.0f, -1.0f, -6.0f);
aabb.m_upper.Set(5.0f, 1.0f, -4.0f);
for (u32 i = 0; i < def.mesh->vertexCount; ++i)
for (u32 i = 0; i < m_cloth.GetMassCount(); ++i)
{
if (aabb.Contains(def.mesh->vertices[i]))
if (aabb.Contains(m_cloth.GetPosition(i)))
{
m_cloth.SetType(i, b3MassType::e_staticMass);
}
}
}
}
static Test* Create()
{
return new SpringCloth();
return new PinnedCloth();
}
b3GridMesh<10, 10> m_clothMesh;
b3GridMesh<10, 10> m_gridMesh;
b3ClothMeshMesh m_gridClothMeshMesh;
b3ClothMesh m_gridClothMesh;
};
#endif

View File

@ -16,41 +16,65 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SPRING_CLOTH_CONTACT_H
#define SPRING_CLOTH_CONTACT_H
#ifndef SHIRT_H
#define SHIRT_H
class SpringClothContact : public SpringClothTest
#include <bounce/garment/garment.h>
#include <bounce/garment/sewing_pattern.h>
#include <bounce/garment/garment_mesh.h>
class Shirt : public SpringClothTest
{
public:
SpringClothContact()
Shirt()
{
// Generate 2D mesh
m_shirtGarmentMesh.Set(&m_shirtGarment, 0.1f);
// Create 3D mesh
m_shirtClothMesh.Set(&m_shirtGarmentMesh);
// Perform fitting
for (u32 i = 0; i < 3; ++i)
{
b3ClothMeshMesh* front = m_shirtClothMesh.meshes + i;
for (u32 j = 0; j < front->vertexCount; ++j)
{
u32 v = front->startVertex + j;
m_shirtClothMesh.vertices[v].z = -1.0f;
}
}
for (u32 i = 3; i < 6; ++i)
{
b3ClothMeshMesh* back = m_shirtClothMesh.meshes + i;
for (u32 j = 0; j < back->vertexCount; ++j)
{
u32 v = back->startVertex + j;
m_shirtClothMesh.vertices[v].z = 1.0f;
}
}
// Create cloth
b3SpringClothDef def;
def.allocator = &m_clothAllocator;
def.mesh = &m_clothMesh;
def.mesh = &m_shirtClothMesh;
def.density = 0.2f;
def.ks = 1000.0f;
def.kd = 0.0f;
def.ks = 10000.0f;
def.r = 0.2f;
def.gravity.Set(0.0f, -10.0f, 0.0f);
m_cloth.Initialize(def);
m_clothCapsule.m_centers[0].Set(0.0f, -2.0f, 2.0f);
m_clothCapsule.m_centers[1].Set(0.0f, -2.0f, -2.0f);
m_clothCapsule.m_radius = 2.0f;
m_clothCapsule.SetFriction(1.0f);
m_cloth.AddShape(&m_clothCapsule);
}
static Test* Create()
{
return new SpringClothContact();
return new Shirt();
}
b3GridMesh<10, 10> m_clothMesh;
b3CapsuleShape m_clothCapsule;
b3ShirtGarment m_shirtGarment;
b3GarmentMesh m_shirtGarmentMesh;
b3GarmentClothMesh m_shirtClothMesh;
};
#endif

View File

@ -43,8 +43,8 @@ public:
{
B3_ASSERT(m_isSelected);
b3Mesh* m = m_cloth->GetMesh();
b3Triangle* t = m->triangles + m_selection;
b3ClothMesh* m = m_cloth->GetMesh();
b3ClothMeshTriangle* t = m->triangles + m_selection;
b3Vec3 A = m->vertices[t->v1];
b3Vec3 B = m->vertices[t->v2];
@ -70,8 +70,8 @@ public:
m_isSelected = true;
b3Mesh* m = m_cloth->GetMesh();
b3Triangle* t = m->triangles + m_selection;
b3ClothMesh* m = m_cloth->GetMesh();
b3ClothMeshTriangle* t = m->triangles + m_selection;
m_t1 = m_cloth->GetType(t->v1);
m_cloth->SetType(t->v1, b3MassType::e_staticMass);
@ -108,8 +108,8 @@ public:
{
B3_ASSERT(m_isSelected);
b3Mesh* m = m_cloth->GetMesh();
b3Triangle* t = m->triangles + m_selection;
b3ClothMesh* m = m_cloth->GetMesh();
b3ClothMeshTriangle* t = m->triangles + m_selection;
b3Vec3 A = GetPointA();
b3Vec3 B = GetPointB();
@ -135,8 +135,8 @@ public:
m_isSelected = false;
b3Mesh* m = m_cloth->GetMesh();
b3Triangle* t = m->triangles + m_selection;
b3ClothMesh* m = m_cloth->GetMesh();
b3ClothMeshTriangle* t = m->triangles + m_selection;
m_cloth->SetType(t->v1, m_t1);
m_cloth->SetType(t->v2, m_t2);
@ -144,22 +144,99 @@ public:
}
private:
bool RayCast(b3RayCastOutput* output, u32 triangleIndex) const
{
b3ClothMesh* mesh = m_cloth->GetMesh();
B3_ASSERT(triangleIndex < mesh->triangleCount);
b3ClothMeshTriangle* triangle = mesh->triangles + triangleIndex;
b3Vec3 v1 = mesh->vertices[triangle->v1];
b3Vec3 v2 = mesh->vertices[triangle->v2];
b3Vec3 v3 = mesh->vertices[triangle->v3];
b3Vec3 p1 = m_ray->A();
b3Vec3 p2 = m_ray->B();
b3Vec3 d = p2 - p1;
float32 maxFraction = b3Length(d);
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
n.Normalize();
float32 numerator = b3Dot(n, v1 - p1);
float32 denominator = b3Dot(n, d);
if (denominator == 0.0f)
{
return false;
}
float32 t = numerator / denominator;
// Is the intersection not on the segment?
if (t < 0.0f || maxFraction < t)
{
return false;
}
b3Vec3 q = p1 + t * d;
// Barycentric coordinates for q
b3Vec3 Q = q;
b3Vec3 A = v1;
b3Vec3 B = v2;
b3Vec3 C = v3;
b3Vec3 AB = B - A;
b3Vec3 AC = C - A;
b3Vec3 QA = A - Q;
b3Vec3 QB = B - Q;
b3Vec3 QC = C - Q;
b3Vec3 QB_x_QC = b3Cross(QB, QC);
b3Vec3 QC_x_QA = b3Cross(QC, QA);
b3Vec3 QA_x_QB = b3Cross(QA, QB);
b3Vec3 AB_x_AC = b3Cross(AB, AC);
float32 u = b3Dot(QB_x_QC, AB_x_AC);
float32 v = b3Dot(QC_x_QA, AB_x_AC);
float32 w = b3Dot(QA_x_QB, AB_x_AC);
// This tolerance helps intersections lying on
// shared edges to not be missed.
const float32 kTol = -0.005f;
// Is the intersection on the triangle?
if (u > kTol && v > kTol && w > kTol)
{
output->fraction = t;
// Does the ray start from below or above the triangle?
if (numerator > 0.0f)
{
output->normal = -n;
}
else
{
output->normal = n;
}
return true;
}
return false;
}
bool Select(u32& selection, float32& fraction) const
{
b3Mesh* m = m_cloth->GetMesh();
b3MeshShape ms;
ms.m_mesh = m;
b3ClothMesh* m = m_cloth->GetMesh();
b3RayCastInput input;
input.p1 = m_ray->A();
input.p2 = m_ray->B();
input.maxFraction = m_ray->fraction;
b3Transform transform;
transform.SetIdentity();
float32 minFraction = B3_MAX_FLOAT;
b3Vec3 minNormal(0.0f, 0.0f, 0.0f);
u32 minIndex = ~0;
@ -167,7 +244,7 @@ private:
for (u32 i = 0; i < m->triangleCount; ++i)
{
b3RayCastOutput subOutput;
if (ms.RayCast(&subOutput, input, transform, i) == true)
if (RayCast(&subOutput, i) == true)
{
if (subOutput.fraction < minFraction)
{

View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef TABLE_CLOTH_H
#define TABLE_CLOTH_H
class TableCloth : public SpringClothTest
{
public:
TableCloth()
{
// Shift vertices up
for (u32 i = 0; i < m_gridMesh.vertexCount; ++i)
{
m_gridMesh.vertices[i].y = 5.0f;
}
m_gridClothMesh.vertexCount = m_gridMesh.vertexCount;
m_gridClothMesh.vertices = m_gridMesh.vertices;
m_gridClothMesh.triangleCount = m_gridMesh.triangleCount;
m_gridClothMesh.triangles = (b3ClothMeshTriangle*)m_gridMesh.triangles;
m_gridClothMeshMesh.vertexCount = m_gridClothMesh.vertexCount;
m_gridClothMeshMesh.startVertex = 0;
m_gridClothMeshMesh.triangleCount = m_gridClothMesh.triangleCount;
m_gridClothMeshMesh.startTriangle = 0;
m_gridClothMesh.meshCount = 1;
m_gridClothMesh.meshes = &m_gridClothMeshMesh;
m_gridClothMesh.sewingLineCount = 0;
m_gridClothMesh.sewingLines = nullptr;
b3SpringClothDef def;
def.allocator = &m_clothAllocator;
def.mesh = &m_gridClothMesh;
def.density = 0.2f;
def.ks = 10000.0f;
def.kd = 0.0f;
def.r = 0.05f;
def.gravity.Set(0.0f, -10.0f, 0.0f);
m_cloth.Initialize(def);
m_tableHull.SetAsCylinder(5.0f, 2.0f);
m_tableShape.m_hull = &m_tableHull;
m_tableShape.m_radius = 0.2f;
m_tableShape.SetFriction(1.0f);
m_cloth.AddShape(&m_tableShape);
}
static Test* Create()
{
return new TableCloth();
}
b3GridMesh<10, 10> m_gridMesh;
b3ClothMeshMesh m_gridClothMeshMesh;
b3ClothMesh m_gridClothMesh;
b3QHull m_tableHull;
b3HullShape m_tableShape;
};
#endif

View File

@ -60,11 +60,31 @@ class TensionMapping : public SpringClothTest
public:
TensionMapping()
{
m_gridClothMesh.vertexCount = m_gridMesh.vertexCount;
m_gridClothMesh.vertices = m_gridMesh.vertices;
m_gridClothMesh.triangleCount = m_gridMesh.triangleCount;
m_gridClothMesh.triangles = (b3ClothMeshTriangle*)m_gridMesh.triangles;
m_gridClothMeshMesh.vertexCount = m_gridClothMesh.vertexCount;
m_gridClothMeshMesh.startVertex = 0;
m_gridClothMeshMesh.triangleCount = m_gridClothMesh.triangleCount;
m_gridClothMeshMesh.startTriangle = 0;
m_gridClothMesh.meshCount = 1;
m_gridClothMesh.meshes = &m_gridClothMeshMesh;
m_gridClothMesh.sewingLineCount = 0;
m_gridClothMesh.sewingLines = nullptr;
b3SpringClothDef def;
def.allocator = &m_clothAllocator;
def.mesh = &m_clothMesh;
def.mesh = &m_gridClothMesh;
def.density = 0.2f;
def.ks = 100000.0f;
def.ks = 10000.0f;
def.kd = 0.0f;
def.r = 0.2f;
def.gravity.Set(0.0f, -10.0f, 0.0f);
m_cloth.Initialize(def);
@ -73,9 +93,9 @@ public:
aabb.m_lower.Set(-5.0f, -1.0f, -6.0f);
aabb.m_upper.Set(5.0f, 1.0f, -4.0f);
for (u32 i = 0; i < def.mesh->vertexCount; ++i)
for (u32 i = 0; i < m_cloth.GetMassCount(); ++i)
{
if (aabb.Contains(def.mesh->vertices[i]))
if (aabb.Contains(m_cloth.GetPosition(i)))
{
m_cloth.SetType(i, b3MassType::e_staticMass);
}
@ -92,13 +112,13 @@ public:
b3StackArray<b3Vec3, 100> T;
m_cloth.GetTension(T);
for (u32 i = 0; i < m_clothMesh.triangleCount; ++i)
for (u32 i = 0; i < m_gridClothMesh.triangleCount; ++i)
{
b3Triangle* t = m_clothMesh.triangles + i;
b3ClothMeshTriangle* t = m_gridClothMesh.triangles + i;
b3Vec3 v1 = m_clothMesh.vertices[t->v1];
b3Vec3 v2 = m_clothMesh.vertices[t->v2];
b3Vec3 v3 = m_clothMesh.vertices[t->v3];
b3Vec3 v1 = m_gridClothMesh.vertices[t->v1];
b3Vec3 v2 = m_gridClothMesh.vertices[t->v2];
b3Vec3 v3 = m_gridClothMesh.vertices[t->v3];
b3Draw_draw->DrawSegment(v1, v2, b3Color_black);
b3Draw_draw->DrawSegment(v2, v3, b3Color_black);
@ -142,7 +162,9 @@ public:
return new TensionMapping();
}
b3GridMesh<10, 10> m_clothMesh;
b3GridMesh<10, 10> m_gridMesh;
b3ClothMeshMesh m_gridClothMeshMesh;
b3ClothMesh m_gridClothMesh;
};
#endif