GLSL纹理大小

Geo*_*hef 5 opengl textures glsl

我的片段着色器有问题。我想获取纹理的大小(从图像加载)。

我知道可以使用textureSize(sampler)获取包含纹理大小的ivec2。但是我不知道为什么这不起作用(它不能编译):

#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;
}
Run Code Online (Sandbox Code Playgroud)

Geo*_*hef 6

问题是我的 GLSL 版本太低(在 1.30 中实现)并且我缺少一个参数。

这是工作版本:

#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;
}
Run Code Online (Sandbox Code Playgroud)