Triangulation 3D not working

Hey guys, I’m trying to use the Triangulator3 class and it keeps me giving 0 triangles, here is my code.

    points = [(0,0,0), (1,0,0), (1,1,0), (0,1,0)]
    drawPolygon(points)

def drawPolygon(points, color_face=LVecBase4f(1, 1, 1, 1)):
    """ Method to draw a polygon based on a set of points CCW oriented

    :param points: A list of points
    :type points: list
    """

    """ Setting parameters to draw a convex polygon"""
    format = GeomVertexFormat.getV3n3cpt2()
    vdata = GeomVertexData('polygon', format, Geom.UHStatic)
    vdata.setNumRows(len(points))

    vertex = GeomVertexWriter(vdata, 'vertex')
    normal = GeomVertexWriter(vdata, 'normal')
    color = GeomVertexWriter(vdata, 'color')
    texcoord = GeomVertexWriter(vdata, 'texcoord')

    """ Loop through the points adding them to the GeomVertexWriter object
    and adding to the triangulator 
    """
    triangulator = Triangulator3()
    for point in points:
        print(point)
        x = point[0]
        y = point[1]
        z = point[2]
        vertex.addData3(x, y, z)
        normal.addData3(normalized(2*x-1, 2*y-1, 2*z-1))
        color.addData4(color_face[0], color_face[1], color_face[2], color_face[3])
        triangulator.addVertex(x, y, z)

    triangulator.triangulate()
    print(triangulator.getNumTriangles())

    prim = GeomTriangles(Geom.UHStatic)
    for i in range(triangulator.getNumTriangles()):
        prim.addVertices(triangulator.getTriangleV0(i),
                         triangulator.getTriangleV1(i),
                         triangulator.getTriangleV2(i))
        prim.closePrimitive()

I have never used the Triangulator class before, but try removing the pass keyword at the end of the drawPolygons function. Looking at the Python side, pass meand that don’t do anything or an empty body for a block of code, like void drawPolygons(int[][] points, LVecBase4f color_face) in C++ Panda3D.

Removed the pass keyword, same result. 0 triangles for a square. Should be 2.

All that add_vertex does is add a vertex to a “vertex pool”; it doesn’t use those vertices to actually form the polygon.
So you also have to define the order in which the vertices are to be added to the polygon. The method to do this is add_polygon_vertex, which is a method of the Triangulator baseclass. If you change the triangulator-specific part of your code to this:

    """ Loop through the points adding them to the GeomVertexWriter object
    and adding to the triangulator 
    """
    triangulator = Triangulator3()
    for i, point in enumerate(points):
        print(point)
        x = point[0]
        y = point[1]
        z = point[2]
        vertex.addData3(x, y, z)
        normal.addData3(normalized(2*x-1, 2*y-1, 2*z-1))
        color.addData4(color_face[0], color_face[1], color_face[2], color_face[3])
        triangulator.addVertex(x, y, z)
        # add the vertex index to the polygon
        triangulator.add_polygon_vertex(i)

    triangulator.triangulate()
    print(triangulator.getNumTriangles())

then it should work.

1 Like

It worked, thanks!