ID3D11DeviceContext :: DrawIndexed()失败

时间:2014-08-17 09:43:09

标签: directx directx-11

我的程序是Directx程序,它在其中绘制一个容器立方体较小的立方体....这些较小的立方体随时间推移我希望你理解我的意思...

该程序尚未完成......它应该只绘制容器....但它什么都没画......只有背景颜色可见......我只包括我认为需要的东西...

这是初始化程序的例程

bool Game::init(HINSTANCE hinst,HWND _hw){
Directx11 ::init(hinst , _hw);
return LoadContent();}

的DirectX11 ::的init()

bool Directx11::init(HINSTANCE hinst,HWND hw){
_hinst=hinst;_hwnd=hw;
RECT rc;
GetClientRect(_hwnd,&rc);
height= rc.bottom - rc.top;
width = rc.right - rc.left;
UINT flags=0;
#ifdef _DEBUG
flags |=D3D11_CREATE_DEVICE_DEBUG;
#endif
HR(D3D11CreateDevice(0,_driverType,0,flags,0,0,D3D11_SDK_VERSION,&d3dDevice,&_featureLevel,&d3dDeviceContext));
if (d3dDevice == 0 || d3dDeviceContext == 0)
    return 0;
DXGI_SWAP_CHAIN_DESC sdesc;
ZeroMemory(&sdesc,sizeof(DXGI_SWAP_CHAIN_DESC));
sdesc.Windowed=true;
sdesc.BufferCount=1;
sdesc.BufferDesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;
sdesc.BufferDesc.Height=height;
sdesc.BufferDesc.Width=width;
sdesc.BufferDesc.Scaling=DXGI_MODE_SCALING_UNSPECIFIED;
sdesc.BufferDesc.ScanlineOrdering=DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sdesc.OutputWindow=_hwnd;
sdesc.BufferDesc.RefreshRate.Denominator=1;
sdesc.BufferDesc.RefreshRate.Numerator=60;
sdesc.Flags=0;
sdesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
if (m4xMsaaEnable)
{
    sdesc.SampleDesc.Count=4;
    sdesc.SampleDesc.Quality=m4xMsaaQuality-1;
}
else
{
    sdesc.SampleDesc.Count=1;
    sdesc.SampleDesc.Quality=0;
}
IDXGIDevice *Device=0;
HR(d3dDevice->QueryInterface(__uuidof(IDXGIDevice),reinterpret_cast <void**> (&Device)));
IDXGIAdapter*Ad=0;
HR(Device->GetParent(__uuidof(IDXGIAdapter),reinterpret_cast <void**> (&Ad)));
IDXGIFactory* fac=0;
HR(Ad->GetParent(__uuidof(IDXGIFactory),reinterpret_cast <void**> (&fac)));
fac->CreateSwapChain(d3dDevice,&sdesc,&swapchain);
ReleaseCOM(Device);
ReleaseCOM(Ad);
ReleaseCOM(fac);
ID3D11Texture2D *back = 0;
HR(swapchain->GetBuffer(0,__uuidof(ID3D11Texture2D),reinterpret_cast <void**> (&back)));
HR(d3dDevice->CreateRenderTargetView(back,0,&RenderTarget));
D3D11_TEXTURE2D_DESC Tdesc;
ZeroMemory(&Tdesc,sizeof(D3D11_TEXTURE2D_DESC));
Tdesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
Tdesc.ArraySize = 1;
Tdesc.Format= DXGI_FORMAT_D24_UNORM_S8_UINT;
Tdesc.Height= height;
Tdesc.Width = width;
Tdesc.Usage = D3D11_USAGE_DEFAULT;
Tdesc.MipLevels=1;
if (m4xMsaaEnable)
{
    Tdesc.SampleDesc.Count=4;
    Tdesc.SampleDesc.Quality=m4xMsaaQuality-1;
}
else
{
    Tdesc.SampleDesc.Count=1;
    Tdesc.SampleDesc.Quality=0;
}
HR(d3dDevice->CreateTexture2D(&Tdesc,0,&depthview));
HR(d3dDevice->CreateDepthStencilView(depthview,0,&depth));
d3dDeviceContext->OMSetRenderTargets(1,&RenderTarget,depth);
D3D11_VIEWPORT vp;
vp.TopLeftX=0.0f;
vp.TopLeftY=0.0f;
vp.Width = static_cast <float> (width);
vp.Height= static_cast <float> (height);
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
d3dDeviceContext -> RSSetViewports(1,&vp);
return true;

SetBuild()为较小的多维数据集准备容器内的矩阵....我没有编程来绘制较小的多维数据集

这是绘制场景的功能

void Game::Render(){
d3dDeviceContext->ClearRenderTargetView(RenderTarget,reinterpret_cast <const float*> (&Colors::LightSteelBlue));
d3dDeviceContext->ClearDepthStencilView(depth,D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,1.0f,0);

d3dDeviceContext-> IASetInputLayout(_layout);
d3dDeviceContext-> IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
d3dDeviceContext->IASetIndexBuffer(indices,DXGI_FORMAT_R32_UINT,0);
UINT strides=sizeof(Vertex),off=0;
d3dDeviceContext->IASetVertexBuffers(0,1,&vertices,&strides,&off);

D3DX11_TECHNIQUE_DESC des;
Tech->GetDesc(&des);
Floor * Lookup;                 /*is a variable to Lookup inside the matrices structure (Floor Contains      XMMATRX Piese[9])*/
std::vector<XMFLOAT4X4> filled; // saves the matrices of the smaller cubes
XMMATRIX V=XMLoadFloat4x4(&View),P = XMLoadFloat4x4(&Proj);
XMMATRIX vp = V * P;XMMATRIX wvp;
for (UINT i = 0; i < des.Passes; i++)
{
    d3dDeviceContext->RSSetState(BuildRast);
    wvp = XMLoadFloat4x4(&(B.Memory[0].Pieces[0])) * vp; // Loading The Matrix at translation(0,0,0) 

    HR(ShadeMat->SetMatrix(reinterpret_cast<float*> ( &wvp)));
    HR(Tech->GetPassByIndex(i)->Apply(0,d3dDeviceContext));
    d3dDeviceContext->DrawIndexed(build_ind_count,build_ind_index,build_vers_index);

    d3dDeviceContext->RSSetState(PieseRast);
    UINT r1=B.GetSize(),r2=filled.size();
    for (UINT j = 0; j < r1; j++)
    {
        Lookup = &B.Memory[j];
        for (UINT r = 0; r < Lookup->filledindeces.size(); r++)
        {
            filled.push_back(Lookup->Pieces[Lookup->filledindeces[r]]);
        }
    }
    for (UINT j = 0; j < r2; j++)
    {
        ShadeMat->SetMatrix( reinterpret_cast<const float*> (&filled[i]));
        Tech->GetPassByIndex(i)->Apply(0,d3dDeviceContext);
        d3dDeviceContext->DrawIndexed(piese_ind_count,piese_ind_index,piese_vers_index);
    }
}
HR(swapchain->Present(0,0));}

先谢谢

1 个答案:

答案 0 :(得分:1)

您的计划中的一个错误似乎是您正在使用i(当前通行证的索引)作为filled向量的索引,当您显然应该使用{ {1}}。

另一个明显的错误是,在你应该迭代j元素的循环中,你不会迭代所有元素。在您通过期间向其附加任何内容之前,值filled将设置为r2的大小。在第一次传递期间,这意味着此循环不会绘制任何内容。如果您的技术只有一个传递,那么这意味着您的代码中的第二个filled调用永远不会被执行。

看起来你应该只向DrawIndexed添加一次矩阵,无论该技术有多少次传递。你应该考虑你的代码是否真的适用于多次通过的技术。