📄 1steps.cpp
字号:
D3DXMatrixRotationY( &matRotateY, -m_pObjects[1].vR.x );
D3DXMatrixRotationX( &matRotateX, -m_pObjects[1].vR.y );
D3DXMatrixRotationZ( &matRotateZ, -m_pObjects[1].vR.z );
D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld );
m_pObjects[1].matLocal = matWorld;
//************************************************************
// camera stuff
//************************************************************
static D3DXVECTOR3 vLook=D3DXVECTOR3(0.0f,0.0f,1.0);
static D3DXVECTOR3 vUp=D3DXVECTOR3(0.0f,1.0f,0.0f);
static D3DXVECTOR3 vRight=D3DXVECTOR3(1.0f,0.0f,0.0f);
static D3DXVECTOR3 vPos=D3DXVECTOR3(0.0f,0.0f,-5.0f);
FLOAT fPitch,fYaw,fRoll;
fPitch = fYaw = fRoll = 0.0f;
FLOAT fspeed= 1.0f * m_fTimeElapsed;
if (m_bKey[VK_UP]) fPitch=-0.3f * m_fTimeElapsed; // fPitch
if (m_bKey[VK_DOWN]) fPitch=+0.3f * m_fTimeElapsed;
if (m_bKey['C']) fYaw=+0.3f * m_fTimeElapsed; // fYaw
if (m_bKey['X']) fYaw=-0.3f * m_fTimeElapsed;
if (m_bKey[VK_LEFT]) fRoll=-0.3f * m_fTimeElapsed; // fRoll
if (m_bKey[VK_RIGHT]) fRoll=+0.3f * m_fTimeElapsed;
// camera forward
if (m_bKey[VK_HOME]) // Key HOME
{
vPos.x+=fspeed*vLook.x;
vPos.y+=fspeed*vLook.y;
vPos.z+=fspeed*vLook.z;
}
// camera back
if (m_bKey[VK_END]) // Key END
{
vPos.x-=fspeed*vLook.x;
vPos.y-=fspeed*vLook.y;
vPos.z-=fspeed*vLook.z;
}
// base vector regeneration
D3DXVec3Normalize(&vLook, &vLook);
D3DXVec3Cross(&vRight, &vUp, &vLook); // Cross Produkt of the UP and LOOK Vector
D3DXVec3Normalize(&vRight, &vRight);
D3DXVec3Cross(&vUp, &vLook, &vRight); // Cross Produkt of the RIGHT and LOOK Vector
D3DXVec3Normalize(&vUp, &vUp);
// Matrices for pitch, yaw and roll
D3DXMATRIX matPitch, matYaw, matRoll;
D3DXMatrixRotationAxis(&matPitch, &vRight, fPitch );
D3DXMatrixRotationAxis(&matYaw, &vUp, fYaw );
D3DXMatrixRotationAxis(&matRoll, &vLook, fRoll);
// rotate the LOOK & RIGHT Vectors about the UP Vector
D3DXVec3TransformCoord(&vLook, &vLook, &matYaw);
D3DXVec3TransformCoord(&vRight, &vRight, &matYaw);
// rotate the LOOK & UP Vectors about the RIGHT Vector
D3DXVec3TransformCoord(&vLook, &vLook, &matPitch);
D3DXVec3TransformCoord(&vUp, &vUp, &matPitch);
// rotate the RIGHT & UP Vectors about the LOOK Vector
D3DXVec3TransformCoord(&vRight, &vRight, &matRoll);
D3DXVec3TransformCoord(&vUp, &vUp, &matRoll);
D3DXMATRIX view=matWorld;
D3DXMatrixIdentity( &view );
view._11 = vRight.x; view._12 = vUp.x; view._13 = vLook.x;
view._21 = vRight.y; view._22 = vUp.y; view._23 = vLook.y;
view._31 = vRight.z; view._32 = vUp.z; view._33 = vLook.z;
view._41 = - D3DXVec3Dot( &vPos, &vRight );
view._42 = - D3DXVec3Dot( &vPos, &vUp );
view._43 = - D3DXVec3Dot( &vPos, &vLook );
m_pd3dDevice->SetTransform(D3DTS_VIEW, &view);
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()
{
D3DMATERIAL8 mtrl;
// 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
// Set the color for the object
D3DUtil_InitMaterial( mtrl, m_pObjects[0].fR, m_pObjects[0].fG, m_pObjects[0].fB );
m_pd3dDevice->SetMaterial( &mtrl );
// Apply the object's local matrix
m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_pObjects[0].matLocal );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->SetIndices( m_pIB, 0 );
m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
16, // number of vertices
0,
10); // number of primitives
// red object
// Set the color for the object
D3DUtil_InitMaterial( mtrl, m_pObjects[1].fR, m_pObjects[1].fG, m_pObjects[1].fB );
m_pd3dDevice->SetMaterial( &mtrl );
// Apply the object's local matrix
m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_pObjects[1].matLocal );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->SetIndices( m_pIB, 0 );
m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
16, // number of vertices
0,
10); // 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 );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_pFont->RestoreDeviceObjects();
D3DMATERIAL8 mtrl;
D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f );
m_pd3dDevice->SetMaterial( &mtrl );
// 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, -1.0f, 5.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, 1.0f, -10.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);
}
// fill the vertex buffer
m_dwSizeofVertices = sizeof(m_pvObjectVertices);
// Create the vertex buffer
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pVB ) ) )
return E_FAIL;
VOID* pVertices;
if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (BYTE**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, m_pvObjectVertices, m_dwSizeofVertices);
m_pVB->Unlock();
// fill the Index buffer
m_dwSizeofIndices = sizeof(m_pwObjectIndices);
// Create the index buffer
if( FAILED( m_pd3dDevice->CreateIndexBuffer( m_dwSizeofIndices,
0, D3DFMT_INDEX16,
D3DPOOL_MANAGED, &m_pIB ) ) )
return E_FAIL;
VOID* pIndices;
if( FAILED( m_pIB->Lock( 0, m_dwSizeofIndices, (BYTE**)&pIndices, 0 ) ) )
return E_FAIL;
memcpy( pIndices, m_pwObjectIndices, m_dwSizeofIndices );
m_pIB->Unlock();
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();
SAFE_RELEASE( m_pVB );
SAFE_RELEASE( m_pIB );
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();
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()
{
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 + -