• Advertisement

Archived

This topic is now archived and is closed to further replies.

Texture colorkey transparency problems

This topic is 5711 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

I dont know what i did to my code but I know it worked. Here''s the breakdown: I got an app running at 800x600 and loading textures with color key with rgb value (255,0,255) I Dont use d3dxcreatetexturefromfile cause i need support for images greater than texture limits, so I cut up the image myself. I do this by first loading the image into a surface (CreateImageSurface() + D3DXLoadSurfaceFromFile()) I pass D3DCOLOR_XRGB(255, 0, 255) into CreateImageSurface() to get the transparency set right. I then chop up the iamge if necessary and place them into x amount of textures as necessary by the use of copyrects. Now this pretty much works, but I have noticed that occasionally some bits of some textures are registering as transparent when they really arent. Example image: The iamge on the right is what the texture looks like (8bit bmp image), the one on the left is how it''s getting rendered in the game. Those are not black dots, I checked, they are transparent holes. Why, OH WHY?!?!? :''( please help, ive been trying for too long on my own, i now need some real help - Rob ByteMe95::~ByteMe95() My S(h)ite

Share this post


Link to post
Share on other sites
Advertisement
"I pass D3DCOLOR_XRGB(255, 0, 255) into CreateImageSurface() to get the transparency set right." I think you pass the D3DCOLOR_XRGB(255,0,255) to D3DXLoadSurfaceFromFile''s ColorKey.
"ColorKey [in] D3DCOLOR value to replace with transparent black" --DirectX8.1 SDK
because the D3DXLoadSurfaceFromFile use black where the color of the source pixel is colorkey(D3DCOLOR_XRGB(255,0,255)).

Share this post


Link to post
Share on other sites
Sorry, I made a mistake, I do pass D3DCOLOR_XRGB(255, 0, 255) into D3DXLoadSurfaceFromFile().
Still doesn''t work...

ByteMe95::~ByteMe95()
My S(h)ite

Share this post


Link to post
Share on other sites
quote:
Original post by ByteMe95
Sorry, I made a mistake, I do pass D3DCOLOR_XRGB(255, 0, 255) into D3DXLoadSurfaceFromFile().
Still doesn''t work...

ByteMe95::~ByteMe95()
My S(h)ite


in D3DXLoadSurfaceFromFile() function,ColorKey replace with transparent black,so your image(surface) is look that(somewhere is look black).

Share this post


Link to post
Share on other sites
seve: no those aren''t black dots on the block, they are transparent dots, but the screen shot I took had a black background so they came out black, but they are really transparent, I tested it to make sure.

ByteMe95::~ByteMe95()
My S(h)ite

Share this post


Link to post
Share on other sites
What are your render state settings? You''re texture stage state settings?

From the DX8 docs on CopyRects() in the Remarks Section:
"This method does not support stretch, color key, alpha blend, format conversion, or clipping of either source or destination rectangles."

So there is one problem right there. No matter how you set up your color key on the source surface, you will lose it when transfering to texture. I ran into this same problem myself a while ago.

Share this post


Link to post
Share on other sites
Ron,
I read that too in the docs, but this code DID work at some point, I dont remember what I did to cause it to fail. And the transparency does work, for any image with (255,0,255) in it, it will be transparent. The problem here is that it''s taking some pixels that arent (255,0,255) to be transparent as well.

My render state settings are:

  
pDirect3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDirect3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
pDirect3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);
pDirect3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
pDirect3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
pDirect3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
pDirect3DDevice->SetTextureStageState (0,D3DTSS_ALPHAARG1,D3DTA_TFACTOR);
pDirect3DDevice->SetTextureStageState (0,D3DTSS_ALPHAARG2,D3DTA_TEXTURE);
pDirect3DDevice->SetTextureStageState (0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);
pDirect3DDevice->SetTextureStageState (1,D3DTSS_ALPHAOP,D3DTOP_DISABLE);
pDirect3DDevice->SetTextureStageState (0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
pDirect3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
pDirect3DDevice->SetTextureStageState (0,D3DTSS_COLORARG2,D3DTA_TFACTOR);
pDirect3DDevice->SetTextureStageState (1,D3DTSS_COLOROP,D3DTOP_DISABLE);

pDirect3DDevice->SetTextureStageState (0, D3DTSS_MAGFILTER, D3DTEXF_NONE);
pDirect3DDevice->SetTextureStageState (0, D3DTSS_MINFILTER, D3DTEXF_NONE);

pDirect3DDevice->SetTextureStageState (1, D3DTSS_MAGFILTER, D3DTEXF_NONE);
pDirect3DDevice->SetTextureStageState (1, D3DTSS_MINFILTER, D3DTEXF_NONE);


then before each sprite blit that has its own D3DCOLOR Color member I do:

  
pDirect3DDevice->SetRenderState(D3DRS_TEXTUREFACTOR, Color);


Ron: How did you fix this problem?


ByteMe95::~ByteMe95()
My S(h)ite

Share this post


Link to post
Share on other sites
quote:
Original post by ByteMe95


        
pDirect3DDevice->SetTextureStageState (0,D3DTSS_ALPHAARG1,D3DTA_TFACTOR);
pDirect3DDevice->SetTextureStageState (0,D3DTSS_ALPHAARG2,D3DTA_TEXTURE);
pDirect3DDevice->SetTextureStageState (0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);

pDirect3DDevice->SetRenderState(D3DRS_TEXTUREFACTOR, Color);



I take it that Color == (255,0,255)? I *think*, if I read your settings right, this is why your alpha blending is working. I have a feeling that passing (255, 0, 255) to DXLoadSurfaceFromFile() is having no effect, due to the CopyRects() function losing the alpha information in the transfer. I could be wrong, this is not at all how we do this You could test that by passing a 0 in the ColorKey of the Load function and see if you get the same transparency results. I'd be interested to know what you get.

As to why it's taking values that *aren't* supposed to be transparent and making them that way, I don't know. Are the values close to (255,0,255)? Could you possibly be losing accuracy due to a 16 bit surface?

quote:

Ron: How did you fix this problem?



I think the difference is in how we load textures. Like you, we load a large surface from the file, and then break it up into 256x256 textures. In order to preserve the alpha channel(s), I copy the info manually via surface locks. Something like this (I'm taking out the error checking to keep it shorter....)


        
r = Direct3D.D3DDevice->CreateImageSurface(1024, 512, TextureFormat, &SourceSurface);
r = D3DXLoadSurfaceFromFile(SourceSurface, NULL, NULL, FileName, NULL, D3DX_DEFAULT, 0xFF000000, &Info);
r = SourceSurface->LockRect(&SourceRect, NULL, NULL);
SourcePoint = (BYTE*)SourceRect.pBits;

for (int i=0; i<4; i++)
{
for (int j=0; j<2; j++)
{
r = Direct3D.D3DDevice->CreateTexture(256,256,1, NULL, TextureFormat, D3DPOOL_MANAGED, &Race1ScreenTexture[i][j].Texture);
r = Race1ScreenTexture[i][j].Texture->GetSurfaceLevel(0, &DestSurface);
r = DestSurface->LockRect(&DestRect, NULL, NULL);
DestPoint = (BYTE*)DestRect.pBits;
for (int k=0; k<256; k++)
{
for (int l=0; l<256; l++)
{
for (int m=0; m<4; m++)
{
DestPoint[k*DestRect.Pitch + 4*l + m] = SourcePoint[4*((256*i)+l)+m + (256*j+k)*SourceRect.Pitch];
}
}
}
r = DestSurface->UnlockRect();
}
}
r = SourceSurface->UnlockRect();

SAFE_RELEASE( SourceSurface );
SAFE_RELEASE( DestSurface );

That assumes a 32 bit surface, and a few other things. It could be generalized pretty easily. Note we're using straight black as the color key, but you could use whatever you want. This routine preserves our alpha information on the surface, which I'm pretty sure CopyRects will not.

This works for us, maybe it would help you as well?? Something to try anyway.

Ron

[edited by - RonHiler on May 31, 2002 8:45:34 PM]

Share this post


Link to post
Share on other sites
Rion: thanks for the code, i havent tried it out yet but I see u basically skip the CopyRect function and just copy the data yourself.

About Color = (255,0,255), no, not at all. That color is the color I want the texture to be, say I want the texture to be shaded Red with an alpha of 150, then color would D3DCOLOR_ARGB(255,255,0,0), etc etc, for any color/alpha values i want

Copy rect DOES copy the info correctly because transparency DOES work, with (255,0,255) as the color. The only problem as I''ve stated is that it''s doing it in places it shouldn''t be.

I have tried to use 0,0,0 as the color key, and that did in fact fix the problem, but I have a lot of 0,0,0 in my images and dont want to change it, I''d rather fix the problem itself.

Thanks for help, I''ll try that code as soon as i get a chance


ByteMe95::~ByteMe95()
My S(h)ite

Share this post


Link to post
Share on other sites
quote:
Original post by ByteMe95
Rion: thanks for the code, i havent tried it out yet but I see u basically skip the CopyRect function and just copy the data yourself.


Yep. When I tried to use CopyRect (which is what I did the first attempt) I lost all transparency. You seem to be not having this problem, which is odd, heh. But okay.

quote:

About Color = (255,0,255), no, not at all. That color is the color I want the texture to be.


Ah, I see. Okay, never mind then. Apparently your CopyRect is retaining the alpha channel, despite what I think Have you tested this on different video cards?

quote:

I have tried to use 0,0,0 as the color key, and that did in fact fix the problem, but I have a lot of 0,0,0 in my images and dont want to change it, I''d rather fix the problem itself.


My understanding of color keying in DX8+ is that it''s not well supported. Our initial graphics were set up to use a color other than (0,0,0) (that ugly pinkish color, I think it may have been (255,0,255)), but it simply wouldn''t work. No matter how I set up the color key parameters, it would never be transparent. We switched to (0,0,0) and all was fine. You may be running into the same problem. Clearly, you''re getting closer than we did, since at least you''re getting it to be transparent (albiet too much so).

Good luck, I hope you can figure it out.

Ron

Share this post


Link to post
Share on other sites

  • Advertisement