PDA

View Full Version : DirectX surface to texture colorkeying problem? *solved*



FusionWolf
30-01-2005, 05:27 AM
Hi,

Because textures goes to videoram in DirectX and some cards doesn't support very big textures, I decided to use surfaces to load bitmaps (.png) to memory first and then split it into separate textures.

So, in my bitmap I use $FFFF00FF (pink) as an transparent color. It works fine if I use D3DXCreateTextureFromFileEx() function and then try to draw it to the screen (I use ID3DXSprite inteface for drawing).

But if I load bitmap into surface first and then copy parts of that surface to separate textures pink color changes to black rectangle.

Here's the code snipet for a single texture I use to load bitmap (.png file).


procedure SurfaceToTextures(FileName: String; var Texture: IDirect3DTexture9);
var
srcSurface: IDirect3DSurface9;
dstSurface: IDirect3DSurface9;
ImageInfo: D3DXIMAGE_INFO;
begin
D3DXGetImageInfoFromFile(pAnsiChar(FileName), ImageInfo)

// Create offscreen surface to system memory and load image into that.
Device.CreateOffscreenPlainSurface(ImageInfo.Width , ImageInfo.Height, ImageInfo.Format, D3DPOOL_SYSTEMMEM, srcSurface, nil);
D3DXLoadSurfaceFromFile(srcSurface, nil, nil, pAnsiChar(FileName), nil, D3DX_FILTER_NONE, D3DColor_ARGB(255,255, 0, 255), nil);

// Create texture to hold image from offscreen surface.
D3DXCreateTexture(Device, 64, 64, 0, 0, ImageInfo.Format, D3DPOOL_MANAGED, Texture);

// Copy offscreen memory surface to texture surface.
Texture.GetSurfaceLevel(0, dstSurface);
D3DXLoadSurfaceFromSurface(dstSurface, nil, nil, srcSurface, nil, nil, D3DX_FILTER_NONE, D3DColor_ARGB(255, 255, 0, 255));
dstSurface := nil;
end;


I see that my example procedure here is pointless, but it shows how I load bitmap(s) to surface(s) and then transfer it to the texture(s) by using the D3DXLoadSurfaceFromSurface() function.

Along the way my pink color in bitmap changes to black, but that ain't transparent. What to do? All help is welcome - thanks.

edit: SOLVED. Problem was that, I was using format of image to create surface and texture (ImageInfo.Format). The format of display devices surface should be used. I changed wherever there's ImageInfo.Format parameter passed to D3DFMT_A8R8G8B8 and now it works.