Hey guys,
I want to overlay multiple images that contain alpha values onto a plain gray background. Important note: said images may overlap.
I seem to be completely unable to have the images' alpha values be correctly drawn onto the gray background, despite the alpha values being correctly read (as evidenced by my printed logs not included here).
Here is the image (this is just a placeholder, the actual image is a lot more intricate):
The goal is to, once drawn onto the gray background, the final saved texture can look like this (note: I rotated one image to better illustrate the overlapping effect):
Currently, this is what I end up with:
My code was greatly inspired by a lot of (supposedly functional) code I found on here, so I don't fully understand what is not working. Here is a code snippet:
BaseBitmap* targetTexture = BaseBitmap::Alloc();
targetTexture->Init(512, 512, 24);
targetTexture->AddChannel(TRUE, FALSE); // Not sure if necessary
targetTexture->Clear(128, 128, 128); // Gray background
Filename srcImageFile = Filename("C:\\...pathToImage..\\image.png");
BaseBitmap* srcImageBitmap = BaseBitmap::Alloc();
srcImageBitmap->Init(srcImageFile);
srcImageBitmap->AddChannel(TRUE, FALSE);
BaseBitmap* targetTextureAlpha = targetTexture->GetInternalChannel();
UInt16 r, g, b, a;
// Code in the drawing function:
Int32 offsetX = (targetTexture->GetBw() / 2) - (srcImageBitmap->GetBw() / 2);
Int32 offsetY = (targetTexture->GetBh() / 2) - (srcImageBitmap->GetBh() / 2);
for (Int32 y = 0; y < srcImageBitmap->GetBh(); ++y) {
for (Int32 x = 0; x < srcImageBitmap->GetBw(); ++x) {
srcImageBitmap->GetPixel(x, y, &r, &g, &b);
// As mentioned above, "a" returns the correct alpha value
srcImageBitmap->GetAlphaPixel(srcImageBitmap->GetInternalChannel(), x, y, &a);
if(a != 0)
{
targetTexture->SetPixel(x + offsetX, y + offsetY, r, g, b);
targetTexture->SetAlphaPixel(targetTextureAlpha, x + offsetX, y + offsetY, a);
}
}
}
Any thoughts? Do I have to resort to some type of "Blend()" function?
Thank you very very much for any insight or guidance. I really appreciate it!