📄 xfiles.cpp
字号:
// transform and rotation value
vTrans = m_pvVelocity * m_fTimeElapsed * m_fSpeed;
vRot = m_pvAngularVelocity* m_fTimeElapsed * m_fAngularSpeed;
// Update position and view matricies
D3DXMATRIX matT, matR;
D3DXQUATERNION qR;
D3DXMatrixTranslation (&matT, vTrans.x, vTrans.y, vTrans.z); // step 1
D3DXMatrixMultiply (&m_matPosition, &matT, &m_matPosition);
D3DXQuaternionRotationYawPitchRoll (&qR, vRot.x, vRot.y, vRot.z); // step 2
D3DXMatrixRotationQuaternion (&matR, &qR); // step 3
D3DXMatrixMultiply (&m_matPosition, &matR, &m_matPosition); // step 4
D3DXMatrixInverse (&m_matView, NULL, &m_matPosition); // step 5
m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_matView );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
// Clear the viewport | z-buffer
m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0x00000000, 1.0f, 0L );
// Begin the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
// yellow object
m_pd3dDevice->SetMaterial( &m_oYellow.mtrl );
// Apply the object's local matrix
m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_oYellow.matLocal );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( FVF_CUSTOMVERTEX );
m_pd3dDevice->SetIndices( m_pIB, 0 );
m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0,
m_dwNumVertices, // number of vertices
0,
m_dwNumFaces); // number of primitives
// red object
m_pd3dDevice->SetMaterial( &m_oRed.mtrl);
// Apply the object's local matrix
m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_oRed.matLocal );
m_pd3dDevice->SetStreamSource( 0, m_pVB2, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( FVF_CUSTOMVERTEX );
m_pd3dDevice->SetIndices( m_pIB2, 0 );
m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0,
m_dwNumVertices2, // number of vertices
0,
m_dwNumFaces2); // number of primitives
// Output statistics
m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
m_pFont->InitDeviceObjects( m_pd3dDevice );
// load the file based objects
if( FAILED(m_oYellow.mesh->Create( m_pd3dDevice, _T("boidy2.x") ) ) )
return E_FAIL;
if( FAILED(m_oRed.mesh->Create( m_pd3dDevice, _T("boidr2.x") ) ) )
return E_FAIL;
// Set the FVF type to match the vertex format we want
m_oYellow.mesh->SetFVF( m_pd3dDevice, FVF_CUSTOMVERTEX);
m_oRed.mesh->SetFVF( m_pd3dDevice, FVF_CUSTOMVERTEX);
// Get the number of vertices and faces for the meshes
m_dwNumVertices = m_oYellow.mesh->GetSysMemMesh()->GetNumVertices();
m_dwNumFaces = m_oYellow.mesh->GetSysMemMesh()->GetNumFaces();
m_dwNumVertices2 = m_oRed.mesh->GetSysMemMesh()->GetNumVertices();
m_dwNumFaces2 = m_oRed.mesh->GetSysMemMesh()->GetNumFaces();
m_oYellow.mesh->GetSysMemMesh()->GetVertexBuffer( &m_pVB );
m_oRed.mesh->GetSysMemMesh()->GetVertexBuffer( &m_pVB2);
m_oYellow.mesh->GetSysMemMesh()->GetIndexBuffer( &m_pIB );
m_oRed.mesh->GetSysMemMesh()->GetIndexBuffer( &m_pIB2 );
D3DUtil_InitMaterial(m_oYellow.mtrl,
m_oYellow.mesh->m_pMaterials[0].Diffuse.r,
m_oYellow.mesh->m_pMaterials[0].Diffuse.g,
m_oYellow.mesh->m_pMaterials[0].Diffuse.b,
m_oYellow.mesh->m_pMaterials[0].Diffuse.a);
D3DUtil_InitMaterial(m_oRed.mtrl,
m_oRed.mesh->m_pMaterials[0].Diffuse.r,
m_oRed.mesh->m_pMaterials[0].Diffuse.g,
m_oRed.mesh->m_pMaterials[0].Diffuse.b,
m_oRed.mesh->m_pMaterials[0].Diffuse.a);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_pFont->RestoreDeviceObjects();
m_oYellow.mesh->RestoreDeviceObjects( m_pd3dDevice );
m_oRed.mesh->RestoreDeviceObjects( m_pd3dDevice );
// Set the transform matrices
D3DXMatrixTranslation(&m_matView, 0.0f, 0.0f, -20.0f);
D3DXMatrixTranslation(&m_matPosition, 0.0f, 0.0f, -5.0f);
// Set the projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 500.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
// Set up the lights
if( m_d3dCaps.VertexProcessingCaps & D3DVTXPCAPS_DIRECTIONALLIGHTS )
{
D3DLIGHT8 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.5f, -5.0f, -1.0f );
light.Ambient.r = 0.1f;
light.Ambient.g = 0.1f;
light.Ambient.b = 0.1f;
m_pd3dDevice->SetLight( 0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.5f, 5.0f, -1.0f );
light.Ambient.r = 0.2f;
light.Ambient.g = 0.2f;
light.Ambient.b = 0.2f;
m_pd3dDevice->SetLight( 1, &light );
m_pd3dDevice->LightEnable( 1, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
}
else
{
m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x0c0c0c0c);
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_pFont->InvalidateDeviceObjects();
m_oYellow.mesh->InvalidateDeviceObjects();
m_oRed.mesh->InvalidateDeviceObjects();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
m_pFont->DeleteDeviceObjects();
m_oYellow.mesh->Destroy();
m_oRed.mesh->Destroy();
SAFE_RELEASE( m_pVB);
SAFE_RELEASE( m_pIB);
SAFE_RELEASE( m_pVB2);
SAFE_RELEASE( m_pIB2);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
SAFE_DELETE( m_pFont );
SAFE_DELETE( m_oYellow.mesh);
SAFE_DELETE( m_oRed.mesh);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device intialization, this code checks the device
// for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam )
{
// Record key presses
if( WM_KEYDOWN == uMsg )
{
m_bKey[wParam] = 1;
}
// Perform commands when keys are rleased
if( WM_KEYUP == uMsg )
{
m_bKey[wParam] = 0;
}
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -