Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions include/ppx/tri_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,25 @@ class TriMeshOptions
TriMeshOptions() {}
~TriMeshOptions() {}
// clang-format off
//! Enable/disable indices
TriMeshOptions& Indices(bool value = true) { mEnableIndices = value; return *this; }
//! Enable/disable indices, using UINT32 by default when enabled
TriMeshOptions& Indices(bool value = true)
{
mEnableIndices = value;
if (value && (mIndexType == grfx::INDEX_TYPE_UNDEFINED)) {
mIndexType = grfx::INDEX_TYPE_UINT32;
}
else if (!value) {
mIndexType = grfx::INDEX_TYPE_UNDEFINED;
}
return *this;
}
//! Set the index type; UNDEFINED disables indices, concrete types enable indices
TriMeshOptions& IndexType(grfx::IndexType value)
{
mIndexType = value;
mEnableIndices = (value != grfx::INDEX_TYPE_UNDEFINED);
return *this;
}
//! Enable/disable vertex colors
TriMeshOptions& VertexColors(bool value = true) { mEnableVertexColors = value; return *this; }
//! Enable/disable normals
Expand All @@ -108,18 +125,19 @@ class TriMeshOptions
TriMeshOptions& InvertWinding() { mInvertWinding = true; return *this; }
// clang-format on
private:
bool mEnableIndices = false;
bool mEnableVertexColors = false;
bool mEnableNormals = false;
bool mEnableTexCoords = false;
bool mEnableTangents = false;
bool mEnableObjectColor = false;
bool mInvertTexCoordsV = false;
bool mInvertWinding = false;
float3 mObjectColor = float3(0.7f);
float3 mTranslate = float3(0, 0, 0);
float3 mScale = float3(1, 1, 1);
float2 mTexCoordScale = float2(1, 1);
bool mEnableIndices = false;
bool mEnableVertexColors = false;
bool mEnableNormals = false;
bool mEnableTexCoords = false;
bool mEnableTangents = false;
bool mEnableObjectColor = false;
bool mInvertTexCoordsV = false;
bool mInvertWinding = false;
grfx::IndexType mIndexType = grfx::INDEX_TYPE_UINT32;
float3 mObjectColor = float3(0.7f);
float3 mTranslate = float3(0, 0, 0);
float3 mScale = float3(1, 1, 1);
float2 mTexCoordScale = float2(1, 1);
friend class TriMesh;
};

Expand Down Expand Up @@ -161,6 +179,7 @@ class TriMesh
uint64_t GetDataSizeTangents() const;
uint64_t GetDataSizeBitangents() const;

const uint8_t* GetDataIndicesU8(uint32_t index = 0) const;
const uint16_t* GetDataIndicesU16(uint32_t index = 0) const;
const uint32_t* GetDataIndicesU32(uint32_t index = 0) const;
const float3* GetDataPositions(uint32_t index = 0) const;
Expand Down Expand Up @@ -199,6 +218,7 @@ class TriMesh
static TriMesh CreateFromOBJ(const std::filesystem::path& path, const TriMeshOptions& options = TriMeshOptions());

private:
void AppendIndexU8(uint8_t value);
void AppendIndexU16(uint16_t value);
void AppendIndexU32(uint32_t value);

Expand All @@ -212,7 +232,7 @@ class TriMesh
private:
grfx::IndexType mIndexType = grfx::INDEX_TYPE_UNDEFINED;
TriMeshAttributeDim mTexCoordDim = TRI_MESH_ATTRIBUTE_DIM_UNDEFINED;
std::vector<uint8_t> mIndices; // Stores both 16 and 32 bit indices
std::vector<uint8_t> mIndices; // Stores 8, 16 and 32 bit indices
std::vector<float3> mPositions; // Vertex positions
std::vector<float3> mColors; // Vertex colors
std::vector<float3> mNormals; // Vertex normals
Expand Down
36 changes: 28 additions & 8 deletions include/ppx/wire_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,25 @@ class WireMeshOptions
WireMeshOptions() {}
~WireMeshOptions() {}
// clang-format off
//! Enable/disable indices
WireMeshOptions& Indices(bool value = true) { mEnableIndices = value; return *this; }
//! Enable/disable indices, using UINT32 by default when enabled
WireMeshOptions& Indices(bool value = true)
{
mEnableIndices = value;
if (value && (mIndexType == grfx::INDEX_TYPE_UNDEFINED)) {
mIndexType = grfx::INDEX_TYPE_UINT32;
}
else if (!value) {
mIndexType = grfx::INDEX_TYPE_UNDEFINED;
}
return *this;
}
//! Set the index type; UNDEFINED disables indices, concrete types enable indices
WireMeshOptions& IndexType(grfx::IndexType value)
{
mIndexType = value;
mEnableIndices = (value != grfx::INDEX_TYPE_UNDEFINED);
return *this;
}
//! Enable/disable vertex colors
WireMeshOptions& VertexColors(bool value = true) { mEnableVertexColors = value; return *this; }
//! Set and/or enable/disable object color, object color will override vertex colors
Expand All @@ -62,11 +79,12 @@ class WireMeshOptions
WireMeshOptions& Scale(const float3& scale) { mScale = scale; return *this; }
// clang-format on
private:
bool mEnableIndices = false;
bool mEnableVertexColors = false;
bool mEnableObjectColor = false;
float3 mObjectColor = float3(0.7f);
float3 mScale = float3(1, 1, 1);
bool mEnableIndices = false;
bool mEnableVertexColors = false;
bool mEnableObjectColor = false;
grfx::IndexType mIndexType = grfx::INDEX_TYPE_UINT32;
float3 mObjectColor = float3(0.7f);
float3 mScale = float3(1, 1, 1);
friend class WireMesh;
};

Expand All @@ -93,6 +111,7 @@ class WireMesh
uint64_t GetDataSizePositions() const;
uint64_t GetDataSizeColors() const;

const uint8_t* GetDataIndicesU8(uint32_t index = 0) const;
const uint16_t* GetDataIndicesU16(uint32_t index = 0) const;
const uint32_t* GetDataIndicesU32(uint32_t index = 0) const;
const float3* GetDataPositions(uint32_t index = 0) const;
Expand All @@ -113,6 +132,7 @@ class WireMesh
static WireMesh CreateSphere(float radius, uint32_t usegs, uint32_t vsegs, const WireMeshOptions& options = WireMeshOptions());

private:
void AppendIndexU8(uint8_t value);
void AppendIndexU16(uint16_t value);
void AppendIndexU32(uint32_t value);

Expand All @@ -125,7 +145,7 @@ class WireMesh

private:
grfx::IndexType mIndexType = grfx::INDEX_TYPE_UNDEFINED;
std::vector<uint8_t> mIndices; // Stores both 16 and 32 bit indices
std::vector<uint8_t> mIndices; // Stores 8, 16 and 32 bit indices
std::vector<float3> mPositions; // Vertex positions
std::vector<float3> mColors; // Vertex colors
float3 mBoundingBoxMin; // Bounding box min
Expand Down
61 changes: 49 additions & 12 deletions src/ppx/tri_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "tiny_obj_loader.h"

#include <limits>

namespace ppx {

TriMesh::TriMesh()
Expand All @@ -28,8 +30,6 @@ TriMesh::TriMesh()
TriMesh::TriMesh(grfx::IndexType indexType)
: mIndexType(indexType)
{
// TODO: #514 - Remove assert when UINT8 is supported
PPX_ASSERT_MSG(mIndexType != grfx::INDEX_TYPE_UINT8, "INDEX_TYPE_UINT8 unsupported in TriMesh");
}

TriMesh::TriMesh(TriMeshAttributeDim texCoordDim)
Expand All @@ -40,8 +40,6 @@ TriMesh::TriMesh(TriMeshAttributeDim texCoordDim)
TriMesh::TriMesh(grfx::IndexType indexType, TriMeshAttributeDim texCoordDim)
: mIndexType(indexType), mTexCoordDim(texCoordDim)
{
// TODO: #514 - Remove assert when UINT8 is supported
PPX_ASSERT_MSG(mIndexType != grfx::INDEX_TYPE_UINT8, "INDEX_TYPE_UINT8 unsupported in TriMesh");
}

TriMesh::~TriMesh()
Expand Down Expand Up @@ -162,6 +160,18 @@ uint64_t TriMesh::GetDataSizeBitangents() const
return size;
}

const uint8_t* TriMesh::GetDataIndicesU8(uint32_t index) const
{
if (mIndexType != grfx::INDEX_TYPE_UINT8) {
return nullptr;
}
uint32_t count = GetCountIndices();
if (index >= count) {
return nullptr;
}
return mIndices.data() + index;
}

const uint16_t* TriMesh::GetDataIndicesU16(uint32_t index) const
{
if (mIndexType != grfx::INDEX_TYPE_UINT16) {
Expand Down Expand Up @@ -282,6 +292,11 @@ const float3* TriMesh::GetDataBitangents(uint32_t index) const
return reinterpret_cast<const float3*>(ptr);
}

void TriMesh::AppendIndexU8(uint8_t value)
{
mIndices.push_back(value);
}

void TriMesh::AppendIndexU16(uint16_t value)
{
const uint8_t* pBytes = reinterpret_cast<const uint8_t*>(&value);
Expand All @@ -304,6 +319,9 @@ void TriMesh::PreallocateForTriangleCount(size_t triangleCount, bool enableColor

// Reserve for triangles
switch (mIndexType) {
case grfx::INDEX_TYPE_UINT8:
mIndices.reserve(vertexCount * sizeof(uint8_t));
break;
case grfx::INDEX_TYPE_UINT16:
mIndices.reserve(vertexCount * sizeof(uint16_t));
break;
Expand Down Expand Up @@ -339,7 +357,19 @@ void TriMesh::PreallocateForTriangleCount(size_t triangleCount, bool enableColor

uint32_t TriMesh::AppendTriangle(uint32_t v0, uint32_t v1, uint32_t v2)
{
if (mIndexType == grfx::INDEX_TYPE_UINT16) {
if (mIndexType == grfx::INDEX_TYPE_UINT8) {
PPX_ASSERT_MSG(v0 <= std::numeric_limits<uint8_t>::max(), "v0 is out of range for index type UINT8");
PPX_ASSERT_MSG(v1 <= std::numeric_limits<uint8_t>::max(), "v1 is out of range for index type UINT8");
PPX_ASSERT_MSG(v2 <= std::numeric_limits<uint8_t>::max(), "v2 is out of range for index type UINT8");
mIndices.reserve(mIndices.size() + 3 * sizeof(uint8_t));
AppendIndexU8(static_cast<uint8_t>(v0));
AppendIndexU8(static_cast<uint8_t>(v1));
AppendIndexU8(static_cast<uint8_t>(v2));
}
else if (mIndexType == grfx::INDEX_TYPE_UINT16) {
PPX_ASSERT_MSG(v0 <= std::numeric_limits<uint16_t>::max(), "v0 is out of range for index type UINT16");
PPX_ASSERT_MSG(v1 <= std::numeric_limits<uint16_t>::max(), "v1 is out of range for index type UINT16");
PPX_ASSERT_MSG(v2 <= std::numeric_limits<uint16_t>::max(), "v2 is out of range for index type UINT16");
mIndices.reserve(mIndices.size() + 3 * sizeof(uint16_t));
AppendIndexU16(static_cast<uint16_t>(v0));
AppendIndexU16(static_cast<uint16_t>(v1));
Expand Down Expand Up @@ -462,7 +492,14 @@ Result TriMesh::GetTriangle(uint32_t triIndex, uint32_t& v0, uint32_t& v1, uint3
const uint8_t* pData = mIndices.data();
uint32_t elementSize = grfx::IndexTypeSize(mIndexType);

if (mIndexType == grfx::INDEX_TYPE_UINT16) {
if (mIndexType == grfx::INDEX_TYPE_UINT8) {
size_t offset = 3 * triIndex * elementSize;
const uint8_t* pIndexData = pData + offset;
v0 = static_cast<uint32_t>(pIndexData[0]);
v1 = static_cast<uint32_t>(pIndexData[1]);
v2 = static_cast<uint32_t>(pIndexData[2]);
}
else if (mIndexType == grfx::INDEX_TYPE_UINT16) {
size_t offset = 3 * triIndex * elementSize;
const uint16_t* pIndexData = reinterpret_cast<const uint16_t*>(pData + offset);
v0 = static_cast<uint32_t>(pIndexData[0]);
Expand Down Expand Up @@ -525,7 +562,7 @@ void TriMesh::AppendIndexAndVertexData(
const TriMeshOptions& options,
TriMesh& mesh)
{
grfx::IndexType indexType = options.mEnableIndices ? grfx::INDEX_TYPE_UINT32 : grfx::INDEX_TYPE_UNDEFINED;
grfx::IndexType indexType = mesh.GetIndexType();

// Verify expected vertex count
size_t vertexCount = (vertexData.size() * sizeof(float)) / sizeof(TriMeshVertexData);
Expand Down Expand Up @@ -718,7 +755,7 @@ TriMesh TriMesh::CreatePlane(TriMeshPlane plane, const float2& size, uint32_t us
}
}

grfx::IndexType indexType = options.mEnableIndices ? grfx::INDEX_TYPE_UINT32 : grfx::INDEX_TYPE_UNDEFINED;
grfx::IndexType indexType = options.mEnableIndices ? options.mIndexType : grfx::INDEX_TYPE_UNDEFINED;
TriMeshAttributeDim texCoordDim = options.mEnableTexCoords ? TRI_MESH_ATTRIBUTE_DIM_2 : TRI_MESH_ATTRIBUTE_DIM_UNDEFINED;
TriMesh mesh = TriMesh(indexType, texCoordDim);

Expand All @@ -745,7 +782,7 @@ TriMesh TriMesh::CreatePlane(TriMeshPlane plane, const float2& size, uint32_t us
};
// clang-format on

grfx::IndexType indexType = options.mEnableIndices ? grfx::INDEX_TYPE_UINT32 : grfx::INDEX_TYPE_UNDEFINED;
grfx::IndexType indexType = options.mEnableIndices ? options.mIndexType : grfx::INDEX_TYPE_UNDEFINED;
TriMeshAttributeDim texCoordDim = options.mEnableTexCoords ? TRI_MESH_ATTRIBUTE_DIM_2 : TRI_MESH_ATTRIBUTE_DIM_UNDEFINED;
TriMesh mesh = TriMesh(indexType, texCoordDim);

Expand Down Expand Up @@ -816,7 +853,7 @@ TriMesh TriMesh::CreateCube(const float3& size, const TriMeshOptions& options)
};
// clang-format on

grfx::IndexType indexType = options.mEnableIndices ? grfx::INDEX_TYPE_UINT32 : grfx::INDEX_TYPE_UNDEFINED;
grfx::IndexType indexType = options.mEnableIndices ? options.mIndexType : grfx::INDEX_TYPE_UNDEFINED;
TriMeshAttributeDim texCoordDim = options.mEnableTexCoords ? TRI_MESH_ATTRIBUTE_DIM_2 : TRI_MESH_ATTRIBUTE_DIM_UNDEFINED;
TriMesh mesh = TriMesh(indexType, texCoordDim);

Expand Down Expand Up @@ -894,7 +931,7 @@ TriMesh TriMesh::CreateSphere(float radius, uint32_t usegs, uint32_t vsegs, cons
}
}

grfx::IndexType indexType = options.mEnableIndices ? grfx::INDEX_TYPE_UINT32 : grfx::INDEX_TYPE_UNDEFINED;
grfx::IndexType indexType = options.mEnableIndices ? options.mIndexType : grfx::INDEX_TYPE_UNDEFINED;
TriMeshAttributeDim texCoordDim = options.mEnableTexCoords ? TRI_MESH_ATTRIBUTE_DIM_2 : TRI_MESH_ATTRIBUTE_DIM_UNDEFINED;
TriMesh mesh = TriMesh(indexType, texCoordDim);

Expand All @@ -915,7 +952,7 @@ Result TriMesh::CreateFromOBJ(const std::filesystem::path& path, const TriMeshOp
double fnStartTime = timer.SecondsSinceStart();

// Determine index type and tex coord dim
grfx::IndexType indexType = options.mEnableIndices ? grfx::INDEX_TYPE_UINT32 : grfx::INDEX_TYPE_UNDEFINED;
grfx::IndexType indexType = options.mEnableIndices ? options.mIndexType : grfx::INDEX_TYPE_UNDEFINED;
TriMeshAttributeDim texCoordDim = options.mEnableTexCoords ? TRI_MESH_ATTRIBUTE_DIM_2 : TRI_MESH_ATTRIBUTE_DIM_UNDEFINED;

// Create new mesh
Expand Down
Loading