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:
parent
77ad799d94
commit
490a2963df
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
};
|
||||
|
54
include/bounce/meshgen/cylinder_mesh.h
Normal file
54
include/bounce/meshgen/cylinder_mesh.h
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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 CYM_MESH_H
|
||||
#define CYM_MESH_H
|
||||
|
||||
#include <bounce/common/math/vec3.h>
|
||||
|
||||
// This structure represents a triangle mesh.
|
||||
struct cymMesh
|
||||
{
|
||||
cymMesh()
|
||||
{
|
||||
vertexCount = 0;
|
||||
vertices = nullptr;
|
||||
normals = nullptr;
|
||||
indexCount = 0;
|
||||
indices = nullptr;
|
||||
}
|
||||
|
||||
~cymMesh()
|
||||
{
|
||||
b3Free(vertices);
|
||||
b3Free(normals);
|
||||
b3Free(indices);
|
||||
}
|
||||
|
||||
u32 vertexCount; // number of unique vertices
|
||||
b3Vec3* vertices; // list of unique vertices
|
||||
b3Vec3* normals; // list of vertex normals
|
||||
u32 indexCount; // number of triangle vertex indices
|
||||
u32* indices; // list of triangle vertex index
|
||||
};
|
||||
|
||||
// Create a unit cylinder along the y-axis given the number of segments.
|
||||
// The number of segments must be greater than 2.
|
||||
void cymCreateMesh(cymMesh& output, u32 segments);
|
||||
|
||||
#endif
|
@ -16,8 +16,8 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SPHERE_MESH_H
|
||||
#define SPHERE_MESH_H
|
||||
#ifndef SM_MESH_H
|
||||
#define SM_MESH_H
|
||||
|
||||
#include <bounce/common/math/vec3.h>
|
||||
|
||||
@ -28,18 +28,21 @@ struct smMesh
|
||||
{
|
||||
vertexCount = 0;
|
||||
vertices = nullptr;
|
||||
normals = nullptr;
|
||||
indexCount = 0;
|
||||
indices = nullptr;
|
||||
}
|
||||
|
||||
~smMesh()
|
||||
{
|
||||
free(vertices);
|
||||
free(indices);
|
||||
b3Free(vertices);
|
||||
b3Free(normals);
|
||||
b3Free(indices);
|
||||
}
|
||||
|
||||
u32 vertexCount; // number of unique vertices
|
||||
b3Vec3* vertices; // list of unique vertices
|
||||
b3Vec3* normals; // list of vertex normals
|
||||
u32 indexCount; // number of triangle vertex indices
|
||||
u32* indices; // list of triangle vertex index
|
||||
};
|
@ -273,10 +273,6 @@ 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",
|
||||
|
||||
|
@ -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;
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <testbed/framework/sphere_mesh.h>
|
||||
#include <bounce/meshgen/sphere_mesh.h>
|
||||
|
||||
static inline void smAddVertex(smMesh& mesh, float32 x, float32 y, float32 z)
|
||||
{
|
||||
@ -32,7 +32,7 @@ static inline void smAddTriangle(smMesh& mesh, u32 v1, u32 v2, u32 v3)
|
||||
|
||||
static inline void smSetAsOctahedron(smMesh& mesh)
|
||||
{
|
||||
assert(mesh.vertexCount == 0);
|
||||
B3_ASSERT(mesh.vertexCount == 0);
|
||||
|
||||
smAddVertex(mesh, 0.0f, -1.0f, 0.0f);
|
||||
smAddVertex(mesh, 0.0f, 0.0f, 1.0f);
|
||||
@ -41,7 +41,7 @@ static inline void smSetAsOctahedron(smMesh& mesh)
|
||||
smAddVertex(mesh, 1.0f, 0.0f, 0.0f);
|
||||
smAddVertex(mesh, 0.0f, 1.0f, 0.0f);
|
||||
|
||||
assert(mesh.indexCount == 0);
|
||||
B3_ASSERT(mesh.indexCount == 0);
|
||||
|
||||
smAddTriangle(mesh, 0, 1, 2);
|
||||
smAddTriangle(mesh, 0, 2, 3);
|
||||
@ -162,8 +162,8 @@ static inline void smCount(u32& vertexCount, u32& indexCount, u32& edgeVertexPai
|
||||
|
||||
void smCreateMesh(smMesh& output, u32 subdivisions)
|
||||
{
|
||||
assert(output.vertexCount == 0);
|
||||
assert(output.indexCount == 0);
|
||||
B3_ASSERT(output.vertexCount == 0);
|
||||
B3_ASSERT(output.indexCount == 0);
|
||||
|
||||
u32 vertexCount, indexCount, edgeVertexPairCount;
|
||||
smCount(vertexCount, indexCount, edgeVertexPairCount, subdivisions);
|
||||
@ -173,11 +173,12 @@ void smCreateMesh(smMesh& output, u32 subdivisions)
|
||||
byteCount += indexCount * sizeof(u32);
|
||||
byteCount += edgeVertexPairCount * sizeof(smEdgeVertexPair);
|
||||
|
||||
u8* bytes = (u8*)malloc(byteCount);
|
||||
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)));
|
||||
|
||||
@ -192,21 +193,24 @@ void smCreateMesh(smMesh& output, u32 subdivisions)
|
||||
smSubdivideMesh(out, map);
|
||||
}
|
||||
|
||||
assert(map.pairCount < edgeVertexPairCount);
|
||||
|
||||
assert(out.vertexCount <= vertexCount);
|
||||
assert(out.indexCount == indexCount);
|
||||
B3_ASSERT(out.vertexCount <= vertexCount);
|
||||
B3_ASSERT(out.indexCount == indexCount);
|
||||
B3_ASSERT(map.pairCount < edgeVertexPairCount);
|
||||
|
||||
output.vertexCount = out.vertexCount;
|
||||
output.vertices = (b3Vec3*)malloc(out.vertexCount * sizeof(b3Vec3));
|
||||
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*)malloc(out.indexCount * sizeof(u32));
|
||||
output.indices = (u32*)b3Alloc(out.indexCount * sizeof(u32));
|
||||
memcpy(output.indices, out.indices, out.indexCount * sizeof(u32));
|
||||
|
||||
free(bytes);
|
||||
b3Free(bytes);
|
||||
|
||||
out.vertices = nullptr;
|
||||
out.normals = nullptr;
|
||||
out.indices = nullptr;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user