I am noob to WebGL, and I am trying to understand how WebGL textures works by reading this tutorial: WebGL Image Processing Continued.
There is another example in the same tutorial serie which is using two textures by setting the uniform
s for both input textures units 0 and 1 explicitly:
// set which texture units to render with.
gl.uniform1i(u_image0Location, 0); // texture unit 0
gl.uniform1i(u_image1Location, 1); // texture unit 1
But in the first example, the fragment shader is using the sampler2D u_image
for the input texture, so i would expect there shall be in code something like:
gl.uniform1i(gl.getUniformLocation(program, "u_image"), 0);
...but i can't find it. How this works? Just guessing: is the texture unit 0 used as default for all 2D samplers in all WebGL programs? Then, why is gl.uniform1i(u_image0Location, 0);
needed in the second example?
EDIT:
So far, what i have understand from the tutorial mentioned above - just correct me if i am wrong:
There are a least two ways to use textures:
- input textures, where i can read from - here, i need to pass to the fragment shader the location (i.e. "u_image")
- output textures, where i can write to - this is the texture currently bound (in the tutorial mentioned above, to the texture unit 0)
I am not able to fully understand how this example works because the u_image
uniform is not set and moreover in the code there isn't any gl.activeTexture()
call
EDIT 2:
Thanks to Rabbid76 I believe I found further clarification in a comment of gman (the Author of the tutorial mentioned above) in this answer to that question:
You need the framebuffers. By attaching a texture to a framebuffer and then binding that framebuffer you're making the shader write to the texture.
gl.bindTexture(gl.TEXTURE_2D, textures[ii % 2]);
so i guess the current texture unit is always 0 and will never change, My doubt are how the input textures works... – deblocker