Hey all,
I have a question regarding blending a texture for transparency. We are trying to optimize the rendering and experimenting by placing bullet holes and blood splatters onto our background texture using glTexSubImage2D(). The issue is that the source pixels are being copied, not blended, onto the background texture. This results in the destination pixels being completely overwritten. I believe glTexSubImage2D() only replaces the pixels completely and may not be using it right in this case.
Is there something I am missing or another method? I have a feeling the solution is much more complex then I expected.
void OGL_Renderer::drawImageToImage(Image& source, const Rectangle_2d& srcRect, Image& destination, const Point_2d& dstPoint)
{
Image subImage(&source, srcRect.x, srcRect.y, srcRect.w, srcRect.h);
glEnable(mTextureTarget);
// Bind our destination texture.
glBindTexture(mTextureTarget, getTextureId(destination));
// Detect which order the pixel data is in to properly feed OGL.
GLint nColors = subImage.getPixels()->format->BytesPerPixel;
GLenum textureFormat;
if(nColors == 4)
{
if(subImage.getPixels()->format->Rmask == 0x000000ff)
textureFormat = GL_RGBA;
else
textureFormat = GL_BGRA;
}
else if(nColors == 3) // no alpha channel
{
if(subImage.getPixels()->format->Rmask == 0x000000ff)
textureFormat = GL_RGB;
else
textureFormat = GL_BGR;
}
else
cout << "Image is not truecolor." << std::endl;
glTexParameteri(mTextureTarget, GL_TEXTURE_MIN_FILTER, TEXTURE_FILTER);
glTexParameteri(mTextureTarget, GL_TEXTURE_MAG_FILTER, TEXTURE_FILTER);
// Check for the need to clip the source texture.
Rectangle_2d clipRect;
if ((dstPoint.x + srcRect.w) > destination.getWidth())
{
clipRect.w = srcRect.w - ((dstPoint.x + srcRect.w) - destination.getWidth());
}
else
clipRect.w = srcRect.w;
if ((dstPoint.y + srcRect.h) > destination.getHeight())
{
clipRect.h = srcRect.h - ((dstPoint.y + srcRect.h) - destination.getHeight());
}
else
clipRect.h = srcRect.h;
glColor3f(1.0, 1.0, 1.0);
// Copy source onto destination.
glTexSubImage2D(mTextureTarget, 0, dstPoint.x, dstPoint.y, clipRect.w, clipRect.h, textureFormat, GL_UNSIGNED_BYTE, subImage.getPixels()->pixels);
glDisable(mTextureTarget);
}
We already have GL_BLEND enabled and use
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);