builtin-programs/draw/fill.folk

Wish the GPU compiles pipeline "fillTriangle" {
    {mat3 surfaceToClip vec2 p0 vec2 p1 vec2 p2 vec4 color} {
        vec2 vertices[6] = vec2[6](p0, p1, p2, p0, p0, p0);
        vec3 v = surfaceToClip * vec3(vertices[gl_VertexIndex], 1.0);
        return vec4(v.xy/v.z, 0.0, 1.0);
    } {
        return color;
    }
}

When the color map is /colorMap/ {

When /someone/ wishes to draw a triangle with /...options/ {
    dict with options {
        if {![info exists layer]} { set layer 0 }
        set color [dict getdef $colorMap $color $color]
        Wish the GPU draws pipeline "fillTriangle" with arguments \
            [list $p0 $p1 $p2 $color] layer $layer
    }
}
When /someone/ wishes to draw a quad onto /p/ with /...options/ &\
     /p/ has canvas /id/ with /...wiOptions/ &\
     /p/ has canvas projection /surfaceToClip/ {
    dict with options {
        if {![info exists layer]} { set layer 0 }
        set color [dict getdef $colorMap $color $color]
        Wish the GPU draws pipeline "fillTriangle" onto canvas $id with arguments \
            [list $surfaceToClip $p1 $p2 $p3 $color] layer $layer
        Wish the GPU draws pipeline "fillTriangle" onto canvas $id with arguments \
            [list $surfaceToClip $p0 $p1 $p3 $color] layer $layer
    }
}
When /someone/ wishes to draw a polygon with /...options/ {
    set points [dict get $options points]
    set color [dict get $options color]
    set layer [dict getdef $options layer 0]

    set num_points [llength $points]
    if {$num_points < 3} {
        error "At least 3 points are required to form a polygon."
    } elseif {$num_points == 3} {
        Wish to draw a triangle with \
            p0 [lindex $points 0] p1 [lindex $points 1] p2 [lindex $points 2] \
            color $color layer $layer
    } elseif {$num_points == 4} {
        Wish to draw a quad with \
            p0 [lindex $points 0] p1 [lindex $points 1] p2 [lindex $points 2] p3 [lindex $points 3] \
            color $color layer $layer
    } else {
        # Get the first point in the list as the "base" point of the triangles
        set p0 [lindex $points 0]

        set color [dict getdef $colorMap $color $color]
        for {set i 1} {$i < $num_points - 1} {incr i} {
            set p1 [lindex $points $i]
            set p2 [lindex $points [expr {$i+1}]]
            Wish the GPU draws pipeline "fillTriangle" with arguments \
                [list $p0 $p1 $p2 $color] layer $layer
        }
    }
}

}

When /someone/ wishes /page/ is filled with /...options/ &\
     /page/ has region /region/ {
  set points [region vertices $region]
  Wish to draw a polygon with points $points {*}$options
}