Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Direct3D and Alpha Masks, impossible?

  1. #1

    Direct3D and Alpha Masks, impossible?

    I was playing around with Direct3D for 2D purposes and I was kinda hoping to be able to use a bitmap that contains the full image and another image (black and white maybe grey) for the mask and transparency. I think that technique is called Masking, but I can't find any tutorials that use it with Direct3D.

    Can anyone tell me how to something like this?
    Do it by the book, but be the author!
    <br />
    <br />Visit the Lion Productions website at:
    <br />http://lionprod.f2o.org

  2. #2

    Direct3D and Alpha Masks, impossible?

    Have you tried using TGA files? I use it for opengl stuff, but I'm quite sure you can also use it for direct3d.

  3. #3

    Direct3D and Alpha Masks, impossible?

    Basically there are two methods to do this: color keying and alpha masks. Color keying basically defines some color (black for example) as transparent, so rendering color-keyed picture means all black pixels are transparent (masked out?). Alpha mask suppose usage of additional layer in picture (called alpha or transparency layer) that defines how much of backround image is composed with your new image.
    From your description (mentioning *grey* color) I assume you want alpha masking. For this you should 1) store your image in files supporting alpha layer - tga, dds allows this (some other formats allows this too); 2) enable alpha blending then rendering your data

    [pascal][background=#FFFFFF][normal=#000000][number=#0000FF][string=#0000FF][comment=#248F24][reserved=#000000] // Set up the textures
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

    m_pd3dDevice.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

    // Set initial alpha-blending mode
    m_pd3dDevice.SetRenderState(D3DRS_ALPHABLENDENABLE , iTrue);
    m_pd3dDevice.SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    m_pd3dDevice.SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    [/pascal]
    There are only 10 types of people in this world; those who understand binary and those who don't.

  4. #4

    Direct3D and Alpha Masks, impossible?

    Thanks for the replies, but they don't explain what I'm really looking for (they might be usefull nevertheless), so I wasn't really clear when I explained what I was looking for !

    Okay... what I actually meant was the old version of this trick to do this... In OpenGL (and Windows Canvas) there is the ability to read the alpha mask from a seperate file then when you apply the texture you first draw the mask (with some special settings mostly) and then you draw the image you actually want to draw over the mask, which would resolve in an image with either tranparency and in some cases even blending (50% transparency and stuff).

    Would that be possible in Direct3D (9) or does DirectX only supports the use of an alpha-channel/layer?

    See it like making an image transparent by using something like multitexturing with a mask-file (a black and white bitmap) and an actual image file (multicolour).
    Do it by the book, but be the author!
    <br />
    <br />Visit the Lion Productions website at:
    <br />http://lionprod.f2o.org

  5. #5

    Direct3D and Alpha Masks, impossible?

    In any case you should get your "mask" image in alpha layer. For "mask" (not alpha-blending) this can be easily done by this (assuming transparent color in your mask is black):
    [pascal][background=#FFFFFF][normal=#000000][number=#0000FF][string=#0000FF][comment=#248F24][reserved=#000000]D3DXCreateTextureFromFileEx(Device, 'mask_file.bmp', D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT, {*}$FF000000{*black*}, nil, nil, OutMaskTexture);
    [/pascal]

    Later you can setup multitexture pipeline like this:
    [pascal][background=#FFFFFF][normal=#000000][number=#0000FF][string=#0000FF][comment=#248F24][reserved=#000000] m_pd3dDevice.SetTexture(0, RealImageTexture);
    m_pd3dDevice.SetTexture(1, OutMaskTexture);


    // Set up the textures
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    m_pd3dDevice.SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

    m_pd3dDevice.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);

    m_pd3dDevice.SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
    m_pd3dDevice.SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

    // Set initial alpha-blending mode
    m_pd3dDevice.SetRenderState(D3DRS_ALPHABLENDENABLE , iTrue);
    m_pd3dDevice.SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    m_pd3dDevice.SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    [/pascal]
    There are only 10 types of people in this world; those who understand binary and those who don't.

  6. #6

    Direct3D and Alpha Masks, impossible?

    Hmmm

    Wouldn't it be more safe to Select the 2nd texture as Alpha argument instead of modulating it with the 1st level.

    I mean, you don't define what is the first alpha argument on the first level ... So, if he didn't "reset" the Texture stage states of an other effect(which implies another alpha argument) then it could change the value of alpha, couldn't it ?

    Code:
    m_pd3dDevice.SetTextureStageState&#40;0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1&#41;; 
      m_pd3dDevice.SetTextureStageState&#40;0, D3DTSS_COLORARG1, D3DTA_TEXTURE&#41;; 
      m_pd3dDevice.SetTextureStageState&#40;0, D3DTSS_COLORARG2, D3DTA_DIFFUSE&#41;; 
      m_pd3dDevice.SetTextureStageState&#40;0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1&#41;; 
      m_pd3dDevice.SetTextureStageState&#40;0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE&#41;; 
    
      m_pd3dDevice.SetTextureStageState&#40;1, D3DTSS_COLOROP,   D3DTOP_SELECTARG2&#41;; 
      m_pd3dDevice.SetTextureStageState&#40;1, D3DTSS_COLORARG1, D3DTA_TEXTURE&#41;; 
      m_pd3dDevice.SetTextureStageState&#40;1, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1&#41;; 
      m_pd3dDevice.SetTextureStageState&#40;1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE&#41;;
    This is how I would do ... Maybe I'm wrong lol

    Bye
    Avatar

    ps : By the way, didn't you made a mistake writing COLORARG instead of ALPHAARG ?

  7. #7

    Direct3D and Alpha Masks, impossible?

    >Wouldn't it be more safe to Select the 2nd texture as Alpha argument instead of modulating it with the 1st level.
    Probable, I've assumed what original texture do not have alpha layer (as TheLion do not want to use it :-( ), so by definition alpha=1 (opaque).

    So settings for "1" stage can be:
    [pascal][background=#FFFFFF][normal=#000000][number=#0000FF][string=#0000FF][comment=#248F24][reserved=#000000]
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    m_pd3dDevice.SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
    [/pascal]
    I prefer to set all arguments for texture stage, so some bad driver will not be confused by possible strange combination of states.

    >ps : By the way, didn't you made a mistake writing COLORARG instead of ALPHAARG ?
    Yep, I've corrected this.
    There are only 10 types of people in this world; those who understand binary and those who don't.

  8. #8
    Is it possible to do this without using Multitexturing??

    I have these two images :
    Texture and Mask.

    I want to blend the two and get an transparent texture! Is it possible?
    Attached Images Attached Images

  9. #9
    You could do this in shader, but it will still require binding two textures simultaneously, or putting both images on the same texture and adjusting texture coordinates. Why can't you integrate the mask as an alpha-channel in the same image?

  10. #10
    Quote Originally Posted by Lifepower View Post
    Why can't you integrate the mask as an alpha-channel in the same image?
    Because this image comes from a Running Video! =)... I need to use the video with Transparent Alpha. On original video file, on the left side of the video I have the colored image, and on the right side I have the mask! I think it is not possible to have ogg videos with alpha-channel on it, but somehow this game does the trick using these kind of masks!

    I'm sure this is possible of doing without using shaders, because I got this video from a game which does not use shaders. But I'm trying to figure out how it's done!

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •