Drawing Triangles 0.4.0 - ZackWilde27/Z3dPy GitHub Wiki

Once you've got your rendered triangles:

for tri in z3dpy.RenderThings([myThing]):

Rastering

If your drawing method only supports pixels or lines, there's TriToPixels() or TriToLines()

def MyPixelDraw(x, y):
   # The current triangle in the for loop can be accessed from here.
   pygame.draw.line(surface, zp.TriGetColour(tri), (x, y), (x + 1, y))

z3dpy.TriToPixels(tri, MyPixelDraw)

# Or

def MyLineDraw(sx, sy, ex, ey):
    pygame.draw.line(surface, z3dpy.TriGetColour(tri), (sx, sy), (ex, ey))

z3dpy.TriToLines(tri, MyLineDraw)

# Or

def MyTexelDraw(x, y, u, v):
    # The UVs are normalized, so they will need to be multiplied and modulo'd by the size of the texture being applied.
    scaleX = len(myTexture[0])
    scaleY = len(myTexture)
    actualU = abs(u * scaleX) % scaleX
    actualV = abs(v * scaleY) % scaleY
    colour = myTexture[floor(actualV)][floor(actualU)]
    pygame.draw.line(surface, colour, (x, y), (x + 1, y))

z3dpy.TriToTexels(tri, MyTexelDraw)

        

But a lot of libraries have built-in triangle support:

PyGame

for tri in z3dpy.Render():
    colour = z3dpy.TriGetColour(tri)

    pygame.draw.polygon(screen, colour, [(tri[0][0], tri[0][1]), (tri[1][0], tri[1][1]), (tri[2][0], tri[2][1])])

Pyglet

def on_draw():

    triangles = [pyglet.shapes.Triangle(tri[0][0], tri[0][1], tri[1][0], tri[1][1], tri[2][0], tri[2][1], color=z3dpy.TriGetColour(tri), batch=myBatch) for tri in z3dpy.Render()]

    myWindow.clear()
    myBatch.draw()

Tkinter

for tri in z3dpy.Render():
    colour = z3dpy.RGBToHex(z3dpy.TriGetColour(tri))
    canvas.create_polygon([tri[0][0], tri[0][1], tri[1][0], tri[1][1], tri[2][0], tri[2][1]], fill=colour)

Kivy

with self.canvas.before:
    for tri in z3dpy.Render():
        # In Kivy, colours are normalized.
        colour = z3dpy.VectorDivF(z3dpy.TriGetColour(tri), 255)

        kivy.graphics.Color(colour[0], colour[1], colour[2], 1, mode='rgba')
        kivy.graphics.Triangle(points=(tri[0][0], tri[0][1], tri[1][0], tri[1][1], tri[2][0], tri[2][1]))

Graphics.py

for tri in z3dpy.Render():
    colour = z3dpy.RGBToHex(z3dpy.TriGetColour(tri))

    points = [Point(tri[0][0], tri[0][1]), Point(tri[1][0], tri[1][1]), Point(tri[2][0], tri[2][1])]

    triangle = Polygon(points)
    triangle.setFill(colour)
    triangle.draw(win)