GLSL:gl_FragCoord 问题

发布于 2024-10-19 08:57:47 字数 1146 浏览 7 评论 0原文

我正在试验 OpenGL ES 2.0 的 GLSL。我有一个四边形和一个正在渲染的纹理。我可以这样成功地做到这一点:

//VERTEX SHADER
attribute highp vec4 vertex;
attribute mediump vec2 coord0;

uniform mediump mat4 worldViewProjection;

varying mediump vec2 tc0;

void main()
{
    // Transforming The Vertex
    gl_Position = worldViewProjection * vertex;

    // Passing The Texture Coordinate Of Texture Unit 0 To The Fragment Shader
    tc0 = vec2(coord0);
}

//FRAGMENT SHADER
varying mediump vec2 tc0;

uniform sampler2D my_color_texture;

void main()
{
    gl_FragColor = texture2D(my_color_texture, tc0);
}

到目前为止一切顺利。但是,我想做一些基于像素的过滤,例如中值。因此,我想使用像素坐标而不是标准化坐标(tc0),然后将结果转换回标准化坐标。因此,我想使用 gl_FragCoord 而不是 uv 属性 (tc0)。但我不知道如何返回标准化坐标,因为我不知道 gl_FragCoords 的范围。知道我怎样才能得到它吗?我已经做到了这一点,使用“标准化”的固定值,尽管它不能完美工作,因为它会导致拉伸和平铺(但至少显示了一些东西):

//FRAGMENT SHADER
varying mediump vec2 tc0;

uniform sampler2D my_color_texture;

void main()
{
    gl_FragColor = texture2D(my_color_texture, vec2(gl_FragCoord) / vec2(256, 256));
}

所以,简单的问题是,我应该在这个地方使用什么的 vec2(256, 256) ,这样我就可以获得与使用 uv 坐标相同的结果。

谢谢!

I am experimenting with GLSL for OpenGL ES 2.0. I have a quad and a texture I am rendering. I can successfully do it this way:

//VERTEX SHADER
attribute highp vec4 vertex;
attribute mediump vec2 coord0;

uniform mediump mat4 worldViewProjection;

varying mediump vec2 tc0;

void main()
{
    // Transforming The Vertex
    gl_Position = worldViewProjection * vertex;

    // Passing The Texture Coordinate Of Texture Unit 0 To The Fragment Shader
    tc0 = vec2(coord0);
}

//FRAGMENT SHADER
varying mediump vec2 tc0;

uniform sampler2D my_color_texture;

void main()
{
    gl_FragColor = texture2D(my_color_texture, tc0);
}

So far so good. However, I'd like to do some pixel-based filtering, e.g. Median. So, I'd like to work in pixel coordinates rather than in normalized (tc0) and then convert the result back to normalized coords. Therefore, I'd like to use gl_FragCoord instead of a uv attribute (tc0). But I don't know how to go back to normalized coords because I don't know the range of gl_FragCoords. Any idea how I could get it? I have got that far, using a fixed value for 'normalization', though it's not working perfectly as it is causing stretching and tiling (but at least is showing something):

//FRAGMENT SHADER
varying mediump vec2 tc0;

uniform sampler2D my_color_texture;

void main()
{
    gl_FragColor = texture2D(my_color_texture, vec2(gl_FragCoord) / vec2(256, 256));
}

So, the simple question is, what should I use in the place of vec2(256, 256) so that I could get the same result as if I were using the uv coords.

Thanks!

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

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

发布评论

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

评论(2

顾铮苏瑾 2024-10-26 08:57:47

gl_FragCoord 采用屏幕坐标,因此要获得标准化坐标,您需要除以视口宽度和高度。您可以使用统一变量将该信息传递给着色器,因为没有内置变量。

gl_FragCoord is in screen coordinates, so to get normalized coords you need to divide by the viewport width and height. You can use a uniform variable to pass that information to the shader, since there is no built in variable for it.

窗影残 2024-10-26 08:57:47

如果满足以下条件,您还可以通过非标准化坐标对纹理进行采样:

  • 通过 GL_TEXTURE_RECTANGLE 中的texture() 进行采样
  • 通过 texelFetch() 从常规纹理或纹理缓冲区进行采样

You can also sample the texture by un-normalized coordinates if:

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