Skip to content

Commit 44e4eb6

Browse files
committed
draw fill antialiasing with indexed lines
fixes #668
1 parent b45524d commit 44e4eb6

7 files changed

+54
-28
lines changed

js/data/buffer/bufferset.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var LineVertexBuffer = require('./linevertexbuffer.js');
44
var LineElementBuffer = require('./lineelementbuffer.js');
55
var FillVertexBuffer = require('./fillvertexbuffer.js');
66
var FillElementBuffer = require('./fillelementsbuffer.js');
7+
var OutlineElementBuffer = require('./outlineelementsbuffer.js');
78
var GlyphVertexBuffer = require('./glyphvertexbuffer.js');
89
var IconVertexBuffer = require('./iconvertexbuffer.js');
910

@@ -14,6 +15,7 @@ module.exports = function(bufferset) {
1415
iconVertex: new IconVertexBuffer(bufferset.iconVertex),
1516
fillVertex: new FillVertexBuffer(bufferset.fillVertex),
1617
fillElement: new FillElementBuffer(bufferset.fillElement),
18+
outlineElement: new OutlineElementBuffer(bufferset.outlineElement),
1719
lineVertex: new LineVertexBuffer(bufferset.lineVertex),
1820
lineElement: new LineElementBuffer(bufferset.lineElement)
1921
};

js/data/buffer/fillvertexbuffer.js

-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,3 @@ FillVertexBuffer.prototype.add = function(x, y) {
2222

2323
this.pos += this.itemSize;
2424
};
25-
26-
// Add a degenerate vertex (= out-of-viewplane) to the buffer.
27-
FillVertexBuffer.prototype.addDegenerate = function() {
28-
this.add(32767, 0);
29-
};
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
var Buffer = require('./buffer.js');
4+
5+
module.exports = OutlineElementsBuffer;
6+
7+
function OutlineElementsBuffer(buffer) {
8+
Buffer.call(this, buffer);
9+
}
10+
11+
OutlineElementsBuffer.prototype = Object.create(Buffer.prototype);
12+
13+
OutlineElementsBuffer.prototype.itemSize = 4; // bytes per line (2 * unsigned short == 4 bytes)
14+
OutlineElementsBuffer.prototype.arrayType = 'ELEMENT_ARRAY_BUFFER';
15+
16+
OutlineElementsBuffer.prototype.add = function(a, b) {
17+
var pos2 = this.pos / 2;
18+
19+
this.resize();
20+
21+
this.ushorts[pos2 + 0] = a;
22+
this.ushorts[pos2 + 1] = b;
23+
24+
this.pos += this.itemSize;
25+
};

js/data/elementgroups.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22

33
module.exports = ElementGroups;
44

