1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
//This example draws a textured quad on a black background
//Our vertex structure: a point, a color and texture coordinates
struct TEXTURED_VERTEX
{
float x, y, z; // Position in 3d space
DWORD color;
float tu, tv; // Texture coordinates
};
//This is a textured quad. Each point is colored white so that the color
//shown is the true texture color. This is set up such that the whole
//texture is displayed on the quad. Note the texture coordinates
TEXTURED_VERTEX sprite_frame[] =
{
{ -1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), 0.0f, 1.0f}, // Bottom left vertex
{ 1.0f , -1.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 1.0f}, // Bottom right vertex
{ 1.0f , 1.0f , 0.0f, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0.0f} // Top right vertex
{ -1.0f, 1.0f , 0.0f, D3DCOLOR_XRGB(255, 255, 255), 0.0f, 0.0f} // Top left vertex
};
//On Reset Device
//Tell DirectX about our vertex structure
D3DVERTEXELEMENT9 vertexDeclaration[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 3*sizeof(float), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
{0, 3*sizeof(float)+sizeof(DWORD), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END()
};
//Assuming you have a LPDIRECT3DDEVICE9 called pDevice
LPDIRECT3DVERTEXDECLARATION9 vertexDeclarationToSet = 0;
pDevice->CreateVertexDeclaration( vertexDeclaration, &vertexDeclarationToSet );
pDevice->SetVertexDeclaration( vertexDeclarationToSet );
//On Render
//Assuming you've loaded your texture, set the texture stage and set
//your matrices correctly
//Clear the scene. Make a black background
pDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );
pDevice->BeginScene();
//Draw our textured quad
pDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, sprite_frame, sizeof(TEXTURED_VERTEX) );
pDevice->EndScene();
pDevice->Present( 0, 0, 0, 0 );
|