Improve code quality
Cleaning up, making the code more consistent, and restructuring commonly used methods.
This commit is contained in:
@@ -136,21 +136,6 @@ ol.render.webgl.Replay = function(tolerance, maxExtent) {
|
|||||||
goog.inherits(ol.render.webgl.Replay, ol.render.VectorContext);
|
goog.inherits(ol.render.webgl.Replay, ol.render.VectorContext);
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @enum {number}
|
|
||||||
*/
|
|
||||||
ol.render.webgl.LineStringInstruction = {
|
|
||||||
ROUND: 2,
|
|
||||||
BEGIN_LINE: 3,
|
|
||||||
END_LINE: 5,
|
|
||||||
BEGIN_LINE_CAP: 7,
|
|
||||||
END_LINE_CAP : 11,
|
|
||||||
BEVEL_FIRST: 13,
|
|
||||||
BEVEL_SECOND: 17,
|
|
||||||
MITER_BOTTOM: 19,
|
|
||||||
MITER_TOP: 23
|
|
||||||
};
|
|
||||||
|
|
||||||
ol.render.webgl.Replay.prototype.getDeleteResourcesFunction = goog.abstractMethod;
|
ol.render.webgl.Replay.prototype.getDeleteResourcesFunction = goog.abstractMethod;
|
||||||
|
|
||||||
ol.render.webgl.Replay.prototype.finish = goog.abstractMethod;
|
ol.render.webgl.Replay.prototype.finish = goog.abstractMethod;
|
||||||
@@ -159,7 +144,60 @@ ol.render.webgl.Replay.prototype.setUpProgram_ = goog.abstractMethod;
|
|||||||
|
|
||||||
ol.render.webgl.Replay.prototype.drawReplay_ = goog.abstractMethod;
|
ol.render.webgl.Replay.prototype.drawReplay_ = goog.abstractMethod;
|
||||||
|
|
||||||
ol.render.webgl.Replay.prototype.drawHitDetectionReplay_ = goog.abstractMethod;
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @param {WebGLRenderingContext} gl gl.
|
||||||
|
* @param {ol.webgl.Context} context Context.
|
||||||
|
* @param {Object.<string, boolean>} skippedFeaturesHash Ids of features
|
||||||
|
* to skip.
|
||||||
|
* @param {function((ol.Feature|ol.render.Feature)): T|undefined} featureCallback Feature callback.
|
||||||
|
* @param {boolean} oneByOne Draw features one-by-one for the hit-detecion.
|
||||||
|
* @param {ol.Extent=} opt_hitExtent Hit extent: Only features intersecting
|
||||||
|
* this extent are checked.
|
||||||
|
* @return {T|undefined} Callback result.
|
||||||
|
* @template T
|
||||||
|
*/
|
||||||
|
ol.render.webgl.Replay.prototype.drawHitDetectionReplay_ = function(gl, context, skippedFeaturesHash,
|
||||||
|
featureCallback, oneByOne, opt_hitExtent) {
|
||||||
|
if (!oneByOne) {
|
||||||
|
// draw all hit-detection features in "once" (by texture group)
|
||||||
|
return this.drawHitDetectionReplayAll_(gl, context,
|
||||||
|
skippedFeaturesHash, featureCallback);
|
||||||
|
} else {
|
||||||
|
// draw hit-detection features one by one
|
||||||
|
return this.drawHitDetectionReplayOneByOne_(gl, context,
|
||||||
|
skippedFeaturesHash, featureCallback, opt_hitExtent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @param {WebGLRenderingContext} gl gl.
|
||||||
|
* @param {ol.webgl.Context} context Context.
|
||||||
|
* @param {Object.<string, boolean>} skippedFeaturesHash Ids of features
|
||||||
|
* to skip.
|
||||||
|
* @param {function((ol.Feature|ol.render.Feature)): T|undefined} featureCallback Feature callback.
|
||||||
|
* @return {T|undefined} Callback result.
|
||||||
|
* @template T
|
||||||
|
*/
|
||||||
|
ol.render.webgl.Replay.prototype.drawHitDetectionReplayAll_ = function(gl, context, skippedFeaturesHash,
|
||||||
|
featureCallback) {
|
||||||
|
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||||
|
this.drawReplay_(gl, context, skippedFeaturesHash, true);
|
||||||
|
|
||||||
|
var result = featureCallback(null);
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
ol.render.webgl.Replay.prototype.drawHitDetectionReplayOneByOne_ = goog.abstractMethod;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ol.webgl.Context} context Context.
|
* @param {ol.webgl.Context} context Context.
|
||||||
@@ -809,7 +847,6 @@ ol.render.webgl.ImageReplay.prototype.drawReplaySkipping_ = function(gl, context
|
|||||||
* @param {Object.<string, boolean>} skippedFeaturesHash Ids of features
|
* @param {Object.<string, boolean>} skippedFeaturesHash Ids of features
|
||||||
* to skip.
|
* to skip.
|
||||||
* @param {function((ol.Feature|ol.render.Feature)): T|undefined} featureCallback Feature callback.
|
* @param {function((ol.Feature|ol.render.Feature)): T|undefined} featureCallback Feature callback.
|
||||||
* @param {boolean} oneByOne Draw features one-by-one for the hit-detecion.
|
|
||||||
* @param {ol.Extent=} opt_hitExtent Hit extent: Only features intersecting
|
* @param {ol.Extent=} opt_hitExtent Hit extent: Only features intersecting
|
||||||
* this extent are checked.
|
* this extent are checked.
|
||||||
* @return {T|undefined} Callback result.
|
* @return {T|undefined} Callback result.
|
||||||
@@ -864,8 +901,8 @@ ol.render.webgl.ImageReplay.prototype.drawHitDetectionReplayAll_ = function(gl,
|
|||||||
* @return {T|undefined} Callback result.
|
* @return {T|undefined} Callback result.
|
||||||
* @template T
|
* @template T
|
||||||
*/
|
*/
|
||||||
ol.render.webgl.ImageReplay.prototype.drawHitDetectionReplayOneByOne_ = function(gl, context, skippedFeaturesHash, featureCallback,
|
ol.render.webgl.ImageReplay.prototype.drawHitDetectionReplayOneByOne_ = function(gl, context, skippedFeaturesHash,
|
||||||
opt_hitExtent) {
|
featureCallback, opt_hitExtent) {
|
||||||
ol.DEBUG && console.assert(this.hitDetectionTextures_.length ===
|
ol.DEBUG && console.assert(this.hitDetectionTextures_.length ===
|
||||||
this.hitDetectionGroupIndices_.length,
|
this.hitDetectionGroupIndices_.length,
|
||||||
'number of hitDetectionTextures and hitDetectionGroupIndices match');
|
'number of hitDetectionTextures and hitDetectionGroupIndices match');
|
||||||
@@ -1055,7 +1092,7 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
|
|||||||
var numVertices = this.vertices_.length;
|
var numVertices = this.vertices_.length;
|
||||||
var numIndices = this.indices_.length;
|
var numIndices = this.indices_.length;
|
||||||
//To save a vertex, the direction of a point is a product of the sign (1 or -1), a prime from
|
//To save a vertex, the direction of a point is a product of the sign (1 or -1), a prime from
|
||||||
//ol.render.webgl.LineStringInstruction, and a rounding factor (1 or 2). If the product is even,
|
//ol.render.webgl.lineStringInstruction, and a rounding factor (1 or 2). If the product is even,
|
||||||
//we round it. If it is odd, we don't.
|
//we round it. If it is odd, we don't.
|
||||||
var lineJoin = this.state_.lineJoin === 'bevel' ? 0 :
|
var lineJoin = this.state_.lineJoin === 'bevel' ? 0 :
|
||||||
this.state_.lineJoin === 'miter' ? 1 : 2;
|
this.state_.lineJoin === 'miter' ? 1 : 2;
|
||||||
@@ -1092,10 +1129,10 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
|
|||||||
|
|
||||||
if (lineCap) {
|
if (lineCap) {
|
||||||
numVertices = this.addVertices_([0, 0], p1, p2,
|
numVertices = this.addVertices_([0, 0], p1, p2,
|
||||||
lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE_CAP * lineCap, numVertices);
|
lastSign * ol.render.webgl.lineStringInstruction.BEGIN_LINE_CAP * lineCap, numVertices);
|
||||||
|
|
||||||
numVertices = this.addVertices_([0, 0], p1, p2,
|
numVertices = this.addVertices_([0, 0], p1, p2,
|
||||||
-lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE_CAP * lineCap, numVertices);
|
-lastSign * ol.render.webgl.lineStringInstruction.BEGIN_LINE_CAP * lineCap, numVertices);
|
||||||
|
|
||||||
this.indices_[numIndices++] = n + 2;
|
this.indices_[numIndices++] = n + 2;
|
||||||
this.indices_[numIndices++] = n;
|
this.indices_[numIndices++] = n;
|
||||||
@@ -1108,10 +1145,10 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
|
|||||||
}
|
}
|
||||||
|
|
||||||
numVertices = this.addVertices_([0, 0], p1, p2,
|
numVertices = this.addVertices_([0, 0], p1, p2,
|
||||||
lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE * (lineCap || 1), numVertices);
|
lastSign * ol.render.webgl.lineStringInstruction.BEGIN_LINE * (lineCap || 1), numVertices);
|
||||||
|
|
||||||
numVertices = this.addVertices_([0, 0], p1, p2,
|
numVertices = this.addVertices_([0, 0], p1, p2,
|
||||||
-lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE * (lineCap || 1), numVertices);
|
-lastSign * ol.render.webgl.lineStringInstruction.BEGIN_LINE * (lineCap || 1), numVertices);
|
||||||
|
|
||||||
lastIndex = n + 3;
|
lastIndex = n + 3;
|
||||||
|
|
||||||
@@ -1128,10 +1165,10 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
|
|||||||
p0 = p0 || [0, 0];
|
p0 = p0 || [0, 0];
|
||||||
|
|
||||||
numVertices = this.addVertices_(p0, p1, [0, 0],
|
numVertices = this.addVertices_(p0, p1, [0, 0],
|
||||||
lastSign * ol.render.webgl.LineStringInstruction.END_LINE * (lineCap || 1), numVertices);
|
lastSign * ol.render.webgl.lineStringInstruction.END_LINE * (lineCap || 1), numVertices);
|
||||||
|
|
||||||
numVertices = this.addVertices_(p0, p1, [0, 0],
|
numVertices = this.addVertices_(p0, p1, [0, 0],
|
||||||
-lastSign * ol.render.webgl.LineStringInstruction.END_LINE * (lineCap || 1), numVertices);
|
-lastSign * ol.render.webgl.lineStringInstruction.END_LINE * (lineCap || 1), numVertices);
|
||||||
|
|
||||||
this.indices_[numIndices++] = n;
|
this.indices_[numIndices++] = n;
|
||||||
this.indices_[numIndices++] = lastIndex - 1;
|
this.indices_[numIndices++] = lastIndex - 1;
|
||||||
@@ -1143,10 +1180,10 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
|
|||||||
|
|
||||||
if (lineCap) {
|
if (lineCap) {
|
||||||
numVertices = this.addVertices_(p0, p1, [0, 0],
|
numVertices = this.addVertices_(p0, p1, [0, 0],
|
||||||
lastSign * ol.render.webgl.LineStringInstruction.END_LINE_CAP * lineCap, numVertices);
|
lastSign * ol.render.webgl.lineStringInstruction.END_LINE_CAP * lineCap, numVertices);
|
||||||
|
|
||||||
numVertices = this.addVertices_(p0, p1, [0, 0],
|
numVertices = this.addVertices_(p0, p1, [0, 0],
|
||||||
-lastSign * ol.render.webgl.LineStringInstruction.END_LINE_CAP * lineCap, numVertices);
|
-lastSign * ol.render.webgl.lineStringInstruction.END_LINE_CAP * lineCap, numVertices);
|
||||||
|
|
||||||
this.indices_[numIndices++] = n + 2;
|
this.indices_[numIndices++] = n + 2;
|
||||||
this.indices_[numIndices++] = n;
|
this.indices_[numIndices++] = n;
|
||||||
@@ -1168,13 +1205,13 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
|
|||||||
? 1 : -1;
|
? 1 : -1;
|
||||||
|
|
||||||
numVertices = this.addVertices_(p0, p1, p2,
|
numVertices = this.addVertices_(p0, p1, p2,
|
||||||
sign * ol.render.webgl.LineStringInstruction.BEVEL_FIRST * (lineJoin || 1), numVertices);
|
sign * ol.render.webgl.lineStringInstruction.BEVEL_FIRST * (lineJoin || 1), numVertices);
|
||||||
|
|
||||||
numVertices = this.addVertices_(p0, p1, p2,
|
numVertices = this.addVertices_(p0, p1, p2,
|
||||||
sign * ol.render.webgl.LineStringInstruction.BEVEL_SECOND * (lineJoin || 1), numVertices);
|
sign * ol.render.webgl.lineStringInstruction.BEVEL_SECOND * (lineJoin || 1), numVertices);
|
||||||
|
|
||||||
numVertices = this.addVertices_(p0, p1, p2,
|
numVertices = this.addVertices_(p0, p1, p2,
|
||||||
-sign * ol.render.webgl.LineStringInstruction.MITER_BOTTOM * (lineJoin || 1), numVertices);
|
-sign * ol.render.webgl.lineStringInstruction.MITER_BOTTOM * (lineJoin || 1), numVertices);
|
||||||
|
|
||||||
if (i > offset) {
|
if (i > offset) {
|
||||||
this.indices_[numIndices++] = n;
|
this.indices_[numIndices++] = n;
|
||||||
@@ -1196,7 +1233,7 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
|
|||||||
//Add miter
|
//Add miter
|
||||||
if (lineJoin) {
|
if (lineJoin) {
|
||||||
numVertices = this.addVertices_(p0, p1, p2,
|
numVertices = this.addVertices_(p0, p1, p2,
|
||||||
sign * ol.render.webgl.LineStringInstruction.MITER_TOP * lineJoin, numVertices);
|
sign * ol.render.webgl.lineStringInstruction.MITER_TOP * lineJoin, numVertices);
|
||||||
|
|
||||||
this.indices_[numIndices++] = n + 1;
|
this.indices_[numIndices++] = n + 1;
|
||||||
this.indices_[numIndices++] = n + 3;
|
this.indices_[numIndices++] = n + 3;
|
||||||
@@ -1212,10 +1249,10 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
|
|||||||
? 1 : -1;
|
? 1 : -1;
|
||||||
|
|
||||||
numVertices = this.addVertices_(p0, p1, p2,
|
numVertices = this.addVertices_(p0, p1, p2,
|
||||||
sign * ol.render.webgl.LineStringInstruction.BEVEL_FIRST * (lineJoin || 1), numVertices);
|
sign * ol.render.webgl.lineStringInstruction.BEVEL_FIRST * (lineJoin || 1), numVertices);
|
||||||
|
|
||||||
numVertices = this.addVertices_(p0, p1, p2,
|
numVertices = this.addVertices_(p0, p1, p2,
|
||||||
-sign * ol.render.webgl.LineStringInstruction.MITER_BOTTOM * (lineJoin || 1), numVertices);
|
-sign * ol.render.webgl.lineStringInstruction.MITER_BOTTOM * (lineJoin || 1), numVertices);
|
||||||
|
|
||||||
this.indices_[numIndices++] = n;
|
this.indices_[numIndices++] = n;
|
||||||
this.indices_[numIndices++] = lastIndex - 1;
|
this.indices_[numIndices++] = lastIndex - 1;
|
||||||
@@ -1416,12 +1453,20 @@ ol.render.webgl.LineStringReplay.prototype.setUpProgram_ = function(gl, context,
|
|||||||
*/
|
*/
|
||||||
ol.render.webgl.LineStringReplay.prototype.drawReplay_ = function(gl, context, skippedFeaturesHash, hitDetection) {
|
ol.render.webgl.LineStringReplay.prototype.drawReplay_ = function(gl, context, skippedFeaturesHash, hitDetection) {
|
||||||
//Save GL parameters.
|
//Save GL parameters.
|
||||||
var tmpDepthFunc = gl.getParameter(gl.DEPTH_FUNC);
|
var tmpDepthFunc = /** @type {number} */ (gl.getParameter(gl.DEPTH_FUNC));
|
||||||
var tmpDepthMask = gl.getParameter(gl.DEPTH_WRITEMASK);
|
var tmpDepthMask = /** @type {boolean} */ (gl.getParameter(gl.DEPTH_WRITEMASK));
|
||||||
|
|
||||||
|
if (!hitDetection) {
|
||||||
|
gl.enable(gl.DEPTH_TEST);
|
||||||
|
gl.depthMask(true);
|
||||||
|
gl.depthFunc(gl.NOTEQUAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
var nextStyle;
|
||||||
|
//Initial styling
|
||||||
|
nextStyle = this.styles_[0];
|
||||||
|
this.setStrokeStyle_(gl, nextStyle[0], nextStyle[1], nextStyle[2]);
|
||||||
|
|
||||||
gl.enable(gl.DEPTH_TEST);
|
|
||||||
gl.depthMask(true);
|
|
||||||
gl.depthFunc(gl.NOTEQUAL);
|
|
||||||
if (!goog.object.isEmpty(skippedFeaturesHash)) {
|
if (!goog.object.isEmpty(skippedFeaturesHash)) {
|
||||||
// TODO: draw by blocks to skip features
|
// TODO: draw by blocks to skip features
|
||||||
} else {
|
} else {
|
||||||
@@ -1429,10 +1474,8 @@ ol.render.webgl.LineStringReplay.prototype.drawReplay_ = function(gl, context, s
|
|||||||
'number of styles and style indices match');
|
'number of styles and style indices match');
|
||||||
|
|
||||||
//Draw by style groups to minimize drawElements() calls.
|
//Draw by style groups to minimize drawElements() calls.
|
||||||
var i, ii, end, nextStyle;
|
var i, ii, end;
|
||||||
//Initial styling
|
|
||||||
nextStyle = this.styles_[0];
|
|
||||||
this.setStrokeStyle_(gl, nextStyle[0], nextStyle[1], nextStyle[2]);
|
|
||||||
for (i = 1, ii = this.styleIndices_.length; i < ii; ++i) {
|
for (i = 1, ii = this.styleIndices_.length; i < ii; ++i) {
|
||||||
end = this.styleIndices_[i];
|
end = this.styleIndices_[i];
|
||||||
this.drawElements_(gl, context, 0, end);
|
this.drawElements_(gl, context, 0, end);
|
||||||
@@ -1442,46 +1485,15 @@ ol.render.webgl.LineStringReplay.prototype.drawReplay_ = function(gl, context, s
|
|||||||
end = this.startIndices_[this.startIndices_.length - 1];
|
end = this.startIndices_[this.startIndices_.length - 1];
|
||||||
this.drawElements_(gl, context, 0, end);
|
this.drawElements_(gl, context, 0, end);
|
||||||
}
|
}
|
||||||
gl.clear(gl.DEPTH_BUFFER_BIT);
|
if (!hitDetection) {
|
||||||
gl.disable(gl.DEPTH_TEST);
|
gl.clear(gl.DEPTH_BUFFER_BIT);
|
||||||
//Restore GL parameters.
|
gl.disable(gl.DEPTH_TEST);
|
||||||
//NOTE: Type checks will be obsolete when Closure Compiler recognizes the return type of specific
|
//Restore GL parameters.
|
||||||
//gl.getParameter() calls.
|
|
||||||
if (typeof tmpDepthMask === 'boolean') {
|
|
||||||
gl.depthMask(tmpDepthMask);
|
gl.depthMask(tmpDepthMask);
|
||||||
}
|
|
||||||
if (typeof tmpDepthFunc === 'number') {
|
|
||||||
gl.depthFunc(tmpDepthFunc);
|
gl.depthFunc(tmpDepthFunc);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
* @param {WebGLRenderingContext} gl gl.
|
|
||||||
* @param {ol.webgl.Context} context Context.
|
|
||||||
* @param {Object.<string, boolean>} skippedFeaturesHash Ids of features
|
|
||||||
* to skip.
|
|
||||||
* @param {function((ol.Feature|ol.render.Feature)): T|undefined} featureCallback Feature callback.
|
|
||||||
* @param {boolean} oneByOne Draw features one-by-one for the hit-detecion.
|
|
||||||
* @param {ol.Extent=} opt_hitExtent Hit extent: Only features intersecting
|
|
||||||
* this extent are checked.
|
|
||||||
* @return {T|undefined} Callback result.
|
|
||||||
* @template T
|
|
||||||
*/
|
|
||||||
ol.render.webgl.LineStringReplay.prototype.drawHitDetectionReplay_ = function(gl, context, skippedFeaturesHash, featureCallback, oneByOne,
|
|
||||||
opt_hitExtent) {
|
|
||||||
//if (!oneByOne) {
|
|
||||||
// draw all hit-detection features in "once" (by texture group)
|
|
||||||
//return this.drawHitDetectionReplayAll_(gl, context,
|
|
||||||
// skippedFeaturesHash, featureCallback);
|
|
||||||
//} else {
|
|
||||||
// draw hit-detection features one by one
|
|
||||||
//return this.drawHitDetectionReplayOneByOne_(gl, context,
|
|
||||||
// skippedFeaturesHash, featureCallback, opt_hitExtent);
|
|
||||||
//}
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
|
|||||||
@@ -44,3 +44,18 @@ ol.render.webgl.defaultStrokeStyle = [0.0, 0.0, 0.0, 1.0];
|
|||||||
* @type {number}
|
* @type {number}
|
||||||
*/
|
*/
|
||||||
ol.render.webgl.defaultLineWidth = 1;
|
ol.render.webgl.defaultLineWidth = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @enum {number}
|
||||||
|
*/
|
||||||
|
ol.render.webgl.lineStringInstruction = {
|
||||||
|
ROUND: 2,
|
||||||
|
BEGIN_LINE: 3,
|
||||||
|
END_LINE: 5,
|
||||||
|
BEGIN_LINE_CAP: 7,
|
||||||
|
END_LINE_CAP : 11,
|
||||||
|
BEVEL_FIRST: 13,
|
||||||
|
BEVEL_SECOND: 17,
|
||||||
|
MITER_BOTTOM: 19,
|
||||||
|
MITER_TOP: 23
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user