summaryrefslogtreecommitdiffstats
path: root/virtual-programs/display/fill.folk
blob: f3057068f6def02b3039af9236e899fdc42494cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Wish the GPU compiles pipeline "fillTriangle" {
    {vec2 p0 vec2 p1 vec2 p2 vec4 color} {
        vec2 vertices[4] = vec2[4](p0, p1, p2, p0);
        return vertices[gl_VertexIndex];
    } {
        return color;
    }
}

When /someone/ wishes to draw a triangle with /...options/ {
    dict with options {
        Wish the GPU draws pipeline "fillTriangle" with arguments \
            [list $p0 $p1 $p2 [getColor $color]]
    }
}
When /someone/ wishes to draw a quad with /...options/ {
    dict with options {
        if {![info exists layer]} { set layer 0 }
        Wish the GPU draws pipeline "fillTriangle" with arguments \
            [list $p1 $p2 $p3 [getColor $color]] layer $layer
        Wish the GPU draws pipeline "fillTriangle" with arguments \
            [list $p0 $p1 $p3 [getColor $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 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
    } 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
    } else {
        # Get the first point in the list as the "base" point of the triangles
        set p0 [lindex $points 0]

        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 [getColor $color]]
        }
    }
}