Microsoft DirectX 8.1 (version 1.0, 1.1) |
This example applies a texture map to the object.
The vertex data contains object position data as well as texture position or uv data. This causes changes to the vertex declaration structure and the fixed vertex function macro. The vertex data is also shown below.
struct CUSTOMVERTEX_POS_TEX1 { float x, y, z; // object position data float tu1, tv1; // texture position data }; #define D3DFVF_CUSTOMVERTEX_POS_TEX1 (D3DFVF_XYZ|D3DFVF_TEX1) CUSTOMVERTEX_POS_TEX1 g_Vertices[]= { // x y z u1 v1 { -0.75f, -0.5f, 0.0f, 0.0f, 0.0f }, // - bottom right { 0.25f, -0.5f, 0.0f, 1.0f, 0.0f }, // - bottom left { 0.25f, 0.5f, 0.0f, 1.0f, -1.0f }, // - top left { -0.75f, 0.5f, 0.0f, 0.0f, -1.0f }, // - top right }; D3DUtil_CreateTexture( m_pd3dDevice, _T("earth.bmp"), &m_pTexture0, D3DFMT_R5G6B5 );
The texture image must be loaded. In this case, the file "earth.bmp" contains a 2-D texture map of the earth and will be used to color the object.
The vertex shader declaration needs to reflect the object position and texture position data.
DWORD dwDecl2[] = { D3DVSD_STREAM(0), D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), D3DVSD_REG(8, D3DVSDT_FLOAT2 ), D3DVSD_END() };
This declaration declares one stream of data that contains the object position and the texture position data. The texture position data is assigned to vertex register 8.
The rendering code tells Microsoft® Direct3D® where to find the data stream and the shader, and sets up texture stages because a texture map is being applied.
m_pd3dDevice->SetStreamSource( 0, m_pQuadVB, sizeof(CUSTOMVERTEX_POS_TEX1) ); m_pd3dDevice->SetVertexShader( m_hVertexShader ); 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->SetTexture( 0, m_pTexture0 ); m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 ); m_pd3dDevice->SetTexture( 0, NULL );
Because there is a single texture, the texture stage states need to be set for texture state 0. These methods tell Direct3D that the texel values will be used to provide diffuse color for the object vertices. In other words, a 2-D texture map will be applied like a decal.
Here is the shader file.
vs.1.0 ; version instruction m4x4 oPos, v0, c0 ; transform vertices by view/projection matrix mov oT0, v8 ; move texture color to output texture register
The shader file contains three instructions. The first is always the version instruction. The second instruction transforms the vertices. The third instruction moves the texture colors from register v8 to the output diffuse color register. That results in a texture mapped object, which is shown below.