PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Complex Polygon in PocketFrog

Does anybody have complex Polygon function?
You probably want to split your complex polygons into convex polygons. This will be far more efficient.
I can't split polygons.
I want to draw complex polygon as CDC function Polygon.
for example, using this points, function must draw 2 separate squares:
points[0].x = 10;
points[0].y = 10;
points[1].x = 110;
points[1].y = 10;
points[2].x = 110;
points[2].y = 110;
points[3].x = 10;
points[3].y = 110;
points[4].x = 10;
points[4].y = 10;

points[5].x = 120;
points[5].y = 120;
points[6].x = 220;
points[6].y = 120;
points[7].x = 220;
points[7].y = 220;
points[8].x = 120;
points[8].y = 220;
points[9].x = 120;
points[9].y = 120;

and using this - frame (20 pix width)
points[0].x = 0;
points[0].y = 0;
points[1].x = 100;
points[1].y = 0;
points[2].x = 100;
points[2].y = 100;
points[3].x = 0;
points[3].y = 100;
points[4].x = 0;
points[4].y = 0;

points[5].x = 20;
points[5].y = 20;
points[6].x = 80;
points[6].y = 20;
points[7].x = 80;
points[7].y = 80;
points[8].x = 20;
points[8].y = 80;
points[9].x = 20;
points[9].y = 20;
As I understand it Frank's polygon code is quite simple however I'm not so sure about the Asteroids polygon code however that source is not easy to find as it was written for a very early PF & the orig. download is no longer there. I might have it at home though.
Are there any constraints on the polygons you're looking to draw? Otherwise, I think T-dogg has the right idea. If you want the border line, just throw in edge flags or something.
I load data from file and i cant change it. Yearly, my program used dc, but it too slowly...
I write this function using Curt McDowell code and it faster than FillPoly in PF =)



Code:
#define MAXVERTICAL 1024

void FillEdges( Point p[], int vertex_cnt, edge *edgeTable[])
{
int i, j, n = vertex_cnt;

for (i = 0; i < MAXVERTICAL; i++)
edgeTable[i] = NULL;

for (i = 0; i < n; i++)
{
Point *p1, *p2, *p3;
struct edge *e;
p1 = &p[i];
p2 = &p[(i + 1) % n];
if (p1->y == p2->y)
continue; /* Skip horiz. edges */
/* Find next vertex not level with p2 */
for (j = (i + 2) % n; ; j = (j + 1) % n)
{
p3 = &p[j];
if (p2->y != p3->y)
break;
}
e = new (struct edge);
e->xNowNumStep = abs(p1->x - p2->x);
if (p2->y > p1->y)
{
e->yTop = p1->y;
e->yBot = p2->y;
e->xNowWhole = p1->x;
if ((p2->x - p1->x) < 0)
e->xNowDir = -1;
else if ((p2->x - p1->x) > 0)
e->xNowDir = 1;
else e->xNowDir = 0;
// e->xNowDir = SGN(p2->x - p1->x);
e->xNowDen = e->yBot - e->yTop;
e->xNowNum = (e->xNowDen >> 1);
if (p3->y > p2->y)
e->yBot--;
}
else
{
e->yTop = p2->y;
e->yBot = p1->y;
e->xNowWhole = p2->x;
if ((p1->x - p2->x) < 0)
e->xNowDir = -1;
else if ((p1->x - p2->x) > 0)
e->xNowDir = 1;
else e->xNowDir = 0;
// e->xNowDir = SGN(p1->x - p2->x);
e->xNowDen = e->yBot - e->yTop;
e->xNowNum = (e->xNowDen >> 1);
if (p3->y < p2->y)
{
e->yTop++;
e->xNowNum += e->xNowNumStep;
while (e->xNowNum >= e->xNowDen)
{
e->xNowWhole += e->xNowDir;
e->xNowNum -= e->xNowDen;
}
}
}
e->next = edgeTable[e->yTop];
edgeTable[e->yTop] = e;
}
}

struct edge *UpdateActive(struct edge *active, edge *edgeTable[], long curY)
{
struct edge *e, **ep;
for (ep = &active, e = *ep; e != NULL; e = *ep)
if (e->yBot < curY)
{
*ep = e->next;
free(e);
}
else
ep = &e->next;

*ep = edgeTable[curY];
return active;
}

void Rasterizer::DrawRuns(edge *active, long curY, Pixel pat)
{
struct edge *e;
static long xCoords[100];
long numCoords = 0;
int i;
for (e = active; e != NULL; e = e->next)
{
for (i = numCoords; i > 0 && xCoords[i - 1] > e->xNowWhole; i--)
xCoords[i] = xCoords[i - 1];
xCoords[i] = e->xNowWhole;
numCoords++;
e->xNowNum += e->xNowNumStep;
while (e->xNowNum >= e->xNowDen)
{
e->xNowWhole += e->xNowDir;
e->xNowNum -= e->xNowDen;
}
}
if (numCoords % 2) /* Protect from degenerate polygons */
xCoords[numCoords] = xCoords[numCoords - 1],
numCoords++;
for (i = 0; i < numCoords; i += 2)
{
/* Here's the graphics-dependent part. */
/* All we need is to draw a horizontal line */
/* from (xCoords[i], curY) to (xCoords[i + 1], curY). */
/* Example: I want to fill the polygon with a pattern. */
/* (This example is very slow because it's done point by */
/* point, thus not taking advantage of the potential for */
/* speed afforded by the scan conversion...) */
/*
for (x = xCoords[i]; x <= xCoords[i + 1]; x++)
if (ras_TestPointModulo(pat, x, curY))
SetPoint(r, x, curY);*/
DrawHLine(xCoords[i], curY, xCoords[i + 1] - xCoords[i], pat);
}
}

