Move pixel/coordinate matrices into frame state, remove canRotate

This commit is contained in:
Tom Payne
2013-01-12 23:56:13 +01:00
parent a927385e72
commit 9764e76975
9 changed files with 71 additions and 133 deletions

View File

@@ -4,6 +4,7 @@ goog.provide('ol.FrameState');
goog.provide('ol.PostRenderFunction');
goog.provide('ol.PreRenderFunction');
goog.require('goog.vec.Mat4');
goog.require('ol.Color');
goog.require('ol.Coordinate');
goog.require('ol.Extent');
@@ -16,9 +17,11 @@ goog.require('ol.layer.LayerState');
/**
* @typedef {{animate: boolean,
* backgroundColor: ol.Color,
* coordinateToPixelMatrix: goog.vec.Mat4.Number,
* extent: (null|ol.Extent),
* layersArray: Array.<ol.layer.Layer>,
* layerStates: Object.<number, ol.layer.LayerState>,
* pixelToCoordinateMatrix: goog.vec.Mat4.Number,
* postRenderFunctions: Array.<ol.PostRenderFunction>,
* size: ol.Size,
* tileQueue: ol.TileQueue,

View File

@@ -43,9 +43,7 @@ ol.interaction.DragPan.prototype.handleDrag = function(mapBrowserEvent) {
var rotation = view.getRotation();
var delta =
new ol.Coordinate(-resolution * this.deltaX, resolution * this.deltaY);
if (map.canRotate() && goog.isDef(rotation)) {
delta.rotate(rotation);
}
var newCenter = new ol.Coordinate(
this.startCenter.x + delta.x, this.startCenter.y + delta.y);
map.requestRenderFrame();

View File

@@ -73,7 +73,7 @@ ol.interaction.DragRotateAndZoom.prototype.handleDragStart =
var browserEvent = mapBrowserEvent.browserEvent;
var map = mapBrowserEvent.map;
var view = map.getView().getView2D();
if (map.canRotate() && this.condition_(browserEvent)) {
if (this.condition_(browserEvent)) {
var resolution = view.getResolution();
var size = map.getSize();
var delta = new goog.math.Vec2(

View File

@@ -61,8 +61,7 @@ ol.interaction.DragRotate.prototype.handleDragStart =
// FIXME supports View2D only
var view = map.getView();
goog.asserts.assert(view instanceof ol.View2D);
if (browserEvent.isMouseActionButton() && this.condition_(browserEvent) &&
map.canRotate()) {
if (browserEvent.isMouseActionButton() && this.condition_(browserEvent)) {
map.requestRenderFrame();
var size = map.getSize();
var offset = mapBrowserEvent.getPixel();

View File

@@ -70,12 +70,8 @@ ol.interaction.DragZoom.prototype.handleDragEnd =
goog.asserts.assert(view instanceof ol.View2D);
var mapSize = /** @type {ol.Size} */ (map.getSize());
view.fitExtent(extent, mapSize);
if (map.canRotate()) {
// FIXME we don't set the rotation if the map doesn't
// support rotation, this will prevent any map using
// that view from rotating, which may not be desired
// FIXME we should preserve rotation
view.setRotation(0);
}
});
}
};

View File

