I think I have mentioned draping repeatedly, as well as Z thrash, without ever clearly explaining what either of these things are.  I happen to have some pictures from recent work on draping, so here’s the basic situation.

## What Is Thrash?

Warning to OpenGL purists: I’m going to play fast and loose with a number of OpenGL rules.  Basically what I say here is the worst case.  Sometimes things work out better than I describe, but if you don’t handle the worst case, you’re going to have problems some times.  Nitpickers, consider yourself “on notice”.

When drawing 3-d polygons in OpenGL, if two polygons are coplanar, most of the time they will experience “Z thrash” – that is, the hidden surface removal hardware on the graphics card removes some parts of one polygon and some parts of the other, often removing different parts on each frame in a flickering pattern.  The results look like this:

In this picture on the left, we see the raw result of coplanar airport polygons on top of the sandy desert terrain that is the airport surface area.  As you can see, the graphics card has been a bit random in deciding whether the runways are “hidden” by the ground or not.  If you could see the camera moving, you would see that the pattern of missing taxiways ‘flickers’ as the camera moves.

If there is an overall take-away point so far, it’s this: you can’t just layer coplanar triangles without getting a mess.

(What about moving the runway slightly higher than the pavement?  This would have several problems.  First, the amount we’d have to move them is horribly dependent on camera angle and video card Z-buffer performance.  Second, we’d see the gap between layers; for markings that really do need to be coplanar, like a rubber mark on a taxiway, this would appear as a bug.)

## The Cure is Worse Than the Disease

Fortunately if we have truly coplanar geometry, OpenGL gives us a weapon to fight this: polygon offset.  Polygon offset is a cheat that tells the video card: “listen, if you ever have doubts about the runway and the ground, favor the runways by a little bit.”  It’s a little bit like putting a spread on a football game: the ground would have to be closer than the runway by a margin before we stop seeing.

When we have coplanar geometry, we can use polygon offset to handicap the geometry we want on top.  Problem solved, right?  So why do I keep writing blog posts with titles like: The Sordid History of ATTR_poly_os or The road to hell is paved with ATTR_poly_os?

Polygon offset has three problems that can bite you:

1. The amount of ‘margin’ you get depends on the camera angle.  This can make for some truly bizarre effects.  The only way to safely manage this is to carefully minimize the margin.  (This is a problem for the programmer, not the artist.)
2. Because the margin must be minimized, the geometry really has to be coplanar – small cheats can exceed the margin.
3. The margin you give your top polygon applies to anything else that is nearby, which can cause even more weird artifacts.  For example, if you polygon offset a taxiway, it can start appearing through the nose wheels of airplane.  That’s another aspect that must be carefully controlled.

If there’s a take-away to polygon offset, it’s this: polygon offset can help fight Z-thrash, but it must be used in a very narrow set of cases, and it must be used very carefully.

In my next post I’ll describe how X-Plane uses draping to fight Z-thrash for sloped runways.

Ben is a software engineer who works on X-Plane; he spends most of his days drinking coffee and swearing at the computer -- sometimes at the same time.

## 7 comments on “Draping Part 1: Z Thrash”

1. Thanks for the explanation Ben. Can poly offset be used on aircraft objects too, to reduce the thrashing often seen from tower view? Thanks.

1. It _can_ be used in an aircraft, but it will _not_ fix the from-tower thrash. The from-tower-view thrash is an X-Plane bug…basically in the tower view, the precision of the Z buffer is too low at the airplane. If you use poly_os to ‘fix’ this, besides causing a number of bizarre problems (parts 2 and 3 can help explain this), you’ll replace one artifact with another. This bug really needs to be fixed in-sim by LR.

1. Thanks for the info Ben, i appreciate it. Are there any plans to look at the tower view thrashing for V10? Thanks.

2. Chris Hallam says:

Hey ben, interesting piece. Helps explains the “flickering” issues I have seen. Also, when changing the time of day when on an aircraft carrier opp. The ship bounce’s all over the place and loud noises occur. Also, sometimes weapons and wing pylon graphics flicker and are distorted ( * this has not happened for quite some time, so hoping that is fixed)

also, when will you have more eye appealing cool screen shots of v-10?

1. *pops open the bottle of scotch…*

3. Chris Hallam says:

And enjoy the scotch over some tasty news or images, even new images of a neighborhood, plane, etc or a description of what exactly will be different about global scenery in v10 vs all others. such as the current scenery is very very repetitive and un real, baseball fields over exaggerated for example. Other than landscape such as mountains, hills, city vs open.. its the same scenery everywhere. Would love to get more details seeing its coming up on 3 months since jan release, there has to be something new.