memononen / libtess2

Game and tools oriented refactored version of GLU tesselator.
Other
465 stars 98 forks source link

Getting wrong Triangles when trying to tesselate #43

Closed CrimNinja closed 1 year ago

CrimNinja commented 1 year ago

I have been trying to use libtess2 to do some tesselation. And in some cases I am getting the wrong triangles. I have included one set of points in which I am getting the wrong output.

std::vector Points{1.3515, -0.8156, 1.3672, -0.8156, 1.3329, -0.5057, 1.3174, -0.5057}; TESStesselator pTess = tessNewTess(nullptr); tessAddContour(pTess, 2, Points.data(), 2 sizeof(float), 4); tessTesselate(pTess, TESS_WINDING_NONZERO, TESS_POLYGONS, 3, 2, 0);

I have accounted for the order of vertices using tessGetVertexElements and matched the right indexes from tessGetElements. With or without it, either way, the resulting triangles seem weird.

image is the triangle I am getting from this.

Am I missing something from the way I call the functions ? Or is there something else that I need to do to get the right triangles?

memononen commented 1 year ago

Can you show the code you use to read back the triangles?

CrimNinja commented 1 year ago
    const int *pElements = tessGetElements(pTess);
    const int *pOrder = tessGetVertexIndices(pTess);
    const int NumElements = tessGetElementCount(pTess);
    const int NumVertices = tessGetVertexCount(pTess);
    const float *Vertices = tessGetVertices(pTess);

    std::vector<int> IndexMap;
    IndexMap.resize(NumVertices);

    int j = 0;
    for(int i = 0; i < NumVertices; ++i)
    {
        if(pOrder[i] != -1)
            IndexMap[pOrder[i]] = j++;
    }
    rTriangles.reserve(NumElements);
    for(int i = 0; i < NumElements; i++)
    {
        rTriangles.emplace_back(IndexMap[pElements[i * 3 + 0]], IndexMap[pElements[i * 3 + 1]],
                                IndexMap[pElements[i * 3 + 2]]);
    }

This is what I am doing to read the indexes after tesselation.

memononen commented 1 year ago

Which vertex data do you use? That IndexMap does not make sense to me.

CrimNinja commented 1 year ago

I am adding points using Points.data(), and in other parts of the code, the points I have added to the vector are referenced by the index 0, 1, 2,.. But libtess2 seemed to rearrange or add vertices in some cases, so I use an IndexMap to ignore the extra vertex that was added and to refer back to the original order I tried adding the vertices.

CrimNinja commented 1 year ago

image

Here is an image of the values that I get from the tesselation, the elements from tessGetElements seem to provide 0, 1, 2, 1, 0, 3 in this case. Which seems strange.

memononen commented 1 year ago

pOrder[pElements[i]] should to the original vertex index, or if it is TESS_UNDEF then it was a vertex that was added because there was an intersection.

How does the triangulation look like if you use the tessGetVertices() to plot the points, not your own mapping?

CrimNinja commented 1 year ago

Omg, that works! Thanks!