維尼的蜂巢

RealTime??!! It’s amazing!!!!

SetTextureStageState 十月 26, 2005

Filed under: DirectX — kevinlin @ 6:59 上午
前面有寫了一個setsamplerstate 那是對單一張貼圖的處理
現在來講 多張貼圖 要怎樣做呢
 
就是用
HRESULT SetTextureStageState(
   DWORD Stage,   //stage的值(從0到7 表示可貼8張)
   D3DTEXTURESTAGESTATETYPE Type,  //你想要改變貼圖狀態的旗標
   DWORD Value //貼圖狀態對應的值
);
Stage – the stage value, ranges from 0 to 7 (a count of 8)
State – this is a flag representing one of the texture states that you want to change.
Value – each state can have a value, what goes here depends totally on the State value.
 
D3DTEXTURESTAGESTATETYPE的列舉如下
typedef enum _D3DTEXTURESTAGESTATETYPE {
    D3DTSS_COLOROP = 1, //per-stage blending controls for colour channels
    D3DTSS_COLORARG1 = 2, //texture argument
    D3DTSS_COLORARG2 = 3, //texture argument
    D3DTSS_ALPHAOP = 4, //per-stage blending controls for alpha channel
    D3DTSS_ALPHAARG1 = 5, //texture argument
    D3DTSS_ALPHAARG2 = 6, //texture argument
    D3DTSS_BUMPENVMAT00 = 7, //bump mapping matrix
    D3DTSS_BUMPENVMAT01 = 8, //bump mapping matrix
    D3DTSS_BUMPENVMAT10 = 9, //bump mapping matrix
    D3DTSS_BUMPENVMAT11 = 10, //bump mapping matrix
    D3DTSS_TEXCOORDINDEX = 11, //identifies which set of texture coordinates index this texture
    D3DTSS_BUMPENVLSCALE = 22, //scale for bump map luminance
    D3DTSS_BUMPENVLOFFSET = 23, //offset for bump map luminance
    D3DTSS_TEXTURETRANSFORMFLAGS = 24, //controls texture transform
    D3DTSS_COLORARG0 = 26, //third argument for triadic ops
    D3DTSS_ALPHAARG0 = 27, //third argument for triadic ops
    D3DTSS_RESULTARG = 28, //argument for result
    D3DTSS_CONSTANT = 32, //per-stage constant
    D3DTSS_FORCE_DWORD = 0x7fffffff
} D3DTEXTURESTAGESTATETYPE;
 
 
而對映的value的值 有很多種 先介紹兩種 一種是給color 或 alpha 的arg value Texture Argument constants另一種是 給 color 或alpha的 op per-stage texture-blending operations
 
per-stage texture-blending operations的列舉如下
typedef enum _D3DTEXTUREOP {
    D3DTOP_DISABLE = 1,
    D3DTOP_SELECTARG1 = 2,
    D3DTOP_SELECTARG2 = 3,
    D3DTOP_MODULATE = 4,
    D3DTOP_MODULATE2X = 5,
    D3DTOP_MODULATE4X = 6,
    D3DTOP_ADD = 7,
    D3DTOP_ADDSIGNED = 8,
    D3DTOP_ADDSIGNED2X = 9,
    D3DTOP_SUBTRACT = 10,
    D3DTOP_ADDSMOOTH = 11,
    D3DTOP_BLENDDIFFUSEALPHA = 12,
    D3DTOP_BLENDTEXTUREALPHA = 13,
    D3DTOP_BLENDFACTORALPHA = 14,
    D3DTOP_BLENDTEXTUREALPHAPM = 15,
    D3DTOP_BLENDCURRENTALPHA = 16,
    D3DTOP_PREMODULATE = 17,
    D3DTOP_MODULATEALPHA_ADDCOLOR = 18,
    D3DTOP_MODULATECOLOR_ADDALPHA = 19,
    D3DTOP_MODULATEINVALPHA_ADDCOLOR = 20,
    D3DTOP_MODULATEINVCOLOR_ADDALPHA = 21,
    D3DTOP_BUMPENVMAP = 22,
    D3DTOP_BUMPENVMAPLUMINANCE = 23,
    D3DTOP_DOTPRODUCT3 = 24,
    D3DTOP_MULTIPLYADD = 25,
    D3DTOP_LERP = 26,
    D3DTOP_FORCE_DWORD = 0x7fffffff
} D3DTEXTUREOP;
參閱MSDN
 
