PocketMatrix forums

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

Polygons

Hello,

I`m very new to gapidraw-programming and my question might be quite simple.

I would like to draw a dark arrow in front of a jpeg-picture background. Displaying a jpeg works without problems, but is there a simple way to construct a polygon (the arrow) and blit it to a CGapiSurface?

The only supported geometry I found were Points, Lines and RECT-Structures.

Can anybody help me by showing a simple way?

thanks in advance,
Max
You might want to have a look at the source code for PocketFrog... When I was maintaining that library, one of the things I added was a polygon drawing function. You should have to change maybe three lines of code to get it working under GD (just the lines calling the line drawing function, and they're probably almost identical anyway). It accepts an array of points (any number of points) and draws your polygon based on that. I even did a filled version, but that might take a tad bit more work to convert (still not much though). I seem to remember doing a flood fill function too (not sure if GD has that off the top of my head), not sure if you need that or not.
this sounds really good, i will try this function,

Thanks a lot
max
Max,

The function Frank refers to is quite slow and does a function call for each pixel being set. I'll see if I can wrap up a nicer version based on that function that uses templates for pixel shaders.

Please let me get back to you shortly.

//Johan
Max,

I did a REALLY quick hack to get this working with GapiDraw and to avoid having a function call on each pixel being rendered to the backbuffer.

Image

I have attached a sample project that you can use. I placed all the Polygon code in the file "GapiBufferTool.h" which is located in the "common" folder. Unzip the sample into your "\gapidraw\samples" folder.

To use the Polygon tool, please check the following code:

Code:

GDBUFFERDESC bufferdesc;
if (SUCCEEDED(pBackBuffer->GetBuffer(&bufferdesc)))
{
// Draw a polygon
CPoint points[3]; points[0].SetPoint(10, 10); points[1].SetPoint(210, 70); points[2].SetPoint(150, 237);
COLORREF colors[3]; colors[0] = RGB(255, 0, 0); colors[1] = RGB(0, 255, 0); colors[2] = RGB(0, 0, 255);

if (bufferdesc.dwPixelFormat & GDPIXELFORMAT_565)
{
CGapiBufferTool< GDShader::Opaque<GDPixelFormat::GD565> > buffertool;
buffertool.FillPoly(&bufferdesc, points, colors, 3);
}
else if (bufferdesc.dwPixelFormat & GDPIXELFORMAT_555)
{
CGapiBufferTool< GDShader::Opaque<GDPixelFormat::GD555> > buffertool;
buffertool.FillPoly(&bufferdesc, points, colors, 3);
}
else if (bufferdesc.dwPixelFormat & GDPIXELFORMAT_444)
{
CGapiBufferTool< GDShader::Opaque<GDPixelFormat::GD444> > buffertool;
buffertool.FillPoly(&bufferdesc, points, colors, 3);
}
pBackBuffer->ReleaseBuffer();
}


As you can see, there are three classes involved:
(1) The Pixel Shader class ("Opaque"). I have attached a 50% quick antialias and an alpha blend shader you can use. To set the opacity value for the alpha blend, simply call buffertool.SetParam1(n).
(2) The Pixel Format class. For maximum performance, GapiDraw renders in native mode on all devices. Thus, on some symbian devices this will be the 444 pixel format. This is generally no issue if you use template code.
(3) The BufferTool class. This at the moment only contains the FillPoly function. Please note that:
1. It does not support clipping at the moment
2. It does not support 1- or 2-point polygons
3. It draws the polygon at incorrect coordinates (I borrowed the code for the FillPoly function (with permission) from the forum user fast_rx). This is probably an issue in the FillPoly function but I have not had the chance to look into this. You clearly see this issue if you first draw the polygon and then call a number of DrawLine at the same coordinates.

If you fix any of the above issues then please feel free to contribute by posting the updates you do.

You can download the sample project with all the code from:
http://www.develant.com/temp/Minimal-FillPoly.zip
Johan,

The sleight offset on the edge calc is caused by the fixed point stuff always rounding down.

The fix is to change "x>>8" to "(x + (1<<7)) >> 8" (basically adding 0.5) to the edge_start_x and edge_end_x calc in PolyEdgeCalc...
Glad I could be of assistance :)

Seriously though, I wrote that function in the PF days and never had any reason to think about it again, so I'm glad there is now an optimized version.
fast_rx: Thanks! I'll keep this file open sourced and will try to add some more stuff to it when I get the time, hopefully you'll find some of the stuff I add to it useful later on...
I'm just curious. Did anyone download the file "Minimal-FillPoly.zip"? If so, do you think you could send it to me? I forgot to backup it when I switched laptops recently.

//Johan
emailed to you :D
Thanks! :)
muff, Can you please send me the Minimal-FillPoly.zip
to me too .. thanks in advance ..
blueabdelrahman@hotmail.com
perhaps Johan could put it up on the forum here somewhere for everyone to access it ?