DirectX - how to update transparency?

Started by
4 comments, last by savail 10 years, 11 months ago

Hey,

I've got quite strange problem connected with transparency and hope that someone could help me here ^^. I wrote an map editor, which is used to load certain sprites with transparent background and place them on a stage. Everything's ok as long as I won't move those loaded sprites on the stage. Here's a screenshot showing how it should always look like:

good.jpg

It looks alright only if I place the black background before placing on it those magic balls tongue.png. Now If I remove the black background and put it once again (so after putting the balls) the result looks like it:

bad.jpg

The old balls have some kind of traces of previous white background(the stage's area is cleared on white in the loop) but new balls that I put on the black background behave correctly in the matter of transparency...

Why does this happen? Should I use some sort of function that updates the rendering area or sth? I would be very grateful for help!

Advertisement

Are you also clearing the depth buffer? Or do you have it disabled completely? Basically if you are clearing the color and depth buffers, then it shouldn't be an issue with blending.

Thanks for an answer! This is how looks my clearing like:


d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
 

I suppose that everything's alright here. I found a solution to my problem anyway. The traces occured becouse of the wrong order of drawing. Before drawing I'm now sorting all my objects by their Z buffer and drawing the most remote images first and the closest at the end. The order was random earlier.

I'm a bit surprised that this problem turned out to be rather rare :P. I haven't seen anybody complaining about it but me. And I would like to ask if that's a good solution or the genuine problem might be somewhere else? Maybe my sorting is only like a work-around? Do you guys also sort all objects in ur game before drawing so that the remotest are drawn first?

That is typically only needed for objects which have transparency (or use blending in your case).

EDIT: There is actually an algorithm called "Order Independent Transparency", but it is a very heavyweight algorithm for doing simple sprite rendering. Still, it might give some insight into the problem if you read up about on the topic.

Generally you give each object a different Z value, and use the Z-buffer to do the culling for you. The Z-buffer is created (Well, usually) when you create the D3D device, if you set the EnableAutoDepthStencil member of the D3DPRESENT_PARAMETERS struct to TRUE, and set the AutoDepthStencilFormat member to a valid format.

That allows you to draw the objects in any order, and the Z-buffer keeps track of the depth of each pixel and correctly occludes pixels as required.

As Jason said however - for alpha'd objects, you still need to render them in back-to-front order for the blending to work correctly.

I guess I'm not using blending in this project. I'm just creating textures from files (.png images with transparent background) with D3DXCreateTextureFromFileEx with ColorKey parameter set to 0xFFFFFFFC.

hmm so another solution to this problem would be to use blending?

edit:

Ah ok, I just wanted to ensure that the sorting is a valid solution ^^. Thanks for help, I'll take a look on the article as well Jason, thanks.

This topic is closed to new replies.

Advertisement