|
问一下,在DX8中,如何对Texture进行半透时运算.
最好不要再用DX7中的lock取数据作+-*/运算,太慢了.
DX8SDK中有一段代码,是c++的,我看不懂.
以下是DX8 SDK中的代码.Blend Two Textures Using a Constant Color
This example blends two texture maps, using the vertex color, to determine how much of each texture map color to use. The differences between this example and the previous example are as follows:
The vertex data structure, the FVF macro, and the vertex data include a second set of texture coordinates because there is a second texture. SetTexture is also called twice, using two texture stage states.
The shader file declares two texture registers and uses the linear interpolate (lrp) instruction to blend the two textures. The values of the diffuse colors determine the ratio of texture0 to texture1 in the output color.
Here is the sample code.
struct CUSTOMVERTEX
{
FLOAT x, y, z;
DWORD color;
FLOAT tu1, tv1;
FLOAT tu2, tv2; // a second set of texture coordinates
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3D_FVF_DIFFUSE|D3DFVF_TEX2|D3DFVF_TEXCOORDSIZE4(0))
static CUSTOMVERTEX g_Vertices[]=
{
// x y z color u1 v1 u2 v2
{ -1.0f, -1.0f, 0.0f, 0xff0000ff, 1.0f, 1.0f, 1.0f, 1.0f },
{ +1.0f, -1.0f, 0.0f, 0xffff0000, 0.0f, 1.0f, 0.0f, 1.0f },
{ +1.0f, +1.0f, 0.0f, 0xffffff00, 0.0f, 0.0f, 0.0f, 0.0f },
{ -1.0f, +1.0f, 0.0f, 0xffffffff, 1.0f, 0.0f, 1.0f, 0.0f },
};
// Create a texture. This file is in the DirectX 8.1 media from the SDK download.
TCHAR strPath[512];
LPDIRECT3DTEXTURE8 m_pTexture0, m_pTexture1;
DXUtil_FindMediaFile( strPath, _T("DX5_Logo.bmp"));
D3DUtil_CreateTexture( m_pd3dDevice, strPath, &m_pTexture0, D3DFMT_R5G6B5 );
DXUtil_FindMediaFile( strPath, _T("snow2.jpg"));
D3DUtil_CreateTexture( m_pd3dDevice, strPath, &m_pTexture1, D3DFMT_R5G6B5 );
// Load the textures stages.
m_pd3dDevice->SetTexture( 0, m_pTexture0 );
m_pd3dDevice->SetTexture( 1, m_pTexture1 ); // Use a second texture stage.
m_pd3dDevice->SetStreamSource( 0, m_pQuadVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->SetPixelShader( m_hPixelShader );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
Contents of the file " ixelShader5.txt"
ps.1.0 // pixel shader version
tex t0 // texture register t0 is loaded from texture stage 0
tex t1 // texture register t1 is loaded from texture stage 1
mov r1, t1 // move texture register1 into output register r1
lrp r0, v0, t0, r1 // linearly interpolate between t0 and r1 by a proportion
// specified in v0
The resulting output is as follows:
(这里省略结果图,是两张重叠的半透明图片.) |
|