1OnCreateDevice函数
// Create the effect pool object if shared param is enabled
if( g_SampleUI.GetCheckBox( IDC_SHARE )->GetChecked() )
V_RETURN( D3DXCreateEffectPool( &g_pEffectPool ) );
// Create the environment map texture
V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"lobby\\lobbycube.dds" ) );
V_RETURN( D3DXCreateCubeTextureFromFile( pd3dDevice, str, &g_pEnvMapTex ) );
以上两段代码,看注释就好。
// Create the meshes
for( int i = 0; i < sizeof( g_MeshListData ) / sizeof( g_MeshListData[0] ); ++i )
{
CEffectMesh NewMesh;
if( SUCCEEDED( NewMesh.Create( g_MeshListData[i].wszFile, DXUTGetD3D9Device() ) ) )
{
g_Meshes.Add( NewMesh );
D3DXMATRIXA16 mW;
LPVOID pVerts = NULL;
D3DXMatrixIdentity( &mW );
//省略,计算包围盒
g_MeshListData[i].dwNumMat = NewMesh.GetNumMaterials();
g_amWorld.Add( mW );
// Set the arcball window size
const D3DSURFACE_DESC* pD3DSD = DXUTGetD3D9BackBufferSurfaceDesc();
g_ArcBall[g_ArcBall.GetSize() - 1].SetWindow( pD3DSD->Width, pD3DSD->Height );
}
}
载入模型。
2OnFrameRender
for( int i = 0; i < g_Meshes.GetSize(); ++i )
{
D3DXMATRIXA16 mWorld = *g_ArcBall[i].GetRotationMatrix() * *g_ArcBall[i].GetTranslationMatrix();
mWorld = g_amWorld[i] * mWorld;
D3DXMATRIXA16 mRot;
D3DXMATRIXA16 mTrans;
D3DXMatrixTranslation( &mTrans, 0.0f, 0.0f, -3.0f );
D3DXMatrixRotationY( &mRot, fAngleDelta * ( i - g_nActiveMesh ) );
mWorld *= mTrans * mRot * g_mScroll;
mWorldView = mWorld * *g_Camera.GetViewMatrix();
mWorldViewProjection = mWorld * mViewProj;
V( g_pEffect->SetMatrix( "g_mWorld", &mWorld ) );
V( g_pEffect->SetMatrix( "g_mView", g_Camera.GetViewMatrix() ) );
V( g_pEffect->SetMatrix( "g_mProj", g_Camera.GetProjMatrix() ) );
g_Meshes[i].Render( pd3dDevice );
}
渲染所有网格。注意,这里每个网格只设定了一次WVP矩阵,因为采用了ParameterShare。