简体   繁体   中英

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). 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. Triangle is visible, because -1 <= z <= 1. But when I enable 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.

So why is it not visible on farthest plane? 1) Is it a webgl bug 2) is it a webgl/opengl feature? 3) if it's a feature, why does it work in this way?

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.

You can try to set 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 ). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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