diff --git a/src/ol/webgl/map.js b/src/ol/webgl/map.js index 9079dc09b4..3cb2d0b115 100644 --- a/src/ol/webgl/map.js +++ b/src/ol/webgl/map.js @@ -80,6 +80,12 @@ ol.webgl.Map = function(target, opt_values) { */ this.programCache_ = {}; + /** + * @private + * @type {Object.} + */ + this.textureCache_ = {}; + /** * @private * @type {ol.webgl.shader.Fragment} @@ -177,6 +183,9 @@ ol.webgl.Map.prototype.disposeInternal = function() { goog.object.forEach(this.shaderCache_, function(shader) { gl.deleteShader(shader); }); + goog.object.forEach(this.textureCache_, function(texture) { + gl.deleteTexture(texture); + }); } goog.base(this, 'disposeInternal'); }; @@ -244,6 +253,29 @@ ol.webgl.Map.prototype.getShader = function(shaderObject) { }; +/** + * @param {Image} image Image. + * @return {WebGLTexture} Texture. + */ +ol.webgl.Map.prototype.getTexture = function(image) { + if (image.src in this.textureCache_) { + return this.textureCache_[image.src]; + } else { + var gl = this.getGL(); + 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, image); + 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); + this.textureCache_[image.src] = texture; + return texture; + } +}; + + /** * @inheritDoc */