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()