GLSL纹理尺寸

时间:2014-09-12 08:28:15

标签: 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;
}

1 个答案:

答案 0 :(得分:2)

问题是我的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;
}
相关问题