Wrap ol.webgl code in define condition

This commit is contained in:
Andreas Hocevar
2017-01-02 21:52:31 +01:00
parent fd92982661
commit 9c79564676
5 changed files with 444 additions and 424 deletions
+16 -12
View File
@@ -4,13 +4,15 @@ goog.require('ol');
goog.require('ol.webgl');
/**
if (ol.ENABLE_WEBGL) {
/**
* @constructor
* @param {Array.<number>=} opt_arr Array.
* @param {number=} opt_usage Usage.
* @struct
*/
ol.webgl.Buffer = function(opt_arr, opt_usage) {
ol.webgl.Buffer = function(opt_arr, opt_usage) {
/**
* @private
@@ -25,31 +27,33 @@ ol.webgl.Buffer = function(opt_arr, opt_usage) {
this.usage_ = opt_usage !== undefined ?
opt_usage : ol.webgl.Buffer.Usage_.STATIC_DRAW;
};
};
/**
/**
* @return {Array.<number>} Array.
*/
ol.webgl.Buffer.prototype.getArray = function() {
ol.webgl.Buffer.prototype.getArray = function() {
return this.arr_;
};
};
/**
/**
* @return {number} Usage.
*/
ol.webgl.Buffer.prototype.getUsage = function() {
ol.webgl.Buffer.prototype.getUsage = function() {
return this.usage_;
};
};
/**
/**
* @enum {number}
* @private
*/
ol.webgl.Buffer.Usage_ = {
ol.webgl.Buffer.Usage_ = {
STATIC_DRAW: ol.webgl.STATIC_DRAW,
STREAM_DRAW: ol.webgl.STREAM_DRAW,
DYNAMIC_DRAW: ol.webgl.DYNAMIC_DRAW
};
};
}
+53 -49
View File
@@ -9,7 +9,9 @@ goog.require('ol.webgl');
goog.require('ol.webgl.ContextEventType');
/**
if (ol.ENABLE_WEBGL) {
/**
* @classdesc
* A WebGL context for accessing low-level WebGL capabilities.
*
@@ -18,7 +20,7 @@ goog.require('ol.webgl.ContextEventType');
* @param {HTMLCanvasElement} canvas Canvas.
* @param {WebGLRenderingContext} gl GL.
*/
ol.webgl.Context = function(canvas, gl) {
ol.webgl.Context = function(canvas, gl) {
/**
* @private
@@ -90,18 +92,18 @@ ol.webgl.Context = function(canvas, gl) {
ol.events.listen(this.canvas_, ol.webgl.ContextEventType.RESTORED,
this.handleWebGLContextRestored, this);
};
ol.inherits(ol.webgl.Context, ol.Disposable);
};
ol.inherits(ol.webgl.Context, ol.Disposable);
/**
/**
* Just bind the buffer if it's in the cache. Otherwise create
* the WebGL buffer, bind it, populate it, and add an entry to
* the cache.
* @param {number} target Target.
* @param {ol.webgl.Buffer} buf Buffer.
*/
ol.webgl.Context.prototype.bindBuffer = function(target, buf) {
ol.webgl.Context.prototype.bindBuffer = function(target, buf) {
var gl = this.getGL();
var arr = buf.getArray();
var bufferKey = String(ol.getUid(buf));
@@ -124,13 +126,13 @@ ol.webgl.Context.prototype.bindBuffer = function(target, buf) {
buffer: buffer
};
}
};
};
/**
/**
* @param {ol.webgl.Buffer} buf Buffer.
*/
ol.webgl.Context.prototype.deleteBuffer = function(buf) {
ol.webgl.Context.prototype.deleteBuffer = function(buf) {
var gl = this.getGL();
var bufferKey = String(ol.getUid(buf));
var bufferCacheEntry = this.bufferCache_[bufferKey];
@@ -138,13 +140,13 @@ ol.webgl.Context.prototype.deleteBuffer = function(buf) {
gl.deleteBuffer(bufferCacheEntry.buffer);
}
delete this.bufferCache_[bufferKey];
};
};
/**
/**
* @inheritDoc
*/
ol.webgl.Context.prototype.disposeInternal = function() {
ol.webgl.Context.prototype.disposeInternal = function() {
ol.events.unlistenAll(this.canvas_);
var gl = this.getGL();
if (!gl.isContextLost()) {
@@ -163,46 +165,46 @@ ol.webgl.Context.prototype.disposeInternal = function() {
gl.deleteRenderbuffer(this.hitDetectionRenderbuffer_);
gl.deleteTexture(this.hitDetectionTexture_);
}
};
};
/**
/**
* @return {HTMLCanvasElement} Canvas.
*/
ol.webgl.Context.prototype.getCanvas = function() {
ol.webgl.Context.prototype.getCanvas = function() {
return this.canvas_;
};
};
/**
/**
* Get the WebGL rendering context
* @return {WebGLRenderingContext} The rendering context.
* @api
*/
ol.webgl.Context.prototype.getGL = function() {
ol.webgl.Context.prototype.getGL = function() {
return this.gl_;
};
};
/**
/**
* Get the frame buffer for hit detection.
* @return {WebGLFramebuffer} The hit detection frame buffer.
*/
ol.webgl.Context.prototype.getHitDetectionFramebuffer = function() {
ol.webgl.Context.prototype.getHitDetectionFramebuffer = function() {
if (!this.hitDetectionFramebuffer_) {
this.initHitDetectionFramebuffer_();
}
return this.hitDetectionFramebuffer_;
};
};
/**
/**
* Get shader from the cache if it's in the cache. Otherwise, create
* the WebGL shader, compile it, and add entry to cache.
* @param {ol.webgl.Shader} shaderObject Shader object.
* @return {WebGLShader} Shader.
*/
ol.webgl.Context.prototype.getShader = function(shaderObject) {
ol.webgl.Context.prototype.getShader = function(shaderObject) {
var shaderKey = String(ol.getUid(shaderObject));
if (shaderKey in this.shaderCache_) {
return this.shaderCache_[shaderKey];
@@ -214,10 +216,10 @@ ol.webgl.Context.prototype.getShader = function(shaderObject) {
this.shaderCache_[shaderKey] = shader;
return shader;
}
};
};
/**
/**
* Get the program from the cache if it's in the cache. Otherwise create
* the WebGL program, attach the shaders to it, and add an entry to the
* cache.
@@ -225,7 +227,7 @@ ol.webgl.Context.prototype.getShader = function(shaderObject) {
* @param {ol.webgl.Vertex} vertexShaderObject Vertex shader.
* @return {WebGLProgram} Program.
*/
ol.webgl.Context.prototype.getProgram = function(
ol.webgl.Context.prototype.getProgram = function(
fragmentShaderObject, vertexShaderObject) {
var programKey =
ol.getUid(fragmentShaderObject) + '/' + ol.getUid(vertexShaderObject);
@@ -240,13 +242,13 @@ ol.webgl.Context.prototype.getProgram = function(
this.programCache_[programKey] = program;
return program;
}
};
};
/**
/**
* FIXME empy description for jsdoc
*/
ol.webgl.Context.prototype.handleWebGLContextLost = function() {
ol.webgl.Context.prototype.handleWebGLContextLost = function() {
ol.obj.clear(this.bufferCache_);
ol.obj.clear(this.shaderCache_);
ol.obj.clear(this.programCache_);
@@ -254,21 +256,21 @@ ol.webgl.Context.prototype.handleWebGLContextLost = function() {
this.hitDetectionFramebuffer_ = null;
this.hitDetectionTexture_ = null;
this.hitDetectionRenderbuffer_ = null;
};
};
/**
/**
* FIXME empy description for jsdoc
*/
ol.webgl.Context.prototype.handleWebGLContextRestored = function() {
};
ol.webgl.Context.prototype.handleWebGLContextRestored = function() {
};
/**
/**
* Creates a 1x1 pixel framebuffer for the hit-detection.
* @private
*/
ol.webgl.Context.prototype.initHitDetectionFramebuffer_ = function() {
ol.webgl.Context.prototype.initHitDetectionFramebuffer_ = function() {
var gl = this.gl_;
var framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
@@ -289,16 +291,16 @@ ol.webgl.Context.prototype.initHitDetectionFramebuffer_ = function() {
this.hitDetectionFramebuffer_ = framebuffer;
this.hitDetectionTexture_ = texture;
this.hitDetectionRenderbuffer_ = renderbuffer;
};
};
/**
/**
* Use a program. If the program is already in use, this will return `false`.
* @param {WebGLProgram} program Program.
* @return {boolean} Changed.
* @api
*/
ol.webgl.Context.prototype.useProgram = function(program) {
ol.webgl.Context.prototype.useProgram = function(program) {
if (program == this.currentProgram_) {
return false;
} else {
@@ -307,17 +309,17 @@ ol.webgl.Context.prototype.useProgram = function(program) {
this.currentProgram_ = program;
return true;
}
};
};
/**
/**
* @param {WebGLRenderingContext} gl WebGL rendering context.
* @param {number=} opt_wrapS wrapS.
* @param {number=} opt_wrapT wrapT.
* @return {WebGLTexture} The texture.
* @private
*/
ol.webgl.Context.createTexture_ = function(gl, opt_wrapS, opt_wrapT) {
ol.webgl.Context.createTexture_ = function(gl, opt_wrapS, opt_wrapT) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
@@ -333,10 +335,10 @@ ol.webgl.Context.createTexture_ = function(gl, opt_wrapS, opt_wrapT) {
}
return texture;
};
};
/**
/**
* @param {WebGLRenderingContext} gl WebGL rendering context.
* @param {number} width Width.
* @param {number} height Height.
@@ -344,7 +346,7 @@ ol.webgl.Context.createTexture_ = function(gl, opt_wrapS, opt_wrapT) {
* @param {number=} opt_wrapT wrapT.
* @return {WebGLTexture} The texture.
*/
ol.webgl.Context.createEmptyTexture = function(
ol.webgl.Context.createEmptyTexture = function(
gl, width, height, opt_wrapS, opt_wrapT) {
var texture = ol.webgl.Context.createTexture_(gl, opt_wrapS, opt_wrapT);
gl.texImage2D(
@@ -352,20 +354,22 @@ ol.webgl.Context.createEmptyTexture = function(
null);
return texture;
};
};
/**
/**
* @param {WebGLRenderingContext} gl WebGL rendering context.
* @param {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} image Image.
* @param {number=} opt_wrapS wrapS.
* @param {number=} opt_wrapT wrapT.
* @return {WebGLTexture} The texture.
*/
ol.webgl.Context.createTexture = function(gl, image, opt_wrapS, opt_wrapT) {
ol.webgl.Context.createTexture = function(gl, image, opt_wrapS, opt_wrapT) {
var texture = ol.webgl.Context.createTexture_(gl, opt_wrapS, opt_wrapT);
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
return texture;
};
};
}
+11 -7
View File
@@ -5,21 +5,25 @@ goog.require('ol.webgl');
goog.require('ol.webgl.Shader');
/**
if (ol.ENABLE_WEBGL) {
/**
* @constructor
* @extends {ol.webgl.Shader}
* @param {string} source Source.
* @struct
*/
ol.webgl.Fragment = function(source) {
ol.webgl.Fragment = function(source) {
ol.webgl.Shader.call(this, source);
};
ol.inherits(ol.webgl.Fragment, ol.webgl.Shader);
};
ol.inherits(ol.webgl.Fragment, ol.webgl.Shader);
/**
/**
* @inheritDoc
*/
ol.webgl.Fragment.prototype.getType = function() {
ol.webgl.Fragment.prototype.getType = function() {
return ol.webgl.FRAGMENT_SHADER;
};
};
}
+14 -10
View File
@@ -4,12 +4,14 @@ goog.require('ol.functions');
goog.require('ol.webgl');
/**
if (ol.ENABLE_WEBGL) {
/**
* @constructor
* @param {string} source Source.
* @struct
*/
ol.webgl.Shader = function(source) {
ol.webgl.Shader = function(source) {
/**
* @private
@@ -17,25 +19,27 @@ ol.webgl.Shader = function(source) {
*/
this.source_ = source;
};
};
/**
/**
* @abstract
* @return {number} Type.
*/
ol.webgl.Shader.prototype.getType = function() {};
ol.webgl.Shader.prototype.getType = function() {};
/**
/**
* @return {string} Source.
*/
ol.webgl.Shader.prototype.getSource = function() {
ol.webgl.Shader.prototype.getSource = function() {
return this.source_;
};
};
/**
/**
* @return {boolean} Is animated?
*/
ol.webgl.Shader.prototype.isAnimated = ol.functions.FALSE;
ol.webgl.Shader.prototype.isAnimated = ol.functions.FALSE;
}
+11 -7
View File
@@ -5,21 +5,25 @@ goog.require('ol.webgl');
goog.require('ol.webgl.Shader');
/**
if (ol.ENABLE_WEBGL) {
/**
* @constructor
* @extends {ol.webgl.Shader}
* @param {string} source Source.
* @struct
*/
ol.webgl.Vertex = function(source) {
ol.webgl.Vertex = function(source) {
ol.webgl.Shader.call(this, source);
};
ol.inherits(ol.webgl.Vertex, ol.webgl.Shader);
};
ol.inherits(ol.webgl.Vertex, ol.webgl.Shader);
/**
/**
* @inheritDoc
*/
ol.webgl.Vertex.prototype.getType = function() {
ol.webgl.Vertex.prototype.getType = function() {
return ol.webgl.VERTEX_SHADER;
};
};
}