Z-Clipping

The main purpose of z-clipping is to prevent a triangle from trashing memory and drawing out of the screenspace.

Okay, now let's get busy.

First I'll explain what the intersection is: The intersection is the point where the line crosses the plane:

```     A
\
\  C
-------\--------   it's the z-plane
\
B```

Now the intersected point is C. So you draw a new line AC instead of AB.

How do we calculate the intersected point? Well, easy: There is a parametric line formula:

```       intersected_x = x1 + t * ( x2 - x1 );
intersected_y = y1 + t * ( y2 - y1 );
intersected_z = z1 + t * ( z2 - z1 );```

You probably ask yourself: "How the fuck do I calculate the t or maybe not?" The answer is simple:

```       z_min = z1 + t * ( z2 - z1 );
z_min - z1 = t * ( z2 - z1 );
t = (z_near - z1 ) / ( z2 - z1 );
z_near and z_min are eqaul```

Think of your triangle as three lines. Just make a procedure for finding the intersected points.

```  point3d TMP;

int z_min=15; // try different values

void clip( point3d *src , point3d *dst )
{
float t = (float)( z_min - dst->z ) / (float) ( src->z - dst->z );

TMP.x = dst->x + t * ( src->x - dst->x ) ;
TMP.y = dst->y + t * ( src->y - dst->y ) ;
TMP.z = dst->z + t * ( src->z - dst->z ) ;
TMP.u = dst->y + t * ( src->u - dst->y ) ;
TMP.v = dst->v + t * ( src->v - dst->v ) ;
}```

How we do it is simple. But first watch this:

```             Y
/|\
|
|3
|2
|1
---------|--------->X
|-1
|-2
|-3
|-4```

So you see: All the axes here are reversed. Unlike in computer, the bigger the Y-axis is, the more below it is.

Just sort your triangle lines by their Z-values as:

A - Top
B - Middle
C - Bottom

If you don't know how to sort, then contact me. ;)

Okay, now let's get to the main part. First we should check if all the triangle is behind the z_min:

So

if ( A.z < z_min ) and ( B.z < z_min ) and ( C.z < z_min ) continue;

because all the triangle is behind the plane and it's invisible.

```   if ( A.z < z_min ) continue ; // its invisible

if ( C.z > z_min )
{
put your triangle on the screen, it's fully visible

triangle(A,B,C);

continue;
}

if ( B.z < Z_min) {

// it needs to be clipped
// you need to find the intersected points now```

```    clip (B,A);

E=TMP;

clip (C,A);

F=TMP;

draw_triangle(A,E,F);

continue;

}```

If it still hasn't continued, we will get a quad:

Just find the intersected points and draw two triangles.

For comments, insults, threats, email bombs or if you need help, contact me.

Greetings go to: kalms, kombat, sosay, velocity, mrz_ai, phred, nick-s, borzom, civax, smallbrain, yoyo, trashey, cycat, adok, cube, radium, lord^crc, gfunk, coaxcable, adept, trip, satanic jungle, gyr, octan, bananmos, biomass, idiotboy, gash, macaw, matja, parody, tanis, timluther, TNSe^98, xls, zippy, sarix, sagacity, gash, asm[exe], the lotus. I hope that I didn't forget somebody.

- MasterBoy
ICQ: 14054887