而Texture Argument constants如下
Argument flags
D3DTA_CURRENT
D3DTA_DIFFUSE
D3DTA_SELECTMASK
D3DTA_SPECULAR
D3DTA_TEMP
D3DTA_TEXTURE
D3DTA_TFACTOR
Modifier flags
D3DTA_ALPHAREPLICATE
D3DTA_COMPLEMENT
 
接下來是
Colour states的詳細說明

D3DTSS_COLOROP – used to define the colour blending operation. There are many options like D3DTOP_MODULATE which multiplies the colour arguments together. You can also select to add them or blend them together or just select the first colour argument or just the second etc. There are a large number of possibilities.
D3DTSS_COLORARG1 – this is the first colour argument supplied to the above operation, it can be D3DTA_CURRENT which means this argument is set to the result of the previous stage (unless this is the first stage where by it is equal to the diffuse colour) or D3DTA_DIFFUSE which sets this argument to the current diffuse colour or D3DTA_TEXTURE which sets the argument to the texture colour. There are others available.
D3DTSS_COLORARG2 – this is the second argument used in the operation and can be set to the same set of values as argument 1 above.

These states provide a host of possibilities especially when you consider that they can be chained together across stages. So the result of one stage can be fed into the input of the next stage. For the first stage the defaults are:

device->SetTextureStageState(0, D3DTSS_COLOROP,D3DTOP_MODULATE);
device->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
device->SetTextureStageState(0, D3DTSS_COLORARG2,D3DTA_CURRENT);

If we interpret this we can see that the operation is to multiply together the colour value in the texture with the diffuse colour (interpolated from vertex components during Gouraud shading). Current normally means the result of the previous stage but since this is the first stage it simply uses the diffuse colour.

Subsequent stages are disabled by default (their operation is set to D3DTOP_DISABLE).

 

Alpha States 的詳細說明

D3DTSS_ALPHAOP

D3DTSS_ALPHAARG1

D3DTSS_ALPHAARG2

These three states take the same values as those specified for the colour states but they act on the alpha channel only. The defaults for the first stage are:

device->SetTextureStageState(0, D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
device->SetTextureStageState(0, D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
device->SetTextureStageState(0, D3DTSS_ALPHAARG2,D3DTA_CURRENT);

This means that the alpha value is set to what is in the texture. Subsequent stages have the alpha operation disabled.

 

Texture Coordinates的說明

D3DTSS_TEXCOORDINDEX – you can specify up to 8 texture coordinate sets in your vertex declaration. Here you can define which of those sets should be used for this stage. An advanced method is to let Direct3D generate the coordinates e.g. use the camera space vertex normal.

  • D3DTSS_TEXTURETRANSFORMFLAGS – allows you to apply a calculation to the texture coordinates.
  •  

    其餘的states 像是bump mapping等 就參閱MSDN

     

    接下來舉個例子

    如果要做Light Map

    首先 先設定第一張貼圖  假設有一面牆

    device->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
    device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    device->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    device->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG2 );
    device->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

    在這個stage中 我們先讓texture的colour跟一個已存在的diffuse colour去做混合(相乘)的計算

     

    接著 設第二張貼圖 一個light map

    device->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE );
    device->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    device->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
    device->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
    device->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );

    再這裡 我們把第二個texture colour跟上一個stage的結果去做modulate(multiply相乘) 這步驟modulate我們的light map到以存在的貼圖上並且貼到幾何圖圖上 然後 alpha channel的值被設定到texture的alpha value上

     

    最後一個步驟

    device->SetTextureStageState( 2, D3DTSS_COLOROP,   D3DTOP_DISABLE );
    device->SetTextureStageState( 2, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

    這邊我們沒做什麼事情 只是disable所有的 operation

     

     

    One Response to “SetTextureStageState”


    發表迴響

    在下方填入你的資料或按右方圖示以社群網站登入:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / 變更 )

    Twitter picture

    You are commenting using your Twitter account. Log Out / 變更 )

    Facebook照片

    You are commenting using your Facebook account. Log Out / 變更 )

    Google+ photo

    You are commenting using your Google+ account. Log Out / 變更 )

    連結到 %s