Tuesday, September 11, 2007

X-Plane Dark Arts: Airplane Object Translucency I

Side note: please do not post tech support questions as blog comments - please use the x-plane-scenery yahoo list or the x-plane.org scenery forum. I would like to keep tech support discussions in easily search-able public places so future users can get answers quickly.

I saw a post on x-plane.org referring to the process of creating translucency in objects attached to airplanes as a dark art. There is definitely a lot of weirdness in how X-Plane draws airplane objects. I will try to shed some light on what's really going on and how to deal with it. For this first post, I'll explain the requirements of the hardware, which shape the ensuing chaos.

First, and most important, in order for X-Plane to render translucent geometry (objects or otherwise), the geometry must be drawn from farthest from the viewer to closest. This is in stark contrast to normal operation -- for transparent or opaque geometry, we can draw the closest objects first, and the graphics card makes sure the far away objects don't "paint over them". But the technology that does this (z-buffering) doesn't work when the closer geometry is translucent. (If the translucent geometry is drawn first, it acts opaque to what is behind it.) For more info on this phenomenon and what to do about it, look here.

So in order for our translucent cockpit objects to draw correctly they need to be drawn from "back to front". But note that this term is relative to the camera -- which object is closest will depend on where the camera is located!

The other cause of cockpit object weirdness is the near clipping plane. At any time when X-Plane is drawing, there are two limits on where we can draw:
  • The near clipping plane defines an invisible wall - anything closer to the viewer than this distance will not be drawn.
  • The far clipping plane defines the other invisible wall - anything farther from the viewer than this distance will not be drawn.
The far clipping plane is usually set far enough away that objects disappear into the fog before hitting it. The near clipping plane is usually set close enough that by the time an object hits it, your plane has crashed.

Now here's the rub: the quality with which the graphics card can discriminate which polygon is closer (via z-buffering) goes down as the ratio of the far to near clipping plane gets larger!

Take a second to think about that. Basically if we want to increase the visibility in X-Plane without losing z-buffering fidelity ,we need to move the near clipping plane farther away from the user.

The real problem is this: X-Plane, with its sometimes long visibilities (when you get up into orbit, you can see a long way!!) really stretches the z-buffering fidelity of even modern cards. So we have to keep the near clip plane fairly far from the viewer in order to have the world look reasonable. But that distance might be a lot larger than the distance from the viewer to the interior of the cockpit!

We work around this by having two separate drawing passes. We first draw the "outside" world, with a near clipping plane that is fairly far away. (Every now and then a user tells me that this clip plane is causing scenery not to be drawn.) We then draw the cockpit interior and the user's plane, with the near and far clip plane both reset to be a lot closer. This way we can use our "z-buffer precision" in different ways for different geometry.

(It should be noted that z-buffering does not correctly handle the relationship between near and far objects when we reset the near and far clip plane. This technique works because we assume that everything drawn in the "cockpit" view will draw over everything drawn in the "outside" view.)

In my next post I will explain what X-Plane actually draws. For now suffice it to say that X-Plane has the task of drawing from farthest to nearest, but also the task of drawing in two phases: a far-away outside view and a close inside view.

No comments: