From 582a9e93e01479d92c5558ab6a357ddf58862d52 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sun, 15 Jul 2012 16:32:00 +0200 Subject: [PATCH] Snapshot WebGL renderer --- src/ol/webgl/arraybuffer.js | 47 ------------ src/ol/webgl/framebuffer.js | 118 ------------------------------ src/ol/webgl/layerrenderer.js | 5 ++ src/ol/webgl/map.js | 24 +++++- src/ol/webgl/staticglobject.js | 49 ------------- src/ol/webgl/texture.js | 63 ---------------- src/ol/webgl/tilelayerrenderer.js | 58 +++++++++++---- src/ol/webgl/uniform.js | 101 ------------------------- src/ol/webgl/vertexattrib.js | 78 -------------------- 9 files changed, 70 insertions(+), 473 deletions(-) delete mode 100644 src/ol/webgl/arraybuffer.js delete mode 100644 src/ol/webgl/framebuffer.js delete mode 100644 src/ol/webgl/staticglobject.js delete mode 100644 src/ol/webgl/texture.js delete mode 100644 src/ol/webgl/uniform.js delete mode 100644 src/ol/webgl/vertexattrib.js diff --git a/src/ol/webgl/arraybuffer.js b/src/ol/webgl/arraybuffer.js deleted file mode 100644 index 6f56029452..0000000000 --- a/src/ol/webgl/arraybuffer.js +++ /dev/null @@ -1,47 +0,0 @@ -goog.provide('ol.webgl.ArrayBuffer'); - -goog.require('goog.webgl'); -goog.require('ol.webgl.StaticGLObject'); - - - -/** - * @constructor - * @extends {ol.webgl.StaticGLObject} - * @param {WebGLRenderingContext} gl GL. - * @param {ArrayBuffer|ArrayBufferView|null|number} data Data. - * @param {number} usage Usage. - */ -ol.webgl.ArrayBuffer = function(gl, data, usage) { - - goog.base(this, gl); - - /** - * @private - * @type {WebGLBuffer} - */ - this.buffer_ = gl.createBuffer(); - gl.bindBuffer(goog.webgl.ARRAY_BUFFER, this.buffer_); - gl.bufferData(goog.webgl.ARRAY_BUFFER, data, usage); - -}; -goog.inherits(ol.webgl.ArrayBuffer, ol.webgl.StaticGLObject); - - -/** - */ -ol.webgl.ArrayBuffer.prototype.bind = function() { - var gl = this.getGL(); - gl.bindBuffer(goog.webgl.ARRAY_BUFFER, this.buffer_); -}; - - -/** - * @inheritDoc - */ -ol.webgl.ArrayBuffer.prototype.disposeInternal = function() { - var gl = this.getGL(); - gl.deleteBuffer(this.buffer_); - this.buffer_ = null; - goog.base(this, 'disposeInternal'); -}; diff --git a/src/ol/webgl/framebuffer.js b/src/ol/webgl/framebuffer.js deleted file mode 100644 index 6e422e8b9a..0000000000 --- a/src/ol/webgl/framebuffer.js +++ /dev/null @@ -1,118 +0,0 @@ -goog.provide('ol.webgl.Framebuffer'); - -goog.require('goog.asserts'); -goog.require('goog.webgl'); -goog.require('ol.webgl.GLObject'); - - - -/** - * @constructor - * @extends {ol.webgl.GLObject} - * @param {number} size Size. - */ -ol.webgl.Framebuffer = function(size) { - - goog.base(this); - - /** - * @private - * @type {WebGLFramebuffer} - */ - this.framebuffer_ = null; - - /** - * @private - * @type {WebGLTexture} - */ - this.texture_ = null; - - /** - * @private - * @type {number} - */ - this.size_ = size; - -}; -goog.inherits(ol.webgl.Framebuffer, ol.webgl.GLObject); - - -/** - */ -ol.webgl.Framebuffer.prototype.bind = function() { - var gl = this.getGL(); - var framebuffer = this.get(); - gl.bindFramebuffer(goog.webgl.FRAMEBUFFER, framebuffer); -}; - - -/** - * @param {number} size Size. - * @private - * @return {WebGLTexture} Texture. - */ -ol.webgl.Framebuffer.prototype.createTexture_ = function(size) { - var gl = this.getGL(); - var texture = gl.createTexture(); - gl.bindTexture(goog.webgl.TEXTURE_2D, this.texture_); - gl.texImage2D(goog.webgl.TEXTURE_2D, 0, goog.webgl.RGBA, size, size, 0, - goog.webgl.RGBA, goog.webgl.UNSIGNED_BYTE, null); - gl.texParameteri( - goog.webgl.TEXTURE_2D, goog.webgl.TEXTURE_MAG_FILTER, goog.webgl.LINEAR); - gl.texParameteri( - goog.webgl.TEXTURE_2D, goog.webgl.TEXTURE_MIN_FILTER, goog.webgl.LINEAR); - return texture; -}; - - -/** - * @return {WebGLFramebuffer} Framebuffer. - */ -ol.webgl.Framebuffer.prototype.get = function() { - goog.asserts.assert(!goog.isNull(this.framebuffer_)); - return this.framebuffer_; -}; - - -/** - * @param {WebGLRenderingContext} gl GL. - */ -ol.webgl.Framebuffer.prototype.setGL = function(gl) { - if (!goog.isNull(this.gl)) { - if (!goog.isNull(this.framebuffer_)) { - this.gl.deleteFramebuffer(this.framebuffer_); - this.framebuffer_ = null; - } - if (!goog.isNull(this.texture_)) { - this.gl.deleteTexture(this.texture_); - this.texture_ = null; - } - } - goog.base(this, 'setGL', gl); - if (!goog.isNull(gl)) { - this.texture_ = this.createTexture_(this.size_); - this.framebuffer_ = gl.createFramebuffer(); - gl.bindFramebuffer(goog.webgl.FRAMEBUFFER, this.framebuffer_); - gl.framebufferTexture2D(goog.webgl.FRAMEBUFFER, - goog.webgl.COLOR_ATTACHMENT0, goog.webgl.TEXTURE_2D, this.texture_, 0); - } -}; - - -/** - * @param {number} size Size. - */ -ol.webgl.Framebuffer.prototype.setSize = function(size) { - var gl = this.getGL(); - goog.asserts.assert(!(size & (size - 1))); - if (this.size_ != size && !goog.isNull(gl)) { - var texture = this.createTexture_(size); - goog.asserts.assert(!goog.isNull(this.framebuffer_)); - gl.bindFramebuffer(goog.webgl.FRAMEBUFFER, this.framebuffer_); - gl.framebufferTexture2D(goog.webgl.FRAMEBUFFER, - goog.webgl.COLOR_ATTACHMENT0, goog.webgl.TEXTURE_2D, texture, 0); - goog.asserts.assert(!goog.isNull(this.texture_)); - gl.deleteTexture(this.texture_); - this.texture = texture; - } -}; diff --git a/src/ol/webgl/layerrenderer.js b/src/ol/webgl/layerrenderer.js index 5fe3f49e32..ca9bf10f6f 100644 --- a/src/ol/webgl/layerrenderer.js +++ b/src/ol/webgl/layerrenderer.js @@ -29,3 +29,8 @@ ol.webgl.LayerRenderer.prototype.getGL = function() { var map = /** @type {ol.webgl.Map} */ this.getMap(); return map.getGL(); }; + + +/** + */ +ol.webgl.LayerRenderer.prototype.redraw = goog.abstractMethod; diff --git a/src/ol/webgl/map.js b/src/ol/webgl/map.js index 0fb3c9c195..0b1a080f30 100644 --- a/src/ol/webgl/map.js +++ b/src/ol/webgl/map.js @@ -324,14 +324,32 @@ ol.webgl.Map.prototype.handleWebGLContextRestored = function() { /** - * @protected + * @inheritDoc */ -ol.webgl.Map.prototype.redraw = function() { +ol.webgl.Map.prototype.redrawInternal = function() { - goog.base(this, 'redraw'); + var animate = goog.base(this, 'redrawInternal'); var gl = this.getGL(); gl.clear(goog.webgl.COLOR_BUFFER_BIT); + gl.bindFramebuffer(goog.webgl.FRAMEBUFFER, null); + + var program = this.getProgram(this.fragmentShader_, this.vertexShader_); + gl.useProgram(program); + + this.forEachLayer(function(layer) { + if (!layer.getVisible()) { + return; + } + var layerRenderer = /** @type {ol.webgl.LayerRenderer} */ ( + this.getLayerRenderer(layer)); + goog.asserts.assert(goog.isDefAndNotNull(layerRenderer)); + layerRenderer.redraw(); + gl.bindTexture(goog.webgl.TEXTURE0, layerRenderer.getTexture()); + }, this); + + return animate; + }; diff --git a/src/ol/webgl/staticglobject.js b/src/ol/webgl/staticglobject.js deleted file mode 100644 index 8ae0c391d7..0000000000 --- a/src/ol/webgl/staticglobject.js +++ /dev/null @@ -1,49 +0,0 @@ -goog.provide('ol.webgl.StaticGLObject'); - -goog.require('goog.Disposable'); - - - -/** - * @constructor - * @extends {goog.Disposable} - * @param {WebGLRenderingContext} gl GL. - */ -ol.webgl.StaticGLObject = function(gl) { - - goog.asserts.assert(!goog.isNull(gl)); - - /** - * @protected - * @type {WebGLRenderingContext} - */ - this.gl = gl; - -}; -goog.inherits(ol.webgl.StaticGLObject, goog.Disposable); - - -/** - * @inheritDoc - */ -ol.webgl.StaticGLObject.prototype.disposeInternal = function() { - this.gl = null; - goog.base(this, 'disposeInternal'); -}; - - -/** - * @return {!WebGLRenderingContext} GL. - */ -ol.webgl.StaticGLObject.prototype.getGL = function() { - goog.asserts.assert(!goog.isNull(this.gl)); - return this.gl; -}; - - -/** - * @return {WebGLRenderingContext} GL. - */ -ol.webgl.StaticGLObject.prototype.unsafeGetGL = function() { - return this.gl; -}; diff --git a/src/ol/webgl/texture.js b/src/ol/webgl/texture.js deleted file mode 100644 index 6f62f2f78e..0000000000 --- a/src/ol/webgl/texture.js +++ /dev/null @@ -1,63 +0,0 @@ -goog.provide('ol.webgl.Texture'); - -goog.require('goog.asserts'); -goog.require('ol.webgl.GLObject'); - - - -/** - * @constructor - * @extends {ol.webgl.GLObject} - */ -ol.webgl.Texture = function() { - - goog.base(this); - - /** - * @private - * @type {WebGLTexture} - */ - this.texture_ = null; - - /** - * @private - * @type {Image} - */ - this.image_ = image; - -}; -goog.inherits(ol.webgl.Texture, ol.webgl.GLObject); - - -/** - */ -ol.webgl.Texture.prototype.bind = function() { - var gl = this.getGL(); - if (goog.isNull(this.texture_)) { - var texture = gl.createTexture(); - gl.bindTexture(goog.webgl.TEXTURE_2D, texture); - gl.texImage2D(goog.webgl.TEXTURE_2D, 0, goog.webgl.RGBA, goog.webgl.RGBA, - goog.webgl.UNSIGNED_BYTE, this.image_); - gl.texParameteri(goog.webgl.TEXTURE_2D, goog.webgl.TEXTURE_MAG_FILTER, - goog.webgl.NEAREST); - gl.texParameteri(goog.webgl.TEXTURE_2D, goog.webgl.TEXTURE_MIN_FILTER, - goog.webgl.NEAREST); - this.texture_ = texture; - } else { - gl.bindTexture(goog.webgl.TEXTURE_2D, this.texture_); - } -}; - - -/** - * @inheritDoc - */ -ol.webgl.Texture.prototype.setGL = function(gl) { - if (!goog.isNull(this.gl)) { - if (!goog.isNull(this.texture_)) { - this.gl.deleteTexture(this.texture_); - this.texture_ = null; - } - } - goog.base(this, 'setGL', gl); -}; diff --git a/src/ol/webgl/tilelayerrenderer.js b/src/ol/webgl/tilelayerrenderer.js index a5c9678f45..446532bd3b 100644 --- a/src/ol/webgl/tilelayerrenderer.js +++ b/src/ol/webgl/tilelayerrenderer.js @@ -52,6 +52,14 @@ ol.webgl.TileLayerRenderer = function(map, tileLayer) { goog.inherits(ol.webgl.TileLayerRenderer, ol.webgl.LayerRenderer); +/** + * @inheritDoc + */ +ol.webgl.TileLayerRenderer.prototype.getTexture = function() { + return this.texture_; +}; + + /** * @private */ @@ -82,16 +90,16 @@ ol.webgl.TileLayerRenderer.prototype.bindFramebuffer_ = function() { goog.webgl.REPEAT); var renderbuffer = gl.createRenderbuffer(); - gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer); - gl.renderbufferStorage( - gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, size.width, size.height); + gl.bindRenderbuffer(goog.webgl.RENDERBUFFER, renderbuffer); + gl.renderbufferStorage(goog.webgl.RENDERBUFFER, + goog.webgl.DEPTH_COMPONENT16, size.width, size.height); var framebuffer = gl.createFramebuffer(); gl.bindFramebuffer(goog.webgl.FRAMEBUFFER, framebuffer); gl.framebufferTexture2D(goog.webgl.FRAMEBUFFER, goog.webgl.COLOR_ATTACHMENT0, goog.webgl.TEXTURE_2D, texture, 0); - gl.framebufferRenderbuffer(goog.webgl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, - gl.RENDERBUFFER, renderbuffer); + gl.framebufferRenderbuffer(goog.webgl.FRAMEBUFFER, + goog.webgl.DEPTH_ATTACHMENT, goog.webgl.RENDERBUFFER, renderbuffer); gl.deleteFramebuffer(this.framebuffer_); gl.deleteRenderbuffer(this.renderbuffer_); @@ -128,6 +136,37 @@ ol.webgl.TileLayerRenderer.prototype.disposeInternal = function() { }; +/** + * @inheritDoc + */ +ol.webgl.TileLayerRenderer.prototype.redraw = function() { + var map = /** @type {ol.webgl.Map} */ (this.getMap()); + var extent = map.getExtent(); + var resolution = map.getResolution(); + if (!goog.isDef(extent) || !goog.isDef(resolution)) { + return; + } + var tileLayer = /** @type {ol.TileLayer} */ (this.getLayer()); + var tileStore = tileLayer.getTileStore(); + var tileGrid = tileStore.getTileGrid(); + var z = tileGrid.getZForResolution(resolution); + var tileBounds = tileGrid.getExtentTileBounds(z, extent); + var tileCoordOrigin = new ol.TileCoord(z, tileBounds.left, tileBounds.bottom); + var tileSize = tileGrid.getTileSize(); + this.size_ = new goog.math.Size( + tileSize.width * (tileBounds.right - tileBounds.left), + tileSize.height * (tileBounds.top - tileBounds.bottom)); + this.bindFramebuffer_(); + tileBounds.forEachTileCoord(z, function(tileCoord) { + var x = tileCoord.x; + var y = tileCoord.y; + var deltaX = tileCoord.x - tileCoordOrigin.x; + var deltaY = tileCoord.y - tileCoordOrigin.y; + return false; + }, this); +}; + + /** * @inheritDoc */ @@ -142,12 +181,3 @@ ol.webgl.TileLayerRenderer.prototype.handleLayerOpacityChange = function() { ol.webgl.TileLayerRenderer.prototype.handleLayerVisibleChange = function() { this.dispatchChangeEvent(); }; - - -/** - * @param {goog.math.Size} size Size. - * @private - */ -ol.webgl.TileLayerRenderer.prototype.setSize_ = function(size) { - this.size_ = size; -}; diff --git a/src/ol/webgl/uniform.js b/src/ol/webgl/uniform.js deleted file mode 100644 index 434eac9ed8..0000000000 --- a/src/ol/webgl/uniform.js +++ /dev/null @@ -1,101 +0,0 @@ -goog.provide('ol.webgl.Uniform'); - -goog.require('goog.asserts'); -goog.require('goog.vec.Mat4'); -goog.require('ol.webgl.GLObject'); - - - -/** - * @constructor - * @extends {ol.webgl.GLObject} - * @param {string} name Name. - */ -ol.webgl.Uniform = function(name) { - - goog.base(this); - - /** - * @private - * @type {WebGLProgram} - */ - this.program_ = null; - - /** - * @private - * @type {string} - */ - this.name_ = name; - - /** - * @private - * @type {WebGLUniformLocation} - */ - this.location_ = null; - -}; -goog.inherits(ol.webgl.Uniform, ol.webgl.GLObject); - - -/** - * @return {string} Name. - */ -ol.webgl.Uniform.prototype.getName = function() { - return this.name_; -}; - - -/** - * @param {WebGLRenderingContext} gl GL. - */ -ol.webgl.Uniform.prototype.setGL = function(gl) { - this.location_ = null; - goog.base(this, 'setGL', gl); -}; - - -/** - * @param {number} value Value. - */ -ol.webgl.Uniform.prototype.set1f = function(value) { - var gl = this.getGL(); - if (!goog.isNull(this.location_)) { - gl.uniform1f(this.location_, value); - } -}; - - -/** - * @param {number} value Value. - */ -ol.webgl.Uniform.prototype.set1i = function(value) { - var gl = this.getGL(); - if (!goog.isNull(this.location_)) { - gl.uniform1i(this.location_, value); - } -}; - - -/** - * @param {boolean} transpose Transpose. - * @param {goog.vec.Mat4.Mat4Like} value Value. - */ -ol.webgl.Uniform.prototype.setMatrix4fv = function(transpose, value) { - var gl = this.getGL(); - if (!goog.isNull(this.location_)) { - gl.uniformMatrix4fv(this.location_, transpose, value); - } -}; - - -/** - * @param {WebGLProgram} program Program. - */ -ol.webgl.Uniform.prototype.setProgram = function(program) { - if (goog.isNull(program)) { - this.location_ = null; - } else { - var gl = this.getGL(); - this.location_ = gl.getUniformLocation(program, this.name_); - } -}; diff --git a/src/ol/webgl/vertexattrib.js b/src/ol/webgl/vertexattrib.js deleted file mode 100644 index cb02476041..0000000000 --- a/src/ol/webgl/vertexattrib.js +++ /dev/null @@ -1,78 +0,0 @@ -goog.provide('ol.webgl.VertexAttrib'); - -goog.require('goog.asserts'); -goog.require('ol.webgl.GLObject'); - - - -/** - * @constructor - * @extends {ol.webgl.GLObject} - * @param {string} name Name. - */ -ol.webgl.VertexAttrib = function(name) { - - goog.base(this); - - /** - * @private - * @type {string} - */ - this.name_ = name; - - /** - * @private - * @type {number} - */ - this.location_ = -1; - -}; -goog.inherits(ol.webgl.VertexAttrib, ol.webgl.GLObject); - - -/** - */ -ol.webgl.VertexAttrib.prototype.enableArray = function() { - var gl = this.getGL(); - goog.asserts.assert(this.location_ != -1); - gl.enableVertexAttribArray(this.location_); -}; - - -/** - * @param {number} size Size. - * @param {number} type Type. - * @param {boolean} normalize Normalized. - * @param {number} stride Stride. - * @param {number} offset Offset. - */ -ol.webgl.VertexAttrib.prototype.pointer = - function(size, type, normalize, stride, offset) { - var gl = this.getGL(); - goog.asserts.assert(this.location_ != -1); - gl.vertexAttribPointer( - this.location_, size, type, normalize, stride, offset); -}; - - -/** - * @param {WebGLRenderingContext} gl GL. - */ -ol.webgl.VertexAttrib.prototype.setGL = function(gl) { - this.location_ = -1; - goog.base(this, 'setGL', gl); -}; - - -/** - * @param {WebGLProgram} program Program. - */ -ol.webgl.VertexAttrib.prototype.setProgram = function(program) { - if (goog.isNull(program)) { - this.location_ = -1; - } else { - var gl = this.getGL(); - this.location_ = gl.getAttribLocation(program, this.name_); - goog.asserts.assert(!goog.isNull(this.location_)); - } -};