TextureState

From WebGL Public Wiki
Revision as of 07:25, 5 August 2012 by Gman (talk | contribs) (Created page with "I hope this pseudo code sample will help make it clearer how activeTexture, bindTexture, and the various texture functio...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

I hope this pseudo code sample will help make it clearer how activeTexture, bindTexture, and the various texture functions in WebGL effect the state of textures.

Imagine textures are defined like this

   Texture2D = function() {
     this.levels = [];
     this.state = {};
   };

And the WebGLRenderingContext is defined like this

   WebGLRenderingContext = function() {
     this.activeTexture = 0;
     this.textureUnits = [
       { TEXTURE_2D: default2DTexture, TEXTURE_CUBE_MAP: defaultCubeMapTexture, },
       { TEXTURE_2D: default2DTexture, TEXTURE_CUBE_MAP: defaultCubeMapTexture, },
       { TEXTURE_2D: default2DTexture, TEXTURE_CUBE_MAP: defaultCubeMapTexture, },
       ...
     ];
   };

Then activeTexture would be implemented like this

   WebGLRenderingContext.prototype.activeTexture = function(texture) {
     this.activeTexture = texture - this.TEXTURE0;
   };

bindTexture would be implemented like this

   WebGLRenderingContext.prototype.bindTexture = function(target, texture) {
     this.textureUnits[this.activeTexture][target] = texture;
   };

And texture functions like texParameter or texImage2D would be implemented like this

   WebGLRenderingContext.prototype.texParameteri = function(target, pname, value) {
     var texture = this.textureUnits[this.activeTexture][target];
     texture.state[pname] = value;
   };