Use sphere mesh. Add soft body test.
This commit is contained in:
parent
695514989e
commit
399a6efc72
@ -28,6 +28,8 @@
|
||||
#include <bounce/common/math/mat44.h>
|
||||
#include <bounce/common/draw.h>
|
||||
|
||||
#include <testbed/framework/sphere_mesh.h>
|
||||
|
||||
#define BUFFER_OFFSET(i) ((char*)NULL + (i))
|
||||
|
||||
extern bool g_glDrawPoints;
|
||||
@ -535,72 +537,22 @@ struct DrawTriangles
|
||||
|
||||
struct DrawWireSphere
|
||||
{
|
||||
enum
|
||||
{
|
||||
e_rings = 12,
|
||||
e_sectors = 12,
|
||||
e_vertexCount = e_rings * e_sectors,
|
||||
e_indexCount = (e_rings - 1) * (e_sectors - 1) * 8
|
||||
};
|
||||
|
||||
DrawWireSphere()
|
||||
{
|
||||
float32 R = 1.0f / float32(e_rings - 1);
|
||||
float32 S = 1.0f / float32(e_sectors - 1);
|
||||
smMesh mesh;
|
||||
smCreateMesh(mesh, 1);
|
||||
|
||||
b3Vec3 vs[e_vertexCount];
|
||||
b3Vec3 ns[e_vertexCount];
|
||||
b3Color cs[e_vertexCount];
|
||||
|
||||
u32 vc = 0;
|
||||
for (u32 r = 0; r < e_rings; r++)
|
||||
{
|
||||
for (u32 s = 0; s < e_sectors; s++)
|
||||
{
|
||||
float32 y = sin(-0.5f * B3_PI + B3_PI * r * R);
|
||||
float32 x = cos(2.0f * B3_PI * s * S) * sin(B3_PI * r * R);
|
||||
float32 z = sin(2.0f * B3_PI * s * S) * sin(B3_PI * r * R);
|
||||
|
||||
vs[vc].Set(x, y, z);
|
||||
cs[vc] = b3Color(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
++vc;
|
||||
}
|
||||
}
|
||||
|
||||
u32 is[e_indexCount];
|
||||
|
||||
u32 ic = 0;
|
||||
for (u32 r = 0; r < e_rings - 1; r++)
|
||||
{
|
||||
for (u32 s = 0; s < e_sectors - 1; s++)
|
||||
{
|
||||
u32 i1 = r * e_sectors + s;
|
||||
u32 i2 = (r + 1) * e_sectors + s;
|
||||
u32 i3 = (r + 1) * e_sectors + (s + 1);
|
||||
u32 i4 = r * e_sectors + (s + 1);
|
||||
|
||||
is[ic++] = i1;
|
||||
is[ic++] = i2;
|
||||
|
||||
is[ic++] = i2;
|
||||
is[ic++] = i3;
|
||||
|
||||
is[ic++] = i3;
|
||||
is[ic++] = i4;
|
||||
|
||||
is[ic++] = i4;
|
||||
is[ic++] = i1;
|
||||
}
|
||||
}
|
||||
m_vertexCount = mesh.vertices.Count();
|
||||
m_indexCount = mesh.triangleIndices.Count();
|
||||
|
||||
glGenBuffers(1, &m_vboId);
|
||||
glGenBuffers(1, &m_iboId);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
|
||||
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.Count() * sizeof(b3Vec3), mesh.vertices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.triangleIndices.Count() * sizeof(u32), mesh.triangleIndices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
AssertGL();
|
||||
|
||||
@ -616,6 +568,8 @@ struct DrawWireSphere
|
||||
|
||||
GLuint m_vboId;
|
||||
GLuint m_iboId;
|
||||
u32 m_vertexCount;
|
||||
u32 m_indexCount;
|
||||
};
|
||||
|
||||
struct DrawWire
|
||||
@ -677,7 +631,7 @@ struct DrawWire
|
||||
glEnableVertexAttribArray(m_vertexAttribute);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphere.m_iboId);
|
||||
glDrawElements(GL_LINES, m_sphere.e_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
|
||||
glDrawElements(GL_LINES, m_sphere.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
|
||||
|
||||
glDisableVertexAttribArray(m_vertexAttribute);
|
||||
|
||||
@ -697,84 +651,25 @@ struct DrawWire
|
||||
|
||||
struct DrawSolidSphere
|
||||
{
|
||||
enum
|
||||
{
|
||||
e_rings = 18,
|
||||
e_sectors = 18,
|
||||
e_vertexCount = e_rings * e_sectors,
|
||||
e_indexCount = (e_rings - 1) * (e_sectors - 1) * 6,
|
||||
e_faceCount = e_indexCount / 3
|
||||
};
|
||||
|
||||
DrawSolidSphere()
|
||||
{
|
||||
float32 R = 1.0f / float32(e_rings - 1);
|
||||
float32 S = 1.0f / float32(e_sectors - 1);
|
||||
smMesh mesh;
|
||||
smCreateMesh(mesh, 1);
|
||||
|
||||
b3Vec3 vs[e_vertexCount];
|
||||
b3Vec3 ns[e_vertexCount];
|
||||
|
||||
u32 vc = 0;
|
||||
for (u32 r = 0; r < e_rings; r++)
|
||||
{
|
||||
for (u32 s = 0; s < e_sectors; s++)
|
||||
{
|
||||
float32 a1 = 2.0f * B3_PI * float32(s) * S;
|
||||
float32 c1 = cos(a1);
|
||||
float32 s1 = sin(a1);
|
||||
|
||||
float32 a2 = -0.5f * B3_PI + B3_PI * float32(r) * R;
|
||||
float32 s2 = sin(a2);
|
||||
|
||||
float32 a3 = B3_PI * float32(r) * R;
|
||||
float32 s3 = sin(a3);
|
||||
|
||||
float32 x = c1 * s3;
|
||||
float32 y = s2;
|
||||
float32 z = s1 * s3;
|
||||
|
||||
b3Vec3 v(x, y, z);
|
||||
v.Normalize();
|
||||
|
||||
vs[vc] = v;
|
||||
ns[vc] = v;
|
||||
++vc;
|
||||
}
|
||||
}
|
||||
|
||||
u32 is[e_indexCount];
|
||||
|
||||
u32 ic = 0;
|
||||
for (u32 r = 0; r < e_rings - 1; r++)
|
||||
{
|
||||
for (u32 s = 0; s < e_sectors - 1; s++)
|
||||
{
|
||||
u32 i1 = r * e_sectors + s;
|
||||
u32 i2 = (r + 1) * e_sectors + s;
|
||||
u32 i3 = (r + 1) * e_sectors + (s + 1);
|
||||
u32 i4 = r * e_sectors + (s + 1);
|
||||
|
||||
is[ic++] = i1;
|
||||
is[ic++] = i2;
|
||||
is[ic++] = i3;
|
||||
|
||||
is[ic++] = i1;
|
||||
is[ic++] = i3;
|
||||
is[ic++] = i4;
|
||||
}
|
||||
}
|
||||
m_vertexCount = mesh.vertices.Count();
|
||||
m_indexCount = mesh.triangleIndices.Count();
|
||||
|
||||
glGenBuffers(3, m_vboIds);
|
||||
glGenBuffers(1, &m_iboId);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.Count() * sizeof(b3Vec3), mesh.vertices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[1]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), ns, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.Count() * sizeof(b3Vec3), mesh.vertices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.triangleIndices.Count() * sizeof(u32), mesh.triangleIndices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
AssertGL();
|
||||
|
||||
@ -790,6 +685,8 @@ struct DrawSolidSphere
|
||||
|
||||
GLuint m_vboIds[2];
|
||||
GLuint m_iboId;
|
||||
u32 m_vertexCount;
|
||||
u32 m_indexCount;
|
||||
};
|
||||
|
||||
struct DrawSolidCylinder
|
||||
@ -1021,7 +918,7 @@ struct DrawSolid
|
||||
glEnableVertexAttribArray(m_normalAttribute);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphere.m_iboId);
|
||||
glDrawElements(GL_TRIANGLES, m_sphere.e_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
|
||||
glDrawElements(GL_TRIANGLES, m_sphere.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
|
||||
|
||||
glDisableVertexAttribArray(m_normalAttribute);
|
||||
|
||||
|
@ -28,6 +28,8 @@
|
||||
#include <bounce/common/math/mat44.h>
|
||||
#include <bounce/common/draw.h>
|
||||
|
||||
#include <testbed/framework/sphere_mesh.h>
|
||||
|
||||
#define BUFFER_OFFSET(i) ((char*)NULL + (i))
|
||||
|
||||
extern bool g_glDrawPoints;
|
||||
@ -551,72 +553,22 @@ struct DrawTriangles
|
||||
|
||||
struct DrawWireSphere
|
||||
{
|
||||
enum
|
||||
{
|
||||
e_rings = 12,
|
||||
e_sectors = 12,
|
||||
e_vertexCount = e_rings * e_sectors,
|
||||
e_indexCount = (e_rings - 1) * (e_sectors - 1) * 8
|
||||
};
|
||||
|
||||
DrawWireSphere()
|
||||
{
|
||||
float32 R = 1.0f / float32(e_rings - 1);
|
||||
float32 S = 1.0f / float32(e_sectors - 1);
|
||||
smMesh mesh;
|
||||
smCreateMesh(mesh, 1);
|
||||
|
||||
b3Vec3 vs[e_vertexCount];
|
||||
b3Vec3 ns[e_vertexCount];
|
||||
b3Color cs[e_vertexCount];
|
||||
|
||||
u32 vc = 0;
|
||||
for (u32 r = 0; r < e_rings; r++)
|
||||
{
|
||||
for (u32 s = 0; s < e_sectors; s++)
|
||||
{
|
||||
float32 y = sin(-0.5f * B3_PI + B3_PI * r * R);
|
||||
float32 x = cos(2.0f * B3_PI * s * S) * sin(B3_PI * r * R);
|
||||
float32 z = sin(2.0f * B3_PI * s * S) * sin(B3_PI * r * R);
|
||||
|
||||
vs[vc].Set(x, y, z);
|
||||
cs[vc] = b3Color(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
++vc;
|
||||
}
|
||||
}
|
||||
|
||||
u32 is[e_indexCount];
|
||||
|
||||
u32 ic = 0;
|
||||
for (u32 r = 0; r < e_rings - 1; r++)
|
||||
{
|
||||
for (u32 s = 0; s < e_sectors - 1; s++)
|
||||
{
|
||||
u32 i1 = r * e_sectors + s;
|
||||
u32 i2 = (r + 1) * e_sectors + s;
|
||||
u32 i3 = (r + 1) * e_sectors + (s + 1);
|
||||
u32 i4 = r * e_sectors + (s + 1);
|
||||
|
||||
is[ic++] = i1;
|
||||
is[ic++] = i2;
|
||||
|
||||
is[ic++] = i2;
|
||||
is[ic++] = i3;
|
||||
|
||||
is[ic++] = i3;
|
||||
is[ic++] = i4;
|
||||
|
||||
is[ic++] = i4;
|
||||
is[ic++] = i1;
|
||||
}
|
||||
}
|
||||
m_vertexCount = mesh.vertices.Count();
|
||||
m_indexCount = mesh.triangleIndices.Count();
|
||||
|
||||
glGenBuffers(1, &m_vboId);
|
||||
glGenBuffers(1, &m_iboId);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
|
||||
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(u32), mesh.triangleIndices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
AssertGL();
|
||||
|
||||
@ -632,6 +584,8 @@ struct DrawWireSphere
|
||||
|
||||
GLuint m_vboId;
|
||||
GLuint m_iboId;
|
||||
u32 m_vertexCount;
|
||||
u32 m_indexCount;
|
||||
};
|
||||
|
||||
struct DrawWire
|
||||
@ -694,7 +648,7 @@ struct DrawWire
|
||||
glEnableVertexAttribArray(m_vertexAttribute);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphere.m_iboId);
|
||||
glDrawElements(GL_LINES, m_sphere.e_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
|
||||
glDrawElements(GL_LINES, m_sphere.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
@ -712,84 +666,25 @@ struct DrawWire
|
||||
|
||||
struct DrawSolidSphere
|
||||
{
|
||||
enum
|
||||
{
|
||||
e_rings = 18,
|
||||
e_sectors = 18,
|
||||
e_vertexCount = e_rings * e_sectors,
|
||||
e_indexCount = (e_rings - 1) * (e_sectors - 1) * 6,
|
||||
e_faceCount = e_indexCount / 3
|
||||
};
|
||||
|
||||
DrawSolidSphere()
|
||||
{
|
||||
float32 R = 1.0f / float32(e_rings - 1);
|
||||
float32 S = 1.0f / float32(e_sectors - 1);
|
||||
smMesh mesh;
|
||||
smCreateMesh(mesh, 1);
|
||||
|
||||
b3Vec3 vs[e_vertexCount];
|
||||
b3Vec3 ns[e_vertexCount];
|
||||
|
||||
u32 vc = 0;
|
||||
for (u32 r = 0; r < e_rings; r++)
|
||||
{
|
||||
for (u32 s = 0; s < e_sectors; s++)
|
||||
{
|
||||
float32 a1 = 2.0f * B3_PI * float32(s) * S;
|
||||
float32 c1 = cos(a1);
|
||||
float32 s1 = sin(a1);
|
||||
|
||||
float32 a2 = -0.5f * B3_PI + B3_PI * float32(r) * R;
|
||||
float32 s2 = sin(a2);
|
||||
|
||||
float32 a3 = B3_PI * float32(r) * R;
|
||||
float32 s3 = sin(a3);
|
||||
|
||||
float32 x = c1 * s3;
|
||||
float32 y = s2;
|
||||
float32 z = s1 * s3;
|
||||
|
||||
b3Vec3 v(x, y, z);
|
||||
v.Normalize();
|
||||
|
||||
vs[vc] = v;
|
||||
ns[vc] = v;
|
||||
++vc;
|
||||
}
|
||||
}
|
||||
|
||||
u32 is[e_indexCount];
|
||||
|
||||
u32 ic = 0;
|
||||
for (u32 r = 0; r < e_rings - 1; r++)
|
||||
{
|
||||
for (u32 s = 0; s < e_sectors - 1; s++)
|
||||
{
|
||||
u32 i1 = r * e_sectors + s;
|
||||
u32 i2 = (r + 1) * e_sectors + s;
|
||||
u32 i3 = (r + 1) * e_sectors + (s + 1);
|
||||
u32 i4 = r * e_sectors + (s + 1);
|
||||
|
||||
is[ic++] = i1;
|
||||
is[ic++] = i2;
|
||||
is[ic++] = i3;
|
||||
|
||||
is[ic++] = i1;
|
||||
is[ic++] = i3;
|
||||
is[ic++] = i4;
|
||||
}
|
||||
}
|
||||
m_vertexCount = mesh.vertices.Count();
|
||||
m_indexCount = mesh.triangleIndices.Count();
|
||||
|
||||
glGenBuffers(3, m_vboIds);
|
||||
glGenBuffers(1, &m_iboId);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[1]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), ns, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(u32), mesh.triangleIndices.Begin(), GL_STATIC_DRAW);
|
||||
|
||||
AssertGL();
|
||||
|
||||
@ -805,6 +700,8 @@ struct DrawSolidSphere
|
||||
|
||||
GLuint m_vboIds[2];
|
||||
GLuint m_iboId;
|
||||
u32 m_vertexCount;
|
||||
u32 m_indexCount;
|
||||
};
|
||||
|
||||
struct DrawSolidCylinder
|
||||
@ -1033,7 +930,7 @@ struct DrawSolid
|
||||
glEnableVertexAttribArray(m_normalAttribute);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphere.m_iboId);
|
||||
glDrawElements(GL_TRIANGLES, m_sphere.e_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
|
||||
glDrawElements(GL_TRIANGLES, m_sphere.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
149
examples/testbed/framework/sphere_mesh.cpp
Normal file
149
examples/testbed/framework/sphere_mesh.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2019 Irlan Robson https://irlanrobson.github.io
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <testbed/framework/sphere_mesh.h>
|
||||
|
||||
// References:
|
||||
// https://github.com/caosdoar/spheres
|
||||
// https://schneide.blog/2016/07/15/generating-an-icosphere-in-c/
|
||||
|
||||
struct smEdge
|
||||
{
|
||||
smEdge() { }
|
||||
|
||||
smEdge(u32 _v1, u32 _v2)
|
||||
{
|
||||
v1 = _v1;
|
||||
v2 = _v2;
|
||||
}
|
||||
|
||||
u32 v1, v2;
|
||||
};
|
||||
|
||||
struct smEdgeVertexPair
|
||||
{
|
||||
smEdgeVertexPair() { }
|
||||
|
||||
smEdgeVertexPair(const smEdge& _edge, u32 _vertex)
|
||||
{
|
||||
edge = _edge;
|
||||
vertex = _vertex;
|
||||
}
|
||||
|
||||
smEdge edge;
|
||||
u32 vertex;
|
||||
};
|
||||
|
||||
struct smEdgeVertexMap
|
||||
{
|
||||
smEdgeVertexMap() { }
|
||||
|
||||
smEdgeVertexPair* Find(const smEdge& edge)
|
||||
{
|
||||
for (u32 i = 0; i < pairs.Count(); ++i)
|
||||
{
|
||||
smEdgeVertexPair* pair = pairs.Get(i);
|
||||
|
||||
if ((pair->edge.v1 == edge.v1 && pair->edge.v2 == edge.v2) ||
|
||||
(pair->edge.v1 == edge.v2 && pair->edge.v2 == edge.v1) )
|
||||
{
|
||||
return pair;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
b3StackArray<smEdgeVertexPair, 32> pairs;
|
||||
};
|
||||
|
||||
//
|
||||
static inline u32 smSubdivideEdge(u32 i1, u32 i2,
|
||||
const b3Vec3& v1, const b3Vec3& v2,
|
||||
smEdgeVertexMap& edgeVertexMap,
|
||||
smMesh& output)
|
||||
{
|
||||
smEdge edge(i1, i2);
|
||||
|
||||
smEdgeVertexPair* pair = edgeVertexMap.Find(edge);
|
||||
|
||||
if (pair)
|
||||
{
|
||||
return pair->vertex;
|
||||
}
|
||||
|
||||
smEdge newEdge(i1, i2);
|
||||
u32 newVertex = output.vertices.Count();
|
||||
|
||||
b3Vec3 v = 0.5f * (v1 + v2);
|
||||
float32 len = v.Normalize();
|
||||
|
||||
output.AddVertex(v.x, v.y, v.z);
|
||||
|
||||
smEdgeVertexPair newPair(newEdge, newVertex);
|
||||
|
||||
edgeVertexMap.pairs.PushBack(newPair);
|
||||
|
||||
return newVertex;
|
||||
}
|
||||
|
||||
//
|
||||
static inline void smSubdivideMesh(smMesh& output, const smMesh& input)
|
||||
{
|
||||
output.vertices = input.vertices;
|
||||
|
||||
smEdgeVertexMap edgeVertexMap;
|
||||
|
||||
for (u32 i = 0; i < input.triangleIndices.Count() / 3; ++i)
|
||||
{
|
||||
u32 vi1 = input.triangleIndices[3 * i + 0];
|
||||
u32 vi2 = input.triangleIndices[3 * i + 1];
|
||||
u32 vi3 = input.triangleIndices[3 * i + 2];
|
||||
|
||||
b3Vec3 v1 = input.vertices[vi1];
|
||||
b3Vec3 v2 = input.vertices[vi2];
|
||||
b3Vec3 v3 = input.vertices[vi3];
|
||||
|
||||
u32 vi4 = smSubdivideEdge(vi1, vi2, v1, v2, edgeVertexMap, output);
|
||||
u32 vi5 = smSubdivideEdge(vi2, vi3, v2, v3, edgeVertexMap, output);
|
||||
u32 vi6 = smSubdivideEdge(vi3, vi1, v3, v1, edgeVertexMap, output);
|
||||
|
||||
output.AddTriangle(vi1, vi4, vi6);
|
||||
output.AddTriangle(vi4, vi2, vi5);
|
||||
output.AddTriangle(vi5, vi3, vi6);
|
||||
output.AddTriangle(vi4, vi5, vi6);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
void smCreateMesh(smMesh& output, u32 subdivisions)
|
||||
{
|
||||
B3_ASSERT(output.vertices.Count() == 0);
|
||||
B3_ASSERT(output.triangleIndices.Count() == 0);
|
||||
|
||||
smMesh input;
|
||||
input.SetAsIcosahedron();
|
||||
|
||||
for (u32 i = 0; i < subdivisions; ++i)
|
||||
{
|
||||
smMesh subOutput;
|
||||
smSubdivideMesh(subOutput, input);
|
||||
input = subOutput;
|
||||
}
|
||||
|
||||
output = input;
|
||||
}
|
113
examples/testbed/framework/sphere_mesh.h
Normal file
113
examples/testbed/framework/sphere_mesh.h
Normal file
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2019 Irlan Robson https://irlanrobson.github.io
|
||||
*
|
||||
* 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 SPHERE_MESH_H
|
||||
#define SPHERE_MESH_H
|
||||
|
||||
#include <bounce/bounce.h>
|
||||
|
||||
//
|
||||
struct smMesh
|
||||
{
|
||||
smMesh() { }
|
||||
|
||||
smMesh& operator=(const smMesh& other)
|
||||
{
|
||||
Copy(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Copy(const smMesh& other)
|
||||
{
|
||||
vertices = other.vertices;
|
||||
triangleIndices = other.triangleIndices;
|
||||
}
|
||||
|
||||
void AddVertex(float32 x, float32 y, float32 z)
|
||||
{
|
||||
vertices.PushBack(b3Vec3(x, y, z));
|
||||
}
|
||||
|
||||
void AddTriangle(u32 v1, u32 v2, u32 v3)
|
||||
{
|
||||
triangleIndices.PushBack(v1);
|
||||
triangleIndices.PushBack(v2);
|
||||
triangleIndices.PushBack(v3);
|
||||
}
|
||||
|
||||
void SetAsIcosahedron()
|
||||
{
|
||||
assert(vertices.Count() == 0);
|
||||
|
||||
float32 t = 0.5f * (1.0f + b3Sqrt(5.0f));
|
||||
|
||||
AddVertex(-1.0f, t, 0.0f);
|
||||
AddVertex(1.0f, t, 0.0f);
|
||||
AddVertex(-1.0f, -t, 0.0f);
|
||||
AddVertex(1.0f, -t, 0.0f);
|
||||
|
||||
AddVertex(0.0f, -1.0f, t);
|
||||
AddVertex(0.0f, 1.0f, t);
|
||||
AddVertex(0.0f, -1.0f, -t);
|
||||
AddVertex(0.0f, 1.0f, -t);
|
||||
|
||||
AddVertex(t, 0.0f, -1.0f);
|
||||
AddVertex(t, 0.0f, 1.0f);
|
||||
AddVertex(-t, 0.0f, -1.0f);
|
||||
AddVertex(-t, 0.0f, 1.0f);
|
||||
|
||||
for (u32 i = 0; i < vertices.Count(); ++i)
|
||||
{
|
||||
vertices[i].Normalize();
|
||||
}
|
||||
|
||||
assert(triangleIndices.Count() == 0);
|
||||
|
||||
AddTriangle(0, 11, 5);
|
||||
AddTriangle(0, 5, 1);
|
||||
AddTriangle(0, 1, 7);
|
||||
AddTriangle(0, 7, 10);
|
||||
AddTriangle(0, 10, 11);
|
||||
|
||||
AddTriangle(1, 5, 9);
|
||||
AddTriangle(5, 11, 4);
|
||||
AddTriangle(11, 10, 2);
|
||||
AddTriangle(10, 7, 6);
|
||||
AddTriangle(7, 1, 8);
|
||||
|
||||
AddTriangle(3, 9, 4);
|
||||
AddTriangle(3, 4, 2);
|
||||
AddTriangle(3, 2, 6);
|
||||
AddTriangle(3, 6, 8);
|
||||
AddTriangle(3, 8, 9);
|
||||
|
||||
AddTriangle(4, 9, 5);
|
||||
AddTriangle(2, 4, 11);
|
||||
AddTriangle(6, 2, 10);
|
||||
AddTriangle(8, 6, 7);
|
||||
AddTriangle(9, 8, 1);
|
||||
}
|
||||
|
||||
b3StackArray<b3Vec3, 256> vertices;
|
||||
b3StackArray<u32, 256> triangleIndices;
|
||||
};
|
||||
|
||||
// Generate an icosphere given the number of subdivisions.
|
||||
void smCreateMesh(smMesh& output, u32 subdivisions);
|
||||
|
||||
#endif SPHERE_MESH_H
|
@ -62,13 +62,11 @@
|
||||
#include <testbed/tests/multiple_pendulum.h>
|
||||
#include <testbed/tests/table_cloth.h>
|
||||
#include <testbed/tests/pinned_cloth.h>
|
||||
#include <testbed/tests/shirt.h>
|
||||
#include <testbed/tests/particle_types.h>
|
||||
#include <testbed/tests/tension_mapping.h>
|
||||
#include <testbed/tests/self_collision.h>
|
||||
#include <testbed/tests/single_pendulum.h>
|
||||
#include <testbed/tests/soft_body.h>
|
||||
#include <testbed/tests/rope_test.h>
|
||||
#include <testbed/tests/mass_spring.h>
|
||||
|
||||
TestEntry g_tests[] =
|
||||
{
|
||||
@ -116,12 +114,10 @@ TestEntry g_tests[] =
|
||||
{ "Multiple Pendulum", &MultiplePendulum::Create },
|
||||
{ "Table Cloth", &TableCloth::Create },
|
||||
{ "Pinned Cloth", &PinnedCloth::Create },
|
||||
{ "Shirt", &Shirt::Create },
|
||||
{ "Particle Types", &ParticleTypes::Create },
|
||||
{ "Tension Mapping", &TensionMapping::Create },
|
||||
{ "Self-Collision", &SelfCollision::Create },
|
||||
{ "Mass-Spring System", &MassSpring::Create },
|
||||
{ "Single Pendulum", &SinglePendulum::Create },
|
||||
{ "Soft Body", &SoftBody::Create },
|
||||
{ "Rope", &Rope::Create },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
@ -1,158 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2019 Irlan Robson https://irlanrobson.github.io
|
||||
*
|
||||
* 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 MASS_SPRING_H
|
||||
#define MASS_SPRING_H
|
||||
|
||||
class MassSpring : public Test
|
||||
{
|
||||
public:
|
||||
MassSpring()
|
||||
{
|
||||
m_x.Set(0.0f, 5.0f, 0.0f);
|
||||
|
||||
m_v.SetZero();
|
||||
|
||||
m_k = 100.0f;
|
||||
|
||||
m_iterations = 0;
|
||||
}
|
||||
|
||||
void Solve(float32 h)
|
||||
{
|
||||
// ODE
|
||||
// f(Y) = dY / dt = [v]
|
||||
// [-k * x]
|
||||
|
||||
// 1. Apply Implicit Euler
|
||||
|
||||
// Y(t + h) = Y(t) + h * f( Y(t + h) )
|
||||
// G( Y(t + h) ) = Y(t + h) - Y(t) - h * f( Y(t + h) ) = 0
|
||||
|
||||
// 2. Solve G = 0
|
||||
|
||||
// Newton-Raphson Iteration
|
||||
//
|
||||
// Y(t + h) =
|
||||
// Y(t + h)_0 - G( Y(t + h)_0 ) / G'( Y(t + h)_0 ) =
|
||||
// Y(t + h)_0 - G'( Y(t + h)_0 )^-1 * ( Y(t + h)_0 - Y(t) - h * f( Y(t + h)_0 )
|
||||
|
||||
// G'( Y ) = I - h * del_f / del_Y
|
||||
|
||||
// del_f / del_Y = [del_f1 / del_x del_f1 / del_v] = [0 I]
|
||||
// [del_f2 / del_x del_f2 / del_v] [-k * I 0]
|
||||
|
||||
// G'( Y ) = [I 0] - [0 h * I] = [I -h * I]
|
||||
// [0 I] [-h * k * I 0] [h * k * I I]
|
||||
|
||||
// Compute Jacobian
|
||||
b3Mat33 I = b3Mat33_identity;
|
||||
|
||||
b3Mat33 A, B, C, D;
|
||||
|
||||
A = I;
|
||||
B = -h * I;
|
||||
C = h * m_k * I;
|
||||
D = I;
|
||||
|
||||
// Invert
|
||||
// Block matrix inversion
|
||||
b3Mat33 invD = b3Inverse(D);
|
||||
b3Mat33 B_invD = B * invD;
|
||||
|
||||
b3Mat33 invJ_A = b3Inverse(A - B_invD * C);
|
||||
b3Mat33 invJ_B = -invJ_A * B_invD;
|
||||
b3Mat33 invJ_C = -invD * C * invJ_A;
|
||||
b3Mat33 invJ_D = invD + invD * C * invJ_A * B_invD;
|
||||
|
||||
// Initial guess
|
||||
b3Vec3 f1 = m_v;
|
||||
b3Vec3 f2 = -m_k * m_x;
|
||||
|
||||
b3Vec3 Y1 = m_x + h * f1;
|
||||
b3Vec3 Y2 = m_v + h * f2;
|
||||
|
||||
const float32 kTol = 0.05f;
|
||||
|
||||
const u32 kMaxIterations = 20;
|
||||
|
||||
float32 eps0 = 0.0f;
|
||||
|
||||
float32 eps1 = B3_MAX_FLOAT;
|
||||
|
||||
m_iterations = 0;
|
||||
|
||||
while (m_iterations < kMaxIterations && eps1 > kTol * kTol * eps0)
|
||||
{
|
||||
// Evaluate f(Y_n-1)
|
||||
f1 = Y2;
|
||||
f2 = -m_k * Y1;
|
||||
|
||||
// Residual vector
|
||||
b3Vec3 G1 = Y1 - m_x - h * f1;
|
||||
b3Vec3 G2 = Y2 - m_v - h * f2;
|
||||
|
||||
eps1 = b3Dot(G1, G1) + b3Dot(G2, G2);
|
||||
|
||||
// Solve Ax = b
|
||||
b3Vec3 x1 = invJ_A * G1 + invJ_B * G2;
|
||||
b3Vec3 x2 = invJ_C * G1 + invJ_D * G2;
|
||||
|
||||
Y1 -= x1;
|
||||
Y2 -= x2;
|
||||
|
||||
++m_iterations;
|
||||
}
|
||||
|
||||
// Update state
|
||||
m_x = Y1;
|
||||
m_v = Y2;
|
||||
}
|
||||
|
||||
void Step()
|
||||
{
|
||||
float32 h = g_testSettings->inv_hertz;
|
||||
|
||||
Solve(h);
|
||||
|
||||
g_draw->DrawSolidSphere(m_x, 0.25f, b3Color_white);
|
||||
|
||||
g_draw->DrawSegment(b3Vec3_zero, m_x, b3Color_white);
|
||||
|
||||
g_draw->DrawString(b3Color_white, "Iterations = %u", m_iterations);
|
||||
|
||||
float32 E = 0.5f * b3Dot(m_v, m_v);
|
||||
g_draw->DrawString(b3Color_white, "E = %f", E);
|
||||
}
|
||||
|
||||
static Test* Create()
|
||||
{
|
||||
return new MassSpring();
|
||||
}
|
||||
|
||||
// State
|
||||
b3Vec3 m_x, m_v;
|
||||
|
||||
// Stiffness
|
||||
float32 m_k;
|
||||
|
||||
//
|
||||
u32 m_iterations;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,142 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2019 Irlan Robson https://irlanrobson.github.io
|
||||
*
|
||||
* 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 SHIRT_H
|
||||
#define SHIRT_H
|
||||
|
||||
class Shirt : public Test
|
||||
{
|
||||
public:
|
||||
Shirt()
|
||||
{
|
||||
// Generate 2D mesh
|
||||
m_shirtGarmentMesh.Set(&m_shirtGarment, 0.1f);
|
||||
|
||||
// Create 3D mesh
|
||||
m_shirtClothMesh.Set(&m_shirtGarmentMesh);
|
||||
|
||||
// Create cloth
|
||||
b3ClothDef def;
|
||||
def.mesh = &m_shirtClothMesh;
|
||||
def.density = 0.2f;
|
||||
def.structural = 1000.0f;
|
||||
|
||||
m_cloth = new b3Cloth(def);
|
||||
|
||||
// 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;
|
||||
b3Particle* p = m_cloth->GetVertexParticle(v);
|
||||
|
||||
b3Vec3 x = p->GetPosition();
|
||||
x.z = -1.0f;
|
||||
|
||||
p->SetPosition(x);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
b3Particle* p = m_cloth->GetVertexParticle(v);
|
||||
|
||||
b3Vec3 x = p->GetPosition();
|
||||
x.z = 1.0f;
|
||||
|
||||
p->SetPosition(x);
|
||||
}
|
||||
}
|
||||
|
||||
m_cloth->SetGravity(b3Vec3(0.0f, -9.8f, 0.0f));
|
||||
m_cloth->SetWorld(&m_world);
|
||||
|
||||
m_clothDragger = new b3ClothDragger(&m_ray, m_cloth);
|
||||
}
|
||||
|
||||
~Shirt()
|
||||
{
|
||||
delete m_clothDragger;
|
||||
delete m_cloth;
|
||||
}
|
||||
|
||||
void Step()
|
||||
{
|
||||
Test::Step();
|
||||
|
||||
m_cloth->Step(g_testSettings->inv_hertz);
|
||||
|
||||
m_cloth->Draw();
|
||||
|
||||
extern u32 b3_clothSolverIterations;
|
||||
g_draw->DrawString(b3Color_white, "Iterations = %d", b3_clothSolverIterations);
|
||||
|
||||
float32 E = m_cloth->GetEnergy();
|
||||
g_draw->DrawString(b3Color_white, "E = %f", E);
|
||||
}
|
||||
|
||||
void MouseMove(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseMove(pw);
|
||||
|
||||
if (m_clothDragger->IsDragging() == true)
|
||||
{
|
||||
m_clothDragger->Drag();
|
||||
}
|
||||
}
|
||||
|
||||
void MouseLeftDown(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftDown(pw);
|
||||
|
||||
if (m_clothDragger->IsDragging() == false)
|
||||
{
|
||||
m_clothDragger->StartDragging();
|
||||
}
|
||||
}
|
||||
|
||||
void MouseLeftUp(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftUp(pw);
|
||||
|
||||
if (m_clothDragger->IsDragging() == true)
|
||||
{
|
||||
m_clothDragger->StopDragging();
|
||||
}
|
||||
}
|
||||
|
||||
static Test* Create()
|
||||
{
|
||||
return new Shirt();
|
||||
}
|
||||
|
||||
b3ShirtGarment m_shirtGarment;
|
||||
b3GarmentMesh m_shirtGarmentMesh;
|
||||
b3GarmentClothMesh m_shirtClothMesh;
|
||||
|
||||
b3Cloth* m_cloth;
|
||||
b3ClothDragger* m_clothDragger;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2019 Irlan Robson https://irlanrobson.github.io
|
||||
*
|
||||
* 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 PENDULUM_H
|
||||
#define PENDULUM_H
|
||||
|
||||
class SinglePendulum : public Test
|
||||
{
|
||||
public:
|
||||
SinglePendulum()
|
||||
{
|
||||
m_g = -10.0f;
|
||||
|
||||
m_r = 10.0f;
|
||||
m_m = 1.0f;
|
||||
m_I = m_m * m_r * m_r;
|
||||
|
||||
// Initial state
|
||||
m_theta = -0.5f * B3_PI;
|
||||
m_omega = 0.0f;
|
||||
}
|
||||
|
||||
void Step()
|
||||
{
|
||||
float32 h = g_testSettings->inv_hertz;
|
||||
|
||||
// Solution (acceleration)
|
||||
float32 omega_dot = -m_g / m_r * sin(m_theta);
|
||||
|
||||
// Integrate acceleration
|
||||
m_omega += h * omega_dot;
|
||||
|
||||
// Integrate velocity
|
||||
m_theta += h * m_omega;
|
||||
|
||||
// Convert from polar coordinates (r, theta) to Cartesian coordinates (x, y)
|
||||
b3Vec3 c;
|
||||
c.x = m_r * sin(m_theta);
|
||||
c.y = m_r * cos(m_theta);
|
||||
c.z = 0.0f;
|
||||
g_draw->DrawSolidSphere(c, 1.0f, b3Color_white);
|
||||
|
||||
b3Vec3 pole;
|
||||
pole.SetZero();
|
||||
g_draw->DrawSegment(pole, c, b3Color_white);
|
||||
|
||||
// Kinetic energy
|
||||
float32 T = 0.5f * m_I * m_omega * m_omega;
|
||||
|
||||
// Potential energy
|
||||
float32 V = -m_m * m_g * m_r * cos(m_theta);
|
||||
|
||||
// Lagrangian
|
||||
float32 L = T - V;
|
||||
|
||||
//
|
||||
g_draw->DrawString(b3Color_white, "T = %f \nV = %f \nL = %f", T, V, L);
|
||||
}
|
||||
|
||||
static Test* Create()
|
||||
{
|
||||
return new SinglePendulum();
|
||||
}
|
||||
|
||||
// Gravity
|
||||
float32 m_g;
|
||||
|
||||
// Mass, inertia
|
||||
float32 m_m, m_I;
|
||||
|
||||
// Radial coordinate
|
||||
float32 m_r;
|
||||
|
||||
// The allowable generalized coordinate in polar coordinate frame.
|
||||
// Only motions satisfying the constraints can be described
|
||||
// in this frame. Therefore, all solutions satisfy the constraints.
|
||||
// This is the so called reduced coordinates approach.
|
||||
float32 m_theta;
|
||||
|
||||
// Velocity
|
||||
float32 m_omega;
|
||||
};
|
||||
|
||||
#endif
|
278
examples/testbed/tests/soft_body.h
Normal file
278
examples/testbed/tests/soft_body.h
Normal file
@ -0,0 +1,278 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2019 Irlan Robson https://irlanrobson.github.io
|
||||
*
|
||||
* 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 SOFT_BODY_H
|
||||
#define SOFT_BODY_H
|
||||
|
||||
#include <testbed/framework/sphere_mesh.h>
|
||||
|
||||
struct b3SphereClothMesh : public b3ClothMesh
|
||||
{
|
||||
b3StackArray<b3Vec3, 256> sphereVertices;
|
||||
b3StackArray<b3ClothMeshTriangle, 256> sphereTriangles;
|
||||
b3ClothMeshMesh sphereMesh;
|
||||
|
||||
b3SphereClothMesh()
|
||||
{
|
||||
smMesh mesh;
|
||||
smCreateMesh(mesh, 1);
|
||||
|
||||
sphereVertices.Resize(mesh.vertices.Count());
|
||||
for (u32 i = 0; i < mesh.vertices.Count(); ++i)
|
||||
{
|
||||
sphereVertices[i] = mesh.vertices[i];
|
||||
}
|
||||
|
||||
sphereTriangles.Resize(mesh.triangleIndices.Count() / 3);
|
||||
for (u32 i = 0; i < mesh.triangleIndices.Count() / 3; ++i)
|
||||
{
|
||||
sphereTriangles[i].v1 = mesh.triangleIndices[3 * i + 0];
|
||||
sphereTriangles[i].v2 = mesh.triangleIndices[3 * i + 1];
|
||||
sphereTriangles[i].v3 = mesh.triangleIndices[3 * i + 2];
|
||||
}
|
||||
|
||||
sphereMesh.startTriangle = 0;
|
||||
sphereMesh.triangleCount = sphereTriangles.Count();
|
||||
sphereMesh.startVertex = 0;
|
||||
sphereMesh.vertexCount = sphereVertices.Count();
|
||||
|
||||
vertexCount = sphereVertices.Count();
|
||||
vertices = sphereVertices.Begin();
|
||||
triangleCount = sphereTriangles.Count();
|
||||
triangles = sphereTriangles.Begin();
|
||||
meshCount = 1;
|
||||
meshes = &sphereMesh;
|
||||
sewingLineCount = 0;
|
||||
sewingLines = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
class SoftBody : public Test
|
||||
{
|
||||
public:
|
||||
SoftBody()
|
||||
{
|
||||
// Scale and translate the cloth mesh upwards
|
||||
for (u32 i = 0; i < m_mesh.vertexCount; ++i)
|
||||
{
|
||||
m_mesh.vertices[i] *= 2.0f;
|
||||
m_mesh.vertices[i].y += 10.0f;
|
||||
}
|
||||
|
||||
// Create cloth
|
||||
b3ClothDef def;
|
||||
def.mesh = &m_mesh;
|
||||
def.density = 0.2f;
|
||||
def.structural = 10000.0f;
|
||||
def.bending = 0.0f;
|
||||
def.damping = 0.0f;
|
||||
|
||||
m_cloth = new b3Cloth(def);
|
||||
|
||||
for (b3Particle* p = m_cloth->GetParticleList().m_head; p; p = p->GetNext())
|
||||
{
|
||||
p->SetRadius(0.05f);
|
||||
p->SetFriction(0.5f);
|
||||
}
|
||||
|
||||
b3Vec3 gravity(0.0f, -9.8f, 0.0f);
|
||||
|
||||
m_cloth->SetGravity(gravity);
|
||||
|
||||
// Attach a world to the cloth
|
||||
m_cloth->SetWorld(&m_world);
|
||||
|
||||
// Create static shapes
|
||||
{
|
||||
b3BodyDef bd;
|
||||
bd.type = e_staticBody;
|
||||
|
||||
b3Body* b = m_world.CreateBody(bd);
|
||||
|
||||
static b3BoxHull boxHull(10.0f, 1.0f, 10.0f);
|
||||
|
||||
b3HullShape boxShape;
|
||||
boxShape.m_hull = &boxHull;
|
||||
|
||||
b3ShapeDef sd;
|
||||
sd.shape = &boxShape;
|
||||
sd.friction = 0.5f;
|
||||
|
||||
b->CreateShape(sd);
|
||||
}
|
||||
|
||||
m_clothDragger = new b3ClothDragger(&m_ray, m_cloth);
|
||||
}
|
||||
|
||||
~SoftBody()
|
||||
{
|
||||
delete m_clothDragger;
|
||||
delete m_cloth;
|
||||
}
|
||||
|
||||
// Compute the volume of soft body
|
||||
float32 ComputeVolume() const
|
||||
{
|
||||
const b3ClothMesh* mesh = m_cloth->GetMesh();
|
||||
|
||||
b3StackArray<b3Vec3, 256> positions;
|
||||
positions.Resize(mesh->vertexCount);
|
||||
|
||||
for (u32 i = 0; i < mesh->vertexCount; ++i)
|
||||
{
|
||||
b3Particle* p = m_cloth->GetVertexParticle(i);
|
||||
positions[i] = p->GetPosition();
|
||||
}
|
||||
|
||||
b3AABB3 aabb;
|
||||
aabb.Compute(positions.Begin(), positions.Count());
|
||||
|
||||
return aabb.Volume();
|
||||
}
|
||||
|
||||
// Apply pressure forces
|
||||
// Explanation available in the paper
|
||||
// "Pressure Model of Soft Body Simulation"
|
||||
void ApplyPressureForces()
|
||||
{
|
||||
const b3ClothMesh* mesh = m_cloth->GetMesh();
|
||||
|
||||
// Volume in m^3
|
||||
float32 V = ComputeVolume();
|
||||
|
||||
// Inverse volume
|
||||
float32 invV = V > 0.0f ? 1.0f / V : 0.0f;
|
||||
|
||||
// Apply pressure forces on particles
|
||||
for (u32 i = 0; i < m_mesh.triangleCount; ++i)
|
||||
{
|
||||
u32 i1 = m_mesh.triangles[i].v1;
|
||||
u32 i2 = m_mesh.triangles[i].v2;
|
||||
u32 i3 = m_mesh.triangles[i].v3;
|
||||
|
||||
b3Particle* p1 = m_cloth->GetVertexParticle(i1);
|
||||
b3Particle* p2 = m_cloth->GetVertexParticle(i2);
|
||||
b3Particle* p3 = m_cloth->GetVertexParticle(i3);
|
||||
|
||||
b3Vec3 v1 = p1->GetPosition();
|
||||
b3Vec3 v2 = p2->GetPosition();
|
||||
b3Vec3 v3 = p3->GetPosition();
|
||||
|
||||
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
|
||||
|
||||
// Triangle area
|
||||
float32 A = n.Normalize();
|
||||
A *= 0.5f;
|
||||
|
||||
// Ideal Gas Approximation
|
||||
|
||||
// Number of gas moles
|
||||
const float32 k_n = 1.0f;
|
||||
|
||||
// Ideal Gas Constant [J / K mol]
|
||||
const float32 k_R = 8.31f;
|
||||
|
||||
// Gas temperature in Kelvin
|
||||
const float32 k_T = 100.0f;
|
||||
|
||||
// Pressure in Pascals
|
||||
float32 P = invV * k_n * k_R * k_T;
|
||||
|
||||
// Pressure vector
|
||||
b3Vec3 Pn = P * n;
|
||||
|
||||
// Pressure force
|
||||
b3Vec3 FP = Pn * A;
|
||||
|
||||
// Distribute
|
||||
p1->ApplyForce(FP);
|
||||
p2->ApplyForce(FP);
|
||||
p3->ApplyForce(FP);
|
||||
}
|
||||
}
|
||||
|
||||
void Step()
|
||||
{
|
||||
Test::Step();
|
||||
|
||||
ApplyPressureForces();
|
||||
|
||||
m_cloth->Step(g_testSettings->inv_hertz);
|
||||
|
||||
m_cloth->Draw();
|
||||
|
||||
if (m_clothDragger->IsDragging())
|
||||
{
|
||||
b3Vec3 pA = m_clothDragger->GetPointA();
|
||||
b3Vec3 pB = m_clothDragger->GetPointB();
|
||||
|
||||
g_draw->DrawPoint(pA, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawPoint(pB, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawSegment(pA, pB, b3Color_white);
|
||||
}
|
||||
|
||||
extern u32 b3_clothSolverIterations;
|
||||
g_draw->DrawString(b3Color_white, "Iterations = %d", b3_clothSolverIterations);
|
||||
|
||||
float32 E = m_cloth->GetEnergy();
|
||||
g_draw->DrawString(b3Color_white, "E = %f", E);
|
||||
}
|
||||
|
||||
void MouseMove(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseMove(pw);
|
||||
|
||||
if (m_clothDragger->IsDragging() == true)
|
||||
{
|
||||
m_clothDragger->Drag();
|
||||
}
|
||||
}
|
||||
|
||||
void MouseLeftDown(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftDown(pw);
|
||||
|
||||
if (m_clothDragger->IsDragging() == false)
|
||||
{
|
||||
m_clothDragger->StartDragging();
|
||||
}
|
||||
}
|
||||
|
||||
void MouseLeftUp(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftUp(pw);
|
||||
|
||||
if (m_clothDragger->IsDragging() == true)
|
||||
{
|
||||
m_clothDragger->StopDragging();
|
||||
}
|
||||
}
|
||||
|
||||
static Test* Create()
|
||||
{
|
||||
return new SoftBody();
|
||||
}
|
||||
|
||||
b3SphereClothMesh m_mesh;
|
||||
b3Cloth* m_cloth;
|
||||
b3ClothDragger* m_clothDragger;
|
||||
};
|
||||
|
||||
#endif
|
@ -273,6 +273,10 @@ workspace(solution_name)
|
||||
|
||||
examples_src_dir .. "/testbed/framework/test.h",
|
||||
|
||||
examples_src_dir .. "/testbed/framework/sphere_mesh.h",
|
||||
|
||||
examples_src_dir .. "/testbed/framework/sphere_mesh.cpp",
|
||||
|
||||
examples_src_dir .. "/testbed/framework/body_dragger.h",
|
||||
examples_src_dir .. "/testbed/framework/cloth_dragger.h",
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user