I'm a tad confused about how to bind a texture to a slot in GLSL. Here are my shaders:
#version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 texCoords;
out vec4 ex_texCoords;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;
void main(void)
{
gl_Position = (ProjectionMatrix * ViewMatrix * ModelMatrix) * in_Position;
ex_texCoords = texCoords;
}
#version 330
in vec4 ex_texCoords;
out vec4 out_Color;
uniform sampler2D textureSampler;
void main(void)
{
out_Color = texture(textureSampler2, ex_texCoords.xy);
}
When I set my matrices up, I need to query the uniform location, and then manually set the values at that location, doing something like this:
modelMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ModelMatrix");
...
glUniformMatrix4fv(modelMatrixUniformLocation, 1, GL_FALSE, modelMatrix.data);
However, as far as I can tell, I don't need to do that to initialize my texture sampler. At set-up time, all I seem to need to do is this:
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
glTexImage2D(...);
So, my questions are, why don't I need to do the same process for textures that I need to do for matrices? How does the OpenGL runtime "know" that the texture I've bound is supposed to be the sampler I defined? If I wanted to have multiple textures, how would I go about doing this?