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

📄 ch24p3_imgmanip.cpp

📁 游戏开发特殊技巧-special.effects.game.programming
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  
  // render onto the off-screen texture
  LPDIRECT3DSURFACE8 pDepthSurf;
  m_pd3dDevice->GetDepthStencilSurface(&pDepthSurf);

  LPDIRECT3DSURFACE8 pOldRenderTarget;
  m_pd3dDevice->GetRenderTarget(&pOldRenderTarget);

  LPDIRECT3DSURFACE8 psurfRender;
  m_texRender->GetSurfaceLevel(0, &psurfRender);

  // set new rendering target & clear
  m_pd3dDevice->SetRenderTarget(psurfRender, m_surfRenderDepth);
  
  HRESULT hr;
  if (FAILED(hr = RenderSceneOntoTexture(m_Camera.GetViewMatrix()))) {
    return(hr);
  }

  m_pd3dDevice->SetRenderTarget(pOldRenderTarget, pDepthSurf);
  m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
  
  psurfRender->Release();
  pOldRenderTarget->Release();
  pDepthSurf->Release();
  
  // Clear the backbuffer
  m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                       0x000000, 1.0f, 0L );
  
  
  if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  {
    D3DXMATRIX projmat;
    D3DXMatrixOrthoLH(&projmat, 1000.0f, 1000.0f, 0.0, 100.0);
    m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &projmat);

    D3DXMATRIX viewmat, worldmat, transmat, scalemat;

    D3DXMatrixIdentity(&viewmat);
    m_pd3dDevice->SetTransform(D3DTS_VIEW, &viewmat);

    D3DXMatrixScaling(&scalemat, 400.0f, 400.0f, 1.0f);
    D3DXMatrixTranslation(&transmat, -250.0f, 0.0f, 0.0f);
    
    worldmat = scalemat * transmat;
    m_pd3dDevice->SetTransform(D3DTS_WORLD, &worldmat);
    
    VERTEX_IMGMANIP pVertices[6];

    // first triangle
    pVertices[0].color = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
    pVertices[0].position = D3DXVECTOR3(-0.5f, 0.5f, 0.0f);
    pVertices[0].tu0 = 0.0f; pVertices[0].tv0 = 0.0f;

    pVertices[1].color = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
    pVertices[1].position = D3DXVECTOR3(0.5f, 0.5f, 0.0f);
    pVertices[1].tu0 = 1.0f; pVertices[1].tv0 = 0.0f;

    pVertices[2].color = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
    pVertices[2].position = D3DXVECTOR3(0.5f, -0.5f, 0.0f);
    pVertices[2].tu0 = 1.0f; pVertices[2].tv0 = 1.0f;

    // second triangle
    pVertices[3].color = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
    pVertices[3].position = D3DXVECTOR3(-0.5f, 0.5f, 0.0f);
    pVertices[3].tu0 = 0.0f; pVertices[3].tv0 = 0.0f;

    pVertices[4].color = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
    pVertices[4].position = D3DXVECTOR3(0.5f, -0.5f, 0.0f);
    pVertices[4].tu0 = 1.0f; pVertices[4].tv0 = 1.0f;

    pVertices[5].color = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
    pVertices[5].position = D3DXVECTOR3(-0.5f, -0.5f, 0.0f);
    pVertices[5].tu0 = 0.0f; pVertices[5].tv0 = 1.0f;

    m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
    m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
    
    m_pd3dDevice->SetTexture(0, m_texRender);
    m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
	  m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1);
    m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);

	  m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP,   D3DTOP_DISABLE); 
	  
    

    // set normal vertex shader
    m_pd3dDevice->SetVertexShader(D3DFVF_IMGMANIP);

    // set normal pixel shader

    m_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, pVertices, 
      sizeof(VERTEX_IMGMANIP));
    
    D3DXMatrixScaling(&scalemat, 400.0f, 400.0f, 1.0f);
    D3DXMatrixTranslation(&transmat, 250.0f, 0.0f, 0.0f);
    
    worldmat = scalemat * transmat;
    m_pd3dDevice->SetTransform(D3DTS_WORLD, &worldmat);
    
    m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);

    // set effect pixel shader
    m_pd3dDevice->SetPixelShader(m_PixelShaders[m_iCurPixelShader].m_dwID);
    m_pd3dDevice->SetTexture(0, m_texRender);
    m_pd3dDevice->SetTexture(1, m_texRender);
    m_pd3dDevice->SetTexture(2, m_texRender);
    m_pd3dDevice->SetTexture(3, m_texRender);

    m_pd3dDevice->SetVertexShader(m_dwImageVertexShader);
    // Set up the vertex shader constants
    {
      D3DXMATRIX matWorldViewProj;
    
      matWorldViewProj = worldmat * projmat;
    
      D3DXMatrixTranspose(&matWorldViewProj, &matWorldViewProj);

      float fPerTexelOffset = 0.5 / m_iTextureSize;

      D3DXVECTOR4 vConstants0(-fPerTexelOffset, -fPerTexelOffset, 0.0, 0.0);
      D3DXVECTOR4 vConstants1(-fPerTexelOffset,  fPerTexelOffset, 0.0, 0.0);
      D3DXVECTOR4 vConstants2( fPerTexelOffset, -fPerTexelOffset, 0.0, 0.0);
      D3DXVECTOR4 vConstants3( fPerTexelOffset,  fPerTexelOffset, 0.0, 0.0);
      
	    m_pd3dDevice->SetVertexShaderConstant(0, &matWorldViewProj, 4);
      m_pd3dDevice->SetVertexShaderConstant(5, &vConstants0, 1);
      m_pd3dDevice->SetVertexShaderConstant(6, &vConstants1, 1);
      m_pd3dDevice->SetVertexShaderConstant(7, &vConstants2, 1);
      m_pd3dDevice->SetVertexShaderConstant(8, &vConstants3, 1);
    }

    m_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, pVertices, 
      sizeof(VERTEX_IMGMANIP));

    m_pd3dDevice->SetPixelShader(NULL);
    
    
    char buf[256];
    _snprintf(buf, 256, "Time: %.5f", m_fTotalTime);
    m_pFontSmall->DrawText( 2,  0,  D3DCOLOR_ARGB(255,255,255,0), buf );

    _snprintf(buf, 256, "On The Left:  Normal Image");
    m_pFontSmall->DrawText( 2,  15,  D3DCOLOR_ARGB(255,0,255,0), buf );
    
    _snprintf(buf, 256, "On The Right: %s ('P' to change)", m_PixelShaders[m_iCurPixelShader].m_strName.c_str());
    m_pFontSmall->DrawText( 2,  30,  D3DCOLOR_ARGB(255,0,255,0), buf );

    // 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 );
	
  return S_OK;
}


