TextureState: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
Imagine textures are defined like this | Imagine textures are defined like this | ||
< | <syntaxhighlight lang="javascript"> | ||
Texture2D = function() { | Texture2D = function() { | ||
this.levels = []; | this.levels = []; | ||
Line 9: | Line 9: | ||
}; | }; | ||
</ | </syntaxhighlight> | ||
And the WebGLRenderingContext is defined like this | And the WebGLRenderingContext is defined like this | ||
< | <syntaxhighlight lang="javascript"> | ||
WebGLRenderingContext = function() { | WebGLRenderingContext = function() { | ||
// | // activeTextureUnit is just an index into textureUnits | ||
this. | this.activeTextureUnit = 0; | ||
// these are the textureUnits for this context. | // these are the textureUnits for this context. | ||
this.textureUnits = [ | this.textureUnits = [ | ||
Line 26: | Line 26: | ||
]; | ]; | ||
}; | }; | ||
</ | </syntaxhighlight> | ||
Then [[Reference:activeTexture|activeTexture]] would be implemented like this | Then [[Reference:activeTexture|activeTexture]] would be implemented like this | ||
< | <syntaxhighlight lang="javascript"> | ||
WebGLRenderingContext.prototype.activeTexture = function( | WebGLRenderingContext.prototype.activeTexture = function(textureUnit) { | ||
this. | this.activeTextureUnit = textureUnit - this.TEXTURE0; | ||
}; | }; | ||
</ | </syntaxhighlight> | ||
[[Reference:bindTexture|bindTexture]] would be implemented like this | [[Reference:bindTexture|bindTexture]] would be implemented like this | ||
< | <syntaxhighlight lang="javascript"> | ||
WebGLRenderingContext.prototype.bindTexture = function(target, texture) { | WebGLRenderingContext.prototype.bindTexture = function(target, texture) { | ||
this.textureUnits[this. | this.textureUnits[this.activeTextureUnit][target] = texture; | ||
}; | }; | ||
</ | </syntaxhighlight> | ||
And texture functions like [[Reference::texParameteri|texParameter]] or [[Reference::texImage2D|texImage2D]] would be implemented like this | And texture functions like [[Reference::texParameteri|texParameter]] or [[Reference::texImage2D|texImage2D]] would be implemented like this | ||
< | <syntaxhighlight lang="javascript"> | ||
WebGLRenderingContext.prototype.texParameteri = function(target, pname, value) { | WebGLRenderingContext.prototype.texParameteri = function(target, pname, value) { | ||
var texture = this.textureUnits[this. | var texture = this.textureUnits[this.activeTextureUnit][target]; | ||
texture.state[pname] = value; | texture.state[pname] = value; | ||
}; | }; | ||
</ | </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;
};