简体   繁体   English

Webgl:最远的飞机能见度

[英]Webgl: farthest plane visibility

Well, I have a very simple example: just a triangle whose z coordinates are equal to 1 (NO view or projection matrices). 好吧,我有一个非常简单的示例:只是一个z坐标等于1的三角形(没有视图或投影矩阵)。 Coordinates are: 坐标为:

0.0,  0.6,  1.0
-0.5, -0.4,  1.0
0.5, -0.4,  1.0

So that triangle lies on farthest plane. 这样三角形就位于最远的平面上。 DEPTH_TEST is disabled. DEPTH_TEST已禁用。 Triangle is visible, because -1 <= z <= 1. But when I enable DEPTH_TEST: 三角形可见,因为-1 <= z <=1。但是当我启用DEPTH_TEST时:

gl.enable(gl.DEPTH_TEST);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

the triangle is not visible anymore. 三角形不再可见。 When I change all z coordinates to -1 (so it is on the nearest plane) it becomes visible again. 当我将所有z坐标都更改为-1(因此它在最近的平面上)时,它再次变得可见。

So why is it not visible on farthest plane? 那么,为什么在最远的平面上看不到它呢? 1) Is it a webgl bug 2) is it a webgl/opengl feature? 1)是webgl错误2)是webgl / opengl功能吗? 3) if it's a feature, why does it work in this way? 3)如果它是一项功能,为什么它会以这种方式工作?

The initial value for glDepthFunc is GL_LESS , so even if you set glClearDepthf to 1.0, the comparision 1.0 < 1.0 yields false, so the fragments will be discarded. glDepthFunc的初始值为GL_LESS ,因此即使将glClearDepthf设置为1.0,比较1.0 <1.0也会产生false,因此片段将被丢弃。

You can try to set glDepthFunc(GL_LEQUAL) . 您可以尝试设置glDepthFunc(GL_LEQUAL)

Note that you also should be aware of floating point issues. 请注意,您还应该注意浮点问题。 Comparing for exact equality is always problematic. 比较完全相等总是有问题的。 Even if you don't transform the object space coordinates and pass them through in the vertex shader, the z value will be transformed from the NDC range [-1,1] to the window space range [0,1] (or whatever you set with glDepthRangef ). 即使您不变换对象空间坐标并在顶点着色器中传递它们,z值也将从NDC范围[-1,1]变换为窗口空间范围[0,1](或任何其他值)用glDepthRangef设置)。 This shouldn't be a problem for the -1 and 1 input case, though, as the intermediate results should be exactly representable without loss of precision. 但是,对于-1和1输入情况,这应该不成问题,因为中间结果应该可以精确表示而不会损失精度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM