Reputation: 43753
I'm using WebGL (≈ OpenGL ES 2.0). At the moment, my application uses texture unit 0 as a per-model texture, and 1 and 2 as constant textures used by the shader.
In order to load and set up a texture, I do
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(...);
gl.texParameteri(...);
However, this stomps on the state of which texture is bound to the current texture unit. In order to avoid this program, I added
gl.activeTexture(gl.TEXTURE0);
to the beginning of the above code, so that it only affects the binding for texture unit 0 which is reset before drawing any geometry anyway.
Is there a better approach, i.e. one that does not involve happening to have a “scratch” texture unit available? Is there an established best practice for this state management problem?
Upvotes: 2
Views: 1550
Reputation: 13877
For me this issue comes under the same category as a few other: what can you assume about the GL state at certain points in the program?
AFAICT you basically have two options:
glActiveTextrure()
as you are currently doingglActiveTexture(gl.TEXTURE0)
when it is done, so the pre-drawing parts can assume it's set that way.But really, don't think too much about it. In OpenGL you optimise your program by maximising (within certain practical limits) the ratio of polygons to function calls, which you can do as well by passing lots of polygons do your glDrawArrays()
calls etc.
Upvotes: 1