Refactor to a more convenient internal API

This commit is contained in:
Andreas Hocevar
2016-06-22 23:41:00 +02:00
parent cf7ff841a7
commit 6b4ee42c90
34 changed files with 497 additions and 554 deletions
@@ -1,7 +1,7 @@
goog.provide('ol.renderer.webgl.ImageLayer');
goog.require('goog.asserts');
goog.require('ol.matrix');
goog.require('ol.transform');
goog.require('goog.webgl');
goog.require('ol.ImageBase');
goog.require('ol.ViewHint');
@@ -40,7 +40,7 @@ ol.renderer.webgl.ImageLayer = function(mapRenderer, imageLayer) {
/**
* @private
* @type {?ol.Matrix}
* @type {?ol.Transform}
*/
this.hitTransformationMatrix_ = null;
@@ -164,9 +164,9 @@ ol.renderer.webgl.ImageLayer.prototype.prepareFrame = function(frameState, layer
// Translate and scale to flip the Y coord.
var texCoordMatrix = this.texCoordMatrix;
ol.matrix.makeIdentity(texCoordMatrix);
ol.matrix.scale(texCoordMatrix, 1, -1);
ol.matrix.translate(texCoordMatrix, 0, -1);
ol.transform.reset(texCoordMatrix);
ol.transform.scale(texCoordMatrix, 1, -1);
ol.transform.translate(texCoordMatrix, 0, -1);
this.image_ = image;
this.texture = texture;
@@ -196,18 +196,18 @@ ol.renderer.webgl.ImageLayer.prototype.updateProjectionMatrix_ = function(canvas
var canvasExtentHeight = canvasHeight * viewResolution;
var projectionMatrix = this.projectionMatrix;
ol.matrix.makeIdentity(projectionMatrix);
ol.matrix.scale(projectionMatrix,
ol.transform.reset(projectionMatrix);
ol.transform.scale(projectionMatrix,
pixelRatio * 2 / canvasExtentWidth,
pixelRatio * 2 / canvasExtentHeight);
ol.matrix.rotate(projectionMatrix, -viewRotation);
ol.matrix.translate(projectionMatrix,
ol.transform.rotate(projectionMatrix, -viewRotation);
ol.transform.translate(projectionMatrix,
imageExtent[0] - viewCenter[0],
imageExtent[1] - viewCenter[1]);
ol.matrix.scale(projectionMatrix,
ol.transform.scale(projectionMatrix,
(imageExtent[2] - imageExtent[0]) / 2,
(imageExtent[3] - imageExtent[1]) / 2);
ol.matrix.translate(projectionMatrix, 1, 1);
ol.transform.translate(projectionMatrix, 1, 1);
};
@@ -233,9 +233,8 @@ ol.renderer.webgl.ImageLayer.prototype.forEachLayerAtPixel = function(pixel, fra
if (this.getLayer().getSource() instanceof ol.source.ImageVector) {
// for ImageVector sources use the original hit-detection logic,
// so that for example also transparent polygons are detected
var coordinate = pixel.slice();
ol.matrix.multVec2(
frameState.pixelToCoordinateMatrix, coordinate, coordinate);
var coordinate = ol.transform.apply(
frameState.pixelToCoordinateTransform, pixel.slice());
var hasFeature = this.forEachFeatureAtCoordinate(
coordinate, frameState, ol.functions.TRUE, this);
@@ -253,9 +252,8 @@ ol.renderer.webgl.ImageLayer.prototype.forEachLayerAtPixel = function(pixel, fra
frameState.size, imageSize);
}
var pixelOnFrameBuffer = [0, 0];
ol.matrix.multVec2(
this.hitTransformationMatrix_, pixel, pixelOnFrameBuffer);
var pixelOnFrameBuffer = ol.transform.apply(
this.hitTransformationMatrix_, pixel.slice());
if (pixelOnFrameBuffer[0] < 0 || pixelOnFrameBuffer[0] > imageSize[0] ||
pixelOnFrameBuffer[1] < 0 || pixelOnFrameBuffer[1] > imageSize[1]) {
@@ -286,35 +284,31 @@ ol.renderer.webgl.ImageLayer.prototype.forEachLayerAtPixel = function(pixel, fra
* pixel on the map.
* @param {ol.Size} mapSize The map size.
* @param {ol.Size} imageSize The image size.
* @return {ol.Matrix} The transformation matrix.
* @return {ol.Transform} The transformation matrix.
* @private
*/
ol.renderer.webgl.ImageLayer.prototype.getHitTransformationMatrix_ = function(mapSize, imageSize) {
// the first matrix takes a map pixel, flips the y-axis and scales to
// a range between -1 ... 1
var mapCoordMatrix = ol.matrix.create();
ol.matrix.translate(mapCoordMatrix, -1, -1);
ol.matrix.scale(mapCoordMatrix, 2 / mapSize[0], 2 / mapSize[1]);
ol.matrix.translate(mapCoordMatrix, 0, mapSize[1]);
ol.matrix.scale(mapCoordMatrix, 1, -1);
var mapCoordTransform = ol.transform.create();
ol.transform.translate(mapCoordTransform, -1, -1);
ol.transform.scale(mapCoordTransform, 2 / mapSize[0], 2 / mapSize[1]);
ol.transform.translate(mapCoordTransform, 0, mapSize[1]);
ol.transform.scale(mapCoordTransform, 1, -1);
// the second matrix is the inverse of the projection matrix used in the
// shader for drawing
var projectionMatrixInv = ol.matrix.create();
ol.matrix.invert(this.projectionMatrix, projectionMatrixInv);
var projectionMatrixInv = ol.transform.invert(this.projectionMatrix.slice());
// the third matrix scales to the image dimensions and flips the y-axis again
var imageCoordMatrix = ol.matrix.create();
ol.matrix.translate(imageCoordMatrix, 0, imageSize[1]);
ol.matrix.scale(imageCoordMatrix, 1, -1);
ol.matrix.scale(imageCoordMatrix, imageSize[0] / 2, imageSize[1] / 2);
ol.matrix.translate(imageCoordMatrix, 1, 1);
var transform = ol.transform.create();
ol.transform.translate(transform, 0, imageSize[1]);
ol.transform.scale(transform, 1, -1);
ol.transform.scale(transform, imageSize[0] / 2, imageSize[1] / 2);
ol.transform.translate(transform, 1, 1);
var transformMatrix = ol.matrix.create();
ol.matrix.multiply(
imageCoordMatrix, projectionMatrixInv, transformMatrix);
ol.matrix.multiply(
transformMatrix, mapCoordMatrix, transformMatrix);
ol.transform.multiply(transform, projectionMatrixInv);
ol.transform.multiply(transform, mapCoordTransform);
return transformMatrix;
return transform;
};
+7 -7
View File
@@ -2,7 +2,7 @@ goog.provide('ol.renderer.webgl.Layer');
goog.require('goog.webgl');
goog.require('ol.layer.Layer');
goog.require('ol.matrix');
goog.require('ol.transform');
goog.require('ol.render.Event');
goog.require('ol.render.EventType');
goog.require('ol.render.webgl.Immediate');
@@ -63,15 +63,15 @@ ol.renderer.webgl.Layer = function(mapRenderer, layer) {
/**
* @protected
* @type {ol.Matrix}
* @type {ol.Transform}
*/
this.texCoordMatrix = ol.matrix.create();
this.texCoordMatrix = ol.transform.create();
/**
* @protected
* @type {ol.Matrix}
* @type {ol.Transform}
*/
this.projectionMatrix = ol.matrix.create();
this.projectionMatrix = ol.transform.create();
/**
* @private
@@ -209,7 +209,7 @@ ol.renderer.webgl.Layer.prototype.dispatchComposeEvent_ = function(type, context
/**
* @return {!ol.Matrix} Matrix.
* @return {!ol.Transform} Matrix.
*/
ol.renderer.webgl.Layer.prototype.getTexCoordMatrix = function() {
return this.texCoordMatrix;
@@ -225,7 +225,7 @@ ol.renderer.webgl.Layer.prototype.getTexture = function() {
/**
* @return {!ol.Matrix} Matrix.
* @return {!ol.Transform} Matrix.
*/
ol.renderer.webgl.Layer.prototype.getProjectionMatrix = function() {
return this.projectionMatrix;
@@ -4,7 +4,7 @@
goog.provide('ol.renderer.webgl.TileLayer');
goog.require('goog.asserts');
goog.require('ol.matrix');
goog.require('ol.transform');
goog.require('goog.webgl');
goog.require('ol.TileRange');
goog.require('ol.TileState');
@@ -351,21 +351,21 @@ ol.renderer.webgl.TileLayer.prototype.prepareFrame = function(frameState, layerS
this.updateLogos(frameState, tileSource);
var texCoordMatrix = this.texCoordMatrix;
ol.matrix.makeIdentity(texCoordMatrix);
ol.matrix.translate(texCoordMatrix,
ol.transform.reset(texCoordMatrix);
ol.transform.translate(texCoordMatrix,
(center[0] - framebufferExtent[0]) /
(framebufferExtent[2] - framebufferExtent[0]),
(center[1] - framebufferExtent[1]) /
(framebufferExtent[3] - framebufferExtent[1]));
if (viewState.rotation !== 0) {
ol.matrix.rotate(texCoordMatrix, viewState.rotation);
ol.transform.rotate(texCoordMatrix, viewState.rotation);
}
ol.matrix.scale(texCoordMatrix,
ol.transform.scale(texCoordMatrix,
frameState.size[0] * viewState.resolution /
(framebufferExtent[2] - framebufferExtent[0]),
frameState.size[1] * viewState.resolution /
(framebufferExtent[3] - framebufferExtent[1]));
ol.matrix.translate(texCoordMatrix, -0.5, -0.5);
ol.transform.translate(texCoordMatrix, -0.5, -0.5);
return true;
};
@@ -383,9 +383,8 @@ ol.renderer.webgl.TileLayer.prototype.forEachLayerAtPixel = function(pixel, fram
pixel[0] / frameState.size[0],
(frameState.size[1] - pixel[1]) / frameState.size[1]];
var pixelOnFrameBufferScaled = [0, 0];
ol.matrix.multVec2(
this.texCoordMatrix, pixelOnMapScaled, pixelOnFrameBufferScaled);
var pixelOnFrameBufferScaled = ol.transform.apply(
this.texCoordMatrix, pixelOnMapScaled.slice());
var pixelOnFrameBuffer = [
pixelOnFrameBufferScaled[0] * this.framebufferDimension,
pixelOnFrameBufferScaled[1] * this.framebufferDimension];
@@ -5,7 +5,7 @@ goog.require('ol.events');
goog.require('ol.ViewHint');
goog.require('ol.extent');
goog.require('ol.layer.Vector');
goog.require('ol.matrix');
goog.require('ol.transform');
goog.require('ol.render.webgl.ReplayGroup');
goog.require('ol.renderer.vector');
goog.require('ol.renderer.webgl.Layer');
@@ -154,9 +154,8 @@ ol.renderer.webgl.VectorLayer.prototype.hasFeatureAtCoordinate = function(coordi
* @inheritDoc
*/
ol.renderer.webgl.VectorLayer.prototype.forEachLayerAtPixel = function(pixel, frameState, callback, thisArg) {
var coordinate = pixel.slice();
ol.matrix.multVec2(
frameState.pixelToCoordinateMatrix, coordinate, coordinate);
var coordinate = ol.transform.apply(
frameState.pixelToCoordinateTransform, pixel.slice());
var hasFeature = this.hasFeatureAtCoordinate(coordinate, frameState);
if (hasFeature) {