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:
Irlan
2019-04-21 16:06:51 -03:00
parent 77ad799d94
commit 490a2963df
9 changed files with 276 additions and 271 deletions

View File

@ -28,7 +28,8 @@
#include <bounce/common/math/mat44.h>
#include <bounce/common/draw.h>
#include <testbed/framework/sphere_mesh.h>
#include <bounce/meshgen/sphere_mesh.h>
#include <bounce/meshgen/cylinder_mesh.h>
#define BUFFER_OFFSET(i) ((char*)NULL + (i))
@ -666,7 +667,7 @@ struct DrawSolidSphere
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[1]);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.normals, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(u32), mesh.indices, GL_STATIC_DRAW);
@ -691,96 +692,25 @@ struct DrawSolidSphere
struct DrawSolidCylinder
{
enum
{
e_segments = 64,
e_vertexCount = e_segments * 6,
};
DrawSolidCylinder()
{
b3Vec3 vs[e_vertexCount];
b3Vec3 ns[e_vertexCount];
cymMesh mesh;
cymCreateMesh(mesh, 20);
u32 vc = 0;
for (u32 i = 0; i < e_segments; ++i)
{
float32 t0 = 2.0f * B3_PI * float32(i) / float32(e_segments);
float32 t1 = 2.0f * B3_PI * float32(i + 1) / float32(e_segments);
float32 c0 = cos(t0);
float32 s0 = sin(t0);
float32 c1 = cos(t1);
float32 s1 = sin(t1);
b3Vec3 v1;
v1.x = s0;
v1.y = -0.5f;
v1.z = c0;
b3Vec3 v2;
v2.x = s1;
v2.y = -0.5f;
v2.z = c1;
b3Vec3 v3;
v3.x = s1;
v3.y = 0.5f;
v3.z = c1;
b3Vec3 v4;
v4.x = s0;
v4.y = 0.5f;
v4.z = c0;
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
n.Normalize();
vs[vc] = v1;
ns[vc] = n;
++vc;
vs[vc] = v2;
ns[vc] = n;
++vc;
vs[vc] = v3;
ns[vc] = n;
++vc;
vs[vc] = v1;
ns[vc] = n;
++vc;
vs[vc] = v3;
ns[vc] = n;
++vc;
vs[vc] = v4;
ns[vc] = n;
++vc;
}
u32 is[e_vertexCount];
u32 ic = vc;
for (u32 i = 0; i < vc; ++i)
{
is[i] = i;
}
m_vertexCount = mesh.vertexCount;
m_indexCount = mesh.indexCount;
glGenBuffers(2, 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, 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.normals, 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.indices, GL_STATIC_DRAW);
AssertGL();
@ -796,6 +726,8 @@ struct DrawSolidCylinder
GLuint m_vboIds[2];
GLuint m_iboId;
u32 m_vertexCount;
u32 m_indexCount;
};
struct DrawSolid
@ -875,7 +807,7 @@ struct DrawSolid
glEnableVertexAttribArray(m_normalAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_cylinder.m_iboId);
glDrawElements(GL_TRIANGLES, m_cylinder.e_vertexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, m_cylinder.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDisableVertexAttribArray(m_normalAttribute);

View File

@ -28,7 +28,8 @@
#include <bounce/common/math/mat44.h>
#include <bounce/common/draw.h>
#include <testbed/framework/sphere_mesh.h>
#include <bounce/meshgen/sphere_mesh.h>
#include <bounce/meshgen/cylinder_mesh.h>
#define BUFFER_OFFSET(i) ((char*)NULL + (i))
@ -706,96 +707,25 @@ struct DrawSolidSphere
struct DrawSolidCylinder
{
enum
{
e_segments = 64,
e_vertexCount = e_segments * 6,
};
DrawSolidCylinder()
{
b3Vec3 vs[e_vertexCount];
b3Vec3 ns[e_vertexCount];
cymMesh mesh;
cymCreateMesh(mesh, 20);
u32 vc = 0;
for (u32 i = 0; i < e_segments; ++i)
{
float32 t0 = 2.0f * B3_PI * float32(i) / float32(e_segments);
float32 t1 = 2.0f * B3_PI * float32(i + 1) / float32(e_segments);
float32 c0 = cos(t0);
float32 s0 = sin(t0);
float32 c1 = cos(t1);
float32 s1 = sin(t1);
b3Vec3 v1;
v1.x = s0;
v1.y = -0.5f;
v1.z = c0;
b3Vec3 v2;
v2.x = s1;
v2.y = -0.5f;
v2.z = c1;
b3Vec3 v3;
v3.x = s1;
v3.y = 0.5f;
v3.z = c1;
b3Vec3 v4;
v4.x = s0;
v4.y = 0.5f;
v4.z = c0;
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
n.Normalize();
vs[vc] = v1;
ns[vc] = n;
++vc;
vs[vc] = v2;
ns[vc] = n;
++vc;
vs[vc] = v3;
ns[vc] = n;
++vc;
vs[vc] = v1;
ns[vc] = n;
++vc;
vs[vc] = v3;
ns[vc] = n;
++vc;
vs[vc] = v4;
ns[vc] = n;
++vc;
}
u32 is[e_vertexCount];
u32 ic = vc;
for (u32 i = 0; i < vc; ++i)
{
is[i] = i;
}
m_vertexCount = mesh.vertexCount;
m_indexCount = mesh.indexCount;
glGenBuffers(2, 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, 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.normals, 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.indices, GL_STATIC_DRAW);
AssertGL();
@ -811,6 +741,8 @@ struct DrawSolidCylinder
GLuint m_vboIds[2];
GLuint m_iboId;
u32 m_vertexCount;
u32 m_indexCount;
};
struct DrawSolid
@ -891,7 +823,7 @@ struct DrawSolid
glEnableVertexAttribArray(m_normalAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_cylinder.m_iboId);
glDrawElements(GL_TRIANGLES, m_cylinder.e_vertexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, m_cylinder.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

View File

@ -1,212 +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.
*/
#include <testbed/framework/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)
{
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);
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)
{
assert(output.vertexCount == 0);
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*)malloc(byteCount);
smMesh out;
out.vertexCount = 0;
out.vertices = (b3Vec3*)bytes;
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);
}
assert(map.pairCount < edgeVertexPairCount);
assert(out.vertexCount <= vertexCount);
assert(out.indexCount == indexCount);
output.vertexCount = out.vertexCount;
output.vertices = (b3Vec3*)malloc(out.vertexCount * sizeof(b3Vec3));
memcpy(output.vertices, out.vertices, out.vertexCount * sizeof(b3Vec3));
output.indexCount = out.indexCount;
output.indices = (u32*)malloc(out.indexCount * sizeof(u32));
memcpy(output.indices, out.indices, out.indexCount * sizeof(u32));
free(bytes);
out.vertices = nullptr;
out.indices = nullptr;
}

