Skip to content

Commit

Permalink
Add indices constructor to MeshData
Browse files Browse the repository at this point in the history
Add GetVertices to MeshData
  • Loading branch information
KredeGC committed Dec 2, 2023
1 parent d70d1c9 commit 04fd870
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 38 deletions.
6 changes: 2 additions & 4 deletions Mahakam/src/Mahakam/Renderer/GL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ namespace Mahakam
};

// Interleave vertices
MeshData meshData(vertexCount);
meshData.SetIndices(indices, indexCount);
MeshData meshData(vertexCount, indices, indexCount);
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions);
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs);

Expand Down Expand Up @@ -333,8 +332,7 @@ namespace Mahakam
indices[17] = 4;

// Interleave vertices
MeshData meshData(vertexCount);
meshData.SetIndices(indices, indexCount);
MeshData meshData(vertexCount, indices, indexCount);
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions);

return SubMesh::Create(std::move(meshData));
Expand Down
18 changes: 6 additions & 12 deletions Mahakam/src/Mahakam/Renderer/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ namespace Mahakam
// TODO: Support interleaved data
// TODO: Support sparse data sets

Asset<BoneMesh> skinnedMesh = CreateAsset<BoneMesh>(std::move(props));
Asset<BoneMesh> skinnedMesh = CreateAsset<BoneMesh>(props);

// Extract vertex and index values
for (auto& m : model.meshes)
Expand Down Expand Up @@ -306,9 +306,7 @@ namespace Mahakam
MH_ASSERT(indexCount == indexOffset, "Index count mismatch");

// Populate a single struct with the vertex data
MeshData meshData(vertexCount);

meshData.SetIndices(indices.data(), indexCount);
MeshData meshData(vertexCount, std::move(indices));

if (positionOffset)
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
Expand Down Expand Up @@ -500,8 +498,7 @@ namespace Mahakam
}

// Interleave vertices
MeshData meshData(vertexCount);
meshData.SetIndices(std::move(indices));
MeshData meshData(vertexCount, std::move(indices));
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs.data());
meshData.SetVertices(VertexType::Normals, ShaderDataType::Float3, normals.data());
Expand Down Expand Up @@ -563,8 +560,7 @@ namespace Mahakam
}

// Interleave vertices
MeshData meshData(vertexCount);
meshData.SetIndices(std::move(indices));
MeshData meshData(vertexCount, std::move(indices));
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs.data());
meshData.SetVertices(VertexType::Normals, ShaderDataType::Float3, normals.data());
Expand Down Expand Up @@ -627,8 +623,7 @@ namespace Mahakam
}

// Interleave vertices
MeshData meshData(vertexCount);
meshData.SetIndices(std::move(indices));
MeshData meshData(vertexCount, std::move(indices));
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs.data());
meshData.SetVertices(VertexType::Normals, ShaderDataType::Float3, normals.data());
Expand Down Expand Up @@ -727,8 +722,7 @@ namespace Mahakam
}

// Interleave vertices
MeshData meshData(vertexCount);
meshData.SetIndices(std::move(indices));
MeshData meshData(vertexCount, std::move(indices));
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs.data());
meshData.SetVertices(VertexType::Normals, ShaderDataType::Float3, normals.data());
Expand Down
52 changes: 42 additions & 10 deletions Mahakam/src/Mahakam/Renderer/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ namespace Mahakam
m_Indices(Allocator::GetAllocator<uint32_t>()),
m_Offsets(Allocator::GetAllocator<std::pair<const Input, std::pair<size_t, ShaderDataType>>>()) {}

MeshData(uint32_t vertexCount, const uint32_t* indices, uint32_t indexCount) :
m_VertexCount(vertexCount),
m_IndexCount(indexCount),
m_VertexData(Allocator::GetAllocator<uint8_t>()),
m_Indices(indices, indices + indexCount, Allocator::GetAllocator<uint32_t>()),
m_Offsets(Allocator::GetAllocator<std::pair<const Input, std::pair<size_t, ShaderDataType>>>()) {}

template<typename T>
MeshData(uint32_t vertexCount, T&& container) :
m_VertexCount(vertexCount),
m_IndexCount(static_cast<uint32_t>(container.size())),
m_VertexData(Allocator::GetAllocator<uint8_t>()),
m_Indices(std::forward<T>(container)),
m_Offsets(Allocator::GetAllocator<std::pair<const Input, std::pair<size_t, ShaderDataType>>>()) {}

MeshData(const MeshData&) = delete;

MeshData(MeshData&&) noexcept = default;
Expand All @@ -192,6 +207,22 @@ namespace Mahakam
}
}

template<typename T>
const void* GetVertices(T index) const
{
auto iter = m_Offsets.find(size_t(index));
if (iter == m_Offsets.end())
return nullptr;

return m_VertexData.data() + iter->first;
}

template<typename T, typename U>
const T* GetVertices(U index) const
{
return reinterpret_cast<const T*>(GetVertices(index));
}

