Files
Seele/src/Engine/Graphics/Meshlet.cpp
T

81 lines
2.0 KiB
C++
Raw Normal View History

2023-12-23 18:26:54 +01:00
#include "Meshlet.h"
#include "Containers/Map.h"
#include "Containers/List.h"
#include "Containers/Set.h"
using namespace Seele;
2024-04-05 10:41:59 +02:00
struct Triangle
{
StaticArray<uint32, 3> indices;
};
int findIndex(Meshlet& current, uint32 index)
{
for (uint32 i = 0; i < current.numVertices; ++i)
{
if (current.uniqueVertices[i] == index)
{
return i;
}
}
if (current.numVertices == Gfx::numVerticesPerMeshlet)
{
return -1;
}
current.uniqueVertices[current.numVertices] = index;
return current.numVertices++;
}
void completeMeshlet(Array<Meshlet>& meshlets, Meshlet& current)
{
meshlets.add(current);
current = {
2024-04-06 08:29:15 +02:00
.boundingBox = AABB(),
2024-04-05 10:41:59 +02:00
.numVertices = 0,
.numPrimitives = 0,
};
}
void addTriangle(Array<Meshlet>& meshlets, Meshlet& current, Triangle tri)
{
int f1 = findIndex(current, tri.indices[0]);
int f2 = findIndex(current, tri.indices[1]);
int f3 = findIndex(current, tri.indices[2]);
if (f1 == -1 || f2 == -1 || f3 == -1)
{
completeMeshlet(meshlets, current);
f1 = findIndex(current, tri.indices[0]);
f2 = findIndex(current, tri.indices[1]);
f3 = findIndex(current, tri.indices[2]);
}
current.primitiveLayout[current.numPrimitives * 3 + 0] = uint8(f1);
current.primitiveLayout[current.numPrimitives * 3 + 1] = uint8(f2);
current.primitiveLayout[current.numPrimitives * 3 + 2] = uint8(f3);
current.numPrimitives++;
if (current.numPrimitives == Gfx::numPrimitivesPerMeshlet)
{
completeMeshlet(meshlets, current);
}
}
2024-04-06 08:29:15 +02:00
void Meshlet::build(const Array<Vector>& positions, const Array<uint16>& indices, Array<Meshlet>& meshlets)
2023-12-23 18:26:54 +01:00
{
Meshlet current = {
.numVertices = 0,
.numPrimitives = 0,
};
2024-04-06 08:29:15 +02:00
for (size_t i = 0; i < indices.size() / 3; ++i)
2024-04-05 10:41:59 +02:00
{
2024-04-06 08:29:15 +02:00
addTriangle(meshlets, current, Triangle{
.indices = {
indices[i * 3 + 0],
indices[i * 3 + 1],
indices[i * 3 + 2],
},
});
2023-12-23 18:26:54 +01:00
}
}