Skip to main content
GameDev.net gamedev.net
🔒 Locked

Backface culling problem

Started by gonssas Jan 10, 2007 at 10:35 AM 4 replies 850+ views
Original Post
gonssas
gonssas
Hello, I am trying to code backface culling using only software rendering - No directx, no opengl. So I splitted a cube into triangles. Each face has two triangles. Calculation formula (found online) display = y1*(x3 - x2) + y2*(x1 - x3) + y3 *(x2 - x1); Some screenshots of teh results. screenshot "if display > 0" http://img305.imageshack.us/img305/6893/morethanzeroyo8.jpg screenshot "if display < 0" http://img305.imageshack.us/img305/2664/lessthanzerogj3.jpg Triangules are organized like this Face is square defined by P0, P1 P2 P3. P0---P1 | | | | P3---P2 T = P0 P2 P1 T' = P0 P3 P2 Example Front Face Triangules T = 0 1 2 T' = 0 3 2 Example Back Face Triangules T = 5 7 6 T' = 5 8 7 Where the point data is: P0 = -1, 1, 1 P1 = 1, 1, 1 P2 = 1, -1, 1 P3 = -1, -1, 1 P5 = -1, 1, -1 P6 = 1, 1, -1 P7 = 1, -1, -1 P8 = -1, -1, -1 Thanks for your comments. [Edited by - gonssas on January 10, 2007 10:51:17 AM]
gonssas
gonssas
Quote:
Original post by TheAdmiral
And what's the problem?


Doesnt work. Faces are not correctly taken off.

Those screen shots are not up to date since I did some changes in the order of vertices.

Using perspective projection.

Initial value
After rotation x
After some other rotations x

The logic is

For Each Triangle    X1 = Triangle[0].X / Triangle[0].Z;    X2 = Triangle[1].X / Triangle[1].Z;    X3 = Triangle[2].X / Triangle[2].Z;    Y1 = -Triangle[0].Y / Triangle[0].Z;    Y2 = -Triangle[1].Y / Triangle[1].Z;    Y3 = -Triangle[2].Y / Triangle[2].Z;    display = y1*(x3 - x2) + y2*(x1 - x3) + y3 *(x2 - x1);    if ( display > 0 ) {      /* draw */    }
TheAdmiral
TheAdmiral
Is there a reason you're using the projective method for back-face culling? It's ever-so-slightly more robust, but it's quite a lot more costly, and evidently not so easy to implement. Since you're writing a software renderer, I recommend you use the cross-product-based method:

A triangle should be culled iff 'it' faces away from the camera;
iff its face normal points away from the camera
iff the z component of its face normal is positive (respectively, negative)

So we are required only to calculate the sign of the z component of its face normal. This can easily be extracted from the cross-product of two given edges:

n = (v3 - v1) ^ (v2 - v1)
n . ez = (v3.x - v1.x)(v2.y - v1.y) - (v3.y - v1.y)(v2.x - v1.x)

in pseudocode:
For Each Triangle{    X1 = Triangle[2].x - Triangle[0].x;    Y1 = Triangle[1].y - Triangle[0].y;    Y2 = Triangle[2].y - Triangle[0].y;    X2 = Triangle[1].x - Triangle[0].x;    display = X1 * Y1 - Y2 * X2;    if ( display > 0 ) {      /* draw */    }}
I'm not sure on your handedness, so that inequality may need to be inverted.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
gonssas
gonssas
Quote:
Original post by TheAdmiral

A triangle should be culled iff 'it' faces away from the camera;
iff its face normal points away from the camera
iff the z component of its face normal is positive (respectively, negative)

So we are required only to calculate the sign of the z component of its face normal. This can easily be extracted from the cross-product of two given edges:

n = (v3 - v1) ^ (v2 - v1)
n . ez = (v3.x - v1.x)(v2.y - v1.y) - (v3.y - v1.y)(v2.x - v1.x)



Hey, thanks, its working now. I would like to know more about that formula.

What's the 'ez'?

Thanks.
TheAdmiral
TheAdmiral
I'm glad to hear it's working.
ez is notation for the third unit-vector (z-direction) in the standard Cartesian basis:

ez = (0, 0, 1)T

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.