如何在glsl中计算gl_FragCoord

发布于 2024-08-02 22:00:30 字数 520 浏览 3 评论 0原文

好的,在我的 GLSL 片段着色器中,我希望能够计算片段与空间中特定线的距离。

这样做的结果是,我首先尝试在顶点着色器中使用不同的 vec2 集来镜像 gl_FragCoord 中的结果:

varying vec2 fake_frag_coord;
//in vertex shader:
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
fake_frag_coord=(gl_ModelViewProjectionMatrix * gl_Vertex).xy;

现在在片段着色器中我期望:

gl_FragCoord.xy==fake_frag_coord

但事实并非如此。管道对 gl_Position 执行什么操作,将其转换为我在 fake_frag_coord 上忽略的 gl_FragCoord

Ok, in my GLSL fragment shader I want to be able to calculate the distance of the fragment from a particular line in space.

The result of this is that I am first trying to use a varying vec2 set in my vertex shader to mirror what ends up in gl_FragCoord:

varying vec2 fake_frag_coord;
//in vertex shader:
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
fake_frag_coord=(gl_ModelViewProjectionMatrix * gl_Vertex).xy;

Now in the fragment shader I expect:

gl_FragCoord.xy==fake_frag_coord

But it does not. What operation does the pipeline do on gl_Position to turn it into gl_FragCoord that I am neglecting to do on fake_frag_coord?

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

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

发布评论

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

评论(4

星星的軌跡 2024-08-09 22:00:30

gl_FragCoord 是屏幕坐标,因此您需要有关屏幕尺寸等信息才能根据位置生成它。我认为您生成的 fake_frag_coord 将对应于屏幕上标准化设备坐标中的一个点(-1 到 1)。因此,如果您将特定尺寸的屏幕尺寸乘以一半,然后添加该尺寸,您将获得实际的屏幕像素。

gl_FragCoord is screen coordinates, so you'd need to have information about the screen size and such to produce it based on the Position. The fake_frag_coord you've produced will correspond to a point in the screen in normalized device coordinates I think (-1 to 1). So if you were to multiply by half the screen's size in a particular dimension, and then add that size, you'd get actual screen pixels.

粉红×色少女 2024-08-09 22:00:30

您遇到的问题是,在着色器中计算的 gl_ModelViewProjectionMatrix * gl_Vertex 并不总是固定函数管道产生的结果。要获得相同的结果,请执行 fake_frag_coord = ftransform()。

编辑:

然后根据屏幕尺寸缩放它。

The problem you have is that gl_ModelViewProjectionMatrix * gl_Vertex computed in a shader is not always what the fixed function pipeline yields. To get identical results, do fake_frag_coord = ftransform().

Edit:

And then scale it with your screen dimensions.

冰魂雪魄 2024-08-09 22:00:30

我希望我有更多的代表,这样我也可以挑剔。
这是我所知道的。
OpenGL 会自动为您执行除以 W,因此当位置到达片段着色器时,它已经在 NDC 坐标中,到那时 W 将是 1.0,唯一需要除以 W 的时间是如果您想要在 CPU 端执行相同的数学运算并获得相同的结果,或者,如果您出于某种奇怪的原因想要在顶点着色器中获取 NDC 坐标。

I wish I had more rep so I could nitpick too.
Here's what I know.
OpenGL performs the Divide-By-W automatically for you, so by the time a position arrives in a fragment shader, it's already in NDC coordinates, and by then W will be 1.0, the only time you need to divide by W is if you want to do the same math on the CPU side, and get the same results, OR, if you for some odd reason, wanted to get NDC coordinates within your vertex shader.

乙白 2024-08-09 22:00:30
varying vec2 fake_frag_coord;
//in vertex shader:

fake_frag_coord=(gl_ModelViewMatrix * gl_Vertex).xy
varying vec2 fake_frag_coord;
//in vertex shader:

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