GLSL纹理尺寸

6

我有一个关于片段着色器的问题。 我想获取一张纹理的大小(这张纹理是从图片中加载的)。

我知道可以使用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;
}

3
它无法编译 - 你遇到了什么错误?快点。 - Bartek Banachewicz
在编译 glGetShader(handle, GL_COMPILE_STATUS) == GL_FALSE 后返回 true。有没有办法获取更具体的错误信息? - Geosearchef
如果没有的话,那将会相当困难,你同意吗?该函数被称为ShaderInfoLog或类似的名称,如果您拥有一个相当现代的(4.x)系统,则应该具有某个版本的debug_output扩展(应优先使用,因为它更易于使用、可配置并提供更好的信息)。 - Bartek Banachewicz
错误是:ERROR: 0:11: 'textureSize':未找到匹配的重载函数(使用隐式转换)。在哪个版本中实现了textureSize()函数? - Geosearchef
2
这表明它是在版本130中实现的:https://www.opengl.org/sdk/docs/man/html/textureSize.xhtml 但如果我更改版本,它仍然无法工作。 - Geosearchef
1
因为错误提示是没有重载,而不是没有函数。在这种情况下,显然不能要求sampler2D的大小。 - Bartek Banachewicz
1个回答

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接