首页 > 解决方案 > DIB 使用 alpha 混合绘制重叠圆圈

问题描述

这将是我在这里的第一个问题,我试图查看建议列表,但找不到与我的问题相匹配的问题。因此,我们开始了。

我正在尝试复制我看到的效果,我不知道我这样做的方式是否正确。因此,如果您对我应该如何进行操作有任何建议,我将非常感谢您的意见。(我只会做软件渲染)

(这是使用抖动完成的)

想要的结果

事实上,我目前离我想结束的地方还很远。但我对编码本身并不陌生,我只是很久没有做这种图形了。

所以,我的做事方式最初是用 alpha 混合画一些圆圈。后来我想通过在我去的圆圈上向白色移动来实现发光效果,这与我在这里使用 alpha 的方式几乎相同。

代码如下。

// Using mid-point algorithm
void DrawCircle(MEMORY *pMem, uint32 nX, uint32 nY, uint32 nColor)
{
    int32 
        nRadius = 50, rSquared = nRadius * nRadius;

    RECT rcCircle =
    {
        -nRadius, -nRadius, nRadius, nRadius
    };

    for (int32 cY = rcCircle.top; cY < rcCircle.bottom; cY++)
    {
        int32 cYSquared = cY * cY;
        for (int32 cX = rcCircle.left; cX < rcCircle.right; cX++)
        {
            int32
                cXSquared = cX * cX,
                nResult = (cXSquared + cYSquared) - rSquared;

            // Check to see if we're inside the circle
            if (nResult < 0)
            {
                uint32 *pPixelDest = &pMem->Canvas.pBmpMem[(cX + nX) + (cY + nY) * pMem->Canvas.nWidth];

                float
                    dist = fabs(sqrt(cXSquared + cYSquared) - nRadius), // Calculate distance from point to center of the circle
                    A = (dist / nRadius),                               // Base alpha on the distance from circle center to the point (circle fading out)

                    SR = (float)((nColor >> 0) & 0xFF),
                    SG = (float)((nColor >> 8) & 0xFF),
                    SB = (float)((nColor >> 16) & 0xFF),

                    DR = (float)((*pPixelDest >> 0) & 0xFF),
                    DG = (float)((*pPixelDest >> 8) & 0xFF),
                    DB = (float)((*pPixelDest >> 16) & 0xFF),

                    R = (1.0f - A) * DR + A * SR,
                    G = (1.0f - A) * DG + A * SG,
                    B = (1.0f - A) * DB + A * SB;

                    *pPixelDest = ARGB((uint32)A, (uint32)R, (uint32)G, (uint32)B);
            }
        }
    }
}

我正在画一个圆圈,我使用半径作为 alpha 的度量。但结果不是我所期望的(黄色圆圈展示了它看起来没有重叠;

我的结果

正如你所看到的,颜色混合在一起,形成了一个非常纯正的红色,边缘没有 alpha 混合。

我不知道为什么,你呢?谢谢你。

编辑:我修复了它,目标像素格式不是 ABGR 而是 ARGB,正如宏所说,我也从源像素切换了它。我可能是昨晚太累了。

标签: alphaoverlappingblendingdib

解决方案


推荐阅读