# Triangle Clipping

Hallo everyone,
I'm doing a software 3d-engine for my old Atari Falcon030 and after getting up to work 3d trasformation, 2d projection, 3d-2d line clipping, shaded wire-frame rendering, shaded particle-rendering, spherical lightning on asm; I felt pretty kool and said to myself: time to kill my cpu and draw some shaded poligon on the screen.
So I coded a test 2d drawing rutine something like this:
draw_triangle(x0,y0,color0,x1,y1,color1,x2,y2,color2)
{
blabla
draw_scann-line(sx0,scolor0,sx1,scolor1,sy)
blabla
}

And all worked fine until I thought about clipping:
First of all I thought it would be enough to clip by the two y's planes 'cause x-clipping a scannline is child's play, but when I realized that I had to clip also by the z's plane to overcome the z<=0 problem, I become very confuse.
I tryed to get working a clipping rutine based on the line's one. But the resulting poligon was a n-sided one (3<=n<=6).
Then I tryed to code a rutine to split it into triangles.
After 500 lines of code I stopped and wrote this post, I may be on the right way but before continue on this way I'd like to know if there is a better way 'cose this one is full of condition and exception...not very elegant.

I know, my english is not very correct, sorry for that.
If i wasn't very clear here the rude question:
How the hell I perform a 3d-clipping uppon a triangle ?
(by clipping dont mean find out if the triangle is inside the screen, but cutting it in the way it's drawed inside the screen)

• : Hallo everyone,
: I'm doing a software 3d-engine for my old Atari Falcon030 and after getting up to work 3d trasformation, 2d projection, 3d-2d line clipping, shaded wire-frame rendering, shaded particle-rendering, spherical lightning on asm; I felt pretty kool and said to myself: time to kill my cpu and draw some shaded poligon on the screen.
: So I coded a test 2d drawing rutine something like this:
: draw_triangle(x0,y0,color0,x1,y1,color1,x2,y2,color2)
: {
: blabla
: draw_scann-line(sx0,scolor0,sx1,scolor1,sy)
: blabla
: }
:
: And all worked fine until I thought about clipping:
: First of all I thought it would be enough to clip by the two y's planes 'cause x-clipping a scannline is child's play, but when I realized that I had to clip also by the z's plane to overcome the z<=0 problem, I become very confuse.
: I tryed to get working a clipping rutine based on the line's one. But the resulting poligon was a n-sided one (3<=n<=6).
: Then I tryed to code a rutine to split it into triangles.
: After 500 lines of code I stopped and wrote this post, I may be on the right way but before continue on this way I'd like to know if there is a better way 'cose this one is full of condition and exception...not very elegant.
:
: I know, my english is not very correct, sorry for that.
: If i wasn't very clear here the rude question:
: How the hell I perform a 3d-clipping uppon a triangle ?
: (by clipping dont mean find out if the triangle is inside the screen, but cutting it in the way it's drawed inside the screen)
:
:
:

Since you are on my favourite subject i'll gladly help you.First, the only way to clip a triangle is to create a convex polygon wich has 3-4 vertices(3-6 if you clip it against 2 planes). Now you can split it in triangles as you said or ,since you're doing a software renderer, you could draw it as polygon(not too hard). To split it in triangles just create n-3 triangles using points 0, i, i+1 (i=1..n-1), but remember this works only on convex polygons.(500 lines for that !?, lol)

• :(500 lines for that !?, lol)

That's Assembler, what can we do about it
ThX again, now I take a long breath and fall deep into the code.