Minor improvements

ol.render.webgl.LineStringReplay.startIndices_ is initialized with a 0, and the end index of a valid line is added in every valid iteration.

drawElements_ is refactored for a clearer code.
This commit is contained in:
GaborFarkas
2016-06-17 11:20:56 +02:00
parent e2ec6d0b82
commit 99e4009b19

View File

@@ -188,7 +188,7 @@ ol.render.webgl.Replay.prototype.replay = function(context,
context.bindBuffer(goog.webgl.ARRAY_BUFFER, this.verticesBuffer_); context.bindBuffer(goog.webgl.ARRAY_BUFFER, this.verticesBuffer_);
// bind the indices buffer // bind the indices buffer
goog.asserts.assert(!goog.isNull(this.indicesBuffer_), goog.asserts.assert(this.indicesBuffer_,
'indicesBuffer must not be null'); 'indicesBuffer must not be null');
context.bindBuffer(goog.webgl.ELEMENT_ARRAY_BUFFER, this.indicesBuffer_); context.bindBuffer(goog.webgl.ELEMENT_ARRAY_BUFFER, this.indicesBuffer_);
@@ -240,16 +240,19 @@ ol.render.webgl.Replay.prototype.replay = function(context,
/** /**
* @private * @private
* @param {WebGLRenderingContext} gl gl. * @param {WebGLRenderingContext} gl gl.
* @param {ol.webgl.Context} context Context.
* @param {number} start Start index. * @param {number} start Start index.
* @param {number} end End index. * @param {number} end End index.
* @param {number} elementType Element type.
* @param {number} elementSize Element Size.
*/ */
ol.render.webgl.Replay.prototype.drawElements_ = function( ol.render.webgl.Replay.prototype.drawElements_ = function(
gl, start, end, elementType, elementSize) { gl, context, start, end) {
var elementType = context.hasOESElementIndexUint ?
ol.webgl.UNSIGNED_INT : ol.webgl.UNSIGNED_SHORT;
var elementSize = context.hasOESElementIndexUint ? 4 : 2;
var numItems = end - start; var numItems = end - start;
var offsetInBytes = start * elementSize; var offsetInBytes = start * elementSize;
gl.drawElements(goog.webgl.TRIANGLES, numItems, elementType, offsetInBytes); gl.drawElements(ol.webgl.TRIANGLES, numItems, elementType, offsetInBytes);
}; };
/** /**
@@ -710,20 +713,16 @@ ol.render.webgl.ImageReplay.prototype.drawReplay_ = function(gl, context, skippe
var groupIndices = hitDetection ? this.hitDetectionGroupIndices_ : this.groupIndices_; var groupIndices = hitDetection ? this.hitDetectionGroupIndices_ : this.groupIndices_;
ol.DEBUG && console.assert(textures.length === groupIndices.length, ol.DEBUG && console.assert(textures.length === groupIndices.length,
'number of textures and groupIndeces match'); 'number of textures and groupIndeces match');
var elementType = context.hasOESElementIndexUint ?
ol.webgl.UNSIGNED_INT : ol.webgl.UNSIGNED_SHORT;
var elementSize = context.hasOESElementIndexUint ? 4 : 2;
if (!ol.obj.isEmpty(skippedFeaturesHash)) { if (!ol.obj.isEmpty(skippedFeaturesHash)) {
this.drawReplaySkipping_( this.drawReplaySkipping_(
gl, skippedFeaturesHash, textures, groupIndices, gl, context, skippedFeaturesHash, textures, groupIndices);
elementType, elementSize);
} else { } else {
var i, ii, start; var i, ii, start;
for (i = 0, ii = textures.length, start = 0; i < ii; ++i) { for (i = 0, ii = textures.length, start = 0; i < ii; ++i) {
gl.bindTexture(ol.webgl.TEXTURE_2D, textures[i]); gl.bindTexture(ol.webgl.TEXTURE_2D, textures[i]);
var end = groupIndices[i]; var end = groupIndices[i];
this.drawElements_(gl, start, end, elementType, elementSize); this.drawElements_(gl, context, start, end);
start = end; start = end;
} }
} }
@@ -750,15 +749,14 @@ ol.render.webgl.ImageReplay.prototype.drawReplay_ = function(gl, context, skippe
* *
* @private * @private
* @param {WebGLRenderingContext} gl gl. * @param {WebGLRenderingContext} gl gl.
* @param {ol.webgl.Context} context Context.
* @param {Object.<string, boolean>} skippedFeaturesHash Ids of features * @param {Object.<string, boolean>} skippedFeaturesHash Ids of features
* to skip. * to skip.
* @param {Array.<WebGLTexture>} textures Textures. * @param {Array.<WebGLTexture>} textures Textures.
* @param {Array.<number>} groupIndices Texture group indices. * @param {Array.<number>} groupIndices Texture group indices.
* @param {number} elementType Element type.
* @param {number} elementSize Element Size.
*/ */
ol.render.webgl.ImageReplay.prototype.drawReplaySkipping_ = function(gl, skippedFeaturesHash, textures, groupIndices, ol.render.webgl.ImageReplay.prototype.drawReplaySkipping_ = function(gl, context, skippedFeaturesHash, textures,
elementType, elementSize) { groupIndices) {
var featureIndex = 0; var featureIndex = 0;
var i, ii; var i, ii;
@@ -778,7 +776,7 @@ ol.render.webgl.ImageReplay.prototype.drawReplaySkipping_ = function(gl, skipped
// feature should be skipped // feature should be skipped
if (start !== end) { if (start !== end) {
// draw the features so far // draw the features so far
this.drawElements_(gl, start, end, elementType, elementSize); this.drawElements_(gl, context, start, end);
} }
// continue with the next feature // continue with the next feature
start = (featureIndex === this.startIndices_.length - 1) ? start = (featureIndex === this.startIndices_.length - 1) ?
@@ -795,7 +793,7 @@ ol.render.webgl.ImageReplay.prototype.drawReplaySkipping_ = function(gl, skipped
if (start !== end) { if (start !== end) {
// draw the remaining features (in case there was no skipped feature // draw the remaining features (in case there was no skipped feature
// in this texture group, all features of a group are drawn together) // in this texture group, all features of a group are drawn together)
this.drawElements_(gl, start, end, elementType, elementSize); this.drawElements_(gl, context, start, end);
} }
} }
}; };
@@ -868,9 +866,6 @@ ol.render.webgl.ImageReplay.prototype.drawHitDetectionReplayOneByOne_ = function
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');
var elementType = context.hasOESElementIndexUint ?
ol.webgl.UNSIGNED_INT : ol.webgl.UNSIGNED_SHORT;
var elementSize = context.hasOESElementIndexUint ? 4 : 2;
var i, groupStart, start, end, feature, featureUid; var i, groupStart, start, end, feature, featureUid;
var featureIndex = this.startIndices_.length - 1; var featureIndex = this.startIndices_.length - 1;
@@ -892,7 +887,7 @@ ol.render.webgl.ImageReplay.prototype.drawHitDetectionReplayOneByOne_ = function
/** @type {Array<number>} */ (opt_hitExtent), /** @type {Array<number>} */ (opt_hitExtent),
feature.getGeometry().getExtent()))) { feature.getGeometry().getExtent()))) {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
this.drawElements_(gl, start, end, elementType, elementSize); this.drawElements_(gl, context, start, end);
var result = featureCallback(feature); var result = featureCallback(feature);
if (result) { if (result) {
@@ -1023,6 +1018,8 @@ ol.render.webgl.LineStringReplay = function(tolerance, maxExtent) {
miterLimit: undefined miterLimit: undefined
}; };
this.startIndices_ = [0];
}; };
ol.inherits(ol.render.webgl.LineStringReplay, ol.render.webgl.Replay); ol.inherits(ol.render.webgl.LineStringReplay, ol.render.webgl.Replay);
@@ -1260,10 +1257,10 @@ ol.render.webgl.LineStringReplay.prototype.drawLineString = function(lineStringG
var flatCoordinates = lineStringGeometry.getFlatCoordinates(); var flatCoordinates = lineStringGeometry.getFlatCoordinates();
var stride = lineStringGeometry.getStride(); var stride = lineStringGeometry.getStride();
if (flatCoordinates.length > stride) { if (flatCoordinates.length > stride) {
this.startIndices_.push(this.indices_.length);
this.startIndicesFeature_.push(feature);
this.drawCoordinates_( this.drawCoordinates_(
flatCoordinates, 0, flatCoordinates.length, stride); flatCoordinates, 0, flatCoordinates.length, stride);
this.startIndices_.push(this.indices_.length);
this.startIndicesFeature_.push(feature);
} }
}; };
@@ -1272,20 +1269,21 @@ ol.render.webgl.LineStringReplay.prototype.drawLineString = function(lineStringG
* @inheritDoc * @inheritDoc
*/ */
ol.render.webgl.LineStringReplay.prototype.drawMultiLineString = function(multiLineStringGeometry, feature) { ol.render.webgl.LineStringReplay.prototype.drawMultiLineString = function(multiLineStringGeometry, feature) {
this.startIndices_.push(this.indices_.length); var indexCount = this.indices_.length;
this.startIndicesFeature_.push(feature);
var lineStringGeometries = multiLineStringGeometry.getLineStrings(); var lineStringGeometries = multiLineStringGeometry.getLineStrings();
var i, ii; var i, ii;
for (i = 0, ii = lineStringGeometries.length; i < ii; ++i) { for (i = 0, ii = lineStringGeometries.length; i < ii; ++i) {
var flatCoordinates = lineStringGeometries[i].getFlatCoordinates(); var flatCoordinates = lineStringGeometries[i].getFlatCoordinates();
var stride = lineStringGeometries[i].getStride(); var stride = lineStringGeometries[i].getStride();
if (flatCoordinates.length > stride) { if (flatCoordinates.length > stride) {
this.startIndices_.push(this.indices_.length);
this.startIndicesFeature_.push(feature);
this.drawCoordinates_( this.drawCoordinates_(
flatCoordinates, 0, flatCoordinates.length, stride); flatCoordinates, 0, flatCoordinates.length, stride);
} }
} }
if (this.indices_.length > indexCount) {
this.startIndices_.push(this.indices_.length);
this.startIndicesFeature_.push(feature);
}
}; };
@@ -1293,8 +1291,6 @@ ol.render.webgl.LineStringReplay.prototype.drawMultiLineString = function(multiL
* @param {ol.webgl.Context} context Context. * @param {ol.webgl.Context} context Context.
**/ **/
ol.render.webgl.LineStringReplay.prototype.finish = function(context) { ol.render.webgl.LineStringReplay.prototype.finish = function(context) {
this.startIndices_.push(this.indices_.length);
// create, bind, and populate the vertices buffer // create, bind, and populate the vertices buffer
this.verticesBuffer_ = new ol.webgl.Buffer(this.vertices_); this.verticesBuffer_ = new ol.webgl.Buffer(this.vertices_);
context.bindBuffer(goog.webgl.ARRAY_BUFFER, this.verticesBuffer_); context.bindBuffer(goog.webgl.ARRAY_BUFFER, this.verticesBuffer_);
@@ -1395,15 +1391,11 @@ ol.render.webgl.LineStringReplay.prototype.setUpProgram_ = function(gl, context,
* @param {boolean} hitDetection Hit detection mode. * @param {boolean} hitDetection Hit detection mode.
*/ */
ol.render.webgl.LineStringReplay.prototype.drawReplay_ = function(gl, context, skippedFeaturesHash, hitDetection) { ol.render.webgl.LineStringReplay.prototype.drawReplay_ = function(gl, context, skippedFeaturesHash, hitDetection) {
var elementType = context.hasOESElementIndexUint ?
goog.webgl.UNSIGNED_INT : goog.webgl.UNSIGNED_SHORT;
var elementSize = context.hasOESElementIndexUint ? 4 : 2;
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 {
var end = this.startIndices_[this.startIndices_.length - 1]; var end = this.startIndices_[this.startIndices_.length - 1];
this.drawElements_(gl, 0, end, elementType, elementSize); this.drawElements_(gl, context, 0, end);
} }
}; };