Hamilton
Hamilton

Reputation: 11

DIB drawing overlapping circles with alpha blending

This'll be me first question here, I tried to look through the list of suggestions but couldn't find one that matches me question. Thus, here we go.

I'm trying to replicate an effect I saw, I wouldn't know if the way I'm doing it would be the right approach. So if you have any suggestions as to how I should proceed, I'd gladly appreciate the input. (I'll only be doing software rendering)

(This was done using dithering)

Wanted result

As it is, I'm currently far from where I wanna end up. But I'm no stranger to coding per se, I just haven't been doing this kind of graphics for that long.

So, my way of doing things is initially drawing some circles with alpha-blending. Later on I was thinking of achieving a glowing effect by moving towards white the further in on the circle I go, pretty much the same way I'm doing here with the alpha.

Code follows.

// 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);
            }
        }
    }
}

I'm drawing a circle and I'm using the radius as a measurement of the alpha. Result though, is not what I'd expect (the yellow circle demonstrates how it looks without overlapping;

My result

As you can see, the colors blend together and create a pretty much solid red with no alpha blending around the edges.

I have no idea why, do you? Thank you.

EDIT: I fixed it, destination pixelformat was not ABGR but ARGB as the macro said, I had also switched it around from the sourcepixel. I was probably too tired last night.

Upvotes: 1

Views: 50

Answers (0)

Related Questions