use b3ClothMesh as a constant resource
This commit is contained in:
parent
7060f513ad
commit
bea108510f
@ -97,7 +97,7 @@ public:
|
|||||||
{
|
{
|
||||||
Test::Step();
|
Test::Step();
|
||||||
|
|
||||||
b3ClothMesh* mesh = m_cloth->GetMesh();
|
const b3ClothMesh* mesh = m_cloth->GetMesh();
|
||||||
|
|
||||||
b3StackArray<b3Vec3, 256> tension;
|
b3StackArray<b3Vec3, 256> tension;
|
||||||
tension.Resize(mesh->vertexCount);
|
tension.Resize(mesh->vertexCount);
|
||||||
@ -124,9 +124,9 @@ public:
|
|||||||
{
|
{
|
||||||
b3ClothMeshTriangle* t = m_rectangleClothMesh.triangles + i;
|
b3ClothMeshTriangle* t = m_rectangleClothMesh.triangles + i;
|
||||||
|
|
||||||
b3Vec3 v1 = mesh->particles[t->v1]->GetPosition();
|
b3Vec3 v1 = m_cloth->GetVertexParticle(t->v1)->GetPosition();
|
||||||
b3Vec3 v2 = mesh->particles[t->v2]->GetPosition();
|
b3Vec3 v2 = m_cloth->GetVertexParticle(t->v2)->GetPosition();
|
||||||
b3Vec3 v3 = mesh->particles[t->v3]->GetPosition();
|
b3Vec3 v3 = m_cloth->GetVertexParticle(t->v3)->GetPosition();
|
||||||
|
|
||||||
b3Draw_draw->DrawSegment(v1, v2, b3Color_black);
|
b3Draw_draw->DrawSegment(v1, v2, b3Color_black);
|
||||||
b3Draw_draw->DrawSegment(v2, v3, b3Color_black);
|
b3Draw_draw->DrawSegment(v2, v3, b3Color_black);
|
||||||
|
@ -63,9 +63,9 @@ public:
|
|||||||
m_triangle = m_mesh->triangles + rayOut.triangle;
|
m_triangle = m_mesh->triangles + rayOut.triangle;
|
||||||
m_x = rayOut.fraction;
|
m_x = rayOut.fraction;
|
||||||
|
|
||||||
b3Particle* p1 = m_mesh->particles[m_triangle->v1];
|
b3Particle* p1 = m_cloth->GetVertexParticle(m_triangle->v1);
|
||||||
b3Particle* p2 = m_mesh->particles[m_triangle->v2];
|
b3Particle* p2 = m_cloth->GetVertexParticle(m_triangle->v2);
|
||||||
b3Particle* p3 = m_mesh->particles[m_triangle->v3];
|
b3Particle* p3 = m_cloth->GetVertexParticle(m_triangle->v3);
|
||||||
|
|
||||||
b3Vec3 v1 = p1->GetPosition();
|
b3Vec3 v1 = p1->GetPosition();
|
||||||
b3Vec3 v2 = p2->GetPosition();
|
b3Vec3 v2 = p2->GetPosition();
|
||||||
@ -151,13 +151,13 @@ public:
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
b3Particle* p1 = m_mesh->particles[m_triangle->v1];
|
b3Particle* p1 = m_cloth->GetVertexParticle(m_triangle->v1);
|
||||||
p1->ApplyTranslation(dx);
|
p1->ApplyTranslation(dx);
|
||||||
|
|
||||||
b3Particle* p2 = m_mesh->particles[m_triangle->v2];
|
b3Particle* p2 = m_cloth->GetVertexParticle(m_triangle->v2);
|
||||||
p2->ApplyTranslation(dx);
|
p2->ApplyTranslation(dx);
|
||||||
|
|
||||||
b3Particle* p3 = m_mesh->particles[m_triangle->v3];
|
b3Particle* p3 = m_cloth->GetVertexParticle(m_triangle->v3);
|
||||||
p3->ApplyTranslation(dx);
|
p3->ApplyTranslation(dx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -166,8 +166,6 @@ public:
|
|||||||
{
|
{
|
||||||
B3_ASSERT(IsDragging() == true);
|
B3_ASSERT(IsDragging() == true);
|
||||||
|
|
||||||
m_cloth = nullptr;
|
|
||||||
|
|
||||||
if (m_spring)
|
if (m_spring)
|
||||||
{
|
{
|
||||||
m_cloth->DestroyForce(m_s1);
|
m_cloth->DestroyForce(m_s1);
|
||||||
@ -177,24 +175,21 @@ public:
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
b3Particle* p1 = m_mesh->particles[m_triangle->v1];
|
m_cloth->GetVertexParticle(m_triangle->v1)->SetType(m_t1);
|
||||||
p1->SetType(m_t1);
|
m_cloth->GetVertexParticle(m_triangle->v2)->SetType(m_t2);
|
||||||
|
m_cloth->GetVertexParticle(m_triangle->v3)->SetType(m_t3);
|
||||||
b3Particle* p2 = m_mesh->particles[m_triangle->v2];
|
|
||||||
p2->SetType(m_t2);
|
|
||||||
|
|
||||||
b3Particle* p3 = m_mesh->particles[m_triangle->v3];
|
|
||||||
p3->SetType(m_t3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_cloth = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
b3Vec3 GetPointA() const
|
b3Vec3 GetPointA() const
|
||||||
{
|
{
|
||||||
B3_ASSERT(IsDragging() == true);
|
B3_ASSERT(IsDragging() == true);
|
||||||
|
|
||||||
b3Vec3 A = m_mesh->particles[m_triangle->v1]->GetPosition();
|
b3Vec3 A = m_cloth->GetVertexParticle(m_triangle->v1)->GetPosition();
|
||||||
b3Vec3 B = m_mesh->particles[m_triangle->v2]->GetPosition();
|
b3Vec3 B = m_cloth->GetVertexParticle(m_triangle->v2)->GetPosition();
|
||||||
b3Vec3 C = m_mesh->particles[m_triangle->v3]->GetPosition();
|
b3Vec3 C = m_cloth->GetVertexParticle(m_triangle->v3)->GetPosition();
|
||||||
|
|
||||||
return m_u * A + m_v * B + (1.0f - m_u - m_v) * C;
|
return m_u * A + m_v * B + (1.0f - m_u - m_v) * C;
|
||||||
}
|
}
|
||||||
@ -212,7 +207,7 @@ private:
|
|||||||
b3World* m_world;
|
b3World* m_world;
|
||||||
|
|
||||||
b3Cloth* m_cloth;
|
b3Cloth* m_cloth;
|
||||||
b3ClothMesh* m_mesh;
|
const b3ClothMesh* m_mesh;
|
||||||
b3ClothMeshTriangle* m_triangle;
|
b3ClothMeshTriangle* m_triangle;
|
||||||
float32 m_u, m_v;
|
float32 m_u, m_v;
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ struct b3ClothDef
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cloth mesh
|
// Cloth mesh
|
||||||
b3ClothMesh* mesh;
|
const b3ClothMesh* mesh;
|
||||||
|
|
||||||
// Cloth density in kg/m^3
|
// Cloth density in kg/m^3
|
||||||
float32 density;
|
float32 density;
|
||||||
@ -106,7 +106,10 @@ public:
|
|||||||
bool RayCast(b3RayCastOutput* output, const b3RayCastInput* input, u32 triangleIndex) const;
|
bool RayCast(b3RayCastOutput* output, const b3RayCastInput* input, u32 triangleIndex) const;
|
||||||
|
|
||||||
// Return the cloth mesh proxy.
|
// Return the cloth mesh proxy.
|
||||||
b3ClothMesh* GetMesh() const;
|
const b3ClothMesh* GetMesh() const;
|
||||||
|
|
||||||
|
// Return the particle associated with the given vertex.
|
||||||
|
b3Particle* GetVertexParticle(u32 i);
|
||||||
|
|
||||||
// Return the list of particles in this cloth.
|
// Return the list of particles in this cloth.
|
||||||
const b3List2<b3Particle>& GetParticleList() const;
|
const b3List2<b3Particle>& GetParticleList() const;
|
||||||
@ -148,8 +151,11 @@ private:
|
|||||||
void Solve(float32 dt, const b3Vec3& gravity);
|
void Solve(float32 dt, const b3Vec3& gravity);
|
||||||
|
|
||||||
// Proxy mesh
|
// Proxy mesh
|
||||||
b3ClothMesh* m_mesh;
|
const b3ClothMesh* m_mesh;
|
||||||
|
|
||||||
|
// Vertex particles
|
||||||
|
b3Particle** m_vertexParticles;
|
||||||
|
|
||||||
// Mesh density
|
// Mesh density
|
||||||
float32 m_density;
|
float32 m_density;
|
||||||
|
|
||||||
@ -180,7 +186,7 @@ inline b3World* b3Cloth::GetWorld()
|
|||||||
return m_world;
|
return m_world;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline b3ClothMesh* b3Cloth::GetMesh() const
|
inline const b3ClothMesh* b3Cloth::GetMesh() const
|
||||||
{
|
{
|
||||||
return m_mesh;
|
return m_mesh;
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,6 @@
|
|||||||
#include <bounce/common/math/vec2.h>
|
#include <bounce/common/math/vec2.h>
|
||||||
#include <bounce/common/math/vec3.h>
|
#include <bounce/common/math/vec3.h>
|
||||||
|
|
||||||
class b3Particle;
|
|
||||||
|
|
||||||
struct b3ClothMeshTriangle
|
struct b3ClothMeshTriangle
|
||||||
{
|
{
|
||||||
u32 v1, v2, v3;
|
u32 v1, v2, v3;
|
||||||
@ -47,7 +45,6 @@ struct b3ClothMesh
|
|||||||
{
|
{
|
||||||
u32 vertexCount;
|
u32 vertexCount;
|
||||||
b3Vec3* vertices;
|
b3Vec3* vertices;
|
||||||
b3Particle** particles;
|
|
||||||
u32 triangleCount;
|
u32 triangleCount;
|
||||||
b3ClothMeshTriangle* triangles;
|
b3ClothMeshTriangle* triangles;
|
||||||
u32 meshCount;
|
u32 meshCount;
|
||||||
|
@ -157,8 +157,10 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo
|
|||||||
m_world = world;
|
m_world = world;
|
||||||
m_mesh = def.mesh;
|
m_mesh = def.mesh;
|
||||||
m_density = def.density;
|
m_density = def.density;
|
||||||
|
|
||||||
|
const b3ClothMesh* m = m_mesh;
|
||||||
|
|
||||||
b3ClothMesh* m = m_mesh;
|
m_vertexParticles = (b3Particle**)b3Alloc(m->vertexCount * sizeof(b3Particle*));
|
||||||
|
|
||||||
// Create particles
|
// Create particles
|
||||||
for (u32 i = 0; i < m->vertexCount; ++i)
|
for (u32 i = 0; i < m->vertexCount; ++i)
|
||||||
@ -170,7 +172,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo
|
|||||||
b3Particle* p = CreateParticle(pd);
|
b3Particle* p = CreateParticle(pd);
|
||||||
|
|
||||||
p->m_vertex = i;
|
p->m_vertex = i;
|
||||||
m->particles[i] = p;
|
|
||||||
|
m_vertexParticles[i] = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute mass
|
// Compute mass
|
||||||
@ -192,8 +195,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo
|
|||||||
{
|
{
|
||||||
b3UniqueEdge* e = uniqueEdges + i;
|
b3UniqueEdge* e = uniqueEdges + i;
|
||||||
|
|
||||||
b3Particle* p1 = m->particles[e->v1];
|
b3Particle* p1 = m_vertexParticles[e->v1];
|
||||||
b3Particle* p2 = m->particles[e->v2];
|
b3Particle* p2 = m_vertexParticles[e->v2];
|
||||||
|
|
||||||
b3SpringForceDef fd;
|
b3SpringForceDef fd;
|
||||||
fd.Initialize(p1, p2, def.structural, def.damping);
|
fd.Initialize(p1, p2, def.structural, def.damping);
|
||||||
@ -206,10 +209,10 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo
|
|||||||
{
|
{
|
||||||
b3SharedEdge* e = sharedEdges + i;
|
b3SharedEdge* e = sharedEdges + i;
|
||||||
|
|
||||||
b3Particle* p1 = m->particles[e->v1];
|
b3Particle* p1 = m_vertexParticles[e->v1];
|
||||||
b3Particle* p2 = m->particles[e->v2];
|
b3Particle* p2 = m_vertexParticles[e->v2];
|
||||||
b3Particle* p3 = m->particles[e->nsv1];
|
b3Particle* p3 = m_vertexParticles[e->nsv1];
|
||||||
b3Particle* p4 = m->particles[e->nsv2];
|
b3Particle* p4 = m_vertexParticles[e->nsv2];
|
||||||
|
|
||||||
b3BendForceDef fd;
|
b3BendForceDef fd;
|
||||||
fd.Initialize(p1, p2, p3, p4, def.bending, def.damping);
|
fd.Initialize(p1, p2, p3, p4, def.bending, def.damping);
|
||||||
@ -225,8 +228,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo
|
|||||||
{
|
{
|
||||||
b3ClothMeshSewingLine* line = m->sewingLines + i;
|
b3ClothMeshSewingLine* line = m->sewingLines + i;
|
||||||
|
|
||||||
b3Particle* p1 = m->particles[line->v1];
|
b3Particle* p1 = m_vertexParticles[line->v1];
|
||||||
b3Particle* p2 = m->particles[line->v2];
|
b3Particle* p2 = m_vertexParticles[line->v2];
|
||||||
|
|
||||||
b3SpringForceDef fd;
|
b3SpringForceDef fd;
|
||||||
fd.Initialize(p1, p2, def.structural, def.damping);
|
fd.Initialize(p1, p2, def.structural, def.damping);
|
||||||
@ -245,6 +248,8 @@ b3Cloth::~b3Cloth()
|
|||||||
p0->~b3Particle();
|
p0->~b3Particle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b3Free(m_vertexParticles);
|
||||||
|
|
||||||
b3Force* f = m_forceList.m_head;
|
b3Force* f = m_forceList.m_head;
|
||||||
while (f)
|
while (f)
|
||||||
{
|
{
|
||||||
@ -267,7 +272,7 @@ void b3Cloth::DestroyParticle(b3Particle* particle)
|
|||||||
{
|
{
|
||||||
if (particle->m_vertex != ~0)
|
if (particle->m_vertex != ~0)
|
||||||
{
|
{
|
||||||
m_mesh->particles[particle->m_vertex] = NULL;
|
m_vertexParticles[particle->m_vertex] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_particleList.Remove(particle);
|
m_particleList.Remove(particle);
|
||||||
@ -298,6 +303,12 @@ float32 b3Cloth::GetEnergy() const
|
|||||||
return 0.5f * E;
|
return 0.5f * E;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b3Particle* b3Cloth::GetVertexParticle(u32 i)
|
||||||
|
{
|
||||||
|
B3_ASSERT(i < m_mesh->vertexCount);
|
||||||
|
return m_vertexParticles[i];
|
||||||
|
}
|
||||||
|
|
||||||
void b3Cloth::ComputeMass()
|
void b3Cloth::ComputeMass()
|
||||||
{
|
{
|
||||||
for (b3Particle* p = m_particleList.m_head; p; p = p->m_next)
|
for (b3Particle* p = m_particleList.m_head; p; p = p->m_next)
|
||||||
@ -322,9 +333,9 @@ void b3Cloth::ComputeMass()
|
|||||||
|
|
||||||
float32 mass = rho * area;
|
float32 mass = rho * area;
|
||||||
|
|
||||||
b3Particle* p1 = m_mesh->particles[triangle->v1];
|
b3Particle* p1 = m_vertexParticles[triangle->v1];
|
||||||
b3Particle* p2 = m_mesh->particles[triangle->v2];
|
b3Particle* p2 = m_vertexParticles[triangle->v2];
|
||||||
b3Particle* p3 = m_mesh->particles[triangle->v3];
|
b3Particle* p3 = m_vertexParticles[triangle->v3];
|
||||||
|
|
||||||
p1->m_mass += inv3 * mass;
|
p1->m_mass += inv3 * mass;
|
||||||
p2->m_mass += inv3 * mass;
|
p2->m_mass += inv3 * mass;
|
||||||
@ -408,9 +419,9 @@ bool b3Cloth::RayCast(b3RayCastOutput* output, const b3RayCastInput* input, u32
|
|||||||
B3_ASSERT(triangleIndex < m_mesh->triangleCount);
|
B3_ASSERT(triangleIndex < m_mesh->triangleCount);
|
||||||
b3ClothMeshTriangle* triangle = m_mesh->triangles + triangleIndex;
|
b3ClothMeshTriangle* triangle = m_mesh->triangles + triangleIndex;
|
||||||
|
|
||||||
b3Vec3 v1 = m_mesh->particles[triangle->v1]->m_position;
|
b3Vec3 v1 = m_vertexParticles[triangle->v1]->m_position;
|
||||||
b3Vec3 v2 = m_mesh->particles[triangle->v2]->m_position;
|
b3Vec3 v2 = m_vertexParticles[triangle->v2]->m_position;
|
||||||
b3Vec3 v3 = m_mesh->particles[triangle->v3]->m_position;
|
b3Vec3 v3 = m_vertexParticles[triangle->v3]->m_position;
|
||||||
|
|
||||||
b3Vec3 p1 = input->p1;
|
b3Vec3 p1 = input->p1;
|
||||||
b3Vec3 p2 = input->p2;
|
b3Vec3 p2 = input->p2;
|
||||||
@ -773,8 +784,8 @@ void b3Cloth::Draw() const
|
|||||||
for (u32 i = 0; i < m->sewingLineCount; ++i)
|
for (u32 i = 0; i < m->sewingLineCount; ++i)
|
||||||
{
|
{
|
||||||
b3ClothMeshSewingLine* s = m->sewingLines + i;
|
b3ClothMeshSewingLine* s = m->sewingLines + i;
|
||||||
b3Particle* p1 = m->particles[s->v1];
|
b3Particle* p1 = m_vertexParticles[s->v1];
|
||||||
b3Particle* p2 = m->particles[s->v2];
|
b3Particle* p2 = m_vertexParticles[s->v2];
|
||||||
|
|
||||||
b3Draw_draw->DrawSegment(p1->m_position, p2->m_position, b3Color_white);
|
b3Draw_draw->DrawSegment(p1->m_position, p2->m_position, b3Color_white);
|
||||||
}
|
}
|
||||||
@ -783,9 +794,9 @@ void b3Cloth::Draw() const
|
|||||||
{
|
{
|
||||||
b3ClothMeshTriangle* t = m->triangles + i;
|
b3ClothMeshTriangle* t = m->triangles + i;
|
||||||
|
|
||||||
b3Particle* p1 = m->particles[t->v1];
|
b3Particle* p1 = m_vertexParticles[t->v1];
|
||||||
b3Particle* p2 = m->particles[t->v2];
|
b3Particle* p2 = m_vertexParticles[t->v2];
|
||||||
b3Particle* p3 = m->particles[t->v3];
|
b3Particle* p3 = m_vertexParticles[t->v3];
|
||||||
|
|
||||||
b3Vec3 v1 = p1->m_position;
|
b3Vec3 v1 = p1->m_position;
|
||||||
b3Vec3 v2 = p2->m_position;
|
b3Vec3 v2 = p2->m_position;
|
||||||
|
@ -25,7 +25,6 @@ b3GarmentClothMesh::b3GarmentClothMesh()
|
|||||||
{
|
{
|
||||||
vertexCount = 0;
|
vertexCount = 0;
|
||||||
vertices = nullptr;
|
vertices = nullptr;
|
||||||
particles = nullptr;
|
|
||||||
triangleCount = 0;
|
triangleCount = 0;
|
||||||
triangles = nullptr;
|
triangles = nullptr;
|
||||||
meshCount = 0;
|
meshCount = 0;
|
||||||
@ -37,7 +36,6 @@ b3GarmentClothMesh::b3GarmentClothMesh()
|
|||||||
b3GarmentClothMesh::~b3GarmentClothMesh()
|
b3GarmentClothMesh::~b3GarmentClothMesh()
|
||||||
{
|
{
|
||||||
b3Free(vertices);
|
b3Free(vertices);
|
||||||
b3Free(particles);
|
|
||||||
b3Free(triangles);
|
b3Free(triangles);
|
||||||
b3Free(meshes);
|
b3Free(meshes);
|
||||||
b3Free(sewingLines);
|
b3Free(sewingLines);
|
||||||
@ -51,9 +49,7 @@ void b3GarmentClothMesh::Set(const b3GarmentMesh* garment)
|
|||||||
vertexCount += garment->meshes[i].vertexCount;
|
vertexCount += garment->meshes[i].vertexCount;
|
||||||
}
|
}
|
||||||
vertices = (b3Vec3*)b3Alloc(vertexCount * sizeof(b3Vec3));
|
vertices = (b3Vec3*)b3Alloc(vertexCount * sizeof(b3Vec3));
|
||||||
particles = (b3Particle**)b3Alloc(vertexCount * sizeof(b3Particle*));
|
|
||||||
memset(particles, 0, vertexCount * sizeof(b3Particle*));
|
|
||||||
|
|
||||||
B3_ASSERT(triangleCount == 0);
|
B3_ASSERT(triangleCount == 0);
|
||||||
for (u32 i = 0; i < garment->meshCount; ++i)
|
for (u32 i = 0; i < garment->meshCount; ++i)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user