Archived

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

Possibility

Colorkeying in D3D

Recommended Posts

Hi, I have a Direct3D program and its working fine and everything, but I have some images (texture) where I want portions of it transparent, so I was using colorkeying, inparticular I was using the pure green color for the colorkey, but when the textures appear they have a bright green ring around them, where the green color was blended with the surrounding color and was no longer pure green and was thus not made transparent, so how do I fix this and get rid of that green edge around my textures? Possibility

Share this post


Link to post
Share on other sites
Hehe, I have absolutely not a clue what that means Serge, but I have heard of alpha blending which is one of the reasons I am getting into directx, all I have managed to put in so far is the 3D, the 3 kinds of light sources, some simple objects like cubes and to apply textures to them, i guess I will eventually get to alpha blending but I was looking now was just colorkeying out an area of an image(texture) to make it totaly transparent.

Possibility

Share this post


Link to post
Share on other sites
First post:
Are you sure the edges are pure green?
It may be fault of your bitmap loading routines if you are stretching your images.

Second/third post:
You should stray away from colorkeys in D3D because not all cards support that type of transparency, whereas almost all cards support alpha bitmodes.

the alpha value is basically the value of a texture''s opaqueness/translucency.

What serge meant, is that if you''re using 16-bit modes, try and create a texture where 1 bit is for alpha, 5 for red, green and blue respectively.

To get texture formats with Alpha values you need to enumerate the surface formats (read up if you don''t know) and select one with an alpha value. Then you need to change your bitmap loading routines to check for your transparent color and if so, set the pixel of the texture to have an alpha of total transparency.

I would go through these steps before continuing because you will need them if you ever plan to do anything fun with d3d

Share this post


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

..but I was looking now was just colorkeying out an area of an image(texture) to make it totaly transparent.



You can do it with alpha test.

pD3DDev->SetRenderState( D3DRENDERSTATE_ALPHATESTENABLE, TRUE );
pD3DDev->SetRenderState( D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATEREQUAL );
pD3DDev->SetRenderState( D3DRENDERSTATE_ALPHAREF, 255 );

Share this post


Link to post
Share on other sites
Almost all cards support colorkeying, except my virge. And according to my experiments somewhat faster than alpha channel. You should set:

d3dDevice->SetRenderState( D3DRENDERSTATE_COLORKEYENABLE, TRUE );

Look for the following routine in the SDK to find out how to set a texture''s colorkey property.

HRESULT DDSetColorKey( LPDIRECTDRAWSURFACE4 pdds, COLORREF rgb );

( I would have been glad for this answer 2 years ago. )


bernie
www.bkgames.com

Share this post


Link to post
Share on other sites
ok, what I did was is exactly like bernatk said. What I have for example is a square image, in that image i painted a image, like a red circle, and surroundin that red circle i laid pure green down to fill in the corners of the square image, and I then colorkeyed out that area. Now when I put that image as a texture on a surface, it does in deed make the green transparent so I cant see it, except on the edge of the circle, where the green blends with the red, so the red circle has a ''kind of'' greenish ring around it, but I will indeed look into that alpha stuff, it sounds cool.

Possibility

Share this post


Link to post
Share on other sites
quote:
Original post by bernatk
Almost all cards support colorkeying, except my virge.



Now I know of two cards that don''t support colorkeying, the aforesaid and my diamond card, yet I still don''t know of any which doesn''t support alpha texture formats. I would say that more support is for alpha textures than to color keying (all of the sdk examples use alpha, except one specific color keying example, which might be an indication too).

Share this post


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

ok, what I did was is exactly like bernatk said. What I have for example is a square image, in that image i painted a image, like a red circle, and surroundin that red circle i laid pure green down to fill in the corners of the square image, and I then colorkeyed out that area. Now when I put that image as a texture on a surface, it does in deed make the green transparent so I cant see it, except on the edge of the circle, where the green blends with the red, so the red circle has a ''kind of'' greenish ring around it, but I will indeed look into that alpha stuff, it sounds cool.

Possibility


