2D polygon drawing/ rastering algorithm with proper antialiasing

Started by
2 comments, last by DeathRay2K 16 years, 2 months ago
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
Advertisement
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.
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.
Unfortunately there are problems with that approach, such as near-vertical lines, as well as antialiasing the vertices.

This topic is closed to new replies.

Advertisement