5-
function ElementGroups(vertexBuffer, elementBuffer, groups) {
5+
function ElementGroups(vertexBuffer, elementBuffer, secondElementBuffer) {
66

77
this.vertexBuffer = vertexBuffer;
88
this.elementBuffer = elementBuffer;
9-
10-
if (groups) {
11-
this.groups = groups;
12-
} else {
13-
this.groups = [];
14-
}
9+
this.secondElementBuffer = secondElementBuffer;
10+
this.groups = [];
1511
}
1612

1713
ElementGroups.prototype.makeRoomFor = function(numVertices) {
1814
if (!this.current || this.current.vertexLength + numVertices > 65535) {
19-
this.current = new ElementGroup(this.vertexBuffer.index, this.elementBuffer && this.elementBuffer.index);
15+
this.current = new ElementGroup(this.vertexBuffer.index,
16+
this.elementBuffer && this.elementBuffer.index,
17+
this.secondElementBuffer && this.secondElementBuffer.index);
2018
this.groups.push(this.current);
2119
}
2220
};
2321

24-
function ElementGroup(vertexStartIndex, elementStartIndex) {
22+
function ElementGroup(vertexStartIndex, elementStartIndex, secondElementStartIndex) {
2523
// the offset into the vertex buffer of the first vertex in this group
2624
this.vertexStartIndex = vertexStartIndex;
2725
this.elementStartIndex = elementStartIndex;
26+
this.secondElementStartIndex = secondElementStartIndex;
2827
this.elementLength = 0;
2928
this.vertexLength = 0;
29+
this.secondElementLength = 0;
3030
}

js/data/fillbucket.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = FillBucket;
77
function FillBucket(info, buffers, placement, elementGroups) {
88
this.info = info;
99
this.buffers = buffers;
10-
this.elementGroups = elementGroups || new ElementGroups(buffers.fillVertex, buffers.fillElement);
10+
this.elementGroups = elementGroups || new ElementGroups(buffers.fillVertex, buffers.fillElement, buffers.outlineElement);
1111
}
1212

1313
FillBucket.prototype.addFeatures = function() {
@@ -45,9 +45,9 @@ FillBucket.prototype.addFill = function(vertices) {
4545

4646
var fillVertex = this.buffers.fillVertex;
4747
var fillElement = this.buffers.fillElement;
48+
var outlineElement = this.buffers.outlineElement;
4849

4950
// Start all lines with a degenerate vertex
50-
fillVertex.addDegenerate();
5151
elementGroup.vertexLength++;
5252

5353
// We're generating triangle fans, so we always start with the first coordinate in this polygon.
@@ -67,6 +67,11 @@ FillBucket.prototype.addFill = function(vertices) {
6767
elementGroup.elementLength++;
6868
}
6969

70+
if (i >= 1) {
71+
outlineElement.add(prevIndex, currentIndex);
72+
elementGroup.secondElementLength++;
73+
}
74+
7075
prevIndex = currentIndex;
7176
}
7277
};

js/render/drawfill.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ function drawFill(gl, painter, bucket, layerStyle, posMatrix, params, imageSprit
4545
elements = bucket.buffers.fillElement;
4646
elements.bind(gl);
4747

48+
var offset, elementOffset;
4849
for (var i = 0; i < bucket.elementGroups.groups.length; i++) {
4950
group = bucket.elementGroups.groups[i];
50-
var offset = group.vertexStartIndex * vertex.itemSize;
51+
offset = group.vertexStartIndex * vertex.itemSize;
5152
gl.vertexAttribPointer(painter.fillShader.a_pos, 2, gl.SHORT, false, 4, offset + 0);
5253

5354
count = group.elementLength * 3;
54-
var elementOffset = group.elementStartIndex * elements.itemSize;
55+
elementOffset = group.elementStartIndex * elements.itemSize;
5556
gl.drawElements(gl.TRIANGLES, count, gl.UNSIGNED_SHORT, elementOffset);
5657
}
5758

@@ -90,15 +91,17 @@ function drawFill(gl, painter, bucket, layerStyle, posMatrix, params, imageSprit
9091

9192
// Draw all buffers
9293
vertex = bucket.buffers.fillVertex;
93-
elements = bucket.buffers.fillElement;
94+
elements = bucket.buffers.outlineElement;
95+
elements.bind(gl);
9496

9597
for (var k = 0; k < bucket.elementGroups.groups.length; k++) {
9698
group = bucket.elementGroups.groups[k];
97-
gl.vertexAttribPointer(painter.outlineShader.a_pos, 2, gl.SHORT, false, 0, 0);
99+
offset = group.vertexStartIndex * vertex.itemSize;
100+
gl.vertexAttribPointer(painter.outlineShader.a_pos, 2, gl.SHORT, false, 4, offset + 0);
98101

99-
var begin = group.vertexStartIndex;
100-
count = group.vertexLength;
101-
gl.drawArrays(gl.LINE_STRIP, begin, count);
102+
count = group.secondElementLength * 2;
103+
elementOffset = group.secondElementStartIndex * elements.itemSize;
104+
gl.drawElements(gl.LINES, count, gl.UNSIGNED_SHORT, elementOffset);
102105
}
103106
}
104107

shaders/outline.vertex.glsl

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ uniform vec2 u_world;
55
varying vec2 v_pos;
66

77
void main() {
8-
// If the x coordinate is the maximum integer, we move the z coordinates out
9-
// of the view plane so that the triangle gets clipped. This makes it easier
10-
// for us to create degenerate triangle strips.
11-
float z = step(32767.0, a_pos.x);
12-
gl_Position = u_posmatrix * vec4(a_pos, z, 1);
8+
gl_Position = u_posmatrix * vec4(a_pos, 0, 1);
139
v_pos = (gl_Position.xy + 1.0) / 2.0 * u_world;
1410
}

0 commit comments

Comments
 (0)