Skip to main content
gamedev.net
🔒 Locked

AlphaBlending

Started by Exorzist99 Apr 9, 2002 at 3:48 PM 5 replies 350+ views
Original Post
Exorzist99
Exorzist99
Hi! Im creating an TileBased Engine using DirectDraw7.. DD cant use Hardware AlphaBlending thats why i want to use D3D7 for that job - but i dont know how i have a class called "Sprite" thats how i draw a sprite with transparency: Sprite hero; hero.LoadBitmap("blabla"); hero.SetColorKey(255, 0, 255); hero.DrawSprite(); i want to create a funktion Sprite::SetAlpha() can someone point me to a tutorial or gimme some code please ? thanx a lot ! cu !
BrianH
BrianH
Well... lets see.

I take it you are implementing your sprites as 3d quads since you are using D3D. To get basical alpha blending working (i.e. specify a single value for the entire sprite) here is what you need to do:

1. Use the diffuse material color to set your alpha value. in other words, have your SetAlpha() function set the alpha value in the material. Values 0.0 to 1.0

2. Before you render the sprites you must enable alpha blending and tell it to look at the source alpha value and blend it with the background.

lpd3ddev->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);

lpd3ddev->SetRenderState(D3DRENDERSTATE_SRCBLEND,D3DBLEND_SRCALPHA)
lpd3ddev->SetRenderState(D3DRENDERSTATE_DESTBLEND,D3DBLEND_INVSRCALPHA);

3. You must also tell the renderer how to calculate stuff when it blends...

lpd3ddev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
lpd3ddev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
lpd3ddev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
lpd3ddev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 );
lpd3ddev->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

basically this tells the renderer to take the alpha value out of the diffuse color but take the color value out of the texture.


Then just render your sprites.

-BrianH

Exorzist99
Exorzist99
nono my friend - i use DirectDraw7!

i load a picture into a LPDIRECTDRAWSURFACE7 and want to draw it
with AlphaBlending - there are 2 ways to do it - manuelly or with
D3D - i want to use D3D - any tutorials or some code ?
thx =)
BrianH
BrianH
I understand, you want to use D3D to hardware accelerate the alpha blending. As far as I know the way to do it is to create a quad (2 triangles forming a square), apply the image as its texture, then render it using the D3D renderer. Now, you do not render it exactly the same as normal 3D geometry rather you treat it as pre-transformed geometry. In other words, you dont set the X,Y,Z in world space you set the X,Y,Z in screen space.

In this case, you need to do what i previously posted to achieve alpha blending.

-Brian
BrianH
BrianH
Oh, as for links you can check the Articles & Resources section of GameDev.net. There is a section on DirectX and a couple articles there on using 3D to do 2D.

http://www.gamedev.net/reference/list.asp?categoryid=24

-Brian
Exorzist99
Exorzist99
yes thats what i wanted - thx !!
Exorzist99
Exorzist99
yes thats what i wanted - thx !!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.