Archived

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

Alload

[Direct3D 8] How can I set transparent color in a texture?

Recommended Posts

Alload    122
I used to write programs with DirectDraw till yesterday, and now I''m learning Direct3D. I''d like to rewrite my old projects into D3D code, but I came across a problem: how can I make the RGB color (255, 0, 255) transparent in a texture for instance? I just want to display sprites that are transparents when the color is (255, 0, 255). I tried to found how to do this in the SDK documentation, but I haven''t found yet. So please help me!!!

Share this post


Link to post
Share on other sites
Ratman    181
You have no idea how badly I wish I knew this...

I posted this thread a while ago: http://www.gamedev.net/community/forums/topic.asp?topic_id=52520

I just cant get it. I can ge it to work on 85% of machines tested, but I cant get it to work on every video card. If the card doesnt support a certain pixel format, you will need to use software (which is incredibly slow).

I ended up going back to directdraw. I dont have alpha blending, but it works on every comp Ive tested it on now...

good luck.

Share this post


Link to post
Share on other sites
Zaei    122
Load the texture using D3DXCreateTextureFromFileEx(...). One of the parameters is a transparent color thingy.

Z.

Share this post


Link to post
Share on other sites
SikCiv    122
As I mentioned in your other post, download my complete D3DXWrapper for everything you need.

[qoute]
I just cant get it. I can get it to work on 85% of machines tested, but I cant get it to work on every video card. If the card doesnt support a certain pixel format, you will need to use software (which is incredibly slow).


This is one of my problems with my ZeroOne game, older cards like the i740 and some voodoo3''s dont like colorkeying textures, though by the time my game is released, over 80% of PC''s out there should have a decent 3D Accelerator.
There is also another way of colorkeying without the appropriate hardware, and that is to use Alpha masks, but I couldnt be bothered to implement it at the moment.

  Game Download  ZeroOne Realm

Share this post


Link to post
Share on other sites
zennehoy    122
I have been stuck with the same thing for a while now, until I finally figured out what was going wrong just a few minutes ago. Using D3DXCreateTextureFromFileEx(...) is the right thing to do, but it is not quite enough. The problem I was still running into was the z-buffer. Just because a bit is transparent doesn''t mean it will not affect the z-buffer. I found the following lines in a tutorial somewhere, and while I am still shady on what they mean exactly, they fix all the problems with transparency I ever had.

d3ddev->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE );
d3ddev->SetRenderState(D3DRS_ALPHAREF, 0x02 );
d3ddev->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );

I''ll read up on those states later, but for now they work, so I''m happy,
Zen

Share this post


Link to post
Share on other sites
Corillian    122
here ya go guys, i''ve got tha answer . Have your artists make tga files with an alpha channel and u can just load the image and draw it to the screen. This is better than color keying if you want to have pixels partially blended.
Here''s the structure code:

struct img_vertex
{
FLOAT x, y, z, rhw;
float u, v;
};

#define FVF_SCREENIVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)

Here''s the img loading code:

hr=D3DXCreateTextureFromFileEx(app->g_d3d_device,
inactive, //Filename
D3DX_DEFAULT,
D3DX_DEFAULT,
1, //MipLevels
0, //Usage
D3DFMT_A8R8G8B8, //32-bit
D3DPOOL_MANAGED,//D3D mng mem
D3DX_DEFAULT, //Default filter
D3DX_DEFAULT, //MipFilter
0, //no ColorKey
&inactive_inf, //SourceInfo
NULL, //no Palette
&inactiveT); // Our texture

Here''s the drawing code:

//set our texture
app->g_d3d_device->SetTexture( 0, activeT );

//set texture and rendering states
app->g_d3d_device->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);

app->g_d3d_device->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);

app->g_d3d_device->SetTextureStageStat(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);

app->g_d3d_device->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);

app->g_d3d_device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
app->g_d3d_device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);

//This enables Alpha Blending
app->g_d3d_device->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);

// Render the vertex buffer contents
app->g_d3d_device->SetStreamSource( 0, vbuf, sizeof(img_vertex) );
app->g_d3d_device->SetVertexShader( FVF_SCREENIVERTEX );
app->g_d3d_device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);

Share this post


Link to post
Share on other sites