alpha blending

Started by
7 comments, last by asafpolt 18 years, 7 months ago
Hey, i'm trying to write a particle engine for my game. I read that I will need billboarding and alpha-blending to achieve this. I already learned and implemented billboarding using ID3DXSprite interface and its billboarding flag. I noticed that there is another flag that is related somehow to alpha-blending. I also noticed that ID3DXSprite::Draw() takes a color parameter that has something todo with alpha-blending. I believe that there is a way to config the sprite to work with alpha-blending, the thing is I'm not realy sure what alpha blending is... I already read the SDK documentation I could find on the subject, so if anyone knows a good tutorial on alpha blending or can help me with the sprite configuration it would be highly appreciated. Thanks, Asaf Polturak, Israel.
Advertisement
Well to answer your question, alpha blending is how opaque an object is. This is usually determined by how light or dark the alpha channel of a texture is. Try getting a texture with an alpha channel and applying it like this:

Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);

Device->SetTexture(0, YourTextureHere);
(render your object here)

Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

That is how you would get an object to have see through parts based on the alpha channel of the texture. However, if you are looking to make a particle engine, you should really use point sprites. There is a good tutorial about them here:

http://www.codesampler.com/dx9src/dx9src_6.htm
here:
http://www.codesampler.com/dx9src/dx9src_7.htm#dx9_particle_system
and here:
http://www.two-kings.de/tutorials/dxgraphics/dxgraphics17.html

-Chris
thanks chris, all of this 3 links seem useful.

but just in case I decide to stick with my sprites and not move to point sprites, It would still be helpful if someone will direct me to a relevant place
Here are a few more links about ID3DXSprite for you.

http://www.gamedev.net/reference/articles/article1608.asp
http://triplebuffer.devmaster.net/tutorials/stepping3.php

and a thread on the topic here:
http://www.gamedev.net/community/forums/topic.asp?topic_id=318190

-Chris
Hey I managed to have some opacity to my sprite, but it wasn't what I expected:

I used the alpha-blending flag in the sprite->begin() method.
in the draw method's color parameter I used D3DCOLOR_RGBA(256,256,256,128).

What I saw was a see-through billboard that didn't resemble the initial texture at all. what am I doing wrong?
Hi there asafpolt,
How are you doing?

The Problem
Parameters passed to D3DCOLOR_ARGB() is incorrect

The Solution
This may effect your final result or it may not but just as a side note. The D3DCOLOR_ARGB() macro takes parameters in the range of 0 - 255. You are specifying 256 which is out of bounds ;)

Hope this helps you in the future buddy ;)
Take care.
You are right but the mistake was only made in the forum,
My code specified 255 and not 256.

any other ideas please?
I'm not sure what is exactly your problem, because it's little hard to tell without seeing a screenshot... but anyway, I'll try to help: [smile]

You may want to use additive blending instead of the normal blending mode which is set with the alpha blending flag. To do this, add these lines before sprite drawing code:

device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);device->SetRenderState(D3DRS_SOURCEBLEND, D3DBLEND_ONE);device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);


Then draw your sprites without the alpha blending flag.

This method is often used with particle systems, because it makes particles "brighten" each other when they overlap. It looks a lot better than the default blending mode.
Thanks, I'll try it out.

This topic is closed to new replies.

Advertisement