polygon/attribute index to triangle index?

benblobenblo Posts: 29

I need to get the (GL) triangle index from a GeometryLocation (polygon index + polygon sub-triangle index).
I can get the attribute indices triplets using getPolygonSubTrianglePolygonPoints, but I haven't found a way to go from attribute index to triangle index.

So I assumed that GL triangles are just push in the same order as the polygons, and just aggregate the triangle count until I've reached my polygon:

function UInt32 PolygonMesh.getTriangleIndex( GeometryLocation location )
{
    UInt32 iPoly = location.index;
    UInt32 iTri = location.subIndex;

    UInt32 triCount = 0;
    for (UInt32 iP = 0; iP < iPoly; ++iP)
    {
        triCount += this.getPolygonSubTriangleCount(iP);
    }

    return triCount + iTri;
}

... but it seems kinda dirty though. Is my assumption correct? that works in the few meshes I've tried it with, but will it always be valid? Is there an internal attribute <-> triangle map I can access somewhere?

R&D Developer at Dwarf Animation Studio

Tagged:

Best Answers

Answers

  • benblobenblo Posts: 29

    OK thanks for the confirmation.
    I do use generateAttributesTriangleList, but I don't see how it would help me from "polygon+sub-tri index" to "global tri index"...?

    R&D Developer at Dwarf Animation Studio

  • benblobenblo Posts: 29

    For anyone interested, here's what I ended up with, including the reverse to go from "global tri index" to "polygon+sub-tri index":

    public inline UInt32 PolygonMesh.getTriangleIndex( GeometryLocation location )
    {
        return this.getTriangleIndex(location.index, location.subIndex);
    }
    public UInt32 PolygonMesh.getTriangleIndex( UInt32 polygonIndex, UInt32 subTriangleIndex )
    {
        UInt32 triCount = 0;
        for (UInt32 iP = 0; iP < polygonIndex; ++iP)
        {
            triCount += this.getPolygonSubTriangleCount(iP);
        }
    
        return triCount + subTriangleIndex;
    }
    
    // reverse of getTriangleIndex
    public inline GeometryLocation PolygonMesh.getPolygonAndSubTriangleIndex( UInt32 attributeTriangleIndex )
    {
        GeometryLocation location;
        this.getPolygonAndSubTriangleIndex(attributeTriangleIndex, location.index, location.subIndex);
        return location;
    }
    // reverse of getTriangleIndex
    public PolygonMesh.getPolygonAndSubTriangleIndex( UInt32 attributeTriangleIndex, out UInt32 polygonIndex, out UInt32 subTriangleIndex )
    {
        UInt32 iPoly = 0;
        UInt32 iTri = attributeTriangleIndex;
    
        UInt32 subTriCount = this.getPolygonSubTriangleCount(iPoly);
        while (subTriCount < iTri)
        {
            iPoly++;
            iTri -= subTriCount;
            subTriCount = this.getPolygonSubTriangleCount(iPoly);
        }
    
        polygonIndex = iPoly;
        subTriangleIndex = iTri;
    }
    

    R&D Developer at Dwarf Animation Studio

  • jcgagnonjcgagnon Fabric Employee Posts: 122 Fabric Employee

    Looks right to me. However, since you're looping over half polygons (on average) for each polygon, this won't scale if you're getting it for many polygons, as it would become N^2. For a better performance, it would be preferable to return a full mapping table that can then be used for all polygons. A more fancy version could cache it as meta-data, and update if the mesh structure changes, but that would be more complex :-)

    Jerome Couture-Gagnon

  • benblobenblo Posts: 29

    Yeah for sure if this was performance-critical I'd bake the association, but for now it isn't so I just wanted the API :).

    R&D Developer at Dwarf Animation Studio

Sign In or Register to comment.