overlapping sprites

Started by
2 comments, last by Evil Steve 15 years, 4 months ago
Hello I have a problem that when two sprites overlap in my game one blocks the other even though I used color keying. I used color keying with pink and it seemed to work, the sprite's pink colored parts became black and blended with the (black)background. I assume color keying doesn't work the way I want, what method should I use instead to get what I want?
Advertisement
alot more imformation is needed in your post?

how are you displayign your graphics?

what api?

I'll take a stab in the dark and assume you don't have alphablending enabled?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Sorry, DirectX, c++. I activated alphablending and it worked, thanks. Anything I should know about this other than it solves my problem?

Guiding me with a tutorial I had to add this line of code too.
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

and this other two came up aswell(Although removing them didnt change anything)
d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);

I also had to change the texture's(?) format from
D3DFMT_X8R8G8B8
to
D3DFMT_A8R8G8B8

Edit: found another way of doing it without adding any of the above(except changing the format) just using
d3dSprite->Begin(D3DXSPRITE_ALPHABLEND);

:S

[Edited by - Antonym on December 17, 2008 5:43:56 AM]
Quote:Original post by Antonym
Sorry, DirectX, c++. I activated alphablending and it worked, thanks. Anything I should know about this other than it solves my problem?

Guiding me with a tutorial I had to add this line of code too.
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

and this other two came up aswell(Although removing them didnt change anything)
d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
That is the usual way to do alpha blending, it takes the alpha value of the texel in your sprite, and multiplies that by the colour of the texel, then adds it to one minus the alpha value of the texel in your sprite multiplied by the colour at that pixel in the image.
If you're rendering a red pixel with 50% alpha on top of a green pixel already drawn to the backbuffer, the graphics card determines the new colour by using:
src_colour = RGB(1, 0, 0)
src_alpha = 0.5
dest_colour = RGB(0, 1, 0)
output_colour = 0.5 * RGB(1, 0, 0) + (1-0.5) * RGB(0, 1, 0)
= RGB(0.5, 0, 0) + RGB(0, 0.5, 0)
= RGB(0.5, 0.5, 0)
Which results in a yellow pixel being drawn to the backbuffer.

The reason removing the last line didn't change anything was that it's the default value, so setting it has no effect unless it was changed elsewhere in your code. The D3DRS_SRCBLEND value defaults to D3DBLEND_ONE (Full colour of the source pixel), which would mean you'd eventually get an output colour of RGB(1, 0.5, 0) - A redy-yellow colour.

Quote:Original post by Antonym
I also had to change the texture's(?) format from
D3DFMT_X8R8G8B8
to
D3DFMT_A8R8G8B8
That's to add an alpha channel to the texture. If you don't have one, then the alpha is assumed to be 1.0, which means that the above equation won't work out (You'll just end up drawing a red pixel - try the maths and see).

Quote:Original post by Antonym
Edit: found another way of doing it without adding any of the above(except changing the format) just using
d3dSprite->Begin(D3DXSPRITE_ALPHABLEND);

:S
Yup, that just does the exact same as setting D3DRS_SRCBLEND, D3DRS_DESTBLEND and D3DRS_BLENDOP as you did above. It's preferable to use that method, since it's less code, and D3DXSprite uses a state block to manage states, meaning it's slightly more efficient to let ID3DXSprite do the work for you.

This topic is closed to new replies.

Advertisement