Sign in to follow this  
King_DuckZ

How does clipping fit into the pipeline?

Recommended Posts

King_DuckZ    128
Hello everybody, I'm trying to figure out how to implement clipping in my code. My understanding is that when a triangle crosses a clipping plane (ie the frustum), new vertices are added to avoid disappearing triangles artifacts. This is done on the CPU (correct?), and any object is eligible for clipping. But my question is: doesn't this make GL_STATIC_DRAW flag utopic? I mean, an object standing on the screen border gets clipped every frame, so its vertex data is far from being static (new vertices added and removed continuously).
Moreover, how does it fit with skinned meshes or any other GPU-generated vertices? Anyone got a good reference on clipping? All I've managed to find is either abstract theory or very basic examples.

Share this post


Link to post
Share on other sites
Hodgman    51334
The GPU clips primitives automatically. AFAIK, there's no way to turn it [i]off[/i].
Manual clipping is only required for software renderers (not GL/D3D).

In GL, after your vertex shader, the transformed vertices are in Normalized Device Coordinates (NDC), which is a cube from (-1,-1,-1) to (1,1,1). Any primitives that clip the edge of this cube are handled appropriately by the GPU's rasterization hardware.

Share this post


Link to post
Share on other sites
King_DuckZ    128
Ok, thanks for clarifying! I can't find it right now, but one of the tutorial I stumbled upon stated that clipping was necessary in order for the rasterizer to remain inside its buffer. Somehow, I understood it was my responsibility to do that, not the hardware's. All the better then, I'll just remove invisible polygons and leave partially visible polygons as they are! Edited by King_DuckZ

Share this post


Link to post
Share on other sites
tanzanite7    1410
[quote name='King_DuckZ' timestamp='1348229245' post='4982334']
Hello everybody, I'm trying to figure out how to implement clipping in my code. My understanding is that when a triangle crosses a clipping plane (ie the frustum), new vertices are added to avoid disappearing triangles artifacts.
[/quote]
This is how OpenGL is specified - however, no-one actually does it that way (for eons). "Guard bands" are used ( http://en.wikipedia.org/wiki/Guard_band_clipping ). For normal frustum - there are no actual clip-planes used at all (even for near and far plane [i do not know any official names for it tho] - nowadays one can even turn that part off in OpenGL).

Share this post


Link to post
Share on other sites
mhagain    13430
[quote name='King_DuckZ' timestamp='1348231914' post='4982353']
Ok, thanks for clarifying! I can't find it right now, but one of the tutorial I stumbled upon stated that clipping was necessary in order for the rasterizer to remain inside its buffer. Somehow, I understood it was my responsibility to do that, not the hardware's. All the better then, I'll just remove invisible polygons and leave partially visible polygons as they are!
[/quote]

A lot of tutorial material is woefully out-of-date - either the tutorial was written sometime back in the 90s, or - if more recent - the person writing it hasn't bothered updating their understanding of things since that time. Back then clipping would certainly have been done on CPU, but by your driver, not by you. Since the advent of consumer-level hardware T&L much of the "knowledge" accumulated at that time is now completely irrelevant (or worse, it may lead the unwary to do awful things in their own code) so always be careful to double-check tutorial sources before you put much weight into what they say.

Share this post


Link to post
Share on other sites
Geometrian    1810
[quote name='Hodgman' timestamp='1348229811' post='4982339']In GL, after your vertex shader, the transformed vertices are in Normalized Device Coordinates (NDC), which is a cube from (-1,-1,-1) to (1,1,1). Any primitives that clip the edge of this cube are handled appropriately by the GPU's rasterization hardware.[/quote]Not exactly. The transformed vertices are in clip space, which is a 4D space where every 3D point has a w value. Clipping happens here. The perspective divide happens AFTER this (divide everything by w), bringing it to (eventually) NDC.

It's an important distinction because negative w values are behind the camera and so are invisible. If clipping happened after the perspective divide, then vertices behind would be divided by negative w values and suddenly become visible!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this