gl_Position 的 Zbuffer 规则
以下是我对 opengl 的设置,以防万一它们很重要:
glViewport(0,0,w,h); //w=800, h=600
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
有一段时间我认为裁剪平面将应用于 gl_Position.z 在 -1 到 1 之外的任何内容。 但即使当我在着色器中将 gl_Position.z 强制为常量时:
gl_Position.z = -0.01;
仍然存在基于 gl_Position.w 的正确 z 缓冲和裁剪!那么规则是什么呢?
Here are my settings for opengl, incase they matter:
glViewport(0,0,w,h); //w=800, h=600
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
For a while I thought that the clipping planes will apply to anything for which gl_Position.z is outside -1 to 1.
But even when I force gl_Position.z to a constant number in the shader:
gl_Position.z = -0.01;
There is still correct z-buffering and clipping based on gl_Position.w! So what are the rules?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
gl_Position 的值位于 OpenGL 所谓的“剪辑空间”中。它是一个同质坐标系。特别是,剪辑空间的 XYZ 范围是 [-W, +W]。所以每个顶点都在自己的剪辑空间中。
我不确定你所说的“规则是什么”到底是什么意思。裁剪空间 gl_Position 相对于同质空间中的视图区域进行裁剪。因此它们被裁剪到每个顶点的 [-W, +W] 区域。很难想象在射影齐质空间中,但数学是可行的,所以这并不重要。
之后,剪裁的顶点将变换到标准化设备坐标 (NDC) 空间。这意味着将剪辑空间的 XYZ 除以 W。所有这些顶点都在 [-1, 1] 范围内。
从那里开始,视口变换发生。
glViewport
参数将顶点从 [-1, 1] NDC 空间缩放到视口区域。这会将点转换为窗口空间;它们是在窗口空间中被光栅化的。窗口空间的XY由
glViewport
控制。窗口空间的 Z 值由glDepthRange 控制。默认深度范围是近值 0 和远值 1(警告:近/远平面的透视矩阵与深度范围没有任何共同点。不要混淆两者)。因此它将 NDC 空间中 Z 的 [-1, 1] 映射到窗口空间中的 [near, far]。这些就是“规则”。
The value of gl_Position is in what OpenGL calls "clip space". It is a homogeneous coordinate system. In particular, the XYZ extents of clip space are [-W, +W]. So each vertex is in its own clip space.
I'm not sure exactly what you mean by "what are the rules." The clip-space gl_Position is clipped against the view area in homogeneous space. So they are clipped to the [-W, +W] region for each vertex. It's hard to imagine that in a projective homogeneous space, but the math works out, so it doesn't matter.
After that, the clipped vertices are transformed to normalized device coordinate (NDC) space. This means dividing the XYZ of the clip space by W. All of these vertices are in the range [-1, 1].
From there, the viewport transform happens. The
glViewport
parameters scale the vertices from the [-1, 1] NDC space to the viewport region. This transforms points to window space; it is in window space that they are rasterized.The XY of window space is controlled by
glViewport
. The Z values for window space are controlled byglDepthRange
. The default depth range is a near value of 0 and far value of 1 (WARNING: the perspective matrix near/far planes have nothing in common with the depth range. Do not confuse the two). So it maps the [-1, 1] of the Z in NDC space to [near, far] in window space.Those are "the rules."
你用
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
深度缓冲区的值介于 0 和 1(最大深度)之间,使用选择 GL_LEQUAL 这意味着所有内容都将绘制到屏幕上,因为深度值将小于或等于 1。
为了更清楚地访问:
http://www.zeuscmd.com/tutorials/opengl/11-Depth.php
You use
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
Depth Buffer has a value between 0 and 1(max depth), use chose GL_LEQUAL which means everything would be drawn to the screen as depth value would be less than or equal to 1.
For more clarity visit:
http://www.zeuscmd.com/tutorials/opengl/11-Depth.php