From 9764e76975e1cf1ea7065255f24c391871ee4b97 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sat, 12 Jan 2013 23:56:13 +0100 Subject: [PATCH] Move pixel/coordinate matrices into frame state, remove canRotate --- src/ol/framestate.js | 3 + src/ol/interaction/dragpaninteraction.js | 4 +- .../dragrotateandzoominteraction.js | 2 +- src/ol/interaction/dragrotateinteraction.js | 3 +- src/ol/interaction/dragzoominteraction.js | 8 +- src/ol/map.js | 42 ++++-- src/ol/renderer/dom/dommaprenderer.js | 10 +- src/ol/renderer/maprenderer.js | 122 +++++------------- src/ol/renderer/webgl/webglmaprenderer.js | 10 +- 9 files changed, 71 insertions(+), 133 deletions(-) diff --git a/src/ol/framestate.js b/src/ol/framestate.js index d3d0f5ffa7..d84f4de831 100644 --- a/src/ol/framestate.js +++ b/src/ol/framestate.js @@ -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., * layerStates: Object., + * pixelToCoordinateMatrix: goog.vec.Mat4.Number, * postRenderFunctions: Array., * size: ol.Size, * tileQueue: ol.TileQueue, diff --git a/src/ol/interaction/dragpaninteraction.js b/src/ol/interaction/dragpaninteraction.js index 494d953912..3fcc4d4157 100644 --- a/src/ol/interaction/dragpaninteraction.js +++ b/src/ol/interaction/dragpaninteraction.js @@ -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); - } + delta.rotate(rotation); var newCenter = new ol.Coordinate( this.startCenter.x + delta.x, this.startCenter.y + delta.y); map.requestRenderFrame(); diff --git a/src/ol/interaction/dragrotateandzoominteraction.js b/src/ol/interaction/dragrotateandzoominteraction.js index 0615f25b56..314ee9227f 100644 --- a/src/ol/interaction/dragrotateandzoominteraction.js +++ b/src/ol/interaction/dragrotateandzoominteraction.js @@ -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( diff --git a/src/ol/interaction/dragrotateinteraction.js b/src/ol/interaction/dragrotateinteraction.js index f1aef8630e..2b6e12bbbb 100644 --- a/src/ol/interaction/dragrotateinteraction.js +++ b/src/ol/interaction/dragrotateinteraction.js @@ -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(); diff --git a/src/ol/interaction/dragzoominteraction.js b/src/ol/interaction/dragzoominteraction.js index 22d94334eb..a64437acda 100644 --- a/src/ol/interaction/dragzoominteraction.js +++ b/src/ol/interaction/dragzoominteraction.js @@ -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 - view.setRotation(0); - } + // FIXME we should preserve rotation + view.setRotation(0); }); } }; diff --git a/src/ol/map.js b/src/ol/map.js index 300a09b015..8b29f45a73 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -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_, diff --git a/src/ol/renderer/dom/dommaprenderer.js b/src/ol/renderer/dom/dommaprenderer.js index 8b41096228..ec9590ff5a 100644 --- a/src/ol/renderer/dom/dommaprenderer.js +++ b/src/ol/renderer/dom/dommaprenderer.js @@ -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); + }; diff --git a/src/ol/renderer/maprenderer.js b/src/ol/renderer/maprenderer.js index 38b4af0b0a..922ee1fa13 100644 --- a/src/ol/renderer/maprenderer.js +++ b/src/ol/renderer/maprenderer.js @@ -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.} @@ -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; - - } - -}; diff --git a/src/ol/renderer/webgl/webglmaprenderer.js b/src/ol/renderer/webgl/webglmaprenderer.js index 569a9179c0..28493f1c3a 100644 --- a/src/ol/renderer/webgl/webglmaprenderer.js +++ b/src/ol/renderer/webgl/webglmaprenderer.js @@ -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); + };