void SetIndices(const uint32_t* data, uint32_t indexCount)
{
m_IndexCount = indexCount;
Expand All @@ -204,7 +235,7 @@ namespace Mahakam
{
m_IndexCount = static_cast<uint32_t>(container.size());

m_Indices = std::move(container);
m_Indices = std::forward<T>(container);
}

uint32_t GetVertexCount() const { return m_VertexCount; }
Expand Down Expand Up @@ -240,20 +271,21 @@ namespace Mahakam

virtual uint32_t GetVertexCount() const = 0;

virtual bool HasVertices(int index) const = 0;
virtual const void* GetVertices(int index) const = 0;

const glm::vec3* GetPositions() const { return (const glm::vec3*)GetVertices(0); }
const glm::vec2* GetTexcoords() const { return (const glm::vec2*)GetVertices(1); }
const glm::vec3* GetNormals() const { return (const glm::vec3*)GetVertices(2); }
const glm::vec4* GetTangents() const { return (const glm::vec4*)GetVertices(3); }
const glm::vec4* GetColors() const { return (const glm::vec4*)GetVertices(4); }
const glm::ivec4* GetBoneIDs() const { return (const glm::ivec4*)GetVertices(5); }
const glm::vec4* GetBoneWeights() const { return (const glm::vec4*)GetVertices(6); }

virtual const uint32_t* GetIndices() const = 0;
virtual uint32_t GetIndexCount() const = 0;

bool HasVertices(int index) { return GetVertices(index) == nullptr; }

const glm::vec3* GetPositions() const { return reinterpret_cast<const glm::vec3*>(GetVertices(0)); }
const glm::vec2* GetTexcoords() const { return reinterpret_cast<const glm::vec2*>(GetVertices(1)); }
const glm::vec3* GetNormals() const { return reinterpret_cast<const glm::vec3*>(GetVertices(2)); }
const glm::vec4* GetTangents() const { return reinterpret_cast<const glm::vec4*>(GetVertices(3)); }
const glm::vec4* GetColors() const { return reinterpret_cast<const glm::vec4*>(GetVertices(4)); }
const glm::ivec4* GetBoneIDs() const { return reinterpret_cast<const glm::ivec4*>(GetVertices(5)); }
const glm::vec4* GetBoneWeights() const { return reinterpret_cast<const glm::vec4*>(GetVertices(6)); }

inline static Ref<SubMesh> Create(MeshData&& mesh) { return CreateImpl(std::move(mesh)); }

static Ref<SubMesh> CreateCube(int tessellation, bool reverse = false);
Expand Down
11 changes: 5 additions & 6 deletions Mahakam/src/Platform/Headless/HeadlessMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ namespace Mahakam

virtual void SetVertices(int slot, const void* data) override;

virtual const Bounds& GetBounds() const override { return m_Bounds; }
inline virtual const Bounds& GetBounds() const override { return m_Bounds; }

inline uint32_t GetVertexCount() const override { return m_MeshData.GetVertexCount(); }
inline virtual uint32_t GetVertexCount() const override { return m_MeshData.GetVertexCount(); }

inline virtual bool HasVertices(int index) const override { return m_MeshData.GetOffsets().find(index) != m_MeshData.GetOffsets().end(); }
inline virtual const void* GetVertices(int index) const override { return m_MeshData.GetVertexData().data() + m_MeshData.GetOffsets().at(index).first; }
inline virtual const void* GetVertices(int index) const override { return m_MeshData.GetVertices(index); }

inline const uint32_t* GetIndices() const override { return m_MeshData.GetIndices().data(); }
inline uint32_t GetIndexCount() const override { return m_MeshData.GetIndexCount(); }
inline virtual const uint32_t* GetIndices() const override { return m_MeshData.GetIndices().data(); }
inline virtual uint32_t GetIndexCount() const override { return m_MeshData.GetIndexCount(); }
};
}
11 changes: 5 additions & 6 deletions Mahakam/src/Platform/OpenGL/OpenGLMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ namespace Mahakam

virtual void SetVertices(int slot, const void* data) override;

virtual const Bounds& GetBounds() const override { return m_Bounds; }
inline virtual const Bounds& GetBounds() const override { return m_Bounds; }

inline uint32_t GetVertexCount() const override { return m_MeshData.GetVertexCount(); }
inline virtual uint32_t GetVertexCount() const override { return m_MeshData.GetVertexCount(); }

inline virtual bool HasVertices(int index) const override { return m_MeshData.GetOffsets().find(index) != m_MeshData.GetOffsets().end(); }
inline virtual const void* GetVertices(int index) const override { return m_MeshData.GetVertexData().data() + m_MeshData.GetOffsets().at(index).first; }
inline virtual const void* GetVertices(int index) const override { return m_MeshData.GetVertices(index); }

inline const uint32_t* GetIndices() const override { return m_MeshData.GetIndices().data(); }
inline uint32_t GetIndexCount() const override { return m_MeshData.GetIndexCount(); }
inline virtual const uint32_t* GetIndices() const override { return m_MeshData.GetIndices().data(); }
inline virtual uint32_t GetIndexCount() const override { return m_MeshData.GetIndexCount(); }

private:
void Init();
Expand Down

0 comments on commit 04fd870

Please sign in to comment.