⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameengine.cpp

📁 BSP地形系统和光照贴图的技术详解
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//       the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
// Update user input state
    UpdateInput( &m_UserInput );

	m_Camera.RotateLocalY( m_UserInput.dxMouse );
	m_Camera.RotateLocalX( m_UserInput.dyMouse );

	D3DXVECTOR3 vOrigEye = *(m_Camera.GetEye());
	D3DXVECTOR3 vOrigLookat = *(m_Camera.GetLookat());
	D3DXVECTOR3 vOrigUp = *(m_Camera.GetUp());

	if( m_UserInput.bW )
		m_Camera.MoveLocalZ(  m_fElapsedTime * DEF_SPEED  );
	if( m_UserInput.bS )	
		m_Camera.MoveLocalZ(  -m_fElapsedTime * DEF_SPEED  );
	if( m_UserInput.bD )
		m_Camera.MoveLocalX(  m_fElapsedTime * DEF_SPEED  );
	if( m_UserInput.bA )
		m_Camera.MoveLocalX(  -m_fElapsedTime * DEF_SPEED  );

    // Update the world state according to user input
    D3DXMATRIX matWorld;
    D3DXMATRIX matRotY;
    D3DXMATRIX matRotX;

    if( m_UserInput.fAxisRotateLR )
        m_fWorldRotY += m_fElapsedTime * m_UserInput.fAxisRotateLR;

    if( m_UserInput.fAxisRotateUD )
        m_fWorldRotX += m_fElapsedTime * m_UserInput.fAxisRotateUD;

   

	m_bCollision = FALSE;
	if( m_bspManager.IsCollision( *m_Camera.GetEye() ) )
		m_bCollision = TRUE;
	if( m_bCollision )
		m_Camera.SetView( &vOrigEye, &vOrigLookat, &vOrigUp );
 
	D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
    D3DXMatrixRotationY( &matRotY, m_fWorldRotY );

    D3DXMatrixMultiply( &matWorld, &matRotX, &matRotY );
    m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

	m_pd3dDevice->SetTransform( D3DTS_VIEW, m_Camera.GetViewMatrix() );
/*
	D3DXMATRIX		m;
	D3DXMATRIX *pW, *pV, *pP;
	m_Camera.GetMatrices( &pW, &pV, &pP );
	
///	if( m_bViewLock )
	{
//		m = m_matViewLocked * *pP;
	}
//	else
	{
		m = (*pV) * (*pP);
//		m_terrainUtil.SetCamera( v.x, v.y, v.z );
	}
	m_bspManager.m_frustum.Make( &m );
*/
    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: UpdateInput()
// Desc: Update the user input.  Called once per frame 
//-----------------------------------------------------------------------------
void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
{
	static dwFill = 0, dwPortal =0, dwLight = 0;
    pUserInput->bRButtonDown = ( m_bHasFocus && (GetAsyncKeyState( VK_RBUTTON ) & 0x8000) == 0x8000 );
	pUserInput->bS = ( m_bHasFocus && (GetAsyncKeyState( 'S' ) & 0x8000 ) == 0x8000 );
	pUserInput->bW = ( m_bHasFocus && (GetAsyncKeyState( 'W' ) & 0x8000 ) == 0x8000 );
	pUserInput->bA = ( m_bHasFocus && (GetAsyncKeyState( 'A' ) & 0x8000 ) == 0x8000 );
	pUserInput->bD = ( m_bHasFocus && (GetAsyncKeyState( 'D' ) & 0x8000 ) == 0x8000 );
	if( ( m_bHasFocus && (GetAsyncKeyState( 'P' ) & 0x8000 ) == 0x8000 ) &&
		GetTickCount() - dwPortal > 300 )
	{
		pUserInput->bP = !pUserInput->bP;
		dwPortal = GetTickCount();
	}

	if( ( m_bHasFocus && (GetAsyncKeyState( 'L' ) & 0x8000 ) == 0x8000 ) &&
		GetTickCount() - dwLight > 300 )
	{
		pUserInput->bL = !pUserInput->bL;
		dwLight = GetTickCount();
	}

	if( ( m_bHasFocus && (GetAsyncKeyState( '1' ) & 0x8000 ) == 0x8000 ) &&
		GetTickCount() - dwFill > 300 )
	{
		pUserInput->b1 = !pUserInput->b1;
		dwFill = GetTickCount();
	}
	
	POINT pt;
	GetCursorPos( &pt );

	if(	pUserInput->bRButtonDown )
	{
		pUserInput->dxMouse = ( pt.x - pUserInput->ptMouse.x ) * m_fElapsedTime * 0.2f;
		pUserInput->dyMouse = ( pt.y - pUserInput->ptMouse.y ) * m_fElapsedTime * 0.2f;
		SetCursorPos( pUserInput->ptMouse.x, pUserInput->ptMouse.y );
	}
	else
		GetCursorPos( &pUserInput->ptMouse );
}




