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

📄 ch17p1_alphablendfade.cpp

📁 介绍在Alpha混合过程中实现渐进退色效果的程序及其源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    
      m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
      m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
      m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );


      m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );

      m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
    }



    // 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 );
    m_pFont->DrawText( 2, 60, D3DCOLOR_ARGB(255,  255,   0, 0), "R = Fade to Red");
    m_pFont->DrawText( 2, 80, D3DCOLOR_ARGB(255,  255,   0, 0), "G = Fade to Green");
    m_pFont->DrawText( 2, 100, D3DCOLOR_ARGB(255, 255,   0, 0), "B = Fade to Blue");
    m_pFont->DrawText( 2, 120, D3DCOLOR_ARGB(255, 255,   0, 0), "W = Fade to White");
    m_pFont->DrawText( 2, 140, D3DCOLOR_ARGB(255, 255,   0, 0), "A = Fade to Black");

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

  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
  m_pFont->InitDeviceObjects( m_pd3dDevice );
  m_pFontSmall->InitDeviceObjects( m_pd3dDevice );

  D3DXVECTOR3 vEye = D3DXVECTOR3( 0.0f, -0.0f, -1.0f );
  D3DXVECTOR3 vAt  = D3DXVECTOR3( 0.0f, -0.0f, 0.0f );
  D3DXVECTOR3 vUp  = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  D3DXMatrixLookAtLH( &m_matView, &vEye, &vAt, &vUp );
  m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_matView );

  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
  
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
  HRESULT hr;

  m_pFont->RestoreDeviceObjects();
  m_pFontSmall->RestoreDeviceObjects();

  // Setup render states
  m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
  m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

  // Create vertex buffer
  {
    CUSTOMVERTEX* pVertices;

    if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( 6*sizeof(CUSTOMVERTEX),
                                                       0, D3DFVF_CUSTOMVERTEX,
                                                       D3DPOOL_MANAGED, &m_pVB ) ) )
      return hr;

    if( FAILED( hr = m_pVB->Lock( 0, m_dwNumVertices*sizeof(CUSTOMVERTEX), (BYTE**)&pVertices, 0 ) ) )
      return hr;

    // first triangle
    pVertices[0].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
    pVertices[0].color    = D3DCOLOR_ARGB(200,0,0,0);
    pVertices[0].tu       = 0.0f;
    pVertices[0].tv       = 0.0f;

    pVertices[1].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);
    pVertices[1].color    = D3DCOLOR_ARGB(200,0,0,0);
    pVertices[1].tu       = 1.0f;
    pVertices[1].tv       = 0.0f;

    pVertices[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
    pVertices[2].color    = D3DCOLOR_ARGB(200,0,0,0);
    pVertices[2].tu       = 1.0f;
    pVertices[2].tv       = 1.0f;

    // second triangle
    pVertices[3].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
    pVertices[3].color    = D3DCOLOR_ARGB(200,0,0,0);
    pVertices[3].tu       = 0.0f;
    pVertices[3].tv       = 0.0f;

    pVertices[4].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
    pVertices[4].color    = D3DCOLOR_ARGB(200,0,0,0);
    pVertices[4].tu       = 1.0f;
    pVertices[4].tv       = 1.0f;

    pVertices[5].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
    pVertices[5].color    = D3DCOLOR_ARGB(200,0,0,0);
    pVertices[5].tu       = 0.0f;
    pVertices[5].tv       = 1.0f;

    if( FAILED( hr = m_pVB->Unlock() ) )
      return hr;
  }

  // create original image texture.  This must be created as a render target.
  if (FAILED(hr = D3DXCreateTextureFromFileEx(m_pd3dDevice, "Ch17p1_Island.png", 
    0, 0, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
    0, NULL, NULL, &m_pOrigTexture))) {
    return(hr);
  }
 
  m_fFadeTime = 0.0f;

  D3DSURFACE_DESC desc;
  m_pOrigTexture->GetLevelDesc(0, &desc);
  
  m_iTextureWidth = desc.Width;
  m_iTextureHeight = desc.Height;

  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
  m_pFont->InvalidateDeviceObjects();
  m_pFontSmall->InvalidateDeviceObjects();
  SAFE_RELEASE( m_pVB );
  SAFE_RELEASE( m_pOrigTexture );
  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_pFontSmall->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()
{
  SAFE_DELETE( m_pFont );
  SAFE_DELETE( m_pFontSmall );
  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 )
{
  switch(uMsg) {
    case WM_KEYDOWN:
    {
      switch(toupper(wParam)) {
        case 'R':
          m_fFadeTime = 0.0001f;
          m_FadeToColor = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
          break;
        
        case 'G':
          m_fFadeTime = 0.0001f;
          m_FadeToColor = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);
          break;

        case 'B':
          m_fFadeTime = 0.0001f;
          m_FadeToColor = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);
          break;

        case 'W':
          m_fFadeTime = 0.0001f;
          m_FadeToColor = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
          break;

        case 'A':
          m_fFadeTime = 0.0001f;
          m_FadeToColor = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
          break;
      }
    }
    break;
  }
  // Pass remaining messages to default handler
  return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}

⌨️ 快捷键说明

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