View File

@ -1,51 +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 SPHERE_MESH_H
#define SPHERE_MESH_H
#include <bounce/common/math/vec3.h>
// This structure represents a triangle mesh.
struct smMesh
{
smMesh()
{
vertexCount = 0;
vertices = nullptr;
indexCount = 0;
indices = nullptr;
}
~smMesh()
{
free(vertices);
free(indices);
}
u32 vertexCount; // number of unique vertices
b3Vec3* vertices; // list of unique vertices
u32 indexCount; // number of triangle vertex indices
u32* indices; // list of triangle vertex index
};
// Create a unit icosphere given the number of subdivisions.
// If the number of subdivisions to perform is zero then the output mesh is an octahedron.
void smCreateMesh(smMesh& output, u32 subdivisions);
#endif

View File

@ -19,44 +19,46 @@
#ifndef SOFT_BODY_H
#define SOFT_BODY_H
#include <testbed/framework/sphere_mesh.h>
#include <bounce/meshgen/sphere_mesh.h>
struct b3SphereClothMesh : public b3ClothMesh
struct b3QClothMesh : public b3ClothMesh
{
b3StackArray<b3Vec3, 256> sphereVertices;
b3StackArray<b3ClothMeshTriangle, 256> sphereTriangles;
b3ClothMeshMesh sphereMesh;
b3ClothMeshMesh qMesh;
b3SphereClothMesh()
~b3QClothMesh()
{
free(vertices);
free(triangles);
}
b3QClothMesh()
{
smMesh mesh;
smCreateMesh(mesh, 2);
sphereVertices.Resize(mesh.vertexCount);
vertexCount = mesh.vertexCount;
vertices = (b3Vec3*) malloc(vertexCount * sizeof(b3Vec3));
for (u32 i = 0; i < mesh.vertexCount; ++i)
{
sphereVertices[i] = mesh.vertices[i];
vertices[i] = mesh.vertices[i];
}
sphereTriangles.Resize(mesh.indexCount / 3);
for (u32 i = 0; i < mesh.indexCount / 3; ++i)
triangleCount = mesh.indexCount / 3;
triangles = (b3ClothMeshTriangle*) malloc(triangleCount * sizeof(b3ClothMeshTriangle));
for (u32 i = 0; i < triangleCount; ++i)
{
sphereTriangles[i].v1 = mesh.indices[3 * i + 0];
sphereTriangles[i].v2 = mesh.indices[3 * i + 1];
sphereTriangles[i].v3 = mesh.indices[3 * i + 2];
triangles[i].v1 = mesh.indices[3 * i + 0];
triangles[i].v2 = mesh.indices[3 * i + 1];
triangles[i].v3 = mesh.indices[3 * i + 2];
}
sphereMesh.startTriangle = 0;
sphereMesh.triangleCount = sphereTriangles.Count();
sphereMesh.startVertex = 0;
sphereMesh.vertexCount = sphereVertices.Count();
qMesh.startTriangle = 0;
qMesh.triangleCount = triangleCount;
qMesh.startVertex = 0;
qMesh.vertexCount = vertexCount;
vertexCount = sphereVertices.Count();
vertices = sphereVertices.Begin();
triangleCount = sphereTriangles.Count();
triangles = sphereTriangles.Begin();
meshCount = 1;
meshes = &sphereMesh;
meshes = &qMesh;
sewingLineCount = 0;
sewingLines = nullptr;
}
@ -270,7 +272,7 @@ public:
return new SoftBody();
}
b3SphereClothMesh m_mesh;
b3QClothMesh m_mesh;
b3Cloth* m_cloth;
b3ClothDragger* m_clothDragger;
};