2011-05-17 66 views
0

如何將一個Direct3D 11 2D紋理的任意多邊形區域複製到另一個紋理?我試過使用ID3D11DeviceContext :: CopySubresourceRegion方法,但它只複製矩形部分。我使用Visual C++。將一部分2D紋理複製到另一個Direct3D11

pKeyedMutex11->AcquireSync(1, INFINITE); 
pImmediateContext->CopySubresourceRegion(pBackBuffer11, 0, max(size.width-turned,0.0f), 0, 0, pSharedTexture11, 0, &sourceRegion);    
pKeyedMutex11->ReleaseSync(0); 

pKeyedMutex11_2->AcquireSync(1, INFINITE); 
pImmediateContext->CopySubresourceRegion(pBackBuffer11, 0, max(size_1.width - 2*turned,0.0f) , 0, 0, pSharedTexture11_2, 0, &sourceRegion_2);   
pKeyedMutex11_2->ReleaseSync(0); 

pKeyedMutex11_1->AcquireSync(1, INFINITE); 
     // Copy the content from the shared texture to the back-buffer   
pImmediateContext->CopySubresourceRegion(pBackBuffer11, 0, 0, 0, 0, pSharedTexture11_1, 0, &sourceRegion_1); 
pKeyedMutex11_1->ReleaseSync(0); 

編輯:添加了代碼片段。

回答

1

我不知道是否存在複製多邊形區域的直接方法。但是你可以使用一個 模板緩衝區。將任意多邊形繪製到模板緩衝區。並通過應用模板將整個紋理複製到另一個紋理。你會得到同樣的效果。

編輯::引用添加

你可以看看這個教程。 http://www.rastertek.com/dx11tut03.html

現在我們需要設置深度模板描述。這使我們能夠控制Direct3D將針對每個像素進行哪種類型的深度測試。

// Initialize the description of the stencil state. 
ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc)); 

// Set up the description of the stencil state. 
depthStencilDesc.DepthEnable = true; 
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; 
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; 

depthStencilDesc.StencilEnable = true; 
depthStencilDesc.StencilReadMask = 0xFF; 
depthStencilDesc.StencilWriteMask = 0xFF; 

// Stencil operations if pixel is front-facing. 
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; 
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; 
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; 
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; 

// Stencil operations if pixel is back-facing. 
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; 
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; 
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; 
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; 

隨着描述的填寫,我們現在可以創建一個深度模板狀態。

// Create the depth stencil state. 
result = m_device->CreateDepthStencilState(&depthStencilDesc, &m_depthStencilState); 
if(FAILED(result)) 
{ 
    return false; 
} 

使用創建的深度模板狀態,我們現在可以設置它使其生效。注意我們使用設備上下文來設置它。

// Set the depth stencil state. 
m_deviceContext->OMSetDepthStencilState(m_depthStencilState, 1); 

我們需要創建的下一件事是深度模板緩衝區視圖的描述。我們這樣做是爲了讓Direct3D知道使用深度緩衝區作爲深度模板紋理。填寫完描述後,我們再調用函數CreateDepthStencilView來創建它。

// Initailze the depth stencil view. 
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc)); 

// Set up the depth stencil view description. 
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; 
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; 
depthStencilViewDesc.Texture2D.MipSlice = 0; 

// Create the depth stencil view. 
result = m_device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView); 
if(FAILED(result)) 
{ 
    return false; 
} 

與創建我們就可以調用OMSetRenderTargets。這將把渲染目標視圖和深度模板緩衝區綁定到輸出渲染管道。這樣管道渲染的圖形將被繪製到我們以前創建的後臺緩衝區。通過將圖形寫入後臺緩衝區,我們可以將其交換到前端,並在用戶的屏幕上顯示我們的圖形。

// Bind the render target view and depth stencil buffer to the output render pipeline. 
m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView); 
+0

我想它非常有用。你可以引用一些資源來學習使用Stencil Buffers或者提供一些代碼示例,因爲我是DirectX編程的新手。 – 2011-05-23 06:47:10

+0

我假設你使用着色器來渲染圖像。我對嗎? – 2011-05-23 06:53:19

+0

其實我正在做2D動畫。所以基本上我有兩個紋理,我需要複製第一個紋理上的第二個紋理的一部分(如上面所述的多邊形)並改變多邊形。我沒有使用任何着色器。 – 2011-05-23 06:57:37

相關問題