在glsl中,用于从gl_position计算gl_fragCoord的公式是什么?
如果我错了,请纠正我。 当使用顶点和像素着色器时,我们通常提供代码来计算顶点着色器的输出 gl_position。 然后,我们在像素着色器中使用输入 gl_FragCoord 找到我们。 OpenGL 从 gl_position 计算 gl_FragCoord 所执行的操作的名称是什么?这些“投影”和“剪辑坐标变换”是否正确? 那么,这些操作期间到底执行了哪些转换? 换句话说,gl_FragCoord 和 gl_position 之间的数学关系是什么,我可以用它来替换 openGL 函数?
非常感谢您的任何贡献。
Please correct me if I'm wrong.
When using vertex and pixel shaders, we usually provide the code to compute the output gl_position of the vertex shader.
Then, we find ouselves with the input gl_FragCoord in the pixel shader.
What are the name of the operations performed by OpenGL to compute gl_FragCoord from gl_position ? Is it correct that those are "projection" and "clip coordinates transform" ?
Then, what exactly are the transformations performs during those operations ?
In other terms, what is the mathematical relation between gl_FragCoord and gl_position, that I could use to replace the openGL function ?
Thank you very much for any contribution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
gl_Position 位于投影后齐次坐标中。
值得注意的是,gl_Position 是顶点的(4d)位置,而gl_FragCoord 是片段的(2d)位置。
其间发生的操作是
因此,虽然您可以找到公式来变换从 2d 空间(即视口)中的顶点位置表示的任意点,但它本身并没有那么有用,因为生成的片段不直接与顶点位置对齐。获取顶点 2d 坐标的公式是
再次强调,这不是 gl_FragCoord。如果您想了解更深入的知识,请查看 GL 规范中有关光栅化的详细信息。
我不确定“替换 openGL 函数”到底是什么意思,但是光栅化并不简单,并且远远超出了 SO 答案的范围。
gl_Position is in post-projection homogeneous coordinates.
It's worth noting that gl_Position is the (4d) position of a vertex, while gl_FragCoord is the (2d) position of a fragment.
The operations that happen in between are
So, while you can find the formula to transform the arbitrary point that is represented from the vertex position in the 2d space that is the viewport, it's not in and of itself that useful, as the generated fragments do not align directly to the vertex position. the formula to get the 2d coordinate of the vertex is
Again, this is not gl_FragCoord. Check the details on rasterization in the GL specification if you want more in-depth knowledge.
I'm not sure exactly what you mean by "replace the openGL function", but rasterizing is non-trivial, and way beyond the scope of an SO answer.