Sign in to follow this  
littlewater

Is Alpha in picture like bmp or in porgramm?

Recommended Posts

littlewater    120
I used software "photoshop" and I added the alpha1 chunnel to the picture ,after finished editing it, i saved it using format bmp ( And i checked save alpha option ) but when i load it into my test programm I found nothing transparent but the source pixel... maybe something error in code ? I added only the following : g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); g_pd3dDevice->SetRenderState ( D3DRS_BLENDOP , D3DBLENDOP_ADD ) ; or maybe actually the alpha in photoshop hasn't anything do with the Direct3D Alpha ?

Share this post


Link to post
Share on other sites
I'm not sure if D3DXCreateTextureFromFile understands alpha in a BMP. Try saving as PNG or TGA, both of which D3D should like just fine.

Alpha can come from texture, or code, or a mix of both. You can tell D3D which alpha to use via SetTextureStageState(), setting the ALPHAOP, and ALPHAARGn. For example to mix vertex (or material) alpha with texture alpha you'd do this:

pDev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); // multiply
pDev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); // texturealpha
pDev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); // by vertexalpha

I'm not sure what the default alpha states are, and whether they use texture alpha or not. If just switching to PNG doesn't help, try adding in that block and see if it fixes the problem.

The color and alpha values at the end of your texture stages (or pixel shader) can be either written directly or blended with the frame buffer. By setting the states you've mentioned, you're asking D3D to blend. You'll still want those states after adding the block I've listed.

Share this post


Link to post
Share on other sites
littlewater    120
could i use the following ?

pd3dDevice->SetTexture( 0, m_pTexture );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_ADD);

pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); //add
pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); //add
pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); //add

but i couldn't see anything after i added this code...

Share this post


Link to post
Share on other sites
What that will do is:

color.red = vertex.red + texture.red
color.green = vertex.green + texture.green
color.blue = vertex.blue + texture.blue
color.alpha = vertex.alpha * texture.alpha

If you vertices have an alpha of 0 instead of 255 (or if you're using lighting, and material colors, the material alpha is set of 0 instead of 1.0) you'll see nothing.

To test this quickly without modifying vertices or materials just change the alphaop to d3dta_selectarg1, leaving alphaarg1 as d3dta_texture (ie: choose use texture alpha alone).

Share this post


Link to post
Share on other sites
Etherstar    200
Most .bmp files that come through programs like MS paint are 24 bit bmp images. These do not include an alpha channel. It is possible to have a 32 bit bitmap with an alpha channel, but it's not usually the case. I recommend the .png format.

Share this post


Link to post
Share on other sites
littlewater    120
Quote:

To test this quickly without modifying vertices or materials just change the alphaop to d3dta_selectarg1, leaving alphaarg1 as d3dta_texture (ie: choose use texture alpha alone).


i tried your method but it still had nothing happend...


i use the function : CreateSurfaceFromFile to load the mul-texture then devide it into different m_ptexture which is a texture array

for ordinary picture i use : CreateTextureFromFile / CreateTextureFromFileEx ...

in the step of initial :
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

then in render step :
g_pd3dDevice->SetTexture( 0, m_pTexture );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_ADD);

g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); //add
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); //add

g_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive ( D3DPT_TRIANGLESTRIP, 0, 2 );

i ignore the light-render step , i think it shouldnt be affected this...

[Edited by - littlewater on June 9, 2005 10:55:24 PM]

Share this post


Link to post
Share on other sites
littlewater    120
I find if i change the param D3DTA_TEXTURE to D3DTA_DIFFUSE :

pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE); //add

the alpha effect could be took...

but new problem also come up ...

because i use the vertex format XYZ and NORMAL (for lighting ) and i use
CreateTextruefromFileEx ( this one has the function of colorkey... ) to load the texture.
when i set the material.Diffuse.a = 1.0 for i dont wanna change some vertices, all the picture was shown ... that means the colorkey lost its effect ...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You're telling D3D to use only vertex alpha if you do that. If you use colorkey (ie: you don't set the key to 0) you'll overwrite whatever alpha is already in the image with colorkey alpha.

If you want colorkey, ensure you specify to load into a format with alpha such as A8R8G8B8, not X8R8G8B8. If your image has alpha already, don't use colorkey (pass 0). If your image doesn't have alpha, but you've got a color you'd like to be transparent, specify that color as the color key. For instance, black is 0xFF000000 (notice the full alpha required), blue is 0xFF0000FF.

SELECTARG1, DIFFUSE = use vertex/material alpha only
SELECTARG1, TEXTURE = use texture alpha only
MODULATE, DIFFUSE, TEXTURE = multiply vertex and texture alpha.

Modulate means multiply. When multiplying colors, a texture or vertex value of 255 is 1.0. If your texture had an alpha of 64, and your vertices had alpha of 255, you would blend at (64/255) * (255/255) = 1/4. If the vertices were at 128, your blend would be (64/255) * (128/255) = 1/8.

Share this post


Link to post
Share on other sites
littlewater    120
thx but ... ...

i cant save alpha in texture (ie : in picture )
tho i use photoshop and i checked the alpha channel option ...

i will try again and make sure i have no mistake ...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this