typedef struct D3DXMATRIX {
FLOAT _11, FLOAT _12, FLOAT _13, FLOAT _14,
FLOAT _21, FLOAT _22, FLOAT _23, FLOAT _24,
FLOAT _31, FLOAT _32, FLOAT _33, FLOAT _34,
FLOAT _41, FLOAT _42, FLOAT _43, FLOAT _44 );
} D3DXMATRIX;
D3DXMatrix * m;
D3DMatrixIdentity( & m);
D3DXVec3Normalize( & vLook, & vLook ); // 以Look为标准
D3DXVec3Cross( & vRight, & vUp, & vLook ); // 求Up和Look叉积来设定Right
D3DXVec3Normalize( & vRight, & vRight );
D3DXVec3Cross ( & vUp, & vLook, & vRight); // 求Look和Right叉积来设定Up
D3DXVec3Normalize( & vUp, & vUp );
#include " dxstdafx.h "
// Plain vertex struct
structPlainVertex
{
FLOATx, y, z;
FLOATtu, tv;
};
// replace D3DFVF_XYZRHW with D3DFVF_XYZ
#definePLAIN_FVF (D3DFVF_XYZ | D3DFVF_TEX1)
// Object PlainVertex wrap class
classPlain
{
public :
Plain(IDirect3DDevice9 * device);
~ Plain();
HRESULTCreatePlain();
boolOnFrameMove( IDirect3DDevice9 * pd3dDevice, double fTime );
boolOnFrameRender( D3DMATERIAL9 * mtrl, IDirect3DTexture9 * tex);
public :
IDirect3DDevice9 * m_device;
IDirect3DVertexBuffer9 * m_vb;
IDirect3DIndexBuffer9 * m_ib;
};
// Constructor
Plain::Plain(IDirect3DDevice9 * device)
{
m_device = device;
m_vb = NULL;
m_ib = NULL;
}
// Create the plain first when reset device
inlineHRESULTPlain::CreatePlain()
{
HRESULThr;
PlainVertexplainVertex[] =
{
{ - 1.0f , - 1.0f , 0.0f , 1.0f , 1.0f }, // x, y, z, tu, tv : left bottom
{ 1.0f , - 1.0f , 0.0f , 0.0f , 1.0f }, // right bottom
{ 1.0f , 1.0f , 0.0f , 0.0f , 0.0f }, // right up
{ - 1.0f , 1.0f , 0.0f , 1.0f , 0.0f }, // left up
};
V_RETURN(m_device -> CreateVertexBuffer(
sizeof (plainVertex),
0 ,
PLAIN_FVF,
D3DPOOL_MANAGED,
& m_vb,
NULL));
PlainVertex * v;
V_RETURN(m_vb -> Lock( 0 , 0 , ( void ** ) & v, 0 ));
memcpy( v, plainVertex, sizeof (plainVertex) );
m_vb -> Unlock();
WORDwIndeces[] = { 3 , 2 , 0 , 2 , 1 , 0 };
V_RETURN( m_device -> CreateIndexBuffer( sizeof (wIndeces),
0 , D3DFMT_INDEX16,
D3DPOOL_MANAGED, & m_ib, NULL) );
VOID * pIndeces;
V_RETURN( m_ib -> Lock( 0 , sizeof (wIndeces), & pIndeces, 0 ) );
memcpy( pIndeces, wIndeces, sizeof (wIndeces) );
m_ib -> Unlock();
return S_OK;
}
// Things to do when frame move for this plain
inline bool Plain::OnFrameMove( IDirect3DDevice9 * pd3dDevice, double fTime )
{
D3DXMATRIX matRotY;
D3DXMatrixRotationY( & matRotY, ( float )fTime * 2.0f );
D3DXMATRIX matTrans;
D3DXMatrixTranslation( & matTrans, 0.0f , 0.0f , 0.0f );
D3DXMATRIX matResult;
matResult = matRotY * matTrans;
pd3dDevice -> SetTransform( D3DTS_WORLD, & matResult );
returntrue;
}
// Things to do when frame render for this plain
inline bool Plain::OnFrameRender(D3DMATERIAL9 * mtrl, IDirect3DTexture9 * tex)
{
if ( mtrl )
m_device -> SetMaterial(mtrl);
if ( tex )
m_device -> SetTexture( 0 , tex);
m_device -> SetSamplerState( 0 , D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
m_device -> SetSamplerState( 0 , D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
m_device -> SetSamplerState( 0 , D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
m_device -> SetStreamSource( 0 , m_vb, 0 , sizeof (PlainVertex));
m_device -> SetIndices(m_ib);
m_device -> SetFVF(PLAIN_FVF);
m_device -> DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0 , 0 , 4 , 0 , 2 );
returntrue;
}
// destructor, must invoked when lost device
Plain:: ~ Plain()
{
SAFE_RELEASE(m_vb);
SAFE_RELEASE(m_ib);
}
接下来的事情就是在主回调函数中调用上面函数了
// Declaration
Plain * g_pPlain = NULL;
// codes in OnCreateDevice
// Turn off culling
pd3dDevice -> SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn off D3D lighting
pd3dDevice -> SetRenderState( D3DRS_LIGHTING, FALSE );
// Turn on the zbuffer
pd3dDevice -> SetRenderState( D3DRS_ZENABLE, TRUE );
// Codes in OnResetDevice
g_pPlain = newPlain(pd3dDevice);
V_RETURN ( g_pPlain -> CreatePlain() );
// Codes in OnFrameMove.
// invoke plain’s OnFrameMove fisrt to set the world matrics
g_pPlain -> OnFrameMove( pd3dDevice, fTime );
// Set up our view matrix. A view matrix can be defined given an eye point,
// a point to lookat, and a direction for which way is up. Here, we set the
// eye five units back along the z-axis and up three units, look at the
// origin, and define "up" to be in the y-direction.
D3DXVECTOR3vEyePt( 0.0f , 0.0f , 4.0f );
D3DXVECTOR3vLookatPt( 0.0f , 0.0f , 0.0f );
D3DXVECTOR3vUpVec( 0.0f , 1.0f , 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( & matView, & vEyePt, & vLookatPt, & vUpVec );
pd3dDevice -> SetTransform( D3DTS_VIEW, & matView );
// For the projection matrix, we set up a perspective transform (which
// transforms geometry from 3D view space to 2D viewport space, with
// a perspective divide making objects smaller in the distance). To build
// a perpsective transform, we need the field of view (1/4 pi is common),
// the aspect ratio, and the near and far clipping planes (which define at
// what distances geometry should be no longer be rendered).
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( & matProj, D3DX_PI / 4 , 1.0f , 1.0f , 100.0f );
pd3dDevice -> SetTransform( D3DTS_PROJECTION, & matProj );
// Codes in OnFrameRender
g_pPlain -> OnFrameRender( 0 ,g_pTexture);
// Codes in OnLostDevice
g_pPlain ->~ Plain();