What does the texture() function in GLSL do?

Hello All

I’m currently learning GLSL, and I went through the tutorial on shaders from the OF website (great tutorial by the way). In several of the example shader programs, a texture is passed to the shader, where it comes in as a sample2DRect.

In some of the vertex shaders, this line or a similar one is present:

uniform sampler2DRect tex0;
in vec2 texcoord;

void main() {
    vec4 position = texture(tex0, texcoord);

What exactly does the texture() function do? I can see that it is taking in a sampler2DRect and vec2 as parameters and returning a vec4, but I don’t understand what it really does. Is it simply combining the 2d vector with the texture?

Hi there,

Actually, is not combining the two, per se, but accessing the texture in a given position and returning the color in normalised RGBA (this is important, the output color in GLSL ranges from 0 to 1, not 0 to 255). It’s a lookup function.

sampler2DRect means that the texture is in texel space. That is, it ranges from 0 to its size.
texcoord it’s the variation of that fragment.

So, it’s like using a getColor(x, y) function with pixels. Sort of. :slight_smile:


Thank you!