Skip to content

Commit 4972064

Browse files
authored
Merge pull request #597 from OpenGeoscience/stroke-polygons
Add stroking to polygons
2 parents eb4c2b1 + 6d53729 commit 4972064

File tree

13 files changed

+233
-62
lines changed

13 files changed

+233
-62
lines changed

examples/polygons/main.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,21 @@ $(function () {
3636
return {x: d[0], y: d[1]};
3737
})
3838
.data(data)
39-
.style('uniformPolygon', true)
40-
.style('fillOpacity', query.opacity ? parseFloat(query.opacity) : 0.5)
41-
.style('fillColor', function (d, idx, poly, polyidx) {
42-
return poly.hover ? hoverColor : (polyColor ? polyColor : {
43-
r: (polyidx % 256) / 255,
44-
g: polyidx / (data.length - 1),
45-
b: 0.25
46-
});
39+
.style({
40+
uniformPolygon: true,
41+
fillOpacity: query.opacity ? parseFloat(query.opacity) : 0.5,
42+
fillColor: function (d, idx, poly, polyidx) {
43+
return poly.hover ? hoverColor : (polyColor ? polyColor : {
44+
r: (polyidx % 256) / 255,
45+
g: polyidx / (data.length - 1),
46+
b: 0.25
47+
});
48+
},
49+
stroke: query.stroke !== 'false' ? function (poly, polyidx) {
50+
return poly.hover;
51+
} : false,
52+
strokeWidth: query.strokeWidth ? parseFloat(query.strokeWidth) : 1,
53+
strokeColor: {r: 0, g: 0, b: 0}
4754
})
4855
.geoOn(geo.event.feature.mouseover, function (evt) {
4956
if (!evt.data.hover) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"sinon": "1.17.3",
6565
"style-loader": "^0.13.1",
6666
"url-loader": "^0.5.7",
67-
"vgl": "0.3.6",
67+
"vgl": "0.3.8",
6868
"webpack": "^1.12.14",
6969
"webpack-dev-server": "^1.14.1",
7070
"xmlbuilder": "^8.2.2"

src/gl/lineFeature.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,12 @@ var gl_lineFeature = function (arg) {
6161
'uniform float pixelWidth;',
6262
'uniform float aspect;',
6363

64-
'varying vec3 strokeColorVar;',
65-
'varying float strokeWidthVar;',
66-
'varying float strokeOpacityVar;',
64+
'varying vec4 strokeColorVar;',
6765

6866
'void main(void)',
6967
'{',
70-
/* If any vertex has been deliberately set to a negative opacity,
71-
* skip doing computations on it. */
68+
/* If any vertex has been deliberately set to a negative opacity,
69+
* skip doing computations on it. */
7270
' if (strokeOpacity < 0.0) {',
7371
' gl_Position = vec4(2, 2, 0, 1);',
7472
' return;',
@@ -86,9 +84,7 @@ var gl_lineFeature = function (arg) {
8684
' if (worldPrev.w != 0.0) {',
8785
' worldPrev = worldPrev/worldPrev.w;',
8886
' }',
89-
' strokeColorVar = strokeColor;',
90-
' strokeWidthVar = strokeWidth;',
91-
' strokeOpacityVar = strokeOpacity;',
87+
' strokeColorVar = vec4(strokeColor, strokeOpacity);',
9288
' vec2 deltaNext = worldNext.xy - worldPos.xy;',
9389
' vec2 deltaPrev = worldPos.xy - worldPrev.xy;',
9490
' float angleNext = 0.0, anglePrev = 0.0;',
@@ -98,6 +94,8 @@ var gl_lineFeature = function (arg) {
9894
' else anglePrev = atan(deltaPrev.y / aspect, deltaPrev.x);',
9995
' if (deltaNext.xy == vec2(0.0, 0.0)) angleNext = anglePrev;',
10096
' float angle = (anglePrev + angleNext) / 2.0;',
97+
' if (abs(anglePrev - angleNext) >= PI)',
98+
' angle += PI;',
10199
' float cosAngle = cos(anglePrev - angle);',
102100
' if (cosAngle < 0.1) { cosAngle = sign(cosAngle) * 1.0; angle = 0.0; }',
103101
' float distance = (offset * strokeWidth * pixelWidth) /',
@@ -117,11 +115,9 @@ var gl_lineFeature = function (arg) {
117115
'#ifdef GL_ES',
118116
' precision highp float;',
119117
'#endif',
120-
'varying vec3 strokeColorVar;',
121-
'varying float strokeWidthVar;',
122-
'varying float strokeOpacityVar;',
118+
'varying vec4 strokeColorVar;',
123119
'void main () {',
124-
' gl_FragColor = vec4 (strokeColorVar, strokeOpacityVar);',
120+
' gl_FragColor = strokeColorVar;',
125121
'}'
126122
].join('\n'),
127123
shader = new vgl.shader(vgl.GL.FRAGMENT_SHADER);
@@ -206,7 +202,7 @@ var gl_lineFeature = function (arg) {
206202
nextBuf[dest3 + 1] = position[v.next + 1];
207203
nextBuf[dest3 + 2] = position[v.next + 2];
208204
offsetBuf[dest] = order[k][1];
209-
/* We can ignore the indicies (they will all be zero) */
205+
/* We can ignore the indices (they will all be zero) */
210206
strokeWidthBuf[dest] = v.strokeWidth;
211207
strokeColorBuf[dest3] = v.strokeColor.r;
212208
strokeColorBuf[dest3 + 1] = v.strokeColor.g;

src/gl/object.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
var inherit = require('../inherit');
2-
var sceneObject = require('../sceneObject');
3-
41
//////////////////////////////////////////////////////////////////////////////
52
/**
63
* VGL specific subclass of object which rerenders when the object is drawn.
@@ -19,7 +16,6 @@ var gl_object = function (arg) {
1916
if (!(this instanceof object)) {
2017
return new gl_object(arg);
2118
}
22-
sceneObject.call(this);
2319

2420
var m_this = this,
2521
s_draw = this.draw;
@@ -39,6 +35,5 @@ var gl_object = function (arg) {
3935
return this;
4036
};
4137

42-
inherit(gl_object, sceneObject);
4338
module.exports = gl_object;
4439

src/gl/polygonFeature.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,15 @@ var gl_polygonFeature = function (arg) {
4949
'attribute float fillOpacity;',
5050
'uniform mat4 modelViewMatrix;',
5151
'uniform mat4 projectionMatrix;',
52-
'uniform float pixelWidth;',
53-
'varying vec3 fillColorVar;',
54-
'varying float fillOpacityVar;',
52+
'varying vec4 fillColorVar;',
5553

5654
'void main(void)',
5755
'{',
5856
' vec4 clipPos = projectionMatrix * modelViewMatrix * vec4(pos.xyz, 1);',
5957
' if (clipPos.w != 0.0) {',
6058
' clipPos = clipPos/clipPos.w;',
6159
' }',
62-
' fillColorVar = fillColor;',
63-
' fillOpacityVar = fillOpacity;',
60+
' fillColorVar = vec4(fillColor, fillOpacity);',
6461
' gl_Position = clipPos;',
6562
'}'
6663
].join('\n'),
@@ -74,10 +71,9 @@ var gl_polygonFeature = function (arg) {
7471
'#ifdef GL_ES',
7572
' precision highp float;',
7673
'#endif',
77-
'varying vec3 fillColorVar;',
78-
'varying float fillOpacityVar;',
74+
'varying vec4 fillColorVar;',
7975
'void main () {',
80-
' gl_FragColor = vec4 (fillColorVar, fillOpacityVar);',
76+
' gl_FragColor = fillColorVar;',
8177
'}'
8278
].join('\n'),
8379
shader = new vgl.shader(vgl.GL.FRAGMENT_SHADER);
@@ -105,20 +101,23 @@ var gl_polygonFeature = function (arg) {
105101
var posBuf, posFunc,
106102
fillColor, fillColorFunc, fillColorVal,
107103
fillOpacity, fillOpacityFunc, fillOpacityVal,
104+
fillFunc, fillVal,
108105
uniformPolyFunc, uniform,
109106
indices,
110107
items = [],
111108
target_gcs = m_this.gcs(),
112109
map_gcs = m_this.layer().map().gcs(),
113110
numPts = 0,
114111
geom = m_mapper.geometryData(),
115-
color, opacity, d, d3, vertices, i, j, k, n,
112+
color, opacity, fill, d, d3, vertices, i, j, k, n,
116113
record, item, itemIndex, original;
117114

118115
fillColorFunc = m_this.style.get('fillColor');
119116
fillColorVal = util.isFunction(m_this.style('fillColor')) ? undefined : fillColorFunc();
120117
fillOpacityFunc = m_this.style.get('fillOpacity');
121118
fillOpacityVal = util.isFunction(m_this.style('fillOpacity')) ? undefined : fillOpacityFunc();
119+
fillFunc = m_this.style.get('fill');
120+
fillVal = util.isFunction(m_this.style('fill')) ? undefined : fillFunc();
122121
uniformPolyFunc = m_this.style.get('uniformPolygon');
123122

124123
if (!onlyStyle) {
@@ -190,6 +189,7 @@ var gl_polygonFeature = function (arg) {
190189
d = d3 = 0;
191190
color = fillColorVal;
192191
opacity = fillOpacityVal;
192+
fill = fillVal;
193193
for (k = 0; k < items.length; k += 1) {
194194
n = items[k].triangles.length;
195195
vertices = items[k].vertices;
@@ -205,6 +205,12 @@ var gl_polygonFeature = function (arg) {
205205
opacity = fillOpacityFunc(vertices[0], 0, item, itemIndex);
206206
}
207207
}
208+
if (fillVal === undefined) {
209+
fill = fillFunc(item, itemIndex);
210+
}
211+
if (!fill) {
212+
opacity = 0;
213+
}
208214
if (uniform && onlyStyle && items[k].uniform && items[k].color &&
209215
color.r === items[k].color.r && color.g === items[k].color.g &&
210216
color.b === items[k].color.b && opacity === items[k].opacity) {

src/gl/quadFeature.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ var gl_quadFeature = function (arg) {
262262
m_quads.clrQuads.length * 12 > m_clrposbuf.length) {
263263
setupColorDrawObjects(renderState);
264264
}
265-
mapper.s_render(renderState);
265+
mapper.s_render(renderState, true);
266266

267267
var context = renderState.m_context, opacity = 1, color;
268268

@@ -289,6 +289,7 @@ var gl_quadFeature = function (arg) {
289289
context.drawArrays(vgl.GL.TRIANGLE_STRIP, 0, 4);
290290
});
291291
context.bindBuffer(vgl.GL.ARRAY_BUFFER, null);
292+
mapper.undoBindVertexData(renderState);
292293
};
293294

294295
/**
@@ -307,7 +308,7 @@ var gl_quadFeature = function (arg) {
307308
m_quads.imgQuads.length * 12 > m_imgposbuf.length) {
308309
setupDrawObjects(renderState);
309310
}
310-
mapper.s_render(renderState);
311+
mapper.s_render(renderState, true);
311312

312313
var context = renderState.m_context, opacity = 1;
313314

@@ -335,6 +336,7 @@ var gl_quadFeature = function (arg) {
335336
quad.texture.undoBind(renderState);
336337
});
337338
context.bindBuffer(vgl.GL.ARRAY_BUFFER, null);
339+
mapper.undoBindVertexData(renderState);
338340
};
339341

340342
////////////////////////////////////////////////////////////////////////////

src/jsonReader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ var jsonReader = function (arg) {
269269
})
270270
.position(m_this._position)
271271
.style({
272-
fill: m_this._style('fill', true, polygons),
272+
fill: m_this._style('fill', true),
273273
fillColor: m_this._style('fillColor', '#b0de5c', polygons, convertColor),
274274
fillOpacity: m_this._style('fillOpacity', 0.8, polygons),
275-
stroke: m_this._style('stroke', true, polygons),
275+
stroke: m_this._style('stroke', true),
276276
strokeColor: m_this._style('strokeColor', '#999999', polygons, convertColor),
277277
strokeWidth: m_this._style('strokeWidth', 2, polygons),
278278
strokeOpacity: m_this._style('strokeOpacity', 1, polygons)

src/lineFeature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ var lineFeature = function (arg) {
203203
{},
204204
{
205205
'strokeWidth': 1.0,
206-
// Default to gold color for lines
206+
// Default to gold color for lines
207207
'strokeColor': { r: 1.0, g: 0.8431372549, b: 0.0 },
208208
'strokeStyle': 'solid',
209209
'strokeOpacity': 1.0,
@@ -247,7 +247,7 @@ lineFeature.create = function (layer, spec) {
247247
lineFeature.capabilities = {
248248
/* support for solid-colored, constant-width lines */
249249
basic: 'line.basic',
250-
/* support for lines that very in width and color */
250+
/* support for lines that vary in width and color */
251251
multicolor: 'line.multicolor'
252252
};
253253

0 commit comments

Comments
 (0)