Sign in to follow this  
123abcd1983

The problem of D3DXCreateTexture

Recommended Posts

hi, I'm using D3DXCreateTextureFromFile to display the picture,but i got some problem on it, the picture has a white color background, so i wish to make the white color background transparent. Last time a guy told me use the D3DXCreateTexture or D3DXLoadSurface, and it has a colorkey can make the color transparent, but i not sure which one is the color key? Is it have any example can refer? How to load the picture file i want from my computer by using D3DXCreateTexture .
 
HRESULT D3DXCreateTexture(
  LPDIRECT3DDEVICE9 pDevice,
  UINT Width,
  UINT Height,
  UINT MipLevels,
  DWORD Usage,
  D3DFORMAT Format,
  D3DPOOL Pool,
  LPDIRECT3DTEXTURE9 * ppTexture
);



The code below is how i write the code in my program
   
void InitPlayingScene(IDirect3DDevice9* pd3dDevice)
{	
	D3DXCreateTextureFromFile( pd3dDevice, L"duck3.bmp", &g_pDuckTexture );
	D3DXCreateSprite( pd3dDevice, &g_pDuckSprite);	
}

void CALLBACK onPlayingSceneRender(IDirect3DDevice9* pd3dDevice)
{
       DuckVec = D3DXVECTOR3(x, y, 0);
       // render the background of the playing scene
       SetRect(&rc,  0, 0, 120,60);
       g_pDuckSprite ->Begin( D3DXSPRITE_DONOTSAVESTATE);
       g_pDuckSprite ->Draw ( g_pDuckTexture, &rc, NULL, &DuckVec,  0xFFFFFFFF);
       g_pDuckSprite ->End();
}




Share this post


Link to post
Share on other sites
not sure 100% but try:
set render state of your device

d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); //enable alphablending


then begin draw your sprite with parameter D3DXSPRITE_ALPHABLEND

g_pDuckSprite->Begin( D3DXSPRITE_ALPHABLEND | D3DXSPRITE_DONOTSAVESTATE);
...


if white color doesn't goes 100% transparent try black instead

Share this post


Link to post
Share on other sites
D3DXCreateTexture[Ex] is for creating an empty texture which you then fill. If you're filling it yourself, you'll need to specify an alpha channel (Which you can set to 0 or 255 for fully opaque or fully transparant).

The function the last person told you to use was probably D3DXCreateTextureFrom*Ex (E.g. D3DXCreateTextureFromFileEx). That allows you to load a texture from a file, and specify a colour you want to use as transparant.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
D3DXCreateTexture[Ex] is for creating an empty texture which you then fill. If you're filling it yourself, you'll need to specify an alpha channel (Which you can set to 0 or 255 for fully opaque or fully transparant).

The function the last person told you to use was probably D3DXCreateTextureFrom*Ex (E.g. D3DXCreateTextureFromFileEx). That allows you to load a texture from a file, and specify a colour you want to use as transparant.


Thanks, it really help a lot, at least i know which function i need to call, but i still not sure which parameter i have to change to make the background color transparent, i think should be the ColorKey parameter, and I tried to changed it to 0xFFFFFFFF (white color), because the picture background color is white, and i wish to make it transparent,but when i run the program agains, that white color background became black color. Below is the value i passed into D3DXCreateTextureFromFileEx just wondering what value i need to pass in if i want to make the white color transparent.


HRESULT D3DXCreateTextureFromFileEx( // value pas in my coding
LPDIRECT3DDEVICE9 pDevice, // ---> pd3dDevice
LPCTSTR pSrcFile, // ---> L"Duck.bmp",
UINT Width, // ---> 100
UINT Height, // ---> 50
UINT MipLevels, // ---> D3DX_DEFAULT
DWORD Usage, // ---> 0
D3DFORMAT Format, // ---> D3DFMT_FROM_FILE
D3DPOOL Pool, // ---> D3DPOOL_DEFAULT
DWORD Filter, // ---> D3DX_DEFAULT
DWORD MipFilter, // ---> DD3DX_DEFAULT
D3DCOLOR ColorKey, // ---> 0xFFFFFFFF
D3DXIMAGE_INFO * pSrcInfo, // ---> NULL
PALETTEENTRY * pPalette, // ---> NULL
LPDIRECT3DTEXTURE9 * ppTexture // ---> &g_pDuckTexture
);


Share this post


Link to post
Share on other sites
Your code changes the white colour of the texture to transparent, but you're not enabling transparency. You'll still need to do what redas suggested - although I don't think you need to set the render state - ID3DXSprite will do that when you use the D3DXSPRITE_ALPHABLEND flag.

Also, you're specifying D3DFMT_FROM_FILE for the format - a BMP doesn't have an alpha channel, so it's probably just getting dropped. Try using another format like D3DFMT_A8R8G8B8.

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