Rendering order?

Started by
6 comments, last by BkoChan 16 years ago
Hi all! Ive read in a few different places now that it is good practice to render your scene starting with the objects furthest away from the camera and working towards it (something to do with transparencies). Is this true? If so how do I go about doing this? Instead of rendering straight away do I iterate over my objects adding them to a rendering queue of some sort? Or have I got it totally wrong?! Thanks for any help!
Advertisement
you only need to do this for semitransparent objects, otherwise it doesn't really matter
Normally it is the best practice to sort your objects by material. However, for objects that are not fully opaque you will get rendering errors if you don't render all objects from back to front.

Consider following case:
- A semi-transparent object with depth 0.8 is rendered.
- Now we render a second object directly which is located directly behind the first object. What will we see? Nothing, because the first object has left his trace in the depth buffer.

So render all your opaque objects first, then render your semitransparent ones, from back to front, with z-write disabled. Sorting by triangle is not necessary (if your objects are not too large)


Best regards,
Porthos
The "optimal" way is to first render anything opaque from front to back, then anything transparent from back to front. The rendering order is only critical for the transparent objects, however. So you could just iterate through your objects and render them if they're non-transparent, while adding any transparent objects to a list sorted by depth.
Eh, all of this advice that you don't have to do it for opaque objects is only true if you are doing 3D. The same principle (back-to-front) applies to all objects (opaque and translucent) if you do 2D.
To be specific, the front-to-back order on fully opaque objects is an optimization because it allows closer objects to occlude further back ones. What this does is allow pixels that will never be shown to be rejected as early as possible and, most importantly, before any expensive pixel-shaders are run.

In fact, this is so common that many (most? all?) modern games do what's called a z-only pass where the geometry is rendered, but the color-buffer is not filed, only the z-buffer. Since the Z is already computed and stored this allows the second pass, which fills in the color, to be sorted by material, which is more optimal because context switching is so slow. Some cards, like the Radeon 19xx series IIRC, supported optimized z-only rendering which doubled the rate at which Z values could be computed and stored when the color writes were disabled.

Neither technique applies to pure 2D rendering as alnite states, but they do apply whenever you have a depth-buffer, even in a 2D game which uses one (which is common if the game is written on top of a 3D API such as OpenGL or Direct3D.)

throw table_exception("(? ???)? ? ???");

On my website I have a video tutorial that shows you how you can sort your geometry while you are rending them so that your transparent objects are always rendered last sorted in the correct order.
Great, thanks for the fast replies! mmakrzem, I'll checkout your site when I get home :)

This topic is closed to new replies.

Advertisement