🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

2D polygon clipping

Started by
2 comments, last by DaBit 24 years, 5 months ago
Hi guys, Maybe this sounds a bit weird in the current hardware-3D world, but I need a fast algorithm/routine to clip a flat-shaded triangle against the viewport edges. This is thus a 2D problem. I was thinking of intersecting the triangle''s edges with lines, building a new polygon out of the intersection points and tesselating that one into triangles. Does anyone have a better idea? This seems slow to me, and I need it to be fast. What I mean is this: ------------ / *****/** / * * <- clipping occurs here / * / ------------ This would result into a 4-sided polygon that can be tesselated into two triangles. Or maybe I can get away with an S-buffer? (see http://www.grafix3d.tzo.com Thanks in advance, DaBit.
Advertisement
First off, how do you render your polygons?

If you''re like me, and translate each polygon
into a buncha horizontal (or vertical) lines, then
theres an easier way to do this.

You can just clip each line so that it fits
into the view or screen or whatever. If you need
some source code, or more information, just
post again!

You dont really need to make multiple triangles
to render one thats been clipped.
http://fakemind.com
Well it depends on whether you have a filled in polygon which it sounds like you do or if its hollow.

A hollow one, just clip the lines like the above poster said. If its filled in, there is probably an easier way.

If you are filling it, I assume you are doing it one horizontal line at a time. (Fastest way I know how)

When you start each row, you could say, well am I off the left, if so, than just skip those pixels up to the start point, if my width goes off the right, than don''t draw so far, if I''m above the top of the screen, skip the row, and if I''m below the bottom stop drawing.

The problem with this method is you test every row. But its pretty simple tests. Creating a new polygon is faster in one sense because you take care of the clipping right away, and can just focus on drawing. Another way is to figure out whether you have to clip at all first, and if not, just skip all this clipping code, and only hit it when you need it. That way it only slows down when there is stuff to clip, but clipping is usually faster than drawing anyway, so maybe that''s alright.

Well, that''s what I came up with. Might not be what you want though.
Thanks all for the response.

Yes, I need to render flat-shaded filled polygons. Stupid me, of course clipping spanlines against the window edges is possible.

Next question: While delving deeper into this stuff, I came to the conclusion that S-buffers are the better way to solve this. (There is a S-buffer FAQ written by Paul Nettle somewhere on the internet, for the interested people)
Problem: Is there a better (faster) way of segment insertion than what he describes (linked list)?

DaBit.

This topic is closed to new replies.

Advertisement