If you have green blended with red around the edges of the circle, then you should have some greenish red pixels right? and color keying makes either a yes/no to every pixel based on it''s color and if the pixel isn''t exactly the color you specified then well, it''s shown.

Share this post


Link to post
Share on other sites
Yah, but there are no blended pixels in the image, just open up MS Paint, make an image 64x64 pixels, use the fill and fill in pure green, and select the filled-circle and select the red color and place a circle in the middle of the image, thats all I have done. So I know it is actually D3D that is doing the blending of the green-red on the edge of the circle, so there is a small fading, if I put the image on a texture on a zoom way in close to it, you can see it go from what appears to be near pure green to pure red, and that I dont understand why its doing that, its like its doing anti-aliasing on the image ''before'' its removing the colorkey color, and that is just confusing me cause i never even turned on anti-aliasing.

Possibility

Share this post


Link to post
Share on other sites
Just use alpha . Give up. I could send you a general purpose functions I modified from SDK samples that loads a texture and sets the alpha if you give up .

I would look to the functions which load the bitmap for problems... I didn''t think D3D could magically know the color key value of a texture (supposing it was loaded correctly and there was no green on the surface)... (that made no sense to anybody but me I bet ), which is what makes me think it might be the bitmap loading functions'' fault.

Share this post


Link to post
Share on other sites
Hey, do you know of any tutorials on how I can set the transparency value of an individual pixel on a bitmap? I guess since colorkeying wont work is that I would need to set them pixels to be totally transparent then. and can that be done on an idividual pixel basis? do you need to have a special file format for that?

Possibility

Share this post


Link to post
Share on other sites
Ok, i wrote a small little thing to show exactly what I am trying to do. Its on my server at

ftp://test:test@206.145.228.91

its called test.zip and is a small file, and you can see exactly what I am trying to do. Its just a simple texture put on a square surface, you can zoom in and out with the up and down arrow keys.

Possibility

Share this post


Link to post
Share on other sites
Q. Hey, do you know of any tutorials on how I can set the transparency value of an individual pixel on a bitmap?
A. A bitmap can't hold an alpha channel so finding a tutorial to do so would be unlikely . However, 32bit Targa (.tga) file format is probably what you're looking for, any good image program will let you set the transparency (alpha value) of a Targa image's pixel.

Q. I guess since colorkeying wont work is that I would need to set them pixels to be totally transparent then. and can that be done on an idividual pixel basis?
A. Yep, just lock the DDSurface and access memory directly.

Q. do you need to have a special file format for that?
A. Targa... or you can use bitmaps and just code a color key into the program and not use that color.


Before trying to set alpha values... you should enumerate the texture formats to find a format that has an alpha channel (they can be 1, 2, 4, or 8 bits per pixel). You can either try an set all these bits to zero (fully solid), or all to one (fully transparent), or you can create a DWORD (up to 255) and:

        

// This assumes dwPixel is a DWORD value of a locked

// surface that has been created with a valid pixel format

// after enumerating textures

DWORD dwPixel = *pSurface;

// This kills off the least significant bits of your given

// value until you have a value within the possible amount.

dwAlpha >>= (8 - myDDPixelFormat.dwAlphaBitDepth);

// First clear the existing alpha bits of the pixel

dwPixel &= (myDDPixelFormat.dwRBitMask |
myDDPixelFormat.dwGBitMask |
myDDPixelFormat.dwBBitMask );

// Find out how many bits the alpha needs to be shifted,
// so shift right until you find a one in the first bit

DWORD dwMask = myDDPixelFormat.dwAlphaMask;
for(dwShift = 0; !(dwMask & 1); dwMask >>= 1)
dwShift++;

// Then you need to put the alpha value into the correct bits

// of the pixel's data

dwPixel |= (dwAlpha << dwShift);

// Then store the values

*pSurface = dwPixel;

// Next pixel

pSurface++;



To see examples... in the D3DIM folder, the D3DFrame project, the d3dtext.cpp file...

Edited by - iwasbiggs on July 28, 2000 1:56:10 AM

Share this post


Link to post
Share on other sites