Hidden Surface Removal
- Is the science of determining what parts of an object are visible.
- Object space methods work on entire objects.
- Image space methods work on points in the image.
- Back Face detection
- Compute the surface normal S
- Compute the look at vector V
- If V ċ S > 0 => the two are "pointing" the same direction.
- Which implies the surface is not visible.
- Such a test can be performed in the vertex shader
- Or can be done (to some degree) before that.
- But this doesn't work for obscured objects.
- Depth buffer or z-buffer
- Some systems use two buffers (depth/z-buffer and a frame buffer)
- initialize the z-buffer to infinity. (or perhaps 1.0
- This can be part of clearing the frame buffer (glClearColor, glClear)
- glClearDepth[f](depth)
- Set to 1 by default.
- GlClear(GL_DEPTH_BUFFER_BIT| GL_COLOR_BUFFER_BIT)
- Oh, by the way, you can query for it by
- glGet(GL_DEPTH_CLEAR_VALUE)
- As the fragment is processed, calculate the z value
- The z value can be interpolated during both Bressenham's and scan line conversion.
- But we will probably keep it in float.
- If the z value is less than the value in the buffer, store pixel color (and the depth) in the pixel location, along with z.
- Since items are processed in a random order, alpha values are problematic.
- We might blend with a color which is later obscured by an intermediate surface.
- It can do quite a few unnecessary computations too.
- And requires quite a bit of memory (n bits x screen size)
- A-buffer
- Accumulation buffer
- Deals with transparency
- Single surface at a pixel, store as z-buffer
- Multiple surfaces at a pixel, form a sorted list.
- For each surface Store:
- Depth
- Transparency (α)
- percentage covered
- Other information
- Use the depth flag to indicate the above.
- After all fragments are processed, traverse the list and compute the final pixel value.
- Scan-Line method
- This is a real exercise in data structures.
- Maintain an edge list sorted by y, then by x for all polygons being processed (transformed-projected)
- Keep a list of active edges, along with the polygon to which they belong.
- Interpolate the z position as well.
- If one surface is active, just draw the pixel.
- If more surfaces are active, depth sort in place.
- This can be optimized and made (reasonably) efficient
- (Think Bressenham's)
- Depth Sorting
- In object space sort the surfaces according to depth.
- Scanline convert these surfaces.
- Known as the painter's algorithm (Think Bob Ross)
- The further back surfaces will be drawn first.
- Then the closer ones will be drawn over top.
- This becomes quite messy when the surfaces change relative depth positions.
- Bounding box tests to partially determine this.
- We may need to reorder surfaces in this case.
- Possibly break the surfaces by clipping against each other.
- Hearn/Baker give an algorithm, which at one case involves swapping the order of the surfaces and retesting.
- This can lead to an infinite loop of you are not extremely careful.