Wednesday, April 6, 2011

Fun with the Far Clipping Plane

Historically GLQuake sets it's far clipping plane distance to 4096. In the era of big maps this is woefully inadequate and is easily exceeded by maps that don't even stretch other limits by too much (I'm thinking The Marcher Fortress here).

Some engines resolve this by pushing the far clipping plane out further - to 8192, 16384 or whatever. I don't like that because it's really just a war of attrition; an arms race between engines and maps in which each tries to outdo the other. Other engines resolve it by having an "r_farclip" cvar which allows the player to set a further clipping plane, but I don't like that either as it smells too much like "I couldn't be bothered fixing it properly so I'll make it the player's problem instead" for my tastes.

Previously I've resolved it by using an infinite projection. I was always unhappy with that setup as it scrunches all distant geometry at the far end of the depth buffer and may cause Z-fighting problems. Z-fighting is also an issue with setting your far clipping plane further and further away. Because the depth buffer is non-linear it has much higher resolution for nearer distances but drops off quickly for further ones.

So to fix it properly I've gone back to an old idea where I just get the distance to the furthest surface (well, plane, actually), use the knowledge that it represents one side of a right-angled triangle, invoke Pythagoras, and derive a dynamically adjusting far clipping plane from that.

So now if the current scene needs a far clipping distance of 80 billion, you'll get it. On the other hand if only 22 is what's needed, that's what you'll get too. No need for you to do anything, and no need to compromise the quality of the scene.

An interesting note here regarding sky. One other reason for a large far clipping plane is to accomodate skyboxes, which commonly need to be drawn as the farthest object in the scene. I don't need it for that reason because I'm actually drawing skyboxes as cubemaps on the original sky surfaces. So yeah; that skybox that looks so far away might really just have it's actual surfaces a mere 20 units above your head.

2 comments:

=peg= said...

Sounds neat! Hope it works out well :)

Jasmine said...

Great way of algorithmically handling it! And even in the case of the vanilla skyboxes you could use your technique as stated and then increase the result by some amount. :)