alpha over several textures

Started by
2 comments, last by Qpeg 21 years, 1 month ago
My sprite-class works perfectly, except for one thing: When I draw two sprites over eachother, the background (any other object than sprites) is drawn on the sprites transparent region. It should be both the background an any sprites behind it. What I am wondering, is if there is a flag I have to set to make D3D include other sprites as the source when doing alpha blending (something like this: SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA)), either set when initializing the engine, or in the sprites Render() function. My D3DXSprite wrapper handles this fine, so I guess there is something missing in my render procedure? Best regards, Perry Real programmers don''t comment their code, it was hard to write, it should be hard to understand
Real programmers don't comment their code, it was hard to write, it should be hard to understand
Advertisement
I have a similar problem. Here's my solution:

First, I usually choose D3DBLEND_SRCALPHA for the source blend and D3DBLEND_INVSRCALPHA for the destination blend.

Regardless of the alpha blending states, D3D only blends what you draw with what already has been drawn. So the trick is to draw everything in reverse order. For example, if you have three objects A, B, and C and you know that their order from back to front is B, C, A, then draw them B, C, then A. If A is drawn first, then B and C will not be blended behind A because blending only goes one way.

Another solution is to disable z writes:

SetRenderState(D3DRS_ZWRITEENABLE, FALSE);

If you are only doing 2D, this is the best solution. If you want 3D objects, use choice one.

Hope this helps.


"There are only three types of people in this world: those who can count, and those who can't."

Just3D
Justin Nordin
J Squared Productions
www.jsquaredproductions.com

[edited by - Just3D on March 4, 2003 5:38:06 PM]
"There are only three types of people in this world: those who can count, and those who can't."Just3DJustin NordinJ Squared Productionswww.jsquaredproductions.com
I first tried re-arranging the sprites, but found that the same problem re-appleared when my camera moved to the other side of the sprites. I guess I''ll write a sorting algorithm later on, but for my demo, this is not worth it.

I then went over to trying out the D3DRS_ZWRITEENABLE state, and this worked perfectly! You indicated that it might not work well in 3D scenes, but I can''t see that anything is wrong. I put it in the Render function of my sprite-class, and set it to false just before I call DrawPrimitive, and re-enable it right afterwards - maybe you have only tried to set it once and for all when initializing?

Other hints/tips on this theme is very much appreciated!


Real programmers don''t comment their code, it was hard to write, it should be hard to understand
Real programmers don't comment their code, it was hard to write, it should be hard to understand
Glad it worked.

What I meant by not working well was that if you ever needed full 3D objects in your scene and you needed to move the camera, the polygons will not be drawn in the correct order with z writes turned off. But for 2D things, this is generally not a problem.

If you did want to use my aforementioned method 1, you would have to go about coding a manual polygon sort algorithm, but if method 2 works, then there''s no need to worry about it.

Best of luck.


"There are only three types of people in this world: those who can count, and those who can''t."

Just3D
Justin Nordin
J Squared Productions
www.jsquaredproductions.com
"There are only three types of people in this world: those who can count, and those who can't."Just3DJustin NordinJ Squared Productionswww.jsquaredproductions.com

This topic is closed to new replies.

Advertisement