Basically, from generating to using a texture unit we briefly do the following steps:
unsigned int texture1;
unsigned int texture2;
glGenTextures(1, &texture1);
glGenTextures(1, &texture2);
glBindTexture(.., texture1);
unsigned char* data = load_image_data();
glTexImage( ... , data);
glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
glBindTexture(.., texture2);
data = load_image_data();
glTexImage( ... , data);
glUniform1i(glGetUniformLocation(ourShader.ID, "texture2"), 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
//on the shader side, we declare samplers in fragment shader:
#version 330 core
...
uniform sampler2D texture1;
uniform sampler2D texture2;
void main() { ... }
As we can see above, we are generating the textureID using glGenTextures(1, &textureX)
and then sending the uniform sampler a value using glUniform1i
, then what is the role of the GL_TEXTUREx
at all, and that also using glActiveTexture
? Couldn’t we do something like this:
glUniform1i(glGetUniformLocation(ourShader.ID, "textureX"), GL_TEXTURE0);
?
We’re passing a value 0
to the sampler2D uniform
, and then setting glActiveTexture
to GL_TEXTUREx
, so doesn’t that mean a uniform value of 0
GL_TEXTURE0
, 1
is GL_TEXTURE1
and so on, which is exactly why I thought
glUniform1i(glGetUniformLocation(ourShader.ID, "textureX"), GL_TEXTURE0);
should be done.
But, if glActiveTexture(…) actually sends the value of the ID generate by glGenTextures(1, &textureX)
to the GPU, then why would even glUniform1i
be used?
Please explain me this workflow/framework/mechanism openGL is using for using texture using the glActive
, glGenerate
, glUniform1i
functions for generating textures.