@@ -15,107 +15,18 @@ function drawFill(painter, layer, posMatrix, tile) {
15
15
var translatedPosMatrix = painter . translateMatrix ( posMatrix , tile , layer . paint [ 'fill-translate' ] , layer . paint [ 'fill-translate-anchor' ] ) ;
16
16
17
17
var color = layer . paint [ 'fill-color' ] ;
18
+ var image = layer . paint [ 'fill-pattern' ] ;
19
+ var opacity = layer . paint [ 'fill-opacity' ] || 1 ;
20
+ var shader ;
18
21
19
22
var vertex , elements , group , count ;
20
23
21
- // Draw the stencil mask.
22
-
23
- // We're only drawing to the first seven bits (== support a maximum of
24
- // 127 overlapping polygons in one place before we get rendering errors).
25
- gl . stencilMask ( 0x3F ) ;
26
- gl . clear ( gl . STENCIL_BUFFER_BIT ) ;
27
-
28
- // Draw front facing triangles. Wherever the 0x80 bit is 1, we are
29
- // increasing the lower 7 bits by one if the triangle is a front-facing
30
- // triangle. This means that all visible polygons should be in CCW
31
- // orientation, while all holes (see below) are in CW orientation.
32
- gl . stencilFunc ( gl . NOTEQUAL , 0x80 , 0x80 ) ;
33
-
34
- // When we do a nonzero fill, we count the number of times a pixel is
35
- // covered by a counterclockwise polygon, and subtract the number of
36
- // times it is "uncovered" by a clockwise polygon.
37
- gl . stencilOpSeparate ( gl . FRONT , gl . INCR_WRAP , gl . KEEP , gl . KEEP ) ;
38
- gl . stencilOpSeparate ( gl . BACK , gl . DECR_WRAP , gl . KEEP , gl . KEEP ) ;
39
-
40
- // When drawing a shape, we first draw all shapes to the stencil buffer
41
- // and incrementing all areas where polygons are
42
- gl . colorMask ( false , false , false , false ) ;
43
-
44
- // Draw the actual triangle fan into the stencil buffer.
45
- gl . switchShader ( painter . fillShader , translatedPosMatrix ) ;
46
-
47
- // Draw all buffers
48
24
vertex = tile . buffers . fillVertex ;
49
25
vertex . bind ( gl ) ;
50
26
51
27
elements = tile . buffers . fillElement ;
52
28
elements . bind ( gl ) ;
53
29
54
- var offset , elementOffset ;
55
-
56
- for ( var i = 0 ; i < elementGroups . groups . length ; i ++ ) {
57
- group = elementGroups . groups [ i ] ;
58
- offset = group . vertexStartIndex * vertex . itemSize ;
59
- vertex . setAttribPointers ( gl , painter . fillShader , offset ) ;
60
-
61
- count = group . elementLength * 3 ;
62
- elementOffset = group . elementStartIndex * elements . itemSize ;
63
- gl . drawElements ( gl . TRIANGLES , count , gl . UNSIGNED_SHORT , elementOffset ) ;
64
- }
65
-
66
- // Now that we have the stencil mask in the stencil buffer, we can start
67
- // writing to the color buffer.
68
- gl . colorMask ( true , true , true , true ) ;
69
-
70
- // From now on, we don't want to update the stencil buffer anymore.
71
- gl . stencilOp ( gl . KEEP , gl . KEEP , gl . KEEP ) ;
72
- gl . stencilMask ( 0x0 ) ;
73
-
74
- var strokeColor = layer . paint [ 'fill-outline-color' ] ;
75
-
76
- // Because we're drawing top-to-bottom, and we update the stencil mask
77
- // below, we have to draw the outline first (!)
78
- if ( layer . paint [ 'fill-antialias' ] === true && ! ( layer . paint [ 'fill-pattern' ] && ! strokeColor ) ) {
79
- gl . switchShader ( painter . outlineShader , translatedPosMatrix ) ;
80
- gl . lineWidth ( 2 * browser . devicePixelRatio ) ;
81
-
82
- if ( strokeColor ) {
83
- // If we defined a different color for the fill outline, we are
84
- // going to ignore the bits in 0x3F and just care about the global
85
- // clipping mask.
86
- gl . stencilFunc ( gl . EQUAL , 0x80 , 0x80 ) ;
87
- } else {
88
- // Otherwise, we only want to draw the antialiased parts that are
89
- // *outside* the current shape. This is important in case the fill
90
- // or stroke color is translucent. If we wouldn't clip to outside
91
- // the current shape, some pixels from the outline stroke overlapped
92
- // the (non-antialiased) fill.
93
- gl . stencilFunc ( gl . EQUAL , 0x80 , 0xBF ) ;
94
- }
95
-
96
- gl . uniform2f ( painter . outlineShader . u_world , gl . drawingBufferWidth , gl . drawingBufferHeight ) ;
97
- gl . uniform4fv ( painter . outlineShader . u_color , strokeColor ? strokeColor : color ) ;
98
-
99
- // Draw all buffers
100
- vertex = tile . buffers . fillVertex ;
101
- elements = tile . buffers . outlineElement ;
102
- elements . bind ( gl ) ;
103
-
104
- for ( var k = 0 ; k < elementGroups . groups . length ; k ++ ) {
105
- group = elementGroups . groups [ k ] ;
106
- offset = group . vertexStartIndex * vertex . itemSize ;
107
- vertex . setAttribPointers ( gl , painter . outlineShader , offset ) ;
108
-
109
- count = group . secondElementLength * 2 ;
110
- elementOffset = group . secondElementStartIndex * elements . itemSize ;
111
- gl . drawElements ( gl . LINES , count , gl . UNSIGNED_SHORT , elementOffset ) ;
112
- }
113
- }
114
-
115
- var image = layer . paint [ 'fill-pattern' ] ;
116
- var opacity = layer . paint [ 'fill-opacity' ] || 1 ;
117
- var shader ;
118
-
119
30
if ( image ) {
120
31
// Draw texture fill
121
32
var imagePosA = painter . spriteAtlas . getPosition ( image . from , true ) ;
@@ -154,16 +65,44 @@ function drawFill(painter, layer, posMatrix, tile) {
154
65
} else {
155
66
// Draw filling rectangle.
156
67
shader = painter . fillShader ;
157
- gl . switchShader ( shader , posMatrix ) ;
68
+ gl . switchShader ( shader , translatedPosMatrix ) ;
158
69
gl . uniform4fv ( shader . u_color , color ) ;
159
70
}
160
71
161
- // Only draw regions that we marked
162
- gl . stencilFunc ( gl . NOTEQUAL , 0x0 , 0x3F ) ;
163
- gl . bindBuffer ( gl . ARRAY_BUFFER , painter . tileExtentBuffer ) ;
164
- gl . vertexAttribPointer ( shader . a_pos , painter . tileExtentBuffer . itemSize , gl . SHORT , false , 0 , 0 ) ;
165
- gl . drawArrays ( gl . TRIANGLE_STRIP , 0 , painter . tileExtentBuffer . itemCount ) ;
72
+ var offset , elementOffset ;
73
+
74
+ for ( var i = 0 ; i < elementGroups . groups . length ; i ++ ) {
75
+ group = elementGroups . groups [ i ] ;
76
+ offset = group . vertexStartIndex * vertex . itemSize ;
77
+ vertex . setAttribPointers ( gl , painter . fillShader , offset ) ;
78
+
79
+ count = group . elementLength ;
80
+ elementOffset = group . elementStartIndex * elements . itemSize ;
81
+ gl . drawElements ( gl . TRIANGLES , count , gl . UNSIGNED_SHORT , elementOffset ) ;
82
+ }
83
+
84
+ var strokeColor = layer . paint [ 'fill-outline-color' ] ;
166
85
167
- gl . stencilMask ( 0x00 ) ;
168
- gl . stencilFunc ( gl . EQUAL , 0x80 , 0x80 ) ;
86
+ // Because we're drawing top-to-bottom, we have to draw the outline first
87
+ if ( layer . paint [ 'fill-antialias' ] === true && ! ( layer . paint [ 'fill-pattern' ] && ! strokeColor ) ) {
88
+ gl . switchShader ( painter . outlineShader , translatedPosMatrix ) ;
89
+ gl . lineWidth ( 2 * browser . devicePixelRatio ) ;
90
+
91
+ gl . uniform2f ( painter . outlineShader . u_world , gl . drawingBufferWidth , gl . drawingBufferHeight ) ;
92
+ gl . uniform4fv ( painter . outlineShader . u_color , strokeColor ? strokeColor : color ) ;
93
+
94
+ // Draw all buffers
95
+ elements = tile . buffers . outlineElement ;
96
+ elements . bind ( gl ) ;
97
+
98
+ for ( var k = 0 ; k < elementGroups . groups . length ; k ++ ) {
99
+ group = elementGroups . groups [ k ] ;
100
+ offset = group . vertexStartIndex * vertex . itemSize ;
101
+ vertex . setAttribPointers ( gl , painter . outlineShader , offset ) ;
102
+
103
+ count = group . secondElementLength * 2 ;
104
+ elementOffset = group . secondElementStartIndex * elements . itemSize ;
105
+ gl . drawElements ( gl . LINES , count , gl . UNSIGNED_SHORT , elementOffset ) ;
106
+ }
107
+ }
169
108
}
0 commit comments