Sign in to follow this  

Alpha Blending (Pictures +Code)

This topic is 4685 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 want to render two textures streched across a bunch of quads. I want to use the actual rgb from the first texture and the alpha from the second texture. Here is the first texture. Here is the second texture. It is actually completely black, with just the donut in the alpha channel. I have rendered the texture's alpha channel only here so that you can see what is in it. Ok, here is the code I'm using:
  D3DDevice->SetTexture(0, CrossTexture);
  D3DDevice->SetTexture(1, AlphaTexture);

  D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  D3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR);
  D3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR);

  D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_CURRENT);

  D3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  D3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
  D3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  D3DDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

  // Render the quads...
I already know I am setting these render states wrong, so don't flame me. How should I be setting them to get the desired effect?

Share this post


Link to post
Share on other sites
Stage 0 is fine.
Stage 1 you're wiping out the color from stage 0.

SetTSS(1, D3DTSS_COLORARG1, D3DTA_CURRENT);

You're then saying to use color as the blending factor, not alpha
SetRS(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
SetRS(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

That should do it.

edit: You really should get into the habit of doing this...
SetTSS(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
SetTSS(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

Share this post


Link to post
Share on other sites
Ok, before I try that out, I was just reading on another site that you need a set of uv's for each texture. Is this the case?

I'm using this for my vertices:

struct VERTEX
{
D3DXVECTOR3 P;
D3DXVECTOR3 N;
float U;
float V;
};
#define FVFVERTEX ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 )

Should I be using this instead:

struct VERTEX
{
D3DXVECTOR3 P;
D3DXVECTOR3 N;
float U1;
float V1;
float U2;
float V2;
};

In this case, would my FVF be:

#define FVFVERTEX ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2 )

or

#define FVFVERTEX ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEX2 )

Share this post


Link to post
Share on other sites
If you have two sets of UVs, just use TEX2, not TEX1|TEX2.

You can use 1 set of UVs if you'd like (I'd suggest it in most cases).

SetTSS(0, D3DTSS_TEXCOORDINDEX, 0); // use first set of uvs on stage 0
SetTSS(1, D3DTSS_TEXCOORDINDEX, 0); // use first set of uvs on stage 1

Share this post


Link to post
Share on other sites
Ok it doesn't appear to be working. I'm using the code above, with your changes and a single texture coordinate. Here is a shot.



As you can see, the alpha doesn't appear to be making any difference. How can I be sure that there is actually alpha in the texture. I loaded it in the DX texture tool and viewed the alpha channel and it showed the donut. So I don't know what I'm doing wrong.

Share this post


Link to post
Share on other sites

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