0
votes

After a lot of reading I was able to understand the steps needed to load a texture in OpenGL ES 2.0, but some question are still not answered:

What's the code below is actually doing?

glUniform1i(sampler2DLocation, 0);

If I erase this line from my code, nothing changes. Some books describe it as "Tell the texture uniform sampler to use this texture in the shader by telling it to read from texture unit 0"

This is called after the line:

glActiveTexture(GL_TEXTURE0);

But as stated in khronos.org the default active texture is GL_TEXTURE0, so I guess the line "glActiveTexture(GL_TEXTURE0);" code is just written as a good practice?

One last thing, when I call:

glBindTexture(GL_TEXTURE_2D, genTextures[0]);

I'm saying that future calls that affect "GL_TEXTURE_2D" will affect the texture unit stored in genTextures[0], due the binding. But there is any relation between "GL_TEXTURE_2D" and the active texture unit? I mean, there is an intrinsic chain between the 03 "components"?

genTextures[0] <---> GL_TEXTURE_2D <---> the active texture unit

Thank you,

1

1 Answers

1
votes

The reason why the code continues to work even if you remove the instruction "glUniform1i(sampler2DLocation, 0);" is due, possibly to the default value assigned by the driver when the uniform is not provided.

To better understand it I would need an example of the shared code and the way the uniform ID is taken but I am pretty sure that the instruction is there to say to the GPU through the shader to use the texture unit 0.

The effect of calling "glUniform1i(sampler2DLocation, 0);" says take texture unit 0. The effect of not calling it, sets anyway the sampler uniform to 0 and therefore, even if not formally correct, has the same behavior.

According to OpenGL standard, you activate a texture unit with the glActivateTexture which requires the number of the texture unit and then you bind the texture with glbindtexture to that specific current texture unit.

In other words, first you set the texture unit you want to work on and then you tell to the driver which texture needs to be bound in it.

I hope this helps. Maurizio