Use sphere mesh. Add soft body test.
This commit is contained in:
@ -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 }
|
||||
};
|
||||
|
Reference in New Issue
Block a user