• Advertisement
Sign in to follow this  

Texture mask with stage states

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

I'm having a problem with masking a texture. I'm talking about 2D here, so I'm just drawing a texture on the screen. So what I'm doing is using 2 textures, 1 is the texture that needs masking, the other is the mask. I set the mask texture in stage 0 and the main texture in stage 1. The stage stages are set like this:
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

d3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
d3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
d3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
d3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);


what happens is, in stead of what I would expect, that the texture is only seen in the places the mask texture has some sort of transparency, the mask is correct but only the left top pixel color of the main texture is used. an example: //edit: There was an example here, but I deleted it, so I can delete the images on my webspace :) I don't know, maybe I'm doing something wrong, this is the first time I'm working with texture stages. // edit: I'm using DirectX SDK latest version (I think) for C++ [Edited by - Schildpad on April 5, 2006 1:59:40 PM]

Share this post


Link to post
Share on other sites
Advertisement
You don't have any modulation of the alpha values. Carefully read through your setup. It looks like you have the alpha mask as the first stage and the texture at the second stage. You'll want to reverse those. You also need to modulate the alpha from the first stage (the texture color) with the alpha in the next stage.


// Just send the whole texture through the first stage
d3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
d3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

// Now modulate the texture's alpha value with the alpha value of the mask
d3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
d3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
d3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_TEXTURE);
d3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
d3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);

// Disable the third stage
d3dDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
d3dDevice->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);



It's been a while since I've done this, so I hope I didn't make a mistake. Give it a shot, let me know if it doesn't work, and I'll check it out on my system at home.

Share this post


Link to post
Share on other sites
While you do need a colorop for stage 0, I don't think you want the modulate suggested on stage 1. You do however want to tell stage 1 to use the first set of texture coords (as I'm assuming you have the usual single set of texcoords)

pDev->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);

Share this post


Link to post
Share on other sites
d3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
d3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

d3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
d3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
d3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
d3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
d3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);

I switched my main texture to my first stage and my mask texture to my second, changed the texture stage states to the ones above and added the D3DTSS_TEXCOORDINEX and set it to D3DTSS_TCI_PASSTHRU (equivalent of 0) and it works. I'm asuming you have to set D3DTSS_TEXCOORDINDEX to tell Direct3D you want the same texture coordinates used for the second stage as for the first.
Thank you for your help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement