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

131 lines
3.6 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;
2024-04-07 22:55:17 +02:00
Array<uint32> twoAdjacent;
Array<uint32> oneAdjacent;
int32 meshletId = -1;
2024-04-05 10:41:59 +02:00
};
2024-04-07 22:55:17 +02:00
int findIndex(Meshlet &current, uint32 index)
2024-04-05 10:41:59 +02:00
{
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++;
}
2024-04-07 22:55:17 +02:00
void completeMeshlet(Array<Meshlet> &meshlets, Meshlet &current)
2024-04-05 10:41:59 +02:00
{
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,
};
}
2024-04-07 22:55:17 +02:00
bool addTriangle(const Array<Vector>& positions, Array<Meshlet> &meshlets, Meshlet &current, Triangle tri)
2024-04-05 10:41:59 +02:00
{
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)
{
2024-04-07 22:55:17 +02:00
return false;
2024-04-05 10:41:59 +02:00
}
2024-04-07 22:55:17 +02:00
current.boundingBox.adjust(positions[tri.indices[0]]);
current.boundingBox.adjust(positions[tri.indices[1]]);
current.boundingBox.adjust(positions[tri.indices[2]]);
2024-04-05 10:41:59 +02:00
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++;
2024-04-07 22:55:17 +02:00
return true;
2024-04-05 10:41:59 +02:00
}
2024-04-07 22:55:17 +02:00
void Meshlet::build(const Array<Vector> &positions, const Array<uint32> &indices, Array<Meshlet> &meshlets)
2023-12-23 18:26:54 +01:00
{
Meshlet current = {
.numVertices = 0,
.numPrimitives = 0,
};
2024-04-07 16:33:32 +02:00
Array<Triangle> triangles(indices.size() / 3);
for (size_t i = 0; i < triangles.size(); ++i)
2024-04-05 10:41:59 +02:00
{
2024-04-07 22:55:17 +02:00
triangles[i] = Triangle{
2024-04-06 08:29:15 +02:00
.indices = {
indices[i * 3 + 0],
indices[i * 3 + 1],
indices[i * 3 + 2],
},
2024-04-07 22:55:17 +02:00
};
}
for (size_t i = 0; i < triangles.size(); i++)
{
for (size_t j = 0; j < triangles.size(); j++)
{
if (i == j)
continue;
2024-04-07 16:33:32 +02:00
2024-04-07 22:55:17 +02:00
uint32 adjacency = 0;
if (triangles[i].indices[0] == triangles[j].indices[0]
|| triangles[i].indices[0] == triangles[j].indices[1]
|| triangles[i].indices[0] == triangles[j].indices[2])
{
adjacency++;
}
if (triangles[i].indices[1] == triangles[j].indices[0]
|| triangles[i].indices[1] == triangles[j].indices[1]
|| triangles[i].indices[1] == triangles[j].indices[2])
{
adjacency++;
}
if (triangles[i].indices[1] == triangles[j].indices[0]
|| triangles[i].indices[1] == triangles[j].indices[1]
|| triangles[i].indices[1] == triangles[j].indices[2])
{
adjacency++;
}
if(adjacency == 2)
{
triangles[i].twoAdjacent.add(j);
triangles[j].twoAdjacent.add(i);
}
if(adjacency == 1)
{
triangles[i].oneAdjacent.add(j);
triangles[j].oneAdjacent.add(i);
}
}
}
addTriangle(positions, meshlets, current, triangles.back());
triangles.pop();
while(!triangles.empty())
{
AABB boundingBox;
2024-04-07 16:33:32 +02:00
}
if (current.numVertices > 0)
{
completeMeshlet(meshlets, current);
2023-12-23 18:26:54 +01:00
}
}