TextureState: Difference between revisions

From WebGL Public Wiki
Jump to navigation Jump to search
use syntaxhighlight
No edit summary
 
Line 15: Line 15:
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
     WebGLRenderingContext = function() {
     WebGLRenderingContext = function() {
       // activeTexture is just an index into textureUnits
       // activeTextureUnit is just an index into textureUnits
       this.activeTexture = 0;
       this.activeTextureUnit = 0;
    
    
       // these are the textureUnits for this context.
       // these are the textureUnits for this context.
Line 31: Line 31:


<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
     WebGLRenderingContext.prototype.activeTexture = function(texture) {
     WebGLRenderingContext.prototype.activeTexture = function(textureUnit) {
       this.activeTexture = texture - this.TEXTURE0;
       this.activeTextureUnit = textureUnit - this.TEXTURE0;
     };
     };
</syntaxhighlight>
</syntaxhighlight>
Line 39: Line 39:
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
     WebGLRenderingContext.prototype.bindTexture = function(target, texture) {
     WebGLRenderingContext.prototype.bindTexture = function(target, texture) {
       this.textureUnits[this.activeTexture][target] = texture;
       this.textureUnits[this.activeTextureUnit][target] = texture;
     };
     };
</syntaxhighlight>
</syntaxhighlight>
Line 47: Line 47:
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
     WebGLRenderingContext.prototype.texParameteri = function(target, pname, value) {
     WebGLRenderingContext.prototype.texParameteri = function(target, pname, value) {
       var texture = this.textureUnits[this.activeTexture][target];
       var texture = this.textureUnits[this.activeTextureUnit][target];
       texture.state[pname] = value;
       texture.state[pname] = value;
     };
     };
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 06:49, 7 August 2012

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() {
      // activeTextureUnit is just an index into textureUnits
      this.activeTextureUnit = 0;
  
      // these are the textureUnits for this context.
      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(textureUnit) {
      this.activeTextureUnit = textureUnit - this.TEXTURE0;
    };

bindTexture would be implemented like this

    WebGLRenderingContext.prototype.bindTexture = function(target, texture) {
      this.textureUnits[this.activeTextureUnit][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.activeTextureUnit][target];
      texture.state[pname] = value;
    };