PocketMatrix forums

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

Painting a colored masked image.

Is it possible with PocketFrog to Blit, BlitRotate and BlitStretch only a mask of an image with a specified color?

Regards,
Kornalius
The best way would be to add your own function to the PF rasterizer functions... But you can do it with a user defined pixel shader and some round-about programming.

Code:

struct MyShader : public PixelShader
{
Pixel operator() (Pixel src, Pixel dest) const
{

if(src == themask)
return ( color_as );
else
return ( dest );
}
};

MyShader myshader; // Creates a shader



note that you have to use global variables 'themask' and 'color_as' since the pixelshader can't be passed these.

Also, you have to reset the color mask before the blit, otherwise the mask pixels will be skipped.

Code:

themask = Imgs[1]->GetColorMask(); //get mask color
color_as = Color(255,0,0);

display->SetPixelShader(&myshader);
Imgs[1]->SetColorMask(-1); // turn off mask
display->Blit(50,50, Imgs[1]);

Imgs[1]->SetColorMask(themask); //set mask back to original

display->SetDefaultShader();



Like I said, it would probably be better to add a DrawMask(...) function to PF itself. I can help you with that as well, since I had to do something similar to get a full alpha blitter into PF.
Thanks a lot, exactly what I was looking for.

Regards,
Kornalius
Actually it does what I needed but man it is slow! :D You offered to help me write a faster routine. What I am trying to do is a blit with a lighting level, between 0 and 255 (255 is fully lighten).

What I am doing now, is blit a black mask of the surface and then blit the surface with blending on top of it to create the illusion of light, but man it is slow. What was good about this technic was that I could have a light color source too. Maybe we can apply color source (like a tint) with the new routine?

Hope you can help.
Regards,
Kornalius
From your first post, I though you wanted to see the mask - the part that's not normally drawn...

But from you last post, I think I understand what you want - an Alphablend to a specified color using an image with a colormask.

I thought this would be nice to use in menu buttons to show that they are being pressed - or making an enemy flash when they are hit.

What I did was to make an overloaded blit function...
display->Blit(100,100, frogimg, NULL, color, amount);

The first 4 parameters are the same as the regular blit, color is the mask color, and amount is 0-255 the same as alphablending.

The main part of the code I put into the raster2d.cpp file:
Code:

//added JLE 05/17/2004
void Raster2D::BlitMask( GraphicBuffer& buffer, const Rect& destRect, const GraphicBuffer& source
, const Rect& srcRect, Pixel colorMask, Pixel shade, unsigned char amount )
{
assert( destRect.GetWidth() == srcRect.GetWidth() );
assert( destRect.GetHeight() == srcRect.GetHeight() );

//todo: optimize
const Pixel* pSrc = source.GetPixels( srcRect.left, srcRect.top );
Pixel* pDest = buffer.GetPixels( destRect.left, destRect.top );

for ( int h = srcRect.GetHeight(); h; --h )
{

Pixel *dest = (pDest);
const Pixel *src = (pSrc);

for(int w = srcRect.GetWidth(); w ; --w)
{

if(*src != colorMask)
{
const unsigned long mask = RED_MASK | BLUE_MASK | (GREEN_MASK << 16);

unsigned long d = shade;
unsigned long s = *src;

//split into xGxRxB
d = (d | (d<<16)) & mask;
s = (s | (s<<16)) & mask;

//do alpha op
d = ((((d-s)*(amount>>3))>>5) + s)&mask;

//recombine into RGB
d |= d >> 16;

*dest = d;
}
dest++;
src++;

}

pDest += ((buffer.GetPitch())>>1);
pSrc += ((source.GetPitch())>>1);

}

}




I also had to add code to the rasterizer.cpp file to call the above code. I don't really know if the way I've added things to pocketfrog is correct - although they work. And of course, you want to do this for the blitrotated and blitstretch's as well...

BTW, this is using the single multiplication that Tala talked about in this thread
http://pocketmatrix.com/forums/viewtopi ... t=blending

As far as I can tell, it works.
Thanks for the code.

I cannot seem to make it work though. I am really not that familliar with the internals of PocketFrog. Would it be hard for you to create a little stand-alone .h header file that I can just use from my project?

Regards,
Kornalius
If I still use a proper PixelShader I should be able to produce the right effect without a frame hit. The shader you did for me, is not reducing the fps really. Now what is the best way to just darken an image based on a light setting in a pixel shader? Is there a way to get the RGB value directly and darken it without too much calculations?
Kornalius, is this for PPL? Just curious. Are you still accepting beta testers?

Anyway, if the shader speed is not a problem, just combine the second code into the pixel shader like:
Code:

struct MyShader2 : public PixelShader
{
int shade;
int amount;

void SetVals(int s, int a)
{ shade = s; amount = a; }

Pixel operator() (Pixel src, Pixel dest) const
{
const unsigned long mask = RED_MASK | BLUE_MASK | (GREEN_MASK << 16);

unsigned long d = shade;
unsigned long s = src;

//split into xGxRxB
d = (d | (d<<16)) & mask;
s = (s | (s<<16)) & mask;

//do alpha op
d = ((((d-s)*(amount>>3))>>5) + s)&mask;

//recombine into RGB
d |= d >> 16;

return (unsigned __int16)(d);
}
};


I also realized later that you can put the extra variables in the pixelshader itself instead of having to use globals...

The whole thing with adding a function like that to pocketfrog is that it has to be done in several files...
Thierry, please correct me if I'm wrong, but you need to have one for all three rasterizers (Raster2D, AlphaRaster2D, and GenericRaster2D) in raster2d.cpp and raster2d.h and modify rasterizer.cpp & .h to call it.
If there's an easier way, please let me know.
Attachments:
shaderpic.JPG
Hi,

Yes it is! :D There is always room for beta testers! Send me an email to kornalius @ hotmail.com.

I will try this shader, it looks good. 8O So this shader will allow for
"tinting" a surface with a color and also allow me to set the light level also? How do I set the shader's variables before calling the blit? Can you give a little example on how to do this?

Thanks for your time, I really appreciate it. I am a total moron when it comes time to do graphic stuff and diggin into the roots of PocketFrog is not something I can afford to do at this time. :D

Regards,
Kornalius
Sorry I just realized, I guess I can set the variable shade to color black and the amount to the light level?
MyShader2 myshader; // Creates a shader

myshader.SetVals(Color(0,0,0), 128);
display->SetPixelShader(&myshader);

display->blit...

display->SetDefaultShader();



would give you a dimmed look. increase the 2nd parameter to adjust how much black to blend in. Remember the 2nd parameter is 0-255.

The picture I attached was done with a lighter color as the first parameter (pinkish white to be exact).
Thanks a million. I will try this tomorrow and let you know the results.

I am trying to implement an automatic lighting engine within the GameAPI of PPL. I have other ideas like a dynamic A* pathfinding that I will need to translate from a Delphi project I wrote a couple months ago. Everything will be automatic, or almost! :wink:

Regards,
Kornalius