# 3D Clipping

This topic is 5394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I have been reading gamedev.net for some time now (and gained valuable insights, thanks) and finally decided to take a more active stance and start participating on the forums. So, I have a question to ask: I have an old P166 machine with win98 and no hardware acceleration at my disposal where I work, and decided to write a DOS-based software scanline renderer for fun. I implemented a 3D frustum clipper using an algorithm I came up with after some thinking. It works nicely but I wonder if it could be sped up somehow by modifying it or using a different algo altogether. The algorithm is currently as follows (in pseudo):
clip3d(list<vertex> &L)
{
for(each frustum plane P)
{
for(each consecutive pair of vertices V,W in L)
{
if(edge V,W intersects P)
calculate new vertex and insert into L between V and W
}
for(each vertex V in L)
{
if(V outside P)
erase V
}
}
}

It is quite nice in that it handles arbitrary n-gons, but I don't strictly need that feature. Triangles are enough for my purposes. The intersection calculation itself is quite optimized (using the knowledge that frustum planes are of form x=±z,y=±z etc) but if you know any handy tricks that I might not be aware of, I'd like to hear :) BTW, the clipper may obviously spit out n-gons with n>3. Currently I render them as a triangle fan, but should I implement a generic convex poly rasterizer instead? [Edited by - Sharlin on October 8, 2004 2:19:26 PM]

##### Share on other sites
Hi Sharlin,

You don't have to explicity insert and eraze vertices. Just start with an empty second list (best just make that a statically allocated buffer), and add vertices to it. When an edge is completely visible, add the end vertex. If the edge goes from inside to outside, compute the intersection and add that. If an edge is completely outside, add nothing. And when an edge goes from outside to inside, calculate the intersection, add it, and also the end vertex of that edge. This is the Sutherland-Hodgman algorithm. It works for convex polygons. When you start with a triangle, every new clipped polygon (per plane) will be convex too.

For rasterization, the easiest and at the same time most accurate way is to render convex n-gons as a triangle fan. Trust me, it's too much trouble and doesn't gain you much by rasterizing them as one whole.

##### Share on other sites
Thanks, that sounds good. I had something similar in mind but couldn't get it to work in all special cases.

• 21
• 13
• 9
• 17
• 13