Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL Texture Size

I have a problem with my fragment shader. I want to get the size of a texture (which is loaded from an image).

I know that it is possible to use textureSize(sampler) to get an ivec2 which contains the texture size. But i don't know why this isn't working (it doesn't compile):

#version 120

uniform sampler2D tex;

float textureSize;
float texelSize;


void main()
{
    textureSize = textureSize(tex).x;//first line
    //textureSize = 512.0;//if i set the above line as comment and use this one the shader compiles.
    texelSize = 1.0 / textureSize;

    vec4 color = texture2D(tex,gl_TexCoord[0].st);
    gl_FragColor = color * gl_Color;
}
like image 863
Geosearchef Avatar asked Sep 12 '14 08:09

Geosearchef


1 Answers

Th problem was that my GLSL version was to low (implemented in 1.30) and that i was missing a parameter.

Here the working version:

#version 130

uniform sampler2D tex;

float textureSize;
float texelSize;


void main()
{
    ivec2 textureSize2d = textureSize(tex,0);
    textureSize = float(textureSize2d.x);
    texelSize = 1.0 / textureSize;

    vec4 color = texture2D(tex,gl_TexCoord[0].st);
    gl_FragColor = color * gl_Color;
}
like image 60
Geosearchef Avatar answered Oct 15 '22 01:10

Geosearchef