Archived

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

sagmam

colorkey in Dx8

Recommended Posts

Hi, Say I want to create a static image on top of my 3D scene. For example, in many flight sims, the cockpit is such an image. The 3d scene is "underneath" the cockpit... Is this done by BLTing a texture surface onto the back buffer after the call to Present() ? If so, then how is the colorkey taken care of? I thought that Dx8 doesn''t support it anymore (unlike dx7)... Maybe there''s a better way of doing it? thanx!

Share this post


Link to post
Share on other sites
dx8 simulates a color key by making the color that you set as the color key transparent black. Say u specify pink as the color key, then all the pink pixels are coverted to RGBA(0,0,0,0)

SO you have to have alpha testing/blending on, and you can set this with D3DXCreateTextureFromFileEx(...)


:::Al::: [Triple Buffer V2.0] - Resource leak imminent. Memory status: Fragile

Share this post


Link to post
Share on other sites
1.
Can you be more specific? How and when do I do this? Let''s say I specify a PINK coloreky when I call CreateTextureFromFileEx... What next? Obviously, it''s just replaced with "transparent black"... what does it mean?

Share this post


Link to post
Share on other sites
What the guy with the annoying signature means is:
Color keying does not exist in DX8 - you have to blit with alpha-blending enabled.
You can load a non-alpha texture and have the D3DX function turn a given color in the texture to a transparent one (and to color black).
A

Share this post


Link to post
Share on other sites
Ok... first of all, how do I do enable alpha blending? And what method do I call to blit? CopyRects? MSDN says that it doesn''t support alpha blending/colorkey/whatever...

Anyway, this sounds like a real performance hit... Isn''t there a better way to do this? Maybe something that can pass through the normal 3D pipeline? Maybe a vertex buffer with a simple quad in RHW format and a texture all over it? (assuming that the transparency issue is somehow solved in this way)...

Share this post


Link to post
Share on other sites
Make sure these Texture and Render states are set. You can also specify the alpha color key when you load the texture with the extended D3DXCreateTextureFromFileEx function.


  
// set the texture state

m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);

// set rendering state

m_pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pD3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pD3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

Share this post


Link to post
Share on other sites
quote:
first of all, how do I do enable alpha blending?

T enable alpha blending you have to use render states. Use SetRenderState() to enable/disable alpha blending. Also for something that is either seeable or not seeable it would be better to use alpha testing instead of alpha blending. Alpha testing just rejects or accepts a pixel based on its alpha value. You can enabvle this with render states too.
quote:
Anyway, this sounds like a real performance hit

Its not that big of a performance hit as you may think. Todays 3d cards are probably really optimized to handle alpha blending. actually 3d cards in general *should* be able to handle it pretty easily but...
quote:
Isn't there a better way to do this?

Yes there is. Use alpha testing instead. You cant consider alpha testing as a performance hit. Its just a single comparison with the pixels alpha value. if the comparison is true then that pixel is put on the sceen but if the copmparison is false then that pixel is rejected.
quote:
Maybe a vertex buffer with a simple quad in RHW format and a texture all over it?

You would still have to enable alpha testing/blending to get trnasparent pixels. You can have the alpha values in the bitmap/jpeg/png/etc or you can have the alpha value directly encoded in the vertex, either way, you have to enable alpha.

chack out tutorials on the internet. My site in my signature has some above the annoying banner.

quote:
What the guy with the annoying signature means is:

I do appologize. I was forced to put that there. I WONT GO BACK TO THAT PLACE NEVER NEVER...and the only way is to keep that sig....truly sorry. (it even annoys me)


::: Al::: [Triple Buffer V2.0] - Resource leak imminent. Memory status: Fragile


[edited by - IFooBar on January 29, 2003 1:26:36 PM]

Share this post


Link to post
Share on other sites
Ok, here are my questions:

1.
When I said "I think it''s a performance hit", I didn''t mean the alpha blending... what I meant was that BLTing the texture on top of the back buffer before calling Present() each frame sounds like I''m not taking advantage of some simple hardware feature. Besides, how I''m supposed to do this BLTing process? What method am I supposed to call???? After all, CopyRests is not the answer.

2.
Also, I''ve tried using the technique I wrote (with an RHW quad with a texture on it), and after I combined the render states that you wrote, it worked. But since the "cockpit" texture needs to be highly detailed, I''m not sure if this is the best approach. When I think about it, if my cockpit texture is 1024x1024, then the pipeline shoots out 1M pixels just for the static cockpit image... Maybe it''s better than BLTing - I dunno. What do you think?

3.
I didn''t have to use the SetTextureStageState() calls. What are they for in this case? Are they for blending (and not testing) ? I guess this has nothing to do with the color key right? Sorry for all the lame questions :-)

Share this post


Link to post
Share on other sites
If you don''t need transluency, use the "AlphaTest". You set it with render state


  
m_pDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
m_pDevice->SetRenderState( D3DRS_ALPHAREF, 0xFFFF00FF );
m_pDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_NOTEQUAL );


The first render state turn on alpha test. The second sets the current color key value (0xFFFF00FF means electric purple). The third and last render state (alpha function) is the kind of test that is made with the drawn pixels against the color key value (not equal means : pixels not equal to color key value can be drawn).



/* Bullmax */
-------------
Reality has many forms : good and evil, black and white, yin and yang.

Share this post


Link to post
Share on other sites
Well, I *DO* need translucency. So I tried the ALPHABLENDENABLE option instead, with the usual SRCALPHA and INVSRCALPHA blending modes.
But it had no effect. Actually it''s because I have no idea on how to define "translucency" into my texture buffer!!! I tried changing the alpha values of all texels in my texture buffer to some arbitrary value sich as 0x40... But it had absolutely no effect whatsoever!!!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
here is how I draw 2d transparent sprites.

I make a quad with four vertices rendered as a triangle strip with a texture.

the vertex format is D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1

the z-buffer and lighting are disabled.
and this is how i setup the blending.
m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

before rendering the textured quad(s).
m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
and after
m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
and to setup the texture I either use a .tga with an alpha channel or if its a .jpg or .bmp I set the color key when loading the texture with D3DXCreateTextureFromFileEx()

Share this post


Link to post
Share on other sites