Sign in to follow this  
fuhbaer

2D polygon drawing/ rastering algorithm with proper antialiasing

Recommended Posts

Hi folks, I searched the internet quite some time now w/o real success. For a toy project of mine I'm looking for a simple 2D polygon (usually 3 to 4 edges/points or more) drawing algorithm with proper antialiasing. The result should look somehow like this: http://www.binaryriot.org/polygons.png I have: - ARGB or RGBA pixelbuffer (linear array of 32bit longwords) - polygons I want to draw are all convex - polygon points have floating point precision - antialiasing shall define with which intensity (ALPHA component) the RGB values are rendered into the pixelbuffer - no need for clipping, all points are inside the pixelbufferarea - preferably something precedural single-function-ish (C, pascal, etc., I can convert as long its not some C++) maybe like this? :) void drawpolygon(unsigned long *argbbuffer, int bufferwidth, int bufferheight, int numpoints, float *polygonpoints, unsigned long color); I looked into Antigrain and cairo libraries, but they are far too complex for my purpose and stuff is not easy to rip off w/o getting to deal with tons of dependencies and license issues. I also found the AAPolyScan from Graphic Gems, but I have to admit I don't understand how to use/ extend it properly to make it work. If somebody has some simple 'ready to use' solution.. this would be very appreciated. I really don't have much time to try to get some own lausy algorithm together (have one w/o antialiasing atm. which works but not very good). With best regards, fuhbaer

Share this post


Link to post
Share on other sites
I had to do this once some time ago, and I just supersampled the edges to handle antialiasing. Its not a great solution really, but I don't know of a better one.

If you already have an algorithm, all you need to do is check when you reach a pixel that's just outside of the polygon and then sample several points within it. Take the number of points that were inside the polygon and divide by the number of points you checked; that'll give you your alpha value.

Share this post


Link to post
Share on other sites
One idea that crossed my mind while reading this is this:

for the left side of each scanline find the last whole pixel that lies completely outside the edge and the first whole pixel that lies completely inside the edge then interpolate the alpha value from 0.0 to 1.0 across those pixels and use the alpha value to blend the pixels current colour with the polys colour. I see problems with edges that are closer to vertical than horizontal though.

I think to do it more accurately you'd need to work out the coverage of the first partially covered pixel and the last partially covered pixel and interpolate between those values.

The same would have to be done for the right hand edge too, just reversed.

I may look into this myself as I'm using bilinear filtering and the jaggies on the edges spoil the image slightly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this