Re: Problem with Texture Blending

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Marco" <Marco@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:E5C2BFEB-544A-426A-BBFB-689A0C8C7A09@xxxxxxxxxxxxxxxx
> "Robert Dunlop [MS MVP]" wrote:
>>
>> out = current * (1-TFactor) + TFactor * (mask*TFactor +
>> texture*(1-TFactor))
>>
>
> Thank you. Now I can blend two textures with a factor. For example a 50:50
> blending. I think the main problem was the vertex format. I need two Tu/Tv
> pairs.

If you need different coordinates for each texture, then yes, this is the
case. If two or more textures can use the same coordinates, you can share
texture coordinates between them. The texture coordinate set that each
texture stage uses from the vertex format is specified by
TextureCoordinateIndex, which is a zero-based index and by default will be
set to assume separate texture coordinates for each stage, i.e.
TextureStageStates.TextureCoordinateIndex defaults to 0 for stage 0, 1 for
stage 1, etc. You could specify that both texture stages use the first set
of texture coordinates like this:

d3dDev.SetTextureStageState(0, TextureStageStates.TextureCoordinateIndex,
0);
d3dDev.SetTextureStageState(1, TextureStageStates.TextureCoordinateIndex,
0);

> But one thing want not work: I want to blend two textures with a custom
> mask. I have a mask, for example a black & white mask (also a texture) and
> two other textures. At the white pixels of the mask I want to see texture
> one
> and at the black pixels of the mask I want the texture 2. I think this is
> the
> formula:
> out = texture1 * mask + texture2 * (1 - mask)
> I tryed with the Lerp operation but it does not work.

You might be able to use lerp in this manner by using the temporary register
to load the mask color into, if the device supports
MiscCaps.SupportsTexturStageStateArgumentTemp and
TextureOperationCaps.SupportsLerp. You would load the mask into the
temporary texture register in the first stage, like this:

// mask texture
d3dDev.SetTextureStageState(0, TextureStageStates.ResultArgument,
(int)TextureArgument.Temp);
d3dDev.SetTextureStageState(0, TextureStageStates.ColorOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(0, TextureStageStates.ColorArgument1,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(0, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(0, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);

// texture 1
d3dDev.SetTextureStageState(1, TextureStageStates.ResultArgument,
(int)TextureArgument.Current);
d3dDev.SetTextureStageState(1, TextureStageStates.ColorOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(1, TextureStageStates.ColorArgument1,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(1, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(1, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);

// texture 2
d3dDev.SetTextureStageState(2, TextureStageStates.ResultArgument,
(int)TextureArgument.Current);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorOperation,
(int)TextureOperation.Lerp);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument1,
(int)TextureArgument.Current);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument2,
(int)TextureArgument.TextureColor);
// this is shown as Arg3 in lerp equation
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument0,
(int)TextureArgument.Temp);
d3dDev.SetTextureStageState(2, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(2, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);

Preferably, though, your mask texture should contain an alpha channel that
contains a blending value. If you are loading the mask from a file, you
could create an alpha-only texture file in DDS format using the texture tool
from the SDK, or use a color key value of 0xFF000000 with
TextureLoader.FromFile() to load the mask into a texture format containing
alpha, and set alpha to 0 for black pixels and 255 for non-black pixels.

With a mask texture containing the blending factors in the alpha channel,
you won't require temporary register support, because the blending can be
accomplished using the texture stage alpha channel:

// mask texture
d3dDev.SetTextureStageState(0, TextureStageStates.ColorOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(0, TextureStageStates.ColorArgument1,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(0, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(0, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);

// texture 1
d3dDev.SetTextureStageState(1, TextureStageStates.ColorOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(1, TextureStageStates.ColorArgument1,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(1, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(1, TextureStageStates.AlphaArgument1,
(int)TextureArgument.Current);

// texture 2
d3dDev.SetTextureStageState(2, TextureStageStates.ColorOperation,
(int)TextureOperation.BlendCurrentAlpha);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument1,
(int)TextureArgument.Current);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument2,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(2, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(2, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);

--
Robert Dunlop
The X-Zone
http://www.directxzone.com/
Microsoft DirectX MVP
-------------
The opinions expressed in this message are my own personal views and do not
reflect the official views of the Microsoft Corporation.
The MVP program does not constitute employment or contractual obligation
with Microsoft.


.



Relevant Pages

  • Re: Textures and Masking
    ... This is independent from what is in the frame buffer before, not like Blending. ... " It's also very important that your image has a BLACK background and the mask has a ... Mask for the other. ... I expected the color texture to be only partially visible, in stead, the Mask texture shines through. ...
    (comp.graphics.api.opengl)
  • How do you blend two textures using bitmap/alpha mask
    ... I'm trying to figure out how I can use an bitmap mask to blend a rock texture ... with my grass terrain. ...
    (microsoft.public.win32.programmer.directx.graphics)
  • How to use a bitmap mask to blend two textures
    ... I'm trying to figure out how I can use an bitmap mask to blend a rock texture ... with my grass terrain. ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: TextureBlend Experts Required
    ... contains a mask for the path texture. ... can use D3DXCreateTextureFromFileEx to generate an alpha channel based ... You can include the mask in an alpha channel in your texture file at ... Once you have your alpha mask in your path texture, ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: TextureBlend Experts Required
    ... with alpha, i.e using 0xFF000000 as the colorkey during load. ... we are already pushing the envelope regarding texture memory, ... the ability to use the blend cascade to get this effect would make life so ... You can include the mask in an alpha channel in your texture file at ...
    (microsoft.public.win32.programmer.directx.graphics)