?? dxutmisc.cpp
字號:
m_EffectCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Device event callbacks
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnCreateDevice( IDirect3DDevice9 *pd3dDevice )
{
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnResetDevice( IDirect3DDevice9 *pd3dDevice )
{
// Call OnResetDevice on all effect and font objects
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
m_EffectCache[i].pEffect->OnResetDevice();
for( int i = 0; i < m_FontCache.GetSize(); ++i )
m_FontCache[i].pFont->OnResetDevice();
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnLostDevice()
{
// Call OnLostDevice on all effect and font objects
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
m_EffectCache[i].pEffect->OnLostDevice();
for( int i = 0; i < m_FontCache.GetSize(); ++i )
m_FontCache[i].pFont->OnLostDevice();
// Release all the default pool textures
for( int i = m_TextureCache.GetSize() - 1; i >= 0; --i )
if( m_TextureCache[i].Pool == D3DPOOL_DEFAULT )
{
SAFE_RELEASE( m_TextureCache[i].pTexture );
m_TextureCache.Remove( i ); // Remove the entry
}
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnDestroyDevice()
{
// Release all resources
for( int i = m_EffectCache.GetSize() - 1; i >= 0; --i )
{
SAFE_RELEASE( m_EffectCache[i].pEffect );
m_EffectCache.Remove( i );
}
for( int i = m_FontCache.GetSize() - 1; i >= 0; --i )
{
SAFE_RELEASE( m_FontCache[i].pFont );
m_FontCache.Remove( i );
}
for( int i = m_TextureCache.GetSize() - 1; i >= 0; --i )
{
SAFE_RELEASE( m_TextureCache[i].pTexture );
m_TextureCache.Remove( i );
}
return S_OK;
}
//--------------------------------------------------------------------------------------
CD3DArcBall::CD3DArcBall()
{
Reset();
m_vDownPt = D3DXVECTOR3(0,0,0);
m_vCurrentPt = D3DXVECTOR3(0,0,0);
m_Offset.x = m_Offset.y = 0;
RECT rc;
GetClientRect( GetForegroundWindow(), &rc );
SetWindow( rc.right, rc.bottom );
}
//--------------------------------------------------------------------------------------
void CD3DArcBall::Reset()
{
D3DXQuaternionIdentity( &m_qDown );
D3DXQuaternionIdentity( &m_qNow );
D3DXMatrixIdentity( &m_mRotation );
D3DXMatrixIdentity( &m_mTranslation );
D3DXMatrixIdentity( &m_mTranslationDelta );
m_bDrag = FALSE;
m_fRadiusTranslation = 1.0f;
m_fRadius = 1.0f;
}
//--------------------------------------------------------------------------------------
D3DXVECTOR3 CD3DArcBall::ScreenToVector( float fScreenPtX, float fScreenPtY )
{
// Scale to screen
FLOAT x = -(fScreenPtX - m_Offset.x - m_nWidth/2) / (m_fRadius*m_nWidth/2);
FLOAT y = (fScreenPtY - m_Offset.y - m_nHeight/2) / (m_fRadius*m_nHeight/2);
FLOAT z = 0.0f;
FLOAT mag = x*x + y*y;
if( mag > 1.0f )
{
FLOAT scale = 1.0f/sqrtf(mag);
x *= scale;
y *= scale;
}
else
z = sqrtf( 1.0f - mag );
// Return vector
return D3DXVECTOR3( x, y, z );
}
//--------------------------------------------------------------------------------------
D3DXQUATERNION CD3DArcBall::QuatFromBallPoints(const D3DXVECTOR3 &vFrom, const D3DXVECTOR3 &vTo)
{
D3DXVECTOR3 vPart;
float fDot = D3DXVec3Dot(&vFrom, &vTo);
D3DXVec3Cross(&vPart, &vFrom, &vTo);
return D3DXQUATERNION(vPart.x, vPart.y, vPart.z, fDot);
}
//--------------------------------------------------------------------------------------
void CD3DArcBall::OnBegin( int nX, int nY )
{
// Only enter the drag state if the click falls
// inside the click rectangle.
if( nX >= m_Offset.x &&
nX < m_Offset.x + m_nWidth &&
nY >= m_Offset.y &&
nY < m_Offset.y + m_nHeight )
{
m_bDrag = true;
m_qDown = m_qNow;
m_vDownPt = ScreenToVector( (float)nX, (float)nY );
}
}
//--------------------------------------------------------------------------------------
void CD3DArcBall::OnMove( int nX, int nY )
{
if (m_bDrag)
{
m_vCurrentPt = ScreenToVector( (float)nX, (float)nY );
m_qNow = m_qDown * QuatFromBallPoints( m_vDownPt, m_vCurrentPt );
}
}
//--------------------------------------------------------------------------------------
void CD3DArcBall::OnEnd()
{
m_bDrag = false;
}
//--------------------------------------------------------------------------------------
// Desc:
//--------------------------------------------------------------------------------------
LRESULT CD3DArcBall::HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
// Current mouse position
int iMouseX = (short)LOWORD(lParam);
int iMouseY = (short)HIWORD(lParam);
switch( uMsg )
{
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
SetCapture( hWnd );
OnBegin( iMouseX, iMouseY );
return TRUE;
case WM_LBUTTONUP:
ReleaseCapture();
OnEnd();
return TRUE;
case WM_CAPTURECHANGED:
if( (HWND)lParam != hWnd )
{
ReleaseCapture();
OnEnd();
}
return TRUE;
case WM_RBUTTONDOWN:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN:
case WM_MBUTTONDBLCLK:
SetCapture( hWnd );
// Store off the position of the cursor when the button is pressed
m_ptLastMouse.x = iMouseX;
m_ptLastMouse.y = iMouseY;
return TRUE;
case WM_RBUTTONUP:
case WM_MBUTTONUP:
ReleaseCapture();
return TRUE;
case WM_MOUSEMOVE:
if( MK_LBUTTON&wParam )
{
OnMove( iMouseX, iMouseY );
}
else if( (MK_RBUTTON&wParam) || (MK_MBUTTON&wParam) )
{
// Normalize based on size of window and bounding sphere radius
FLOAT fDeltaX = ( m_ptLastMouse.x-iMouseX ) * m_fRadiusTranslation / m_nWidth;
FLOAT fDeltaY = ( m_ptLastMouse.y-iMouseY ) * m_fRadiusTranslation / m_nHeight;
if( wParam & MK_RBUTTON )
{
D3DXMatrixTranslation( &m_mTranslationDelta, -2*fDeltaX, 2*fDeltaY, 0.0f );
D3DXMatrixMultiply( &m_mTranslation, &m_mTranslation, &m_mTranslationDelta );
}
else // wParam & MK_MBUTTON
{
D3DXMatrixTranslation( &m_mTranslationDelta, 0.0f, 0.0f, 5*fDeltaY );
D3DXMatrixMultiply( &m_mTranslation, &m_mTranslation, &m_mTranslationDelta );
}
// Store mouse coordinate
m_ptLastMouse.x = iMouseX;
m_ptLastMouse.y = iMouseY;
}
return TRUE;
}
return FALSE;
}
//--------------------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------------------
CBaseCamera::CBaseCamera()
{
m_cKeysDown = 0;
ZeroMemory( m_aKeys, sizeof(BYTE)*CAM_MAX_KEYS );
ZeroMemory( m_GamePad, sizeof(DXUT_GAMEPAD)*DXUT_MAX_CONTROLLERS );
// Set attributes for the view matrix
D3DXVECTOR3 vEyePt = D3DXVECTOR3(0.0f,0.0f,0.0f);
D3DXVECTOR3 vLookatPt = D3DXVECTOR3(0.0f,0.0f,1.0f);
// Setup the view matrix
SetViewParams( &vEyePt, &vLookatPt );
// Setup the projection matrix
SetProjParams( D3DX_PI/4, 1.0f, 1.0f, 1000.0f );
GetCursorPos( &m_ptLastMousePosition );
m_bMouseLButtonDown = false;
m_bMouseMButtonDown = false;
m_bMouseRButtonDown = false;
m_nCurrentButtonMask = 0;
m_nMouseWheelDelta = 0;
m_fCameraYawAngle = 0.0f;
m_fCameraPitchAngle = 0.0f;
SetRect( &m_rcDrag, LONG_MIN, LONG_MIN, LONG_MAX, LONG_MAX );
m_vVelocity = D3DXVECTOR3(0,0,0);
m_bMovementDrag = false;
m_vVelocityDrag = D3DXVECTOR3(0,0,0);
m_fDragTimer = 0.0f;
m_fTotalDragTimeToZero = 0.25;
m_vRotVelocity = D3DXVECTOR2(0,0);
m_fRotationScaler = 0.01f;
m_fMoveScaler = 5.0f;
m_bInvertPitch = false;
m_bEnableYAxisMovement = true;
m_bEnablePositionMovement = true;
m_vMouseDelta = D3DXVECTOR2(0,0);
m_fFramesToSmoothMouseData = 2.0f;
m_bClipToBoundary = false;
m_vMinBoundary = D3DXVECTOR3(-1,-1,-1);
m_vMaxBoundary = D3DXVECTOR3(1,1,1);
}
//--------------------------------------------------------------------------------------
// Client can call this to change the position and direction of camera
//--------------------------------------------------------------------------------------
VOID CBaseCamera::SetViewParams( D3DXVECTOR3* pvEyePt, D3DXVECTOR3* pvLookatPt )
{
if( NULL == pvEyePt || NULL == pvLookatPt )
return;
m_vDefaultEye = m_vEye = *pvEyePt;
m_vDefaultLookAt = m_vLookAt = *pvLookatPt;
// Calc the view matrix
D3DXVECTOR3 vUp(0,1,0);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -