Snapshot WebGL renderer

This commit is contained in:
Tom Payne
2012-07-15 16:32:00 +02:00
parent 1d567bb2bf
commit 582a9e93e0
9 changed files with 70 additions and 473 deletions

View File

@@ -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');
};

View File

@@ -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;
}
};

View File

@@ -29,3 +29,8 @@ ol.webgl.LayerRenderer.prototype.getGL = function() {
var map = /** @type {ol.webgl.Map} */ this.getMap(); var map = /** @type {ol.webgl.Map} */ this.getMap();
return map.getGL(); return map.getGL();
}; };
/**
*/
ol.webgl.LayerRenderer.prototype.redraw = goog.abstractMethod;

View File

@@ -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(); var gl = this.getGL();
gl.clear(goog.webgl.COLOR_BUFFER_BIT); 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;
}; };

View File

@@ -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;
};

View File

@@ -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);
};

View File

@@ -52,6 +52,14 @@ ol.webgl.TileLayerRenderer = function(map, tileLayer) {
goog.inherits(ol.webgl.TileLayerRenderer, ol.webgl.LayerRenderer); goog.inherits(ol.webgl.TileLayerRenderer, ol.webgl.LayerRenderer);
/**
* @inheritDoc
*/
ol.webgl.TileLayerRenderer.prototype.getTexture = function() {
return this.texture_;
};
/** /**
* @private * @private
*/ */
@@ -82,16 +90,16 @@ ol.webgl.TileLayerRenderer.prototype.bindFramebuffer_ = function() {
goog.webgl.REPEAT); goog.webgl.REPEAT);
var renderbuffer = gl.createRenderbuffer(); var renderbuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer); gl.bindRenderbuffer(goog.webgl.RENDERBUFFER, renderbuffer);
gl.renderbufferStorage( gl.renderbufferStorage(goog.webgl.RENDERBUFFER,
gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, size.width, size.height); goog.webgl.DEPTH_COMPONENT16, size.width, size.height);
var framebuffer = gl.createFramebuffer(); var framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(goog.webgl.FRAMEBUFFER, framebuffer); gl.bindFramebuffer(goog.webgl.FRAMEBUFFER, framebuffer);
gl.framebufferTexture2D(goog.webgl.FRAMEBUFFER, gl.framebufferTexture2D(goog.webgl.FRAMEBUFFER,
goog.webgl.COLOR_ATTACHMENT0, goog.webgl.TEXTURE_2D, texture, 0); goog.webgl.COLOR_ATTACHMENT0, goog.webgl.TEXTURE_2D, texture, 0);
gl.framebufferRenderbuffer(goog.webgl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.framebufferRenderbuffer(goog.webgl.FRAMEBUFFER,
gl.RENDERBUFFER, renderbuffer); goog.webgl.DEPTH_ATTACHMENT, goog.webgl.RENDERBUFFER, renderbuffer);
gl.deleteFramebuffer(this.framebuffer_); gl.deleteFramebuffer(this.framebuffer_);
gl.deleteRenderbuffer(this.renderbuffer_); 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 * @inheritDoc
*/ */
@@ -142,12 +181,3 @@ ol.webgl.TileLayerRenderer.prototype.handleLayerOpacityChange = function() {
ol.webgl.TileLayerRenderer.prototype.handleLayerVisibleChange = function() { ol.webgl.TileLayerRenderer.prototype.handleLayerVisibleChange = function() {
this.dispatchChangeEvent(); this.dispatchChangeEvent();
}; };
/**
* @param {goog.math.Size} size Size.
* @private
*/
ol.webgl.TileLayerRenderer.prototype.setSize_ = function(size) {
this.size_ = size;
};

View File

@@ -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_);
}
};

View File

@@ -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_));
}
};