Sign in to follow this  

2D Texture(PNG File) How to set ColorKey

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

2D Texture(PNG File) How to set ColorKey I have a PNG(or TGA or DDS) file,I want to create a texture with ColorKey dependendts on PNG's transparent color. BOOL DoInit() { D3DPRESENT_PARAMETERS d3dpp; D3DDISPLAYMODE d3ddm; BYTE *Ptr; #define PNG_WIDTH 256.0f #define PNG_HEIGHT 256.0f sVertex Verts[4] = { { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f }, { PNG_WIDTH, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f }, { 00.0f, PNG_HEIGHT, 1.0f, 1.0f, 0.0f, 1.0f }, { PNG_WIDTH, PNG_HEIGHT, 1.0f, 1.0f, 1.0f, 1.0f } }; // Do a windowed mode initialization of Direct3D if((g_pD3D = Direct3DCreate8(D3D_SDK_VERSION)) == NULL) return FALSE; if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))) return FALSE; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; d3dpp.EnableAutoDepthStencil = FALSE; if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice))) return FALSE; // Create the vertex buffer and set data g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)*4, 0, VERTEXFVF, D3DPOOL_DEFAULT, &g_pVB); g_pVB->Lock(0,0, (BYTE**)&Ptr, 0); memcpy(Ptr, Verts, sizeof(Verts)); g_pVB->Unlock(); // Load the texture map D3DXCreateTextureFromFile(g_pD3DDevice, "Image1.png", &g_pTexture); return TRUE; }

Share this post


Link to post
Share on other sites
The easest way to do what you're doing, will be to use alpha blending. All you need to do is enable the alpha blend renderstate, and set the soure and blend arguments, and then set the alpha source. You also need to decide whether you want to use a color key, or the actual alpha channel. For png, I recommend the alpha channel, because you can get smoother blending. Here's how it can be done:


d3dDev->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
d3dDev->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
d3dDev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

d3dDev->SetTextureStageState( 0, D3DTSS_ALPHAARG1,D3DTA_TEXTURE );

// When you load you're texture with D3DXCreateTextureFromFileA, set your color
// key to 0 for alpha channel transparency.




Easy :) Hth!

Share this post


Link to post
Share on other sites
If you don't want to use the PNGs Alpha channel, simply use D3DXCreateTextureFromFile[u]Ex[/u](). If you look it up, you see one of the last parameters is for Colorkeying. (D3DX will make an alpha channel on the fly, magically removing the selected color)

Share this post


Link to post
Share on other sites
Quote:
Original post by Pipo DeClown
If you don't want to use the PNGs Alpha channel, simply use D3DXCreateTextureFromFile[u]Ex[/u](). If you look it up, you see one of the last parameters is for Colorkeying. (D3DX will make an alpha channel on the fly, magically removing the selected color)


That still requires you enable alpha blending though :) I updated my above post to reflect that.

Share this post


Link to post
Share on other sites

This topic is 4261 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this