DirectX 11中的绘图顺序

时间:2014-03-05 13:28:44

标签: graph 2d directx-11 orthographic

我正在DirectX 11中使用正交投影和动态索引和顶点缓冲区编写一个小型2d引擎。深度缓冲区被禁用。要绘制图像,我使用两个三角形的四边形构建并在其上放置纹理。每件事都很有效,但绘图顺序有问题。当我在同一个地方绘制图像时,显然是第一个绘制的图像。一切都反过来了!函数ClearDepthStencilView,ClearRenderTargetView和Present在实时循环中运行一次。我为我的英语道歉。请帮忙。

创建深度缓冲区

D3D11_TEXTURE2D_DESC depthBufferDesc;
ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));

// Set up the description of the depth buffer.
// Set up the description of the depth buffer.

depthBufferDesc.Width = Width;
depthBufferDesc.Height = Height;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.Format = DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = 0;
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;


// Create the texture for the depth buffer using the filled out description.
 d3d11Device->CreateTexture2D(&depthBufferDesc, NULL, &depthStencilBuffer);


//Describe our Depth/Stencil Buffer
D3D11_DEPTH_STENCIL_DESC depthStencilDesc;

depthStencilDesc.DepthEnable = false;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
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;
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;

hr = d3d11Device->CreateDepthStencilState(&depthStencilDesc, &depthStencilState);
CheckError(hr,"CreateDepthStencilState");

hr = d3d11Device->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
CheckError(hr,"CreateDepthStencilView"); 

//Create our Render Target
hr = d3d11Device->CreateRenderTargetView( BackBuffer, NULL, &renderTargetView );
CheckError(hr,"CreateRenderTargetView");

//Set our Render Target
d3d11DevCon->OMSetRenderTargets( 1, &renderTargetView, depthStencilView );

绘制图像(使用纹理,顶点和着色器缓冲区更新)

if(inuseTexture!=0 && inuseTexture!=image->Texture)
{
Flush();
}

Vertex quad[4];

quad[0]=Vertex( posx, posy-image->texHeight,                    0.1f, 0.0f, 1.0f);
quad[1]=Vertex( posx, posy,                                     0.1f, 0.0f, 0.0f);
quad[2]=Vertex( posx + image->texWidth,posy,                    0.1f, 1.0f, 0.0f);
quad[3]=Vertex( posx + image->texWidth, posy-image->texHeight,  0.1f, 1.0f, 1.0f);

D3D11_MAPPED_SUBRESOURCE resource;
ZeroMemory(&resource,sizeof(resource));

hr = pd3d11DevCon->Map(VertBuffer, 0, MapType, 0, &resource);
CheckError(hr,"Map-VertBuffer");

char* bufferData = static_cast<char*>(resource.pData);
memcpy(bufferData+QuadCount*sizeof(quad), &quad, sizeof(quad));
pd3d11DevCon->Unmap(VertBuffer, 0);

DWORD QuadIndices[6];

QuadIndices[0] = 0+4*QuadCount;
QuadIndices[1] = 1+4*QuadCount;
QuadIndices[2] = 2+4*QuadCount;
QuadIndices[3] = 0+4*QuadCount;
QuadIndices[4] = 2+4*QuadCount;
QuadIndices[5] = 3+4*QuadCount;

ZeroMemory(&resource,sizeof(resource));

hr = pd3d11DevCon->Map(IndexBuffer, 0, MapType, 0, &resource);
CheckError(hr,"Map-IndexBuffer");

bufferData = static_cast<char*>(resource.pData);
memcpy(bufferData+QuadCount*sizeof(DWORD)*6,&QuadIndices,sizeof(DWORD)*6);
pd3d11DevCon->Unmap(IndexBuffer,0);

inuseTexture = image->Texture;
QuadCount++;
MapType=D3D11_MAP_WRITE_NO_OVERWRITE;

绘制所有内容

//Set the default blend state (no blending) for opaque objects
pd3d11DevCon->OMSetBlendState(0, 0, 0xffffffff);

//Render opaque objects//

pd3d11DevCon->RSSetState(WireFrame);
//Create the Input Layout

pd3d11DevCon->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 ); 

pd3d11DevCon->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );

pd3d11DevCon->PSSetShaderResources( 0, 1, &inuseTexture );
pd3d11DevCon->PSSetSamplers( 0, 1, &CubesTexSamplerState );

pd3d11DevCon->DrawIndexed( 6*QuadCount, 0,0);
//Present the backbuffer to the screen

MapType=D3D11_MAP_WRITE_DISCARD;
QuadCount=0;
inuseTexture=0;

0 个答案:

没有答案