Skip to content

Commit dd7b0a6

Browse files
committed
Add comments about packing values into flags.
Use a function for coordinate transformation in the vertex shader.
1 parent c7c2db0 commit dd7b0a6

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/gl/lineFeature.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ var gl_lineFeature = function (arg) {
105105

106106
'const float PI = 3.14159265358979323846264;',
107107

108+
'vec4 viewCoord(vec3 c) {',
109+
' vec4 result = projectionMatrix * modelViewMatrix * vec4(c.xyz, 1);',
110+
' if (result.w != 0.0) result = result / result.w;',
111+
' return result;',
112+
'}',
113+
108114
'void main(void)',
109115
'{',
110116
/* If any vertex has been deliberately set to a negative opacity,
@@ -117,14 +123,10 @@ var gl_lineFeature = function (arg) {
117123
* calculate the angles between the lines formed by prev-pos and
118124
* pos-next, and between pos-next and next-far, plus know the angle
119125
* (prev)---(pos)---(next)---(far) => A---B---C---D */
120-
' vec4 A = projectionMatrix * modelViewMatrix * vec4(prev.xyz, 1);',
121-
' if (A.w != 0.0) A = A / A.w;',
122-
' vec4 B = projectionMatrix * modelViewMatrix * vec4(pos.xyz, 1);',
123-
' if (B.w != 0.0) B = B / B.w;',
124-
' vec4 C = projectionMatrix * modelViewMatrix * vec4(next.xyz, 1);',
125-
' if (C.w != 0.0) C = C / C.w;',
126-
' vec4 D = projectionMatrix * modelViewMatrix * vec4(far.xyz, 1);',
127-
' if (D.w != 0.0) D = D / D.w;',
126+
' vec4 A = viewCoord(prev);',
127+
' vec4 B = viewCoord(pos);',
128+
' vec4 C = viewCoord(next);',
129+
' vec4 D = viewCoord(far);',
128130
// calculate line segment vector and angle
129131
' vec2 deltaCB = C.xy - B.xy;',
130132
' if (deltaCB == vec2(0.0, 0.0)) {',
@@ -138,6 +140,10 @@ var gl_lineFeature = function (arg) {
138140
' int vertex = int(mod(flags, 4.0));',
139141
' int nearMode = int(mod(floor(flags / 4.0), 8.0));',
140142
' int farMode = int(mod(floor(flags / 32.0), 8.0));',
143+
// we use 11 bits of the flags for the offset, where -1023 to 1023
144+
// maps to -1 to 1. The 11 bits are a signed value, so simply
145+
// selecting the bits will result in an unsigned values that may be
146+
// greater than 1, in which case we have to subtract appropriately.
141147
' float offset = mod(floor(flags / 256.0), 2048.0) / 1023.0;',
142148
' if (offset > 1.0) offset -= 2048.0 / 1023.0;',
143149
// by default, offset by the width and don't extend lines. Later,
@@ -465,6 +471,9 @@ var gl_lineFeature = function (arg) {
465471
v1.strokeOpacity = strokeOpacityVal === undefined ? strokeOpacityFunc(lineItemData, lidx, lineItem, i) : strokeOpacityVal;
466472
v1.strokeOffset = (strokeOffsetVal === undefined ? strokeOffsetFunc(lineItemData, lidx, lineItem, i) : strokeOffsetVal) || 0;
467473
if (v1.strokeOffset) {
474+
/* we use 11 bits to store the offset, and we want to store values
475+
* from -1 to 1, so multiply our values by 1023, and use some bit
476+
* manipulation to ensure that it is packed properly */
468477
v1.posStrokeOffset = Math.round(2048 + 1023 * Math.min(1, Math.max(-1, v1.strokeOffset))) & 0x7FF;
469478
v1.negStrokeOffset = Math.round(2048 - 1023 * Math.min(1, Math.max(-1, v1.strokeOffset))) & 0x7FF;
470479
} else {

0 commit comments

Comments
 (0)