PocketMatrix forums

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

fillpoly()???

are there any function like fillpoly() or something like that to fill a polygon?? back in the times of tc++, i don't remember if it was with the bgi graphic library or with someone else, i remember i used a function like that, it gets a pointer to a matrix of x,y coordinates for parameter, and it makes it a closed polygon, joining x1,y1 with x2,y2, then x2,y2 with x3,y3 .... and then xn,yn with x1,y1... i don't know if i wxplained enough... by the way, sorry for my english...
Yes.

It uses an array of Point's.

It only supports a solid color, but I have a mod that allows a color per vertex if anyone is interested...
fast_rx, can you send that mod my way? Or better yet, just post it here. That fillPoly() function was one I wrote (although I'd forgotten about it!), I'd be interested to see how you did that.
Ok, I'll post it, but beware - it's messy.


In defs.h
Code:

struct RGB_struct {unsigned char R, G, B;};



In rasterizer.cpp
Code:

void Rasterizer::DrawHLine( int x, int y, int length, RGB_struct color1, RGB_struct color2 )
{

if (!m_clipper.IsEmpty())
{
// Trivial rejection
if (y < m_clipper.top || y >= m_clipper.bottom)
return;

// Clipping
if (x < m_clipper.left)
{
length += x - m_clipper.left;
x = m_clipper.left;
}

if (x + length > m_clipper.right)
length = m_clipper.right - x;

//Need to fix color if clipped!!!
if (length <= 0)
return;

}

// Coordinates transform
Point p1( x, y );
Point p2( x + length - 1, y );

if (p1.x < p2.x)
m_raster->HLine( *m_surface->m_buffer, p1, length, color1, color2 );
else
m_raster->HLine( *m_surface->m_buffer, p2, length, color2, color1 );
}



void Rasterizer::FillPoly( const Point vertex_vector[], const RGB_struct Colors[], int vertex_cnt )
{
// Useful declarations
int i;
const Point* current_vertex;
const Point* next_vertex;


const RGB_struct* current_color;
const RGB_struct* next_color;

// Initialize point/vertex pointers and make sure the polygon has at least 3 points.
current_vertex = vertex_vector;
current_color = Colors;

if (vertex_cnt > 2)
{
next_vertex = vertex_vector + 1;
next_color = Colors + 1;
}
else if (vertex_cnt == 2)
{
// Just draw the line and quit
next_vertex = vertex_vector + 1;
DrawLine(current_vertex->x, current_vertex->y, next_vertex->x, next_vertex->y, Color(Colors[0].R, Colors[0].G, Colors[0].B));//?
return;
}
else if (vertex_cnt == 1)
{
// Draw the point and quit
DrawHLine(current_vertex->x, current_vertex->y, 1, Color(Colors[0].R, Colors[0].G, Colors[0].B) );
return;
}
else
{
// Probably some error, but we don't do anything about it (maybe the polygon is out of sight)
return;
}


// Allocate memory for edge vectors
int window_height = m_surface->GetHeight();
int* edge_start_x = new int[window_height];
int* edge_end_x = new int[window_height];

RGB_struct* edge_start_c = new RGB_struct[window_height];
RGB_struct* edge_end_c = new RGB_struct[window_height];


// Initialize edge vectors
for (i = 0; i < window_height; i++)
{
edge_start_x[i] = POINT_UNDEFINED;
edge_end_x[i] = POINT_UNDEFINED;


//not needed... triggers on start_x anyway.
// edge_start_c[i] = POINT_UNDEFINED;
// edge_end_c[i] = POINT_UNDEFINED;

}

// Set starting points for ymin and ymax (inversed)
int ymin = window_height;
int ymax = 0;

// Calculate edge between all the vertexes (except the last)
for (i = 1; i < vertex_cnt; i++)
{
PolyEdgeCalc2(current_vertex->x, current_vertex->y, next_vertex->x, next_vertex->y, *current_color, *next_color, &ymin, &ymax, window_height, edge_start_x, edge_end_x, edge_start_c, edge_end_c);
current_vertex += 1;
next_vertex += 1;

current_color +=1;
next_color += 1;
}

// Final vertex should close the polygon
next_vertex = vertex_vector;
next_color = Colors;
PolyEdgeCalc2(current_vertex->x, current_vertex->y, next_vertex->x, next_vertex->y, *current_color, *next_color, &ymin, &ymax, window_height, edge_start_x, edge_end_x, edge_start_c, edge_end_c);

// Check out-of-bounds
if (ymin < 0)
ymin = 0;
if (ymax > window_height)
ymax = window_height;

// Loop over all the y-coords and draw a HLine between the polygon edges
for (i = ymin; i < ymax; i++)
{
if (edge_start_x[i] != POINT_UNDEFINED)
{
if (edge_end_x[i] == POINT_UNDEFINED)
{
edge_end_x[i] = edge_start_x[i];
edge_end_c[i] = edge_start_c[i];
}


int length = edge_end_x[i] - edge_start_x[i];
if(length > 0)
DrawHLine(edge_start_x[i], i, length, edge_start_c[i], edge_end_c[i] );
else if(length < 0)
DrawHLine(edge_end_x[i], i, -length, edge_end_c[i], edge_start_c[i] );
}
}


// Free memory
delete[] edge_start_x;
delete[] edge_end_x;

delete[] edge_start_c;
delete[] edge_end_c;
}




void Rasterizer::PolyEdgeCalc2(int x1, int y1, int x2, int y2, RGB_struct col1, RGB_struct col2, int* ymin, int* ymax, int window_height, int* edge_start_x, int* edge_end_x, RGB_struct* edge_start_c, RGB_struct* edge_end_c)
{
// We're only interested in lines that expand in the y direction.
// Horizontal lines are handled automaticly _if_ the number of points/vertices
// are greater than two. This is ensured by DrawFilledPolygon().
if (y2 != y1)
{
// We need to have increasing y values for the algorithm to work
if (y2 < y1)
{
// Simply swap the values of the two points/vertices.
int tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
tmp = x1;
x1 = x2;
x2 = tmp;

RGB_struct tmpCol;
tmpCol = col1;
col1 = col2;
col2 = tmpCol;
}

// Keep track of minimum/maximum y-coord.
if (y1 < *ymin)
*ymin = y1;
if (y2 > *ymax)
*ymax = y2;

// Shift value by 8 and down-shift later on to avoid using floats (because they are SLOW)
long x = (long)x1<<8;

long RCol = col1.R<<8;
long GCol = col1.G<<8;
long BCol = col1.B<<8;

// Calculate dx/dy (= the x per y increment)
long dx = ((long)(x2 - x1) << 8) / ((long)(y2 - y1));

long dRCol = ((long)(col2.R - col1.R)<<8)/((long)(y2-y1)); //FIX multiple divs
long dGCol = ((long)(col2.G - col1.G)<<8)/((long)(y2-y1));
long dBCol = ((long)(col2.B - col1.B)<<8)/((long)(y2-y1));

// We ignore the starting point. It will be handled by the closing (ie. the last) vertex.
x += dx;
RCol += dRCol;
GCol += dGCol;
BCol += dBCol;
y1++;

// Loop over all the y coordinates.
for (int y = y1; y <= y2; y++)
{
// Check out of bounds
if ((y >= 0) && (y < window_height))
{
if (edge_start_x[y] == POINT_UNDEFINED)
{
edge_start_x[y] = x >> 8;

edge_start_c[y].R = (unsigned char)(RCol>>8);
edge_start_c[y].G = (unsigned char)(GCol>>8);
edge_start_c[y].B = (unsigned char)(BCol>>8);
}
else
{
edge_end_x[y] = x >> 8;
edge_end_c[y].R = (unsigned char)(RCol>>8);
edge_end_c[y].G = (unsigned char)(GCol>>8);
edge_end_c[y].B = (unsigned char)(BCol>>8);
}
}

x += dx;
RCol += dRCol;
GCol += dGCol;
BCol += dBCol;
}
}
}




Add appropriate things in raster2d.cpp

Code:

void Raster2D::HLine( PHAL::Surface& buffer, const Point& P, int length, RGB_struct color1, RGB_struct color2 )
{ Algo2D::HLine( PixelSourceCopy(), buffer, P, length, color1, color2 ); }

void AlphaRaster2D::HLine( PHAL::Surface& buffer, const Point& P, int length, RGB_struct color1, RGB_struct color2 )
{ Algo2D::HLine( PixelAlphaBlend(m_alpha), buffer, P, length, color1, color2 ); }

void GenericRaster2D::HLine( PHAL::Surface& buffer, const Point& P, int length, RGB_struct color1, RGB_struct color2 )
{ Algo2D::HLine( GenericShader(m_shader), buffer, P, length, color1, color2 ); }



And finally, in algo2d.h
Code:

template<typename PixelFunction> inline
void HLine( PixelFunction& F, PHAL::Surface& buffer, const Point& P, int length, RGB_struct color1, RGB_struct color2)
{
Pixel* pDest = buffer.GetPixels( P.x, P.y );

//transform( pDest, pDest + length, pDest, bind1st( F, color ) );

long dR= ((color2.R - color1.R) << 8)/length;
long dG= ((color2.G - color1.G) << 8)/length;
long dB= ((color2.B - color1.B) << 8)/length;

long R= (color1.R) << 8;
long G= (color1.G) << 8;
long B= (color1.B) << 8;


while(length)
{
*pDest = F(Color(R>>8,G>>8,B>>8), *pDest);

R+=dR; G+=dG; B+=dB;

pDest++;
length--;
}

}



Needs some major rework and optimizations - mainly to move away from having to use that RGB_stuct and probably an optimization in the HLine with respect to the divides. But hey, it works.

Attached is a picture of what it looks like (along with a generic widgets library I've been working on).
Attachments:
polyfill.JPG
That's wild! Thank you!
Very cool! This could come in handy with my physics engine for realtime debugging of impact forces.
fast_rx: Cool, is it ok if I convert it to work with GapiDraw and post the code in the GapiDraw forum?
Johan wrote:fast_rx: Cool, is it ok if I convert it to work with GapiDraw and post the code in the GapiDraw forum?


It wouldn't be the first time you convert PocketFrog code to work "with GapiDraw", wouldn't it? =)
Thierry: I have never taken any code from your projects into GapiDraw. IMHO we have a much better template-based implementation in GapiDraw compared to PocketFrog that is based around the "Adapter" design pattern. This means that we can support all variations of the 16-bit pixel format without having to convert any pixels on the final blit (which on Stationary PCs (555/565) and Symbian (444/565) can be quite expensive).

My re-design of GapiDraw almost two years ago was "inspired" by your template based architecture. But I guess we can call that one even since the layout for your Symbian samples for PocketHAL are virtually identical to the one's we shipped with GapiDraw 3.01. :) I also think your press releases look quite similar to ours... :) Of course, it's not the same content in the samples or the press releases, and it's the same thing with GapiDraw.
Well, most of the code if Frank's (fzammetti) and I just added the color stuff.

I believe I used info from a tutorial by Chris Hecker (http://www.d6.com/users/checker), so you could always just write your own as well.

I don't really care who uses it, but it would be nice if improvements were made and given back.
Johan wrote:Thierry: I have never taken any code from your projects into GapiDraw. IMHO we have a much better template-based implementation in GapiDraw compared to PocketFrog that is based around the "Adapter" design pattern.


I am sorry you take my remark this way. Using the "Adapter" design pattern doesn't mean anything in itself.

Johan wrote:This means that we can support all variations of the 16-bit pixel format without having to convert any pixels on the final blit (which on Stationary PCs (555/565) and Symbian (444/565) can be quite expensive).


Converting to different pixel formats is far less expensive then you might think. When you lower the bitdepth of your images, you are actually lowering the image quality. Not a good thing. In any case, PocketFrog doesn't run on Symbian and all Pocket PC devices are R5G6B5, so really, this is a non issue for PocketFrog.

Johan wrote:My re-design of GapiDraw almost two years ago was "inspired" by your template based architecture. But I guess we can call that one even since the layout for your Symbian samples for PocketHAL are virtually identical to the one's we shipped with GapiDraw 3.01. :)


How many ways is there to use the Symbian UI frameworks anyways? Of course they probably look similar, there is no way around that.