Archived

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

baboonorama

Colour keying with n-bit mask

Recommended Posts

Could anyone give me some pointers on how to blend a texture (A)into a scene using a separate grey-scale texture (B) of the same dimensions as the mask. That is, A has no alpha channel but must use B''s colour values as such. To illustrate: My specific problem involves mapping a coloured cloud texture onto a sphere just slightly larger than a sphere with an Earth texture mapped onto it. Obviously I want to see through the clouds in places. These places are defined by another 8-bit grey cloud ''mask''. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Look up the SetTextureStageState() call and "texture blending" in the DirectX SDK. I''d post code, but I''m at work, sorry :-/

- JQ
Full Speed Games. Coming soon.

Share this post


Link to post
Share on other sites
I find the DirectX SDK documentation useful in most circumstances, but it just serves to confuse over the use of Texture Stage States and alpha blending.

I can figure out how to blend a texture using vertex alpha values and srcalpha/invsrcalpha blend factors, but it is not clear how to blend one texture based on the grey-scale values in another texture.

But thanks anyway.

Share this post


Link to post
Share on other sites
OK, what you need to do is the following (you might have to change some stuff, I''m at work again and can''t test it >_< [some screwy old 4MB card and no SDK])

use SetTexture() for both textures (the alpha and the colour texture) one for Stage 0 and one for Stage 1. Say you used the alpha texture as stage 0.

I''m assuming you also want to blend with the diffuse colour. The code would be something like the following:


// set the textures
d3ddev->SetTexture(0, d3dtAlpha);
d3ddev->SetTexture(1, d3dtMainTexture);

// just use the alpha channel from the alpha texture, but we also need to assign a colour operation
d3ddev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

// we could use something other than texture for the colour argument, but older cards (e.g. Rage 128) don''t like that
d3ddev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
d3ddev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

// modulate the main texture with the diffuse colour, and modulate the alpha value from before with the diffuse alpha
d3ddev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
d3ddev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

// modulate maintexRGB * diffuseRGB
d3ddev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
d3ddev->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_DIFFUSE);

// modulate transparencyA * diffuseA
d3ddev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
d3ddev->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

// we''re done, no more stages
d3ddev->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
d3ddev->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);



If you don''t need the diffuse alpha to be used, just change the line

d3ddev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

to

d3ddev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);


Hope that works, if not, give me a shout, I''ll try and sort you out.

- JQ
Full Speed Games. Coming soon.

Share this post


Link to post
Share on other sites
Thanks for the code. I understand it in principal, but I guess my video card isn''t up to the task as weird things happen when I try to run it.

I have a Creative Voodoo Blaster Banshee (16mb) and a quick peak at the capsviewer reveals that I can have two blending stages, but only one simultaneous texture. Hence I can''t do:

pd3dDevice->SetTexture(1, ....

These capabilities seem a little contradictory to me, as I thought each blending stage was associated with a texture. Hence to have max 2 blending stages and only 1 texture makes no sense.

Oh well, thanks anyway.

Share this post


Link to post
Share on other sites
It does make sense - you can also blend with diffuse and specular colours, or the TFACTOR for example, not only textures.

And yeah, the banshee only supports one texture...
You''ll have to use the alpha channel of your first texture.

- JQ
Full Speed Games. Period.

Share this post


Link to post
Share on other sites