//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
  HRESULT hr;
	m_InputManager.CreateDevices(m_hWnd, true, true);

  m_fTotalTime = 0.0f;
  m_pTeapot = new CD3DMesh();

  if( FAILED( m_pTeapot->Create( m_pd3dDevice, _T("Ch24p3_Teapot.x") ) ) )
    return D3DAPPERR_MEDIANOTFOUND;

  m_pTeapot->RestoreDeviceObjects( m_pd3dDevice );
  m_pTeapot->SetFVF(m_pd3dDevice, D3DFVF_TEAPOT);

  if (FAILED(hr = D3DXCreateTexture(m_pd3dDevice, m_iTextureSize, m_iTextureSize, 
      1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, 
      &m_texRender))) throw(hr);

  // create z buffer for off-screen rendering
  if( FAILED( hr = m_pd3dDevice->CreateDepthStencilSurface(
            m_iTextureSize, m_iTextureSize, 
            D3DFMT_D16, D3DMULTISAMPLE_NONE, &m_surfRenderDepth) ) )
    return(hr);


  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
  
  // Create teapot vertex shader
  DWORD dwTeapotDecl[] =
  {
    D3DVSD_STREAM(0),
    D3DVSD_REG(0, D3DVSDT_FLOAT3),   // position
    D3DVSD_REG(1, D3DVSDT_FLOAT3),   // normal
    D3DVSD_REG(2, D3DVSDT_D3DCOLOR), // diffuse color
    D3DVSD_REG(3, D3DVSDT_FLOAT2),   // tex coords
    D3DVSD_END()
  };
  CreateShader(m_pd3dDevice, "Ch24p3_Teapot.vs", &dwTeapotDecl[0], m_dwTeapotVertexShader);
  
  DWORD dwImageDecl[] =
  {
    D3DVSD_STREAM(0),
    D3DVSD_REG(0, D3DVSDT_FLOAT3),   // position
    D3DVSD_REG(2, D3DVSDT_D3DCOLOR), // diffuse color
    D3DVSD_REG(3, D3DVSDT_FLOAT2),   // tex coords 0
    D3DVSD_REG(4, D3DVSDT_FLOAT2),   // tex coords 1
    D3DVSD_REG(5, D3DVSDT_FLOAT2),   // tex coords 2
    D3DVSD_REG(6, D3DVSDT_FLOAT2),   // tex coords 3
    D3DVSD_END()
  };

  CreateShader(m_pd3dDevice, "Ch24p3_Image.vs", &dwImageDecl[0], m_dwImageVertexShader);

  m_PixelShaders.push_back(CPixelShaderEntry(m_pd3dDevice, "Normal", "Ch24p3_Normal.ps"));
  m_PixelShaders.push_back(CPixelShaderEntry(m_pd3dDevice, "Black And White", "Ch24p3_BlackAndWhite.ps"));
  m_PixelShaders.push_back(CPixelShaderEntry(m_pd3dDevice, "Better Black And White", "Ch24p3_BetterBlackAndWhite.ps"));
  m_PixelShaders.push_back(CPixelShaderEntry(m_pd3dDevice, "Funky Monochrome", "Ch24p3_Monochrome.ps"));
  m_PixelShaders.push_back(CPixelShaderEntry(m_pd3dDevice, "Blur", "Ch24p3_Blur.ps"));
  m_PixelShaders.push_back(CPixelShaderEntry(m_pd3dDevice, "Sharpen", "Ch24p3_Sharpen.ps"));
  m_PixelShaders.push_back(CPixelShaderEntry(m_pd3dDevice, "Inverse", "Ch24p3_Inverse.ps"));

  m_pFont->RestoreDeviceObjects();
  m_pFontSmall->RestoreDeviceObjects();
  m_Camera.SetPosition(D3DXVECTOR3(0.0f, 0.0f, -5.0f));
	
  m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_ONE );
  m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
  m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,  FALSE );
  m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,  D3DCULL_CCW );
  m_pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD );
  
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
  m_pd3dDevice->DeleteVertexShader(m_dwTeapotVertexShader);
  m_pd3dDevice->DeleteVertexShader(m_dwImageVertexShader);
  SAFE_RELEASE(m_texRender);
  SAFE_RELEASE(m_surfRenderDepth);

  // yeah, this isn't good OOD... ideally this deletion would occur in the
  // destructor, 
  for (int q=0; q < m_PixelShaders.size(); q++) {
    m_pd3dDevice->DeletePixelShader(m_PixelShaders[q].m_dwID);
  }
  m_PixelShaders.clear();
  
  m_pTeapot->InvalidateDeviceObjects();
  SAFE_DELETE(m_pTeapot);

  m_InputManager.DestroyDevices();
  m_pFont->InvalidateDeviceObjects();
  m_pFontSmall->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_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 )
{
  if (uMsg == WM_KEYUP && (wParam == 'p' || wParam == 'P')) {
    m_iCurPixelShader++;
    if (m_iCurPixelShader >= m_PixelShaders.size()) m_iCurPixelShader = 0;
  }
  // Pass remaining messages to default handler
  return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}

⌨️ 快捷键说明

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