GLSL Texture Size
18,965
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;
}
Author by
Geosearchef
Updated on August 24, 2022Comments
-
Geosearchef over 1 year
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; }