在 OpenGL ES 2.0 中,如何从采样器读取相邻纹理像素?

发布于 2024-12-10 13:53:09 字数 602 浏览 0 评论 0原文

我正在传递一个 NxM 大小的纹理作为 GLSL 片段着色器(OpenGL ES 2.0)中的采样器。从相邻纹素读取纹素数据的正确方法是什么?我在片段着色器中没有“变化的”纹理坐标。我只能使用片段坐标来读取纹理信息。

以下是我的着色器,我不确定它是否实际读取数据:

precision mediump float;

uniform sampler2D Sampler;

#define OFFSET 1.0

void main()
{

    vec2 T = gl_FragCoord.xy;

    //Find neighboring velocities:
    vec2 N = texture2D(Sampler,vec2(T.x,T.y+OFFSET)).xy;
    vec2 S = texture2D(Sampler,vec2(T.x,T.y-OFFSET)).xy;
    vec2 E = texture2D(Sampler,vec2(T.x+OFFSET,T.y)).xy; 
    vec2 W = texture2D(Sampler,vec2(T.x-OFFSET,T.y)).xy; 
}

OFFSET 值应该是 1.0 还是 NxM 大小纹理的其他值?

I am passing a texture with NxM size as a sampler in the GLSL fragment shader (OpenGL ES 2.0). What is the right way to read the texel data from the neighboring texel ? I do not have a "varying" texture coordinate in the fragment shader. I can only use fragment coordinate to read the texture information.

following is my shader, I am not sure if its actually reading the data:

precision mediump float;

uniform sampler2D Sampler;

#define OFFSET 1.0

void main()
{

    vec2 T = gl_FragCoord.xy;

    //Find neighboring velocities:
    vec2 N = texture2D(Sampler,vec2(T.x,T.y+OFFSET)).xy;
    vec2 S = texture2D(Sampler,vec2(T.x,T.y-OFFSET)).xy;
    vec2 E = texture2D(Sampler,vec2(T.x+OFFSET,T.y)).xy; 
    vec2 W = texture2D(Sampler,vec2(T.x-OFFSET,T.y)).xy; 
}

Is the OFFSET value should be 1.0 OR something else for NxM size texture ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绻影浮沉 2024-12-17 13:53:09

Nem 的回答是正确的,纹理坐标应该在 [0,1] 中。但请记住,gl_FragCoord 的值在 [0,N]x[0,M] 中(假设您的视口也是 NxM)。因此,您在片段坐标上添加偏移量 1 是正确的,但是这个总和必须除以屏幕尺寸(可能与纹理尺寸相同):

precision mediump float;

uniform sampler2D Sampler;
uniform vec2 invScreenSize;

void main()
{

    vec2 T = gl_FragCoord.xy;

    //Find neighboring velocities:
    vec2 N = texture2D(Sampler,vec2(T.x,T.y+1.0)*invScreenSize).xy;
    vec2 S = texture2D(Sampler,vec2(T.x,T.y-1.0)*invScreenSize).xy;
    vec2 E = texture2D(Sampler,vec2(T.x+1.0,T.y)*invScreenSize).xy; 
    vec2 W = texture2D(Sampler,vec2(T.x-1.0,T.y)*invScreenSize).xy; 
}

其中 invScreenSize 是屏幕尺寸的倒数(1/N, 1/M),以防止着色器中的分割。

Nem is right in his answer that the texture coordinate should be in [0,1]. But keep in mind, that the values of gl_FragCoord are in [0,N]x[0,M] (assuming your viewport is NxM, too). So you are correct in adding an offset of 1 to the fragment coordinate, but this sum has then to be divided by the screen size (which is probably the same as the texture size):

precision mediump float;

uniform sampler2D Sampler;
uniform vec2 invScreenSize;

void main()
{

    vec2 T = gl_FragCoord.xy;

    //Find neighboring velocities:
    vec2 N = texture2D(Sampler,vec2(T.x,T.y+1.0)*invScreenSize).xy;
    vec2 S = texture2D(Sampler,vec2(T.x,T.y-1.0)*invScreenSize).xy;
    vec2 E = texture2D(Sampler,vec2(T.x+1.0,T.y)*invScreenSize).xy; 
    vec2 W = texture2D(Sampler,vec2(T.x-1.0,T.y)*invScreenSize).xy; 
}

where invScreenSize is the reciprocal screen size (1/N, 1/M), to prevent division in the shader.

云朵有点甜 2024-12-17 13:53:09

我非常确定在 OpenGL ES 2.0 中,所有纹理坐标都在 0.0 和 1.0 之间浮动,因此添加 1.0 将是错误的,并且可能会环绕到完全相同的位置。您的 x 偏移量应为 1.0/N,y 偏移量应为 1.0/M(基于您的 NxM 声明)。我不确定这是最好的解决方案,但我已经用它来解决类似的问题。

我建议您检查 OpenGL ES 着色语言规范,以验证我关于纹理坐标格式的声明是否正确(几个月前我这样做了)。这是一个 pdf 文件,很容易通过 google 找到,并且在某处有所有内置函数的便捷列表。

I'm pretty sure that in OpenGL ES 2.0 all texture coordinates are floats between 0.0 and 1.0, so adding 1.0 will be wrong, and will probably just wrap around to the exact same position. Your x offset should be 1.0/N and your y offset should be 1.0/M (based on your NxM statement). I am not confident that this is the best solution, but I have used it to solve a similar problem.

I would advise that you check the OpenGL ES Shading Language specification to verify that my claim about the texture coordinate format is correct (I did this a few months ago). It's a pdf that is easy to find with google, and has a convenient list of all the built-in functions somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文