🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

OpenGL tests

Started by
1 comment, last by JoeJ 1 year, 10 months ago

Hey Guys,

I hope you're doing well.

I don't really understand how OpenGL tests work like for example: Depth test, stencil test and alpha test.

I read this in some tutorial:

The depth-buffer is a buffer that, just like the color buffer (that stores all the fragment colors: the visual output), stores information per fragment and has the same width and height as the color buffer. The depth buffer is automatically created by the windowing system and stores its depth values as 16, 24 or 32 bit floats. In most systems you'll see a depth buffer with a precision of 24 bits.

When depth testing is enabled, OpenGL tests the depth value of a fragment against the content of the depth buffer. OpenGL performs a depth test and if this test passes, the fragment is rendered and the depth buffer is updated with the new depth value. If the depth test fails, the fragment is discarded.

But I don't get it, what is the depth buffer and what are its value and how are they fixed? I mean who sets up these values

How is the test exactly performed ? Can you please give me an example ?

Also I really would appreciate it you can give me a simple explanation about how the stencil and alpha tests work.

Thank you all.

Advertisement

Seems somebody does not yet deserve his username, huhuhu ;P

If you look at the screen, it's flat. And still we can generate the illusion of depth in 3D games. Distant trees appear farther away than closer trees. That's maybe why it's called depth.
A better term would be distance, imo. Think of depth as the distance of the pixel to the camera, or precisely the distance to the plane we use for planar projection.
The depth buffer stores this distance. It is needed so the distant tree is properly overdrawn by the closer tree, no matter which one we draw first.
Assuming we draw the distant tree first, numbers of 100 are stored on its depth pixels in the framebuffer.
Then, when drawing the closer tree, we calculate a distance of 10. While drawing, we compare depth for each pixel. 100 is farther away than 10, so we overdraw the distant tree with the current closer tree, also replacing depth values of 100 with new values of 10.
If we swap draw order, first drawing the closer tree, and now the distant one, we see the pixels of depth 10 are already closer than our current distance of 100, so we draw no color and no depth.

Early software rasterized 3D games had no depth buffer (or Z-buffer was the name back then).
Instead, a standard solution was to sort the triangles by distance, drawing distant triangles first, close triangles later. This way closer triangles properly overdraw distant triangles.
Another solution was drawing closer triangles first, but then skip parts of a scanline which has been already drawn before. This way we draw only what's visible and waste no work on pixels to be overdrawn anyway later.

Such ideas are still popular today, e.g. if using a depth first pass. This means we draw only to the depth buffer, no colors at all.
After that, we know depth in advance, and can skip over any costly pixel shading which is not visible. That's often a win, even if we have to draw the scene twice.

The ordering topic is also relevant to the alpha value, which is mostly used for transparency, telling hoe transparent or opaque a pixel is, and how it should be blended with former transparent stuff, which also has been drawn to the same current pixel.
Transparency blending is order dependent, so for that we still need to sort triangles (or at least objects) by distance to do this correctly. That's why transparency is still an open problem causing us to use a lot of hacks.

Thus, the common solution is to use alpha testing, not alpha blending. (Think of drawing many leafs of a tree, which usually uses alpha textures to make leafs partially opaque.)
Alpha test means we still store transparency in it's own alpha channel, but when drawing, we only decide to draw the pixel or not. So the alpha channel allows us to add holes to our triangle, but no soft boundary to blend it with the background.
That's no proper transparency, but this way we can still use the simple depth test to draw thousands of leaf triangles.

True alpha blending and sorting is mostly used for special effects and selected geometry like glass windows.

I have never used the stencil buffer, but it's basically a user value. You could use it for example to mark all skin materials on screen, so you can add some screenspace faked subsurface scattering effect later.

This topic is closed to new replies.

Advertisement