PDA

View Full Version : basic scanline polygon filling



04-05-2001, 08:32 PM
I'm learning openGL basics, and I am supposed to fill non-convex and non-simple 2D polygons using scanlines and edge tables/lists. Can anyone help me figure out how to implement these tables in C? My programming is rusty and my textbook provides poor examples. Please point me in the right direction!!

mango
04-07-2001, 06:05 PM
This has really nothing to do with OpenGL. Here is a function from the allegro library ( http://www.talula.demon.co.uk/allegro/ )

/* polygon3d:
* Draws a 3d polygon in the specified mode. The vertices parameter should
* be followed by that many pointers to V3D structures, which describe each
* vertex of the polygon.
*/
void polygon3d(BITMAP *bmp, int type, BITMAP *texture, int vc, V3D *vtx[])
{
int c;
int flags;
int top = INT_MAX;
int bottom = INT_MIN;
V3D *v1, *v2;
POLYGON_EDGE *edge, *edge0, *start_edge;
POLYGON_EDGE *list_edges = NULL;
POLYGON_SEGMENT info;
SCANLINE_FILLER drawer;

if (vc < 3)
return;

/* set up the drawing mode */
drawer = _get_scanline_filler(type, &flags, &info, texture, bmp);
if (!drawer)
return;

/* allocate some space for the active edge table */
_grow_scratch_mem(sizeof(POLYGON_EDGE) * vc);
start_edge= edge0 = edge = (POLYGON_EDGE *)_scratch_mem;

/* fill the double-linked list of edges (order unimportant) */
v2 = vtx[vc-1];

for (c=0; c<vc; c++) {
v1 = v2;
v2 = vtx[c];

if (_fill_3d_edge_structure(edge, v1, v2, flags, bmp)) {
if (edge->top < top) {
top = edge->top;
start_edge = edge;
}

if (edge->bottom > bottom)
bottom = edge->bottom;

if (list_edges) {
list_edges->next = edge;
edge->prev = list_edges;
}

list_edges = edge;
edge++;
}
}

if (list_edges) {
/* close the double-linked list */
edge0->prev = --edge;
edge->next = edge0;

/* render the polygon */
do_polygon3d(bmp, top, bottom, start_edge, drawer, flags, vtx[0]->c, &info);
}
}