//-----------------------------------------------------------------------------
// 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
    m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         0xffffffff, 1.0f, 0L );

    // Begin the scene
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
    {
        // TODO: render world
        
        // Render stats and help text 
//		
		
//		else
//		m_bspManager.Render( m_pd3dDevice, NULL );
//		m_bspManager.RenderByPortals( m_pd3dDevice );
		if( m_UserInput.bL )
		{
			m_bspManager.RenderLightMap( m_pd3dDevice, m_UserInput.b1, NULL );
//			m_bspManager.RenderLightMap( m_pd3dDevice, m_pFont );
		}
		else
			m_bspManager.RenderPVS( m_pd3dDevice, m_UserInput.b1, m_pFont );

		D3DXVECTOR3 v = m_Camera.m_vLookat - m_Camera.m_vEye;
		m_bspManager.RenderThings( m_pd3dDevice, m_Camera.m_vEye );
		
		if( m_UserInput.bP )
		{
			m_bspManager.RenderPortal( m_pd3dDevice, NULL );
//			m_bspManager.RenderPortal( m_pd3dDevice, m_pFont );
		}
		
		
//		m_bspManager.Render( m_pd3dDevice, m_pFont );
		RenderText();

        // End the scene.
        m_pd3dDevice->EndScene();
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: RenderText()
// Desc: Renders stats and help text to the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RenderText()
{

    D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
	if( !m_UserInput.b1 )
		fontColor = D3DCOLOR_ARGB(255, 0, 0, 255);
    D3DCOLOR fontWarningColor = D3DCOLOR_ARGB(255,0,255,255);
    TCHAR szMsg[MAX_PATH] = TEXT("");

    // Output display stats
    FLOAT fNextLine = 2.f; 

    lstrcpy( szMsg, m_strFrameStats );
    m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

//    lstrcpy( szMsg, m_strDeviceStats );
//   fNextLine += 20.0f;
//   m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

	TCHAR* szMove = _T("FORWARD, BACK, LEFT, RIGHT : W, S, A, D ");
	fNextLine += 20.0f;
	m_pFont->DrawText( 2, fNextLine, fontColor, szMove );

	TCHAR* szLook = _T("LOOK : RBUTTONDOWN & MOUSE MOVE ");
	fNextLine += 20.0f;
	m_pFont->DrawText( 2, fNextLine, fontColor, szLook );


    // Output statistics & help
    fNextLine = (FLOAT) m_d3dsdBackBuffer.Height; 
/*
    sprintf( szMsg, TEXT("POS : %0.3f, %0.3f, %0.3f"), 
                    m_Camera.GetEye()->x, m_Camera.GetEye()->y, m_Camera.GetEye()->z );
    fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

    lstrcpy( szMsg, TEXT("Use arrow keys to update input") );
    fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

    lstrcpy( szMsg, TEXT("Hold 'F5' down to play and repeat a sound") );
    fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

    lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
    fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
*/
	TCHAR* szFill	= ( m_UserInput.b1 ) ? _T("Fill") : _T("Wire");
	TCHAR* szLight	= ( m_UserInput.bL ) ? _T("Render") : _T("");
	TCHAR* szPortal	= ( m_UserInput.bP ) ? _T("Render") : _T("");

	wsprintf( szMsg, _T("FILLMODE[ 1 ] : [ %s ]"), szFill);
	fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
	
	wsprintf( szMsg, _T("PORTAL  [ P ] : [ %s ]"), szPortal);
    fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

	wsprintf( szMsg, _T("LIGHTMAP[ L ] : [ %s ]"), szLight);
    fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

	DWORD dwHeight, dwWidth;
	if( m_bCollision )
	{
		
		dwHeight = m_pFont->GetTexHeight();
		dwWidth = m_pFont->GetTexWidth();
		m_pFont->SetTexHeight( dwHeight + 400 );
		m_pFont->SetTexWidth( dwWidth + 400 );

		char str[15] = "collision!";
		m_pFont->DrawText( 350, 280, 0xffff0000, str );
		
		m_pFont->SetTexHeight( dwHeight );
		m_pFont->SetTexWidth( dwWidth );
	}

	if( m_UserInput.bP )
	{
		dwHeight = m_pFont->GetTexHeight();
		dwWidth = m_pFont->GetTexWidth();
		m_pFont->SetTexHeight( dwHeight + 200 );
		m_pFont->SetTexWidth( dwWidth + 200 );
		m_pFont->DrawText( 500, 20, fontWarningColor, _TEXT("Rendering Portal"));
		m_pFont->SetTexHeight( dwHeight );
		m_pFont->SetTexWidth( dwWidth );
	}

	if( m_UserInput.bL )
	{
		dwHeight = m_pFont->GetTexHeight();
		dwWidth = m_pFont->GetTexWidth();
		m_pFont->SetTexHeight( dwHeight + 200 );
		m_pFont->SetTexWidth( dwWidth + 200 );
		m_pFont->DrawText( 500, 40, fontWarningColor, _TEXT("Rendering LightMap"));
		m_pFont->SetTexHeight( dwHeight );
		m_pFont->SetTexWidth( dwWidth );
	}

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Overrrides the main WndProc, so the sample can do custom message
//       handling (e.g. processing mouse, keyboard, or menu commands).
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg, WPARAM wParam,
                                    LPARAM lParam )
{
    switch( msg )
    {
        case WM_PAINT:
        {
            if( m_bLoadingApp )
            {
                // Draw on the window tell the user that the app is loading
                // TODO: change as needed
                HDC hDC = GetDC( hWnd );
				wsprintf( g_strMsg, TEXT("Loading... Please wait") );
                RECT rct;
                GetClientRect( hWnd, &rct );
                DrawText( hDC, g_strMsg, -1, &rct, DT_CENTER|DT_VCENTER|DT_SINGLELINE );
                ReleaseDC( hWnd, hDC );
            }
            break;
        }

    }

    return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc: Invalidates device objects.  
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
    // TODO: Cleanup any objects created in RestoreDeviceObjects()
    m_pFont->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()
{
    // TODO: Cleanup any objects created in InitDeviceObjects()
    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()
{
    // TODO: Perform any final cleanup needed
    // Cleanup D3D font
    SAFE_DELETE( m_pFont );

    // Cleanup DirectInput
    CleanupDirectInput();

    // Cleanup DirectX audio objects
    SAFE_DELETE( m_pBounceSound );
    SAFE_DELETE( m_pMusicManager );

    // Write the settings to the registry
    WriteSettings();

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: CleanupDirectInput()
// Desc: Cleanup DirectInput 
//-----------------------------------------------------------------------------
VOID CMyD3DApplication::CleanupDirectInput()
{
    // Cleanup DirectX input objects
    SAFE_RELEASE( m_pKeyboard );
    SAFE_RELEASE( m_pDI );

}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -