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

@ -57,8 +57,11 @@
#include <bounce/dynamics/contacts/mesh_contact.h>
#include <bounce/dynamics/rope/rope.h>
#include <bounce/dynamics/cloth/cloth_mesh.h>
#include <bounce/dynamics/cloth/cloth.h>
#include <bounce/dynamics/cloth/spring_cloth.h>
#include <bounce/dynamics/body.h>
//#include <bounce/dynamics/tree/joints/tree_weld_joint.h>

View File

@ -0,0 +1,69 @@
/*
* 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 B3_CLOTH_MESH_H
#define B3_CLOTH_MESH_H
#include <bounce/common/math/vec2.h>
#include <bounce/common/math/vec3.h>
struct b3ClothMeshTriangle
{
u32 v1, v2, v3;
};
struct b3ClothMeshMesh
{
u32 vertexCount;
u32 startVertex;
u32 triangleCount;
u32 startTriangle;
};
struct b3ClothMeshSewingLine
{
u32 s1, s2;
u32 v1, v2;
};
struct b3ClothMesh
{
u32 vertexCount;
b3Vec3* vertices;
u32 triangleCount;
b3ClothMeshTriangle* triangles;
u32 meshCount;
b3ClothMeshMesh* meshes;
u32 sewingLineCount;
b3ClothMeshSewingLine* sewingLines;
};
struct b3GarmentMesh;
// Convenience structure.
struct b3GarmentClothMesh : public b3ClothMesh
{
b3GarmentClothMesh();
~b3GarmentClothMesh();
// Maps a given garment mesh to this mesh.
// This garment mesh must be empty.
void Set(const b3GarmentMesh* garment);
};
#endif

View File

@ -29,7 +29,7 @@ class b3StackAllocator;
class b3Shape;
struct b3Mesh;
struct b3ClothMesh;
struct b3SpringClothDef
{
@ -49,7 +49,7 @@ struct b3SpringClothDef
b3StackAllocator* allocator;
// Cloth mesh
b3Mesh* mesh;
b3ClothMesh* mesh;
// Cloth density in kg/m^3
float32 density;
@ -64,8 +64,8 @@ struct b3SpringClothDef
float32 kd;
// Mass radius
// Typically this is value is small and is intended for correcting visual artifacts when
// the cloth is colliding against a solid.
// This should be a small value. It can be used for correcting visual artifacts when
// the masses are colliding against a solid.
float32 r;
// Acceleration due to gravity (m/s^2)
@ -75,7 +75,8 @@ struct b3SpringClothDef
enum b3SpringType
{
e_strechSpring,
e_bendSpring
e_bendSpring,
// e_sewingSpring
};
struct b3Spring
@ -138,7 +139,7 @@ public:
void Initialize(const b3SpringClothDef& def);
// Return the cloth mesh used to initialize this cloth.
b3Mesh* GetMesh() const;
b3ClothMesh* GetMesh() const;
// Set the gravitational acceleration applied to this cloth.
// Units are m/s^2.
@ -210,7 +211,7 @@ protected:
b3StackAllocator* m_allocator;
b3Mesh* m_mesh;
b3ClothMesh* m_mesh;
float32 m_r;
b3Vec3 m_gravity;
@ -237,7 +238,7 @@ protected:
b3SpringClothStep m_step;
};
inline b3Mesh* b3SpringCloth::GetMesh() const
inline b3ClothMesh* b3SpringCloth::GetMesh() const
{
return m_mesh;
}

View File

@ -0,0 +1,132 @@
/*
* 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 B3_GARMENT_H
#define B3_GARMENT_H
#include <bounce/garment/sewing_pattern.h>
struct b3SewingLine
{
u32 p1, p2;
u32 v1, v2;
};
struct b3Garment
{
u32 patternCount;
b3SewingPattern** patterns;
u32 sewingCount;
b3SewingLine* sewingLines;
};
struct b3ShirtGarment : public b3Garment
{
b3RectanglePattern frontBody;
b3RectanglePattern frontRightSleeve;
b3RectanglePattern frontLeftSleeve;
b3RectanglePattern backBody;
b3RectanglePattern backRightSleeve;
b3RectanglePattern backLeftSleeve;
b3SewingLine shirtSewingLines[12];
b3SewingPattern* shirtPatterns[6];
b3ShirtGarment() :
frontBody(1.0f, 1.0f),
frontRightSleeve(0.2f, 0.2f),
frontLeftSleeve(0.2f, 0.2f),
backBody(1.0f, 1.0f),
backRightSleeve(0.2f, 0.2f),
backLeftSleeve(0.2f, 0.2f)
{
b3Vec2 etr;
etr.x = 1.2f;
etr.y = 0.8f;
b3Vec2 etl;
etl.x = -1.2f;
etl.y = 0.8f;
for (u32 i = 0; i < 4; ++i)
{
frontRightSleeve.vertices[i] += etr;
frontLeftSleeve.vertices[i] += etl;
backRightSleeve.vertices[i] += etr;
backLeftSleeve.vertices[i] += etl;
}
// Perform sewing
u32 count = 0;
// Sew bodies
for (u32 i = 0; i < frontBody.vertexCount; ++i)
{
b3SewingLine line;
line.p1 = 0;
line.p2 = 3;
line.v1 = i;
line.v2 = i;
shirtSewingLines[count++] = line;
}
// Sew sleeves
for (u32 i = 0; i < frontRightSleeve.vertexCount; ++i)
{
b3SewingLine line;
line.p1 = 1;
line.p2 = 4;
line.v1 = i;
line.v2 = i;
shirtSewingLines[count++] = line;
}
for (u32 i = 0; i < frontLeftSleeve.vertexCount; ++i)
{
b3SewingLine line;
line.p1 = 2;
line.p2 = 5;
line.v1 = i;
line.v2 = i;
shirtSewingLines[count++] = line;
}
B3_ASSERT(12 == count);
shirtPatterns[0] = &frontBody;
shirtPatterns[1] = &frontRightSleeve;
shirtPatterns[2] = &frontLeftSleeve;
shirtPatterns[3] = &backBody;
shirtPatterns[4] = &backRightSleeve;
shirtPatterns[5] = &backLeftSleeve;
patternCount = 6;
patterns = shirtPatterns;
sewingCount = 12;
sewingLines = shirtSewingLines;
}
};
#endif

View File

@ -0,0 +1,63 @@
/*
* 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 B3_GARMENT_MESH_H
#define B3_GARMENT_MESH_H
#include <bounce/common/math/vec2.h>
#include <bounce/common/math/vec3.h>
struct b3Garment;
struct b3SewingPatternMeshTriangle
{
u32 v1, v2, v3;
};
struct b3SewingPatternMesh
{
u32 vertexCount;
b3Vec2* vertices;
u32 triangleCount;
b3SewingPatternMeshTriangle* triangles;
};
struct b3GarmentMeshSewingLine
{
u32 s1, s2;
u32 v1, v2;
};
// A garment mesh is a triangulated version of a garment.
struct b3GarmentMesh
{
u32 meshCount;
b3SewingPatternMesh* meshes;
u32 sewingCount;
b3GarmentMeshSewingLine* sewingLines;
b3Garment* garment;
b3GarmentMesh();
~b3GarmentMesh();
void Set(b3Garment* garment, float32 desiredArea);
};
#endif

View File

@ -0,0 +1,81 @@
/*
* 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 B3_SEWING_PATTERN_H
#define B3_SEWING_PATTERN_H
#include <bounce/common/math/vec2.h>
struct b3SewingPattern
{
u32 vertexCount;
b3Vec2* vertices;
};
struct b3RectanglePattern : public b3SewingPattern
{
b3Vec2 rectangleVertices[4];
b3RectanglePattern(float32 ex = 1.0f, float32 ey = 1.0f)
{
B3_ASSERT(ex > 0.0f);
B3_ASSERT(ey > 0.0f);
// R, CCW
rectangleVertices[0].Set(ex, -ey);
rectangleVertices[1].Set(ex, ey);
// L, CW
rectangleVertices[2].Set(-ex, ey);
rectangleVertices[3].Set(-ex, -ey);
vertexCount = 4;
vertices = rectangleVertices;
}
};
template<u32 E = 16>
struct b3CirclePattern : public b3SewingPattern
{
b3Vec2 circleVertices[E];
b3CirclePattern(b3Vec2 center = b3Vec2_zero, float32 radius = 1.0f)
{
float32 x = 2.0f * B3_PI / float32(E);
float32 c = cos(x);
float32 s = sin(x);
b3Mat22 R;
R.x.Set(c, s);
R.y.Set(-s, c);
b3Vec2 n(1.0f, 0.0f);
circleVertices[0] = center + radius * n;
for (u32 i = 1; i < E; ++i)
{
b3Vec2 ni = R * n;
circleVertices[i] = center + radius * n;
n = ni;
}
vertexCount = E;
vertices = circleVertices;
}
};
#endif