@@ -129,6 +129,18 @@ ol.Map = function(mapOptions) {
new goog.async.AnimationDelay(this.renderFrame_, undefined, this);
this.registerDisposable(this.animationDelay_);
/**
* @private
* @type {goog.vec.Mat4.Number}
*/
this.coordinateToPixelMatrix_ = goog.vec.Mat4.createNumber();
/**
* @private
* @type {goog.vec.Mat4.Number}
*/
this.pixelToCoordinateMatrix_ = goog.vec.Mat4.createNumber();
/**
* @private
* @type {?ol.FrameState}
@@ -292,14 +304,6 @@ ol.Map.prototype.addPreRenderFunctions = function(preRenderFunctions) {
};
/**
* @return {boolean} Can rotate.
*/
ol.Map.prototype.canRotate = function() {
return this.renderer_.canRotate();
};
/**
*
* @inheritDoc
@@ -352,7 +356,14 @@ ol.Map.prototype.getControls = function() {
* @return {ol.Coordinate} Coordinate.
*/
ol.Map.prototype.getCoordinateFromPixel = function(pixel) {
return this.isDef() ? this.renderer_.getCoordinateFromPixel(pixel) : null;
var frameState = this.frameState_;
if (goog.isNull(frameState)) {
return null;
} else {
var vec3 = [pixel.x, pixel.y, 0];
goog.vec.Mat4.multVec3(frameState.pixelToCoordinateMatrix, vec3, vec3);
return new ol.Coordinate(vec3[0], vec3[1]);
}
};
@@ -374,13 +385,16 @@ ol.Map.prototype.getLayers = function() {
/**
* @param {ol.Coordinate} coordinate Coordinate.
* @return {ol.Pixel|undefined} Pixel.
* @return {ol.Pixel} Pixel.
*/
ol.Map.prototype.getPixelFromCoordinate = function(coordinate) {
if (this.isDef()) {
return this.renderer_.getPixelFromCoordinate(coordinate);
var frameState = this.frameState_;
if (goog.isNull(frameState)) {
return null;
} else {
return undefined;
var vec3 = [coordinate.x, coordinate.y, 0];
goog.vec.Mat4.multVec3(frameState.coordinateToPixelMatrix, vec3, vec3);
return new ol.Pixel(vec3[0], vec3[1]);
}
};
@@ -595,9 +609,11 @@ ol.Map.prototype.renderFrame_ = function(time) {
animate: false,
backgroundColor: goog.isDef(backgroundColor) ?
backgroundColor : new ol.Color(1, 1, 1, 1),
coordinateToPixelMatrix: this.coordinateToPixelMatrix_,
extent: null,
layersArray: layersArray,
layerStates: layerStates,
pixelToCoordinateMatrix: this.pixelToCoordinateMatrix_,
postRenderFunctions: [],
size: size,
tileQueue: this.tileQueue_,

View File

@@ -48,12 +48,6 @@ ol.renderer.dom.Map = function(container, map) {
goog.inherits(ol.renderer.dom.Map, ol.renderer.Map);
/**
* @inheritDoc
*/
ol.renderer.dom.Map.prototype.canRotate = goog.functions.TRUE;
/**
* @inheritDoc
*/
@@ -73,7 +67,6 @@ ol.renderer.dom.Map.prototype.createLayerRenderer = function(layer) {
* @inheritDoc
*/
ol.renderer.dom.Map.prototype.handleViewPropertyChanged = function() {
goog.base(this, 'handleViewPropertyChanged');
this.getMap().render();
};
@@ -82,7 +75,6 @@ ol.renderer.dom.Map.prototype.handleViewPropertyChanged = function() {
* @inheritDoc
*/
ol.renderer.dom.Map.prototype.handleSizeChanged = function() {
goog.base(this, 'handleSizeChanged');
this.getMap().render();
};
@@ -123,4 +115,6 @@ ol.renderer.dom.Map.prototype.renderFrame = function(frameState) {
this.renderedVisible_ = true;
}
this.calculateMatrices2D(frameState);
};

View File

@@ -54,24 +54,6 @@ ol.renderer.Map = function(container, map) {
*/
this.viewPropertyListenerKey_ = null;
/**
* @private
* @type {goog.vec.Mat4.Number}
*/
this.coordinateToPixelMatrix_ = goog.vec.Mat4.createNumber();
/**
* @private
* @type {goog.vec.Mat4.Number}
*/
this.pixelToCoordinateMatrix_ = goog.vec.Mat4.createNumber();
/**
* @private
* @type {boolean}
*/
this.matricesDirty_ = true;
/**
* @private
* @type {Array.<number>}
@@ -109,9 +91,35 @@ ol.renderer.Map.prototype.addLayer = function(layer) {
/**
* @return {boolean} Can rotate.
* @param {ol.FrameState} frameState FrameState.
* @protected
*/
ol.renderer.Map.prototype.canRotate = goog.functions.FALSE;
ol.renderer.Map.prototype.calculateMatrices2D = function(frameState) {
var view2DState = frameState.view2DState;
var coordinateToPixelMatrix = frameState.coordinateToPixelMatrix;
goog.vec.Mat4.makeIdentity(coordinateToPixelMatrix);
goog.vec.Mat4.translate(coordinateToPixelMatrix,
frameState.size.width / 2,
frameState.size.height / 2,
0);
goog.vec.Mat4.scale(coordinateToPixelMatrix,
1 / view2DState.resolution,
-1 / view2DState.resolution,
1);
goog.vec.Mat4.rotateZ(coordinateToPixelMatrix,
-view2DState.rotation);
goog.vec.Mat4.translate(coordinateToPixelMatrix,
-view2DState.center.x,
-view2DState.center.y,
0);
var inverted = goog.vec.Mat4.invert(
coordinateToPixelMatrix, frameState.pixelToCoordinateMatrix);
goog.asserts.assert(inverted);
};
/**
@@ -140,18 +148,6 @@ ol.renderer.Map.prototype.disposeInternal = function() {
};
/**
* @param {ol.Pixel} pixel Pixel.
* @return {ol.Coordinate} Coordinate.
*/
ol.renderer.Map.prototype.getCoordinateFromPixel = function(pixel) {
this.updateMatrices_();
var vec3 = [pixel.x, pixel.y, 0];
goog.vec.Mat4.multVec3(this.pixelToCoordinateMatrix_, vec3, vec3);
return new ol.Coordinate(vec3[0], vec3[1]);
};
/**
* @param {ol.layer.Layer} layer Layer.
* @protected
@@ -173,18 +169,6 @@ ol.renderer.Map.prototype.getMap = function() {
};
/**
* @param {ol.Coordinate} coordinate Coordinate.
* @return {ol.Pixel} Pixel.
*/
ol.renderer.Map.prototype.getPixelFromCoordinate = function(coordinate) {
this.updateMatrices_();
var vec3 = [coordinate.x, coordinate.y, 0];
goog.vec.Mat4.multVec3(this.coordinateToPixelMatrix_, vec3, vec3);
return new ol.Pixel(vec3[0], vec3[1]);
};
/**
* Handle background color changed.
*/
@@ -237,17 +221,13 @@ ol.renderer.Map.prototype.handleLayersRemove = function(collectionEvent) {
/**
* @protected
*/
ol.renderer.Map.prototype.handleViewPropertyChanged = function() {
this.matricesDirty_ = true;
};
ol.renderer.Map.prototype.handleViewPropertyChanged = goog.nullFunction;
/**
* @protected
*/
ol.renderer.Map.prototype.handleSizeChanged = function() {
this.matricesDirty_ = true;
};
ol.renderer.Map.prototype.handleSizeChanged = goog.nullFunction;
/**
@@ -310,45 +290,3 @@ ol.renderer.Map.prototype.setLayerRenderer = function(layer, layerRenderer) {
goog.asserts.assert(!(key in this.layerRenderers));
this.layerRenderers[key] = layerRenderer;
};
/**
* @private
*/
ol.renderer.Map.prototype.updateMatrices_ = function() {
if (this.matricesDirty_) {
var map = this.map;
var view = map.getView().getView2D();
var center = /** @type {!ol.Coordinate} */ (view.getCenter());
var resolution = /** @type {number} */ (view.getResolution());
var rotation = view.getRotation();
var size = /** @type {!ol.Size} */ (map.getSize());
goog.vec.Mat4.makeIdentity(this.coordinateToPixelMatrix_);
goog.vec.Mat4.translate(this.coordinateToPixelMatrix_,
size.width / 2,
size.height / 2,
0);
goog.vec.Mat4.scale(this.coordinateToPixelMatrix_,
1 / resolution,
-1 / resolution,
1);
if (this.canRotate() && goog.isDef(rotation)) {
goog.vec.Mat4.rotateZ(this.coordinateToPixelMatrix_, -rotation);
}
goog.vec.Mat4.translate(this.coordinateToPixelMatrix_,
-center.x,
-center.y,
0);
var inverted = goog.vec.Mat4.invert(
this.coordinateToPixelMatrix_, this.pixelToCoordinateMatrix_);
goog.asserts.assert(inverted);
this.matricesDirty_ = false;
}
};

View File

@@ -261,12 +261,6 @@ ol.renderer.webgl.Map.prototype.bindTileTexture =
};
/**
* @inheritDoc
*/
ol.renderer.webgl.Map.prototype.canRotate = goog.functions.TRUE;
/**
* @inheritDoc
*/
@@ -379,7 +373,6 @@ ol.renderer.webgl.Map.prototype.handleBackgroundColorChanged = function() {
* @inheritDoc
*/
ol.renderer.webgl.Map.prototype.handleViewPropertyChanged = function() {
goog.base(this, 'handleViewPropertyChanged');
this.getMap().render();
};
@@ -397,7 +390,6 @@ ol.renderer.webgl.Map.prototype.handleLayerRendererChange = function(event) {
* @inheritDoc
*/
ol.renderer.webgl.Map.prototype.handleSizeChanged = function() {
goog.base(this, 'handleSizeChanged');
this.getMap().render();
};
@@ -584,6 +576,8 @@ ol.renderer.webgl.Map.prototype.renderFrame = function(frameState) {
this.renderedVisible_ = true;
}
this.calculateMatrices2D(frameState);
};