[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Public WebGL] Premultiplied alpha blending and issues with color accuracy



You could also order the calls


// bind first texture
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, first_texture);
// bind second texture
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, second_texture);
// set up samplers
gl.useProgram(program);
gl.uniform1i(gl.getUniformLocation(program, 'first_texture'), 0);
gl.uniform1i(gl.getUniformLocation(program, 'second_texture'), 1);

glActiveTexture only sets the texture unit which will be affected by subsequent bind calls. It's state is completely irrelevant when setting the uniform values.

Regards

-Mark



On 12/05/11 18:13, Florian Bösch wrote:
On Fri, May 11, 2012 at 11:00 AM, Thor Harald Johansen <thj@thj.no> wrote:
And okay, from your and Ben's comments, it seems I'm going to have to look more carefully at how multi-texturing actually works. All the examples I found were doing things like switching textures for each side of a cube, etc. None demonstrated blending of multiple textures. I find API references to be really hard to learn from. :/
Using multiple textures is relatively straightforward.

gl.useProgram(program);
// bind first texture
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, first_texture);
gl.uniform1i(gl.getUniformLocation(program, 'first_texture'), 0);
// bind second texture
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, second_texture);
gl.uniform1i(gl.getUniformLocation(program, 'second_texture'), 1);

And then in the shader have:

varying vec2 uv;
uniform sampler2D first_texture, second_texture;
void main(){
  gl_FragColor = texture2D(first_texture, uv) + texture2D(second_texture, uv);
}