Jump to content
  • Advertisement
Sign in to follow this  
HaywireGuy

Grayscale image as alpha channel? How?

This topic is 4853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good day people, I'm here with a simple question. It's about texture alpha blending. For the color part, I can take it from texture factor, not a problem. But when comes to alpha channel, I want to use a grayscale BMP (8 bits) that I have. I went through rounds and rounds of modifications on SetRenderState() and SetTextureStageState() calls, didn't have any luck though. Any tips? Thanks in advance for any help I might be getting! What I have for now: g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE); g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); Source Project (24KB, Zipped Document) [Edited by - HaywireGuy on April 1, 2005 10:31:13 PM]

Share this post


Link to post
Share on other sites
Advertisement
It depends on how you load the texture. I believe you want to load it into a D3DFMT_A8 format surface, using D3DXCreateTextureFromFileEx:

LPDIRECT3DDEVICE9 pDevice; // Your device
const char szFilename[] = "alpha.bmp" // Your texture
LPDIRECT3DTEXTURE9 pTexture; // Output texture

HRESULT hResult = D3DXCreateTextureFromFileEx(pDevice,szFilename,0,0,0,0,
D3DFMT_A8,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,0,NULL,NULL,&pTexture);


Then you can just use the exact render and texture states you have set up.
If your card doesn't support D3DFMT_A8, you'll have to find an alternative format with alpha.

Share this post


Link to post
Share on other sites
If you use a pixel shader you can use blue replicate to get color into blue.
In the fixed pipeline, you can take advantage of DOT3 writing to RGBA, for example to simulate blue replicate you could do a dot3 between the texture and TFACTOR set to 0x008080FF. (ie: 0,0,1)

But you're already using tfactor for color, and I think D3DTA_CONSTANT is only available on card which could do the pixel shader method, which means we need an alternative.

You could put a value into TFACTOR's alpha, get it into a color channel by using ALPHAREPLICATE, and DOT3 with that. 0xAA00000 should give you 0.333,0.333,0.333, which should do the trick. (255=1, and 128=0, so 127 = 1 unit. We want 1/3. 127/3 + 128 = 170 = 0xAA)

Share this post


Link to post
Share on other sites
Thanks people for your replies. I'm currently trying the method Steve has provided (simply because it's zero-effort to change from my current codes). Did not have any luck I'm not sure what did I miss out. I have uploaded the project with the texture file, very simple sample application. I'm not sure if anyone would want to check for me? Thanks again!

Notice that the texture file (texture.bmp) has to reside in the same directory as the executable, if you're building a Release version, make sure you copy that texture there too.

P/S: For project download, it's in my original post. :)

Share this post


Link to post
Share on other sites
Thanks Namethatnobodyelsetook, I have already accepted the fact that A8 is not
supported, and resort to using A8R8G8B8 even its primary purpose is for
masking. Well it seems like the additional 75% overhead is unavoidable, let's
live with it. Thanks again!

P/S: My Radeon 9500 Pro obviously does not support it, but Caps Viewer says otherwise.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!