如何绘制半透明图像在已显示的图像上?
概述:像素RGBA中的Alpha通道记录了当前颜色的透明度,通过设置这个透明度权重配比RGB的取值就可以实现颜色混合绘制(srcColor:显示在前面的图像; dstColor:显示在后面的图像)

代码实现
void GPU::drawPoint(const uint32_t& x, const uint32_t& y, const RGBA& color) { if (x >= mFrameBuffer->mWidth || y >= mFrameBuffer->mHeight) { return; }
uint32_t pixelPos = y * mFrameBuffer->mWidth + x;
RGBA result = color;
if (mEnableBlending) { auto src = color; auto dst = mFrameBuffer->mColorBuffer[pixelPos]; float weight = static_cast<float>(src.mA) / 255.0f;
result.mR = static_cast<float>(src.mR) * weight + static_cast<float>(dst.mR) * (1.0f - weight); result.mG = static_cast<float>(src.mG) * weight + static_cast<float>(dst.mG) * (1.0f - weight); result.mB = static_cast<float>(src.mB) * weight + static_cast<float>(dst.mB) * (1.0f - weight); result.mA = static_cast<float>(src.mA) * weight + static_cast<float>(dst.mA) * (1.0f - weight); }
mFrameBuffer->mColorBuffer[pixelPos] = result; }
|
补充:为什么要使用Alpha通道值作为权重配比RGB值?
当我们使用权重时是为了达到一种均衡的效果,通过配置为各部分占比和为1来达到这种均衡。