# [Solved] Help me with color key on textures !

## Recommended Posts

For the 3rd time, I hope someone wwill be able to answer this. I'm have been trying to put some transparency on my textures. It worked. I'm not sure if it's the best way to do it but that's what I found using the docs.
		rslt=D3DXCreateTextureFromFileEx(pDevice, imageFile, D3DX_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255,255,255), &in, NULL, &texture);

As you see, the color key is set to D3DCOLOR_XRGB(255,255,255) white. So D3D test every pixel in the texture to see if it's white and if it's the case, change it to black (with alpha value 0) That means "draw pixels with alpha value > 0". So all white pixels are not drawn. pDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER ); pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); But in my scene I've got some line primitives too. (using a LINELIST) My FVF for the lines is : D3DFVF_BASEVERTEXLINE (D3DFVF_XYZ | D3DFVF_DIFFUSE) And the diffuse color of my lines is GREEN (0,255,0) But why does my lines becomes black after drawing them ??? No way I can put a color key i'm obviously not using D3DXCreateTextureFromFileEx to draw lines. So is it because when you set a color key it stays active for every other primitives and textures you draw ?? P.S : I tried to draw lines first then textures. Nothing changed. Help me ! [Edited by - Juksosah on June 11, 2006 1:37:22 PM]

##### Share on other sites
Do you have culling enabled? When drawing lines, you should disable culling.

##### Share on other sites
I'm sorry but modifying this :
SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

to no culling
SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE)

didn't do anything...

Any other ideas ?

##### Share on other sites
your lines are probably getting textured. You need to set the texture back to null after drawing the primitives with the texture if you want the line to be untextured.

As a further note, when D3DX applies a color key, it sets the alpha in any pixel with that key to 0, not 255. It may just have been a typo, but I just wanted to point it out to anyone else reading :).

Hope this helps.

##### Share on other sites
Just FYI: In general alpha testing and alpha blending, you want to not draw where alpha == 0. Alpha means "coverage" and thus 255 means "fully opaque" in a traditional set-up. Don't know if that's actually your problem, but it'll certainly confuse you if you're trying to work off most tutorials.

Also, test test you configure means "draw where alpha greater than reference value". You're not showing the reference value -- if the reference value is 255 here, nothing will be drawn.

##### Share on other sites
Oops I did mistakes in my post. I'm sorry it's been a while since I read the docs.

I hope it won't confuse other readers.

Any 'simple' tutorials on this subject ?

My lines are back again when I set every textures to NULL !

Great but now I see ugly white quads everywhere...

If I set my textures to NULL, will I have to re-create them every frame using D3DXCreateTextureFromFileEx ? I call this function at initialisation only.
It seems time-consuming doing this at every frame.

And why does my lines are being textured ? My FVF definition does not allow them to have UV coordinates.

##### Share on other sites
You don't need to set the actual texture to null.

After drawing your textured primitives, call Device.SetTexture(0, null). This will tell the device to stop using it for rendering, but keep the resource allocated. Then you draw your lines without textures. When you want to go back to drawing textured primitives, you don't need to create the texture again, you just need to call Device.SetTexture(0, myTexture). This will tell the device to start using it for rendering again.

As for lines being textured without UVs - even if you don't provide UVs, DX still tries to texture your primitives since you have a texture active. The best way to disable texturing is the SetTexture(0, null) call.

Hope this helps.

##### Share on other sites
Thank sirob, everything's fine now !

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627643
• Total Posts
2978362

• 10
• 12
• 22
• 13
• 33