void Rasterizer::FillPoly( Point vertex_vector[], int vertex_cnt, Pixel color )
{
static struct edge *edgeTable[MAXVERTICAL];
struct edge *active;
long curY;

FillEdges(vertex_vector, vertex_cnt, edgeTable);

for (curY = 0; edgeTable[curY] == NULL; curY++)
if (curY == MAXVERTICAL - 1)
return; /* No edges in polygon */

for (active = NULL; (active = UpdateActive(active, edgeTable, curY)) != NULL; curY++)
DrawRuns(active, curY, color);
}
This must be added to rasterizer.h ;)

Code:
struct edge {
struct edge *next;
long yTop, yBot;
long xNowWhole, xNowNum, xNowDen, xNowDir;
long xNowNumStep;
};



void DrawRuns(edge *active, long curY, Pixel pat);
Is this renderer just faster, or does it support concave polys too?
it faster and it can draw complex polygons!

function for drawing pattern poly, using Surface:
Code:

void Rasterizer::DrawRuns(edge *active, long curY, Surface* bitmap)
{
struct edge *e;
long xCoords[200];
long numCoords = 0;
int i;
for (e = active; e != NULL; e = e->next)
{
for (i = numCoords; i > 0 && xCoords[i - 1] > e->xNowWhole; i--)
xCoords[i] = xCoords[i - 1];
xCoords[i] = e->xNowWhole;
numCoords++;
e->xNowNum += e->xNowNumStep;
while (e->xNowNum >= e->xNowDen)
{
e->xNowWhole += e->xNowDir;
e->xNowNum -= e->xNowDen;
}
}
if (numCoords % 2) /* Protect from degenerate polygons */
xCoords[numCoords] = xCoords[numCoords - 1],
numCoords++;

const int width = bitmap->GetWidth();
const int height = bitmap->GetHeight();
const int starty = curY % height;

for (i = 0; i < numCoords; i += 2)
{
// DrawHLine(xCoords[i], curY, xCoords[i + 1] - xCoords[i], Color(0, 0, 0));
int startx = xCoords[i] % width;

if (width - startx > xCoords[i + 1] - xCoords[i])
{
Blit(xCoords[i], curY, bitmap, &Rect(startx, starty, startx + xCoords[i + 1] - xCoords[i], starty + 1));
continue;
}
else
{
Blit(xCoords[i], curY, bitmap, &Rect(startx, starty, width, starty + 1));
xCoords[i] += width - startx;
}

while (xCoords[i] + width <= xCoords[i + 1])
{
Blit(xCoords[i], curY, bitmap, &Rect(0, starty, width, starty + 1));
xCoords[i] += width;
}

Blit(xCoords[i], curY, bitmap, &Rect(0, starty, xCoords[i + 1] - xCoords[i], starty + 1));
}
}


sorry for may different nickname`s =)
Thanks for the code - especially the surface texture one.

For everyone else that hasn't tried it yet, I'll attach a pic.

The texture is repeated to fill the entire poly - which may be desirable or not...

Anyway, I may run a benchmark to see just how much faster it is.
Attachments:
PolyExample.JPG
Pejo Software - Per · 02 Aug 2005, 07:34 UTC
The code above is the same (or from the same source) that I used for 3Dris two and a half years ago (then with GapiDraw). When constructing my new framework I also tried out Franks code and I actually found that faster (I might have modified it somehow though). The difference was small so I use the code that supports complex polygons.

I guess wich code is faster can depend on how the polygon looks.
My benchmarks do show that the original (Frank's) code is a bit faster with just a 3 point poly, so I'd suggest putting in the j.v. code and leaving the original as well.

I did have a crash in my benchmark program when I drew poly's across the edge of the screen, so the clipping may be wrong. If anyone spots the problem, tell me where it's at.
jaluna · 21 Jan 2009, 11:00 UTC — Unable to draw semi-off screen poly
Hi all,

I have tested this function because I need to render complex polygons, however it don't work when some vertex of polygon are off screen.

For example, try to draw this rectangle

P1.x = 50; P1.y = -50;
P2.x = 100; P2.y = -50;
P3.x = 100; P3.y = 50;
P4.x = 50; P4.y = 50;

I have expected that this function render the part inside the screen but nothing is rendered. However, the original function FillPoly (that does not work with complex polygons) produce the expected result.

Image

There are a bufix for this function?

Thanks!
Congrats, you just resurrected a 4.5 years old thread. Most of the people (if not all) on this thread don't visit here anymore.
Kzinti wrote:Congrats, you just resurrected a 4.5 years old thread. Most of the people (if not all) on this thread don't visit here anymore.


Ah, ok, my apologizes for this intrusion.

There are a better place to discuss about this library or it's dead?
PocketFrog is pretty much dead.