Search code examples
c++3dmeshprocedural-generation

How to procedurally generate LODs of a triangle mesh


I am looking for an algorithm that can generate level of details of a given mesh.
Meshes have a vertex and index buffers. Each LOD number of vertices may be computed as following:

LOD0 -> 1 / 2^0 -> 1 * <mesh_number_of_vertices>
LOD1 -> 1 / 2^1 -> 1/2 * <mesh_number_of_vertices>
...
LOD8 -> 1 / 2^8 -> 1 / 256 * <mesh_number_of_vertices>

In term of code, I am looking how to implement the GenerateLOD function:

struct Vertex
{
    Math::Vec3 Position;
    Math::Vec2 Texcoord;
};

void GenerateLOD(const std::vector<Vertex>& InVertices,
    const std::vector<uint32_t>& InIndices,

    std::vector<Vertex>& outVertices,
    std::vector<uint32_t>& outIndices,
    int LODLevel)
{
    const float LODFactor = 1.0f / pow(2, LODLevel);
    // Generation here...
}

int main()
{
    const std::vector<Vertex> MeshVertices{
        { Math::Vec3(-1.0f, -1.0f, 0.0f), Math::Vec2(0.0f, 1.0f)},
        { Math::Vec3(1.0f, -1.0f, 0.0f), Math::Vec2(1.0f, 1.0f)},
        { Math::Vec3(-1.0f, 1.0f, 0.0f), Math::Vec2(0.0f, 0.0f)},
        { Math::Vec3(1.0f, 1.0f, 0.0f), Math::Vec2(1.0f, 0.0f)}
        //etc.....
    };

    const std::vector<uint32_t> MeshIndices{ 0, 1, 2, 1, 2, 3/* etc...*/ };

    std::vector<Vertex> OutVertices;
    std::vector<uint32_t> OutIndices;

    const int LODLevel = 2;
    GenerateLOD(MeshVertices, MeshIndices, OutVertices, OutIndices, LODLevel);

    return EXIT_SUCCESS;
}

Solution

  • I found that the problem is known as Mesh simplification.
    Here great papers about it:
    https://www.cs.cmu.edu/~garland/Papers/quadric2.pdf
    https://cragl.cs.gmu.edu/seamless/

    Open source implementations can be found here:
    https://github.com/cnr-isti-vclab/meshlab
    https://github.com/cnr-isti-vclab/vcglib/