Binding an OpenGL Texture Sampler

opengl, textures

Solution

OpenGL dosen't know which sampler to use, you must bind it manualy using `glActiveTexture(GL_TEXTUREi)` (where `i` from `0` to `GL_MAX_TEXTURES`). Your code works because default sampler is `GL_TEXTURE0`. Obviously you are lucky enough and shader linker set `textureSampler` to 0.

Also, there is a new functionality in OpenGL API of version 3 and higher - glGenSamplers, glBindSampler. It allow you to use texture-independent sampler configuration! You can read http://www.g-truc.net/post-0267.html for more details.

And I recommend you to download samples from http://www.g-truc.net/project-0026.html

Problem

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?

Original source