Skip to main content
GameDev.net gamedev.net
🔒 Locked

alpha channel transparency

Started by jorgander Sep 28, 2002 at 12:14 AM 3 replies 350+ views
Original Post
jorgander
jorgander
i''m trying to get transparency using a texture. that is, in photoshop i add an alpha channel to the image and paint the channel white where i want the image to show up and black where i want it to be transparent. in direct3d, i load the texture using D3DXCreateTextureFromFile() (maybe i should use D3DXCreateTextureFromFileEx()?), then set it like i would any other texture with SetTexture(). what render states do i have to set in order for it to work? i''ve read other threads that have to do with this same thing, but i''ve tried everything they mention. also, i''m not sure if they''re talking about texture alpha channel transparency or vertex alpha transparency (or some other kind of alpha transparency). i''m sorry if i''m asking something that has already been asked a lot before (after all, i *think* it''s a common topic), but i just can''t seem to get it to work. i''ve also read through the alpha blending section in the SDK, but most of it is really a lot more complicated that what i''m looking for. for instance, i don''t really care if the blending is done in the hardware or the software, etc. all i want is for a rendered primitive to be transparent where it''s textures alpha channel is black.
Jim Adams
Jim Adams
Here is a link to the most recent time this was asked (yesterday or the day before). If you are enabling alpha blending and/or alpha testing and it still doesn''t work, then it''s most likely not being saved in the correct format or the texture loading function is not using an alpha-channel in the texture format.


Jim Adams
home.att.net/~rpgbook
Author, Programming Role-Playing Games with DirectX
and Focus On: Advanced Animation with DirectX

helix
helix
This was my solution to that problem. I don''t know if my way is the most correct but it seems to work how I want it too. I created a 32 bit targa file with an alpha channel like you described and did the following for the source code:


  
D3DXCreateTextureFromFile( g_pGame->GetDevice(), "ui.tga", &m_uiTex );

..........

g_pGame->GetDevice()->SetVertexShader( UIVERT_FLAGS );
g_pGame->GetDevice()->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_BLENDTEXTUREALPHA );
g_pGame->GetDevice()->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pGame->GetDevice()->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pGame->GetDevice()->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
g_pGame->GetDevice()->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
g_pGame->GetDevice()->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
jorgander
jorgander
i got it working. the problem was that photoshop was not exporting the alpha channel correctly, or at least exporting it in a format that d3d could read it.

the sdk comes with a tool called DirectX Texture Tool that will create textures that are d3d-friendly (or so it seems). after using it to create the bmp-with-alpha-channel, all i needed were these lines of code in my initialize function:

g_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
g_pd3dDevice->SetRenderState(D3DRS_ALPHAREF, 0x08);
g_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);

i don''t know if calling SetTextureStageState() would make a difference or not, but the effect is there with only the SetRenderState() calls. thanks for the help.
helix
helix
hmm, maybe I''ll try your method. My way was just the only way I found to get it to work.

Topic Locked

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

Sign in to reply to this topic.