Articles Archive
Articles Search
Director Wiki
 

Rendering Only to the Z Buffer

October 28, 2002
by Lucas Meijer

Disclaimer

The trick that this article describes relies on an undocumented 3D Lingo feature. Most of the time people don't care about that, but Intel has announced that it will retract this feature (and replace it with a better alternative) in a future Shockwave version. (Current Shockwave version at the time of this writing is 8.5.1) So the safe place to use this is in projector-distributed projects.

This article assumes quite a bit of knowledge of Director's 3D capabilities. If you're new to Director3D, take a look at these articles, and don't let this one scare you off.

What is the trick?

To render objects in the z buffer only.

Why is it cool?

You can use z buffer rendering to solve numerous problems. In this article, I'll discuss one application: You can have a high-quality backdrop image, which is hand-painted, or rendered (at high resolution) using some 3d package. Instead of appearing as just a flat backdrop, you can render a simplified version of the scene into the z buffer, enabling objects to go in front of and behind the backdrop, giving the user the illusion that the backdrop is a real part of the scene.

You can view the Shockwave version of the sample movie by clicking on the image below.

How is it done?

The basic steps to render into the z buffer are:

  1. Render the objects you want to render into the z buffer
  2. Render a large black plane over the entire screen, with its visibility set to #front_nodepth
  3. Render the "normal" objects

The tricky part is in the second step. Using the special (unsupported) #front_nodepth flag, we're able to "clear" the screen by drawing a large black plane, but keep the z buffer contents. In this example we don't clear the screen, but we draw our backdrop into the screen.

A few problems come up

How do I make sure my z buffer 'stub' geometry models get drawn first?

The Shockwave 3d Xtra renders all children of the default world group in the order that they appear in the world group's child list. The way to control render ordering is to make groups before you do anything else. These statements create new, empty groups in the Shockwave 3D cast member referenced by pW3D:

pW3D.newGroup ("renderPass1")
pW3D.newGroup ("renderPass2")
pW3D.newGroup ("renderPass3")

When you make these new groups, they are automatically added to the child list of the world group, in the order you created them. Now, for any model that we want to draw first, we can set its parent property to group ("renderPass1"); anything we want to draw later, we can make a child of group ("renderPass2") etc.

So, in our case where we have a static background and we want some object to move in front of and behind some elements of that background we put all our z buffer stub models into group ("renderPass1"), we put the background image (which needs to be on a plane model) in group ("renderPass2")), and we put the object(s) that need to maneuver in the scene in group ("renderPass3").

The accompanying source file contains all the code for the example, together with comments on what happens where. Unless you already totally understand how it all works, I strongly suggest that before even looking at the code, to run the movie, and open the 3DPI, and go to the Member tab, where you can inspect the group ("world') hierarchy.

A note on the unsupportedness of the #front_nodepth

It might seem a bit strange to write an article on a feature that has been announced that it will be retracted. However the #nodepth flags have to go, because Intel wants to give better z buffer control, which apparantly will not work well with the #nodepth flags. So while the feature in this syntax form will disappear, the possibility of rendering into the z buffer will still exist. So what you've hopefully picked up in this article will not bein vain for future versions of Director!

A Director 8.5 source file is available in ZIP or SIT format.

Lucas Meijer is a freelance Lingo programmer, working and living in Amsterdam. While 3D games are his preference, he works on anything that is a challenge to program. Whenever he is not busy programming, you can probably find him on the fields playing Ultimate Frisbee.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.