Mesh generation to everybody
Add cylinder mesh generation Mesh generation can be used by everyone Mesh generation also gives vertex normals for convenience
This commit is contained in:
@@ -19,6 +19,9 @@
|
||||
#include <bounce/collision/shapes/qhull.h>
|
||||
#include <bounce/quickhull/qh_hull.h>
|
||||
|
||||
#include <bounce/meshgen/sphere_mesh.h>
|
||||
#include <bounce/meshgen/cylinder_mesh.h>
|
||||
|
||||
template <class T>
|
||||
struct b3UniqueStackArray
|
||||
{
|
||||
@@ -348,35 +351,17 @@ void b3QHull::Set(u32 vtxStride, const void* vtxBase, u32 vtxCount, bool simplif
|
||||
|
||||
void b3QHull::SetAsSphere(float32 radius)
|
||||
{
|
||||
enum
|
||||
B3_ASSERT(radius > 0.0f);
|
||||
|
||||
smMesh mesh;
|
||||
smCreateMesh(mesh, 2);
|
||||
|
||||
for (u32 i = 0; i < mesh.vertexCount; ++i)
|
||||
{
|
||||
e_rings = 8,
|
||||
e_sectors = 8,
|
||||
e_vertexCount = e_rings * e_sectors
|
||||
};
|
||||
|
||||
float32 R = 1.0f / float32(e_rings - 1);
|
||||
float32 S = 1.0f / float32(e_sectors - 1);
|
||||
|
||||
b3Vec3 vs[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);
|
||||
vs[vc] *= radius;
|
||||
++vc;
|
||||
}
|
||||
mesh.vertices[i] *= radius;
|
||||
}
|
||||
|
||||
// Set
|
||||
Set(sizeof(b3Vec3), vs, e_vertexCount, false);
|
||||
Set(sizeof(b3Vec3), mesh.vertices, mesh.vertexCount, false);
|
||||
}
|
||||
|
||||
void b3QHull::SetAsCylinder(float32 radius, float32 ey)
|
||||
@@ -384,51 +369,19 @@ void b3QHull::SetAsCylinder(float32 radius, float32 ey)
|
||||
B3_ASSERT(radius > 0.0f);
|
||||
B3_ASSERT(ey > 0.0f);
|
||||
|
||||
const u32 kEdgeCount = 20;
|
||||
const u32 kVertexCount = 4 * kEdgeCount;
|
||||
b3Vec3 vs[kVertexCount];
|
||||
float32 height = 2.0f * ey;
|
||||
|
||||
u32 count = 0;
|
||||
|
||||
float32 kAngleInc = 2.0f * B3_PI / float32(kEdgeCount);
|
||||
b3Quat q = b3QuatRotationY(kAngleInc);
|
||||
cymMesh mesh;
|
||||
cymCreateMesh(mesh, 20);
|
||||
|
||||
for (u32 i = 0; i < mesh.vertexCount; ++i)
|
||||
{
|
||||
b3Vec3 center(0.0f, -ey, 0.0f);
|
||||
b3Vec3 n1(1.0f, 0.0f, 0.0f);
|
||||
b3Vec3 v1 = center + radius * n1;
|
||||
for (u32 i = 0; i < kEdgeCount; ++i)
|
||||
{
|
||||
b3Vec3 n2 = b3Mul(q, n1);
|
||||
b3Vec3 v2 = center + radius * n2;
|
||||
|
||||
vs[count++] = v1;
|
||||
vs[count++] = v2;
|
||||
|
||||
n1 = n2;
|
||||
v1 = v2;
|
||||
}
|
||||
mesh.vertices[i].x *= radius;
|
||||
mesh.vertices[i].y *= height;
|
||||
mesh.vertices[i].z *= radius;
|
||||
}
|
||||
|
||||
{
|
||||
b3Vec3 center(0.0f, ey, 0.0f);
|
||||
b3Vec3 n1(1.0f, 0.0f, 0.0f);
|
||||
b3Vec3 v1 = center + radius * n1;
|
||||
for (u32 i = 0; i < kEdgeCount; ++i)
|
||||
{
|
||||
b3Vec3 n2 = b3Mul(q, n1);
|
||||
b3Vec3 v2 = center + radius * n2;
|
||||
|
||||
vs[count++] = v1;
|
||||
vs[count++] = v2;
|
||||
|
||||
n1 = n2;
|
||||
v1 = v2;
|
||||
}
|
||||
}
|
||||
|
||||
// Set
|
||||
Set(sizeof(b3Vec3), vs, count, false);
|
||||
Set(sizeof(b3Vec3), mesh.vertices, mesh.vertexCount, false);
|
||||
}
|
||||
|
||||
void b3QHull::SetAsCone(float32 radius, float32 ey)
|
||||
|
129
src/bounce/meshgen/cylinder_mesh.cpp
Normal file
129
src/bounce/meshgen/cylinder_mesh.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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 <bounce/meshgen/cylinder_mesh.h>
|
||||
#include <bounce/common/math/quat.h>
|
||||
|
||||
void cymCreateMesh(cymMesh& output, u32 segments)
|
||||
{
|
||||
B3_ASSERT(segments > 2);
|
||||
|
||||
u32 vertexCount = 2 * segments;
|
||||
b3Vec3* vertices = (b3Vec3*)b3Alloc(vertexCount * sizeof(b3Vec3));
|
||||
b3Vec3* normals = (b3Vec3*)b3Alloc(vertexCount * sizeof(b3Vec3));
|
||||
u32 indexCount = 3 * (2 * segments) + 2 * 3 * (segments - 2);
|
||||
u32* indices = (u32*)b3Alloc(indexCount * sizeof(u32));
|
||||
|
||||
float32 angle = 2.0f * B3_PI / float32(segments);
|
||||
b3Quat q(b3Vec3_y, angle);
|
||||
|
||||
// Lower
|
||||
b3Vec3 v(1.0f, -0.5f, 0.0f);
|
||||
for (u32 i = 0; i < segments; ++i)
|
||||
{
|
||||
vertices[i] = v;
|
||||
v = b3Mul(q, v);
|
||||
}
|
||||
|
||||
// Upper
|
||||
v.Set(1.0f, 0.5f, 0.0f);
|
||||
for (u32 i = 0; i < segments; ++i)
|
||||
{
|
||||
vertices[segments + i] = v;
|
||||
v = b3Mul(q, v);
|
||||
}
|
||||
|
||||
u32 idx = 0;
|
||||
|
||||
// Side triangles
|
||||
for (u32 i = 0; i < segments; ++i)
|
||||
{
|
||||
u32 i1 = i;
|
||||
u32 i2 = i1 + 1 < segments ? i1 + 1 : 0;
|
||||
|
||||
u32 i3 = segments + i;
|
||||
u32 i4 = i3 + 1 < 2 * segments ? i3 + 1 : segments;
|
||||
|
||||
indices[idx++] = i1;
|
||||
indices[idx++] = i2;
|
||||
indices[idx++] = i4;
|
||||
|
||||
indices[idx++] = i4;
|
||||
indices[idx++] = i3;
|
||||
indices[idx++] = i1;
|
||||
}
|
||||
|
||||
// Side normals
|
||||
for (u32 i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
normals[i].SetZero();
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < idx / 3; ++i)
|
||||
{
|
||||
u32 i1 = indices[3 * i + 0];
|
||||
u32 i2 = indices[3 * i + 1];
|
||||
u32 i3 = indices[3 * i + 2];
|
||||
|
||||
b3Vec3 v1 = vertices[i1];
|
||||
b3Vec3 v2 = vertices[i2];
|
||||
b3Vec3 v3 = vertices[i3];
|
||||
|
||||
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
|
||||
n.Normalize();
|
||||
|
||||
normals[i1] += n;
|
||||
normals[i2] += n;
|
||||
normals[i3] += n;
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
normals[i].Normalize();
|
||||
}
|
||||
|
||||
// Lower. Reverse loop to ensure CCW
|
||||
u32 i1 = segments - 1;
|
||||
for (u32 i2 = i1 - 1; i2 > 0; --i2)
|
||||
{
|
||||
u32 i3 = i2 - 1;
|
||||
|
||||
indices[idx++] = i1;
|
||||
indices[idx++] = i2;
|
||||
indices[idx++] = i3;
|
||||
}
|
||||
|
||||
// Upper
|
||||
i1 = segments;
|
||||
for (u32 i2 = i1 + 1; i2 < 2 * segments - 1; ++i2)
|
||||
{
|
||||
u32 i3 = i2 + 1;
|
||||
|
||||
indices[idx++] = i1;
|
||||
indices[idx++] = i2;
|
||||
indices[idx++] = i3;
|
||||
}
|
||||
|
||||
B3_ASSERT(idx == indexCount);
|
||||
|
||||
output.vertexCount = vertexCount;
|
||||
output.vertices = vertices;
|
||||
output.normals = normals;
|
||||
output.indexCount = indexCount;
|
||||
output.indices = indices;
|
||||
}
|
216
src/bounce/meshgen/sphere_mesh.cpp
Normal file
216
src/bounce/meshgen/sphere_mesh.cpp
Normal file
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* 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 <bounce/meshgen/sphere_mesh.h>
|
||||
|
||||
static inline void smAddVertex(smMesh& mesh, float32 x, float32 y, float32 z)
|
||||
{
|
||||
mesh.vertices[mesh.vertexCount++].Set(x, y, z);
|
||||
}
|
||||
|
||||
static inline void smAddTriangle(smMesh& mesh, u32 v1, u32 v2, u32 v3)
|
||||
{
|
||||
mesh.indices[mesh.indexCount++] = v1;
|
||||
mesh.indices[mesh.indexCount++] = v2;
|
||||
mesh.indices[mesh.indexCount++] = v3;
|
||||
}
|
||||
|
||||
static inline void smSetAsOctahedron(smMesh& mesh)
|
||||
{
|
||||
B3_ASSERT(mesh.vertexCount == 0);
|
||||
|
||||
smAddVertex(mesh, 0.0f, -1.0f, 0.0f);
|
||||
smAddVertex(mesh, 0.0f, 0.0f, 1.0f);
|
||||
smAddVertex(mesh, -1.0f, 0.0f, 0.0f);
|
||||
smAddVertex(mesh, 0.0f, 0.0f, -1.0f);
|
||||
smAddVertex(mesh, 1.0f, 0.0f, 0.0f);
|
||||
smAddVertex(mesh, 0.0f, 1.0f, 0.0f);
|
||||
|
||||
B3_ASSERT(mesh.indexCount == 0);
|
||||
|
||||
smAddTriangle(mesh, 0, 1, 2);
|
||||
smAddTriangle(mesh, 0, 2, 3);
|
||||
smAddTriangle(mesh, 0, 3, 4);
|
||||
smAddTriangle(mesh, 0, 4, 1);
|
||||
|
||||
smAddTriangle(mesh, 5, 2, 1);
|
||||
smAddTriangle(mesh, 5, 3, 2);
|
||||
smAddTriangle(mesh, 5, 4, 3);
|
||||
smAddTriangle(mesh, 5, 1, 4);
|
||||
}
|
||||
|
||||
struct smEdge
|
||||
{
|
||||
u32 v1, v2;
|
||||
};
|
||||
|
||||
struct smEdgeVertexPair
|
||||
{
|
||||
smEdge edge;
|
||||
u32 vertex;
|
||||
};
|
||||
|
||||
struct smEdgeVertexMap
|
||||
{
|
||||
u32 pairCount;
|
||||
smEdgeVertexPair* pairs;
|
||||
};
|
||||
|
||||
static inline void smAddPair(smEdgeVertexMap& map, const smEdgeVertexPair& pair)
|
||||
{
|
||||
map.pairs[map.pairCount++] = pair;
|
||||
}
|
||||
|
||||
static inline smEdgeVertexPair* smFind(smEdgeVertexMap& map, u32 v1, u32 v2)
|
||||
{
|
||||
for (u32 i = 0; i < map.pairCount; ++i)
|
||||
{
|
||||
smEdgeVertexPair* pair = map.pairs + i;
|
||||
|
||||
if (pair->edge.v1 == v1 && pair->edge.v2 == v2)
|
||||
{
|
||||
return pair;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static inline u32 smSubdivideEdge(smMesh& in_out, smEdgeVertexMap& map,
|
||||
u32 i1, u32 i2)
|
||||
{
|
||||
smEdgeVertexPair* pair = smFind(map, i2, i1);
|
||||
|
||||
if (pair)
|
||||
{
|
||||
return pair->vertex;
|
||||
}
|
||||
|
||||
smEdge newEdge;
|
||||
newEdge.v1 = i1;
|
||||
newEdge.v2 = i2;
|
||||
|
||||
u32 newVertex = in_out.vertexCount;
|
||||
|
||||
b3Vec3 v1 = in_out.vertices[i1];
|
||||
b3Vec3 v2 = in_out.vertices[i2];
|
||||
b3Vec3 v = 0.5f * (v1 + v2);
|
||||
v.Normalize();
|
||||
|
||||
smAddVertex(in_out, v.x, v.y, v.z);
|
||||
|
||||
smEdgeVertexPair newPair;
|
||||
newPair.edge = newEdge;
|
||||
newPair.vertex = newVertex;
|
||||
|
||||
smAddPair(map, newPair);
|
||||
|
||||
return newVertex;
|
||||
}
|
||||
|
||||
static void smSubdivideMesh(smMesh& in_out, smEdgeVertexMap& map)
|
||||
{
|
||||
map.pairCount = 0;
|
||||
|
||||
u32 inputIndexCount = in_out.indexCount;
|
||||
|
||||
for (u32 i = 0; i < inputIndexCount / 3; ++i)
|
||||
{
|
||||
u32 vi1 = in_out.indices[3 * i + 0];
|
||||
u32 vi2 = in_out.indices[3 * i + 1];
|
||||
u32 vi3 = in_out.indices[3 * i + 2];
|
||||
|
||||
u32 vi4 = smSubdivideEdge(in_out, map, vi1, vi2);
|
||||
u32 vi5 = smSubdivideEdge(in_out, map, vi2, vi3);
|
||||
u32 vi6 = smSubdivideEdge(in_out, map, vi3, vi1);
|
||||
|
||||
smAddTriangle(in_out, vi1, vi4, vi6);
|
||||
smAddTriangle(in_out, vi4, vi2, vi5);
|
||||
smAddTriangle(in_out, vi5, vi3, vi6);
|
||||
smAddTriangle(in_out, vi4, vi5, vi6);
|
||||
}
|
||||
}
|
||||
|
||||
// Compute the maximum number of vertices and
|
||||
// the number of triangle vertex indices in the intermediate mesh.
|
||||
// Also compute the maximum number of edge-vertex pairs per subdivision step
|
||||
static inline void smCount(u32& vertexCount, u32& indexCount, u32& edgeVertexPairCount, u32 subdivisions)
|
||||
{
|
||||
vertexCount = 6;
|
||||
indexCount = 3 * 8;
|
||||
for (u32 i = 0; i < subdivisions; ++i)
|
||||
{
|
||||
vertexCount += 3 * (indexCount / 3);
|
||||
indexCount += 4 * 3 * (indexCount / 3);
|
||||
}
|
||||
edgeVertexPairCount = 3 * (indexCount / 3);
|
||||
}
|
||||
|
||||
void smCreateMesh(smMesh& output, u32 subdivisions)
|
||||
{
|
||||
B3_ASSERT(output.vertexCount == 0);
|
||||
B3_ASSERT(output.indexCount == 0);
|
||||
|
||||
u32 vertexCount, indexCount, edgeVertexPairCount;
|
||||
smCount(vertexCount, indexCount, edgeVertexPairCount, subdivisions);
|
||||
|
||||
u32 byteCount = 0;
|
||||
byteCount += vertexCount * sizeof(b3Vec3);
|
||||
byteCount += indexCount * sizeof(u32);
|
||||
byteCount += edgeVertexPairCount * sizeof(smEdgeVertexPair);
|
||||
|
||||
u8* bytes = (u8*)b3Alloc(byteCount);
|
||||
|
||||
smMesh out;
|
||||
out.vertexCount = 0;
|
||||
out.vertices = (b3Vec3*)bytes;
|
||||
out.normals = nullptr;
|
||||
out.indexCount = 0;
|
||||
out.indices = (u32*) ((u8*)(out.vertices) + (vertexCount * sizeof(b3Vec3)));
|
||||
|
||||
smEdgeVertexMap map;
|
||||
map.pairCount = 0;
|
||||
map.pairs = (smEdgeVertexPair*) ((u8*)(out.indices) + (indexCount * sizeof(u32)));
|
||||
|
||||
smSetAsOctahedron(out);
|
||||
|
||||
for (u32 i = 0; i < subdivisions; ++i)
|
||||
{
|
||||
smSubdivideMesh(out, map);
|
||||
}
|
||||
|
||||
B3_ASSERT(out.vertexCount <= vertexCount);
|
||||
B3_ASSERT(out.indexCount == indexCount);
|
||||
B3_ASSERT(map.pairCount < edgeVertexPairCount);
|
||||
|
||||
output.vertexCount = out.vertexCount;
|
||||
output.vertices = (b3Vec3*)b3Alloc(out.vertexCount * sizeof(b3Vec3));
|
||||
memcpy(output.vertices, out.vertices, out.vertexCount * sizeof(b3Vec3));
|
||||
|
||||
output.normals = (b3Vec3*)b3Alloc(out.vertexCount * sizeof(b3Vec3));
|
||||
memcpy(output.normals, output.vertices, output.vertexCount * sizeof(b3Vec3));
|
||||
|
||||
output.indexCount = out.indexCount;
|
||||
output.indices = (u32*)b3Alloc(out.indexCount * sizeof(u32));
|
||||
memcpy(output.indices, out.indices, out.indexCount * sizeof(u32));
|
||||
|
||||
b3Free(bytes);
|
||||
|
||||
out.vertices = nullptr;
|
||||
out.normals = nullptr;
|
||||
out.indices = nullptr;
|
||||
}
|
Reference in New Issue
Block a user