3D Fundamentals Tutorial 11
Things that are behind get covered up by things that are in front.
The tutorial video is on YouTube here.
- The need for occlusion in scenes with multiple objects 0:24
- We used culling of triangles when drawing convex shapes
- But this doen't solve our problem with occlusion (e.g. multiple meshes in the same scene)
- One could considers the painter's algorithm, but that's inefficient and of limited use
- Solution: the z-buffer: simple, elegant & performant 2:27
- Essentially sorts triangles based on their depth at the pixel level
- Uses two 2D-arrays (for x,y coordinates): a back-buffer (of colors, in
gfxin our case) as a render target, and a z-buffer (of floats) to store depth
- We initialize the z-buffer with infinity
- Whenever we want to write to the back-buffer, we check to see if z-value of the pixel we are drawing is smaller (and set the z-buffer in that case)
- This is a special sorting case 6:26
- Implementation in code 7:06
- Changes to the
if (zb.TestAndSet(x,y,z))before the
- If done well, Z-testing can reduce rendering time 9:54
- Works best if geometries are sorted and drawn from front to back (more Z-test rejections)
- This is contrary to intuition and old/other techniques (e.g. in 2D: drawing sprites and tilemaps)
- These provide more options for configuring the z-buffer, such as depth comparison operators, choice of formats/types
- These typically store 1/z in the buffer, requiring corrections for depth precision (high in the near plane, low in the far plane)
- Dealing with translucent pixels and depth 13:58
- You will need to store the order of all the translucent pixels up until the first opaque one
- Stencil Buffers 14:28
- Provide more flexibility to reject pixels & have more control (general purpose bufffers)
- E.g. for UIs, HUDs, mirrors, shadow effects etc.
- Next up: vertex shaders and working towards dynamic lighting effects 15:00
The GitHub repository for the tutorial code is here.
Paper on depth precision