Backface Culling in Clipping Space?

Started by
5 comments, last by carb 18 years, 9 months ago
Hi there, Could use some help from those of you here familiar with basic 3D rendering theory :) I'm writing a software rasterizer and I'm trying to figure out how to perform backface culling in clipping space (i.e. after multiplying a triangle's vertices by the modelview and projection matrices). Basically, where is the camera? In eye space (after modelview transform), the camera is at (0, 0, 0, 1). I assumed I just need to multiply that value by the projection matrix to obtain the new camera location. (Which is just the 4th column of the projection matrix). I've tried this, but it doesn't seem to work correctly - the result is generally correct, unless I mess around with the projection matrix. Am I missing something here? Thanks, carb
- Ben
Advertisement
Backface culling in clip or screen space can be done as follows:

1. transform the vertex positions of your triangle by modelview and projection.

2. take a cross product of two of the edges of the triangle.

3. the sign of the Z component of the cross product tells you whether the triangle is back or front facing.

Since you only care about the Z component, you can drop all other components to simplify the test.



An alternative if you already have object (model) space face normals is to backface cull before any transformation of the model:

1. rotate the camera view direction vector into model space (transforming one camera vector is cheaper than transforming multiple face normals). The inverse of the top left 3x3 part of the world matrix will transform from world space into model space; the transpose of the top left 3x3 will work if the matrix is only composed of rotations and translations.

2. test the sign of the dot product between each face normal and the camera



Oh, and an important tip for debugging stuff like this: make sure your rasterizer/renderer can draw lines - and then draw all vectors and matrices used for computation as lines - it can really help your intuition of what's going on; just doing that has personally saved me days of head scratching and debugging.

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This is the article for you....

There's so much wisdom in that thing it's astonishing.

You're welcome ;P
------------------<a href="http://jsgc.sourceforge.net>jsgc
Yeah, I know most of the theory, and I've tried taking the Z component alone - but the results don't look right for some reason.

Here's another problem I've come across: Assume I do take the vector from a point on the triangle to the camera, followed by the dot product of that and the triangle normal. That makes perfect sense. However! What if the triangle lies on the XY plane with Z=0? Then the vector from a vertex to the camera will be (a, b, 0), and the normal from the triangle will be (0, 0, c), for some a, b, c. The dot product of that is 0. In particular, the result will be zero regardless of whether the vertices were drawn clockwise or counterclockwise. So what's the behaviour then? Since it's "on" the camera, it's neither backfacing nor frontfacing. Should I just consider that a special case and move on? ;) [phew]

Thanks,
Ben
- Ben
First thing: in eye space I believe the camera is at (0 0 -1 0). Yep, that's minus infinity along the Z axis. Getting the camera position therefore involves multiplying this vector through the inverse transpose of your transformation matrix. However, testing the Z component of the normal in eye space should produce perfect backface culling. If the dot product in the 1st case or Z component in the 2nd case is zero cull the polygon - this means the camera's looking at it side on.
Quote:First thing: in eye space I believe the camera is at (0 0 -1 0). Yep, that's minus infinity along the Z axis.


Just about every article I've read puts the camera at (0, 0, 0).

Quote:However, testing the Z component of the normal in eye space should produce perfect backface culling. If the dot product in the 1st case or Z component in the 2nd case is zero cull the polygon - this means the camera's looking at it side on.


I've tried this, and it works for now, but it doesn't product perfect results. Some triangles get culled which shouldn't. I've stared at this code for hours now ... ;)
- Ben
I think the issue I was having is that I was attempting to backface culling in clip space but *before* division by W, which was incorrect.

Thanks all for your help.
- Ben

This topic is closed to new replies.

Advertisement