xboxscene.org forums

Author Topic: How Do You Alpha-mask A Texture In Directx?  (Read 80 times)

BenJeremy

  • Archived User
  • Hero Member
  • *
  • Posts: 5645
How Do You Alpha-mask A Texture In Directx?
« on: January 22, 2003, 03:03:00 PM »

QUOTE (DevSpook @ Jan 22 2003, 05:56 PM)
Does anyone know how to apply an alpha mask to a texture in DirectX? What I want to do is to load a texture which does not contain an alpha channel, and apply another texture as an alpha channel.

Ah yes.... I want an even better twist for myself:

Load a gray-scale texture and use THAT as an Alpha for a second texture.

I'm sure there's an easy way to do it, but I've done a TON of searching to no avail.
Logged

dankydoo

  • Archived User
  • Full Member
  • *
  • Posts: 145
How Do You Alpha-mask A Texture In Directx?
« Reply #1 on: January 22, 2003, 03:15:00 PM »

What I would do is load both of the textures, set each one SetTexture(pMainTexture, 0)
SetTexture(pAlphaMask, 1), then set the renderstate to use the first texture as an argument and the second texture as another argument, and then set them to modulate.  That should give the desired effect.


If that doesn't work, ask in the gamedev.net forums and someone will help you.
Logged

BenJeremy

  • Archived User
  • Hero Member
  • *
  • Posts: 5645
How Do You Alpha-mask A Texture In Directx?
« Reply #2 on: January 22, 2003, 04:03:00 PM »

QUOTE (dankydoo @ Jan 22 2003, 06:15 PM)
What I would do is load both of the textures, set each one SetTexture(pMainTexture, 0)
SetTexture(pAlphaMask, 1), then set the renderstate to use the first texture as an argument and the second texture as another argument, and then set them to modulate.  That should give the desired effect.


If that doesn't work, ask in the gamedev.net forums and someone will help you.

Sounds right...

I think I just need to find the right OP for what I'm looking to do.

I just want to use an image as a mask.
Logged

Cherry

  • Archived User
  • Jr. Member
  • *
  • Posts: 79
How Do You Alpha-mask A Texture In Directx?
« Reply #3 on: January 23, 2003, 04:01:00 AM »

The XDK docs seem to be fairly lacking in info. Maybe the PC DX8 docs will give you a better idea ?
Logged

DevSpook

  • Archived User
  • Newbie
  • *
  • Posts: 31
How Do You Alpha-mask A Texture In Directx?
« Reply #4 on: January 23, 2003, 09:58:00 AM »

Still no success on the texture mask, but I think I'm beginning to understand the texturestages & ops.

This is what I have so far, and I don't really understand why it doesn't work, maybe my textures are causing additional problems. Anyway, here goes:

// Enable alpha
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );

// Set the 2 textures, first is image, second is alpha
m_pd3dDevice->SetTexture( 0, m_pAlpha1Texture );
m_pd3dDevice->SetTexture( 1, m_pAlpha2Texture );

// Set the first texture stage to just return texture1 unmodified
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1   );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );      

// Set the second texture stage to just keep the texture information calculated in stage 0
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_SELECTARG1   );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_CURRENT );

// Use the alpha from stage 1 as output alpha
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_SELECTARG1   );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );


Alternatively, it should also work by modulating the two alphachannels as dankydoo suggested (replace the last two lines above with the ones below).

// modulate the output from stage 0 (D3DTA_CURRENT) with the alpha from this
// texture (D3DTA_TEXTURE)
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE   );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2, D3DTA_CURRENT );


Anyone care to look this over and check for obvious mistakes? Or even better, try it with some of your own textures and see if you can get it to work.
Logged

BenJeremy

  • Archived User
  • Hero Member
  • *
  • Posts: 5645
How Do You Alpha-mask A Texture In Directx?
« Reply #5 on: January 23, 2003, 10:10:00 AM »

Well, I can't help you at the moment (at work), but I assume you've looked at my sprite code for alpha examples?

I haven't tried using a secondary texture yet, and as I said earlier, I'm pretty sure what i WANT to do (use R,G or B channels as an Alpha from a secondary texture) should be a modification of what you are doing.

It might be the order of the texture stages... not sure here. his stuff can get pretty funky anytime you veer off and do something somebody else has not documented.  sad.gif
Logged

DevSpook

  • Archived User
  • Newbie
  • *
  • Posts: 31
How Do You Alpha-mask A Texture In Directx?
« Reply #6 on: January 25, 2003, 03:46:00 AM »

QUOTE
Make sure you have 2 sets of 2D texcoords for each vertex. You'll need to set this up in your FVF or your streams setup. I don't know what will happen if you miss one out, but it would probably be weird and unexpected.


This is probably what is wrong, I just generate a rectangle with regular texture coordinates.
Do I need to create a vertex shader to process my FVF properly when I add another set of texture coordinates?

Weird & unexpected does indeed match with what I'm getting now.  laugh.gif

Thanks a bunch for the tips, I'll do some research & testing on 2 sets of texture coordinates.
Logged

DevSpook

  • Archived User
  • Newbie
  • *
  • Posts: 31
How Do You Alpha-mask A Texture In Directx?
« Reply #7 on: January 25, 2003, 03:52:00 AM »

QUOTE
Make sure you have 2 sets of 2D texcoords for each vertex. You'll need to set this up in your FVF or your streams setup. I don't know what will happen if you miss one out, but it would probably be weird and unexpected.


One more question, I did play around with the vertexshaders to use two sets of texture coordinates like this:

m_pd3dDevice->SetVertexShader( D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX2  );

Do I need to add another set of texture coordinates to my Vertex structure as well as configure processing for the 2nd set?
Logged

DevSpook

  • Archived User
  • Newbie
  • *
  • Posts: 31
How Do You Alpha-mask A Texture In Directx?
« Reply #8 on: January 25, 2003, 05:17:00 AM »

It works!  biggrin.gif

This is what I had to do:
- Add second set of texture coordinates to my vertex structure
- Set up the FVF flags to use two sets of texcoords

// Vertex structure
struct CUSTOMVERTEX
{
   D3DXVECTOR3 p;
   D3DXVECTOR3 n;
   FLOAT       tu, tv;
   FLOAT       tu2, tv2;
};

// vertex shader setup
m_pd3dDevice->SetVertexShader(  D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEX2    );

After that was done, multitexturing started to behave as I expected it to.

Thanks tommyhl, it would have taken me forever to figure this out on my own.
Logged

jsm

  • Archived User
  • Full Member
  • *
  • Posts: 162
How Do You Alpha-mask A Texture In Directx?
« Reply #9 on: January 25, 2003, 08:39:00 AM »

Hey could I ask a question,
I've done some c, some c++(winapi), some OO COBOL and some Java.
Do you think it's enough to start some Directx programming? or not?
Logged

DevSpook

  • Archived User
  • Newbie
  • *
  • Posts: 31
How Do You Alpha-mask A Texture In Directx?
« Reply #10 on: January 25, 2003, 09:00:00 AM »

It should be enough to get you started, but depending on your understanding of 3D graphics it might take some to adjust.

I suggest picking a D3D sample, compile it, and then start adjusting things to get a feel for it. With no D3D experience it seems pretty daunting to start coding anything from scratch I'm afraid. A decent book would probably do wonders.
Logged

jsm

  • Archived User
  • Full Member
  • *
  • Posts: 162
How Do You Alpha-mask A Texture In Directx?
« Reply #11 on: January 25, 2003, 09:30:00 AM »

Any recommendations? (books)
Logged

KoryBricker

  • Archived User
  • Newbie
  • *
  • Posts: 3
How Do You Alpha-mask A Texture In Directx?
« Reply #12 on: January 26, 2003, 11:11:00 AM »

A good utility to try out all the different combinations of OPs etc is a little utility called BlendView from NVIDIA's developer support website (www.nvidia.com/developer). You can easily experiment with the different combinations until you find what you need and should get the same effect on the XBOX


EDIT: Made the URL linkable...
Logged

BenJeremy

  • Archived User
  • Hero Member
  • *
  • Posts: 5645
How Do You Alpha-mask A Texture In Directx?
« Reply #13 on: January 26, 2003, 11:17:00 AM »

Here's a more direct link to the tool....

The nvidia site is a REALLY good resource, though, in general.
Logged

BenJeremy

  • Archived User
  • Hero Member
  • *
  • Posts: 5645
How Do You Alpha-mask A Texture In Directx?
« Reply #14 on: January 26, 2003, 08:02:00 PM »

sad.gif

I'll have to play around with it a bit more.

I'd like to use the grayscale of an image as an alpha mask for another texture image.
Logged