Using PrimitiveBatch::DrawIndexed with ID3D11DeviceContext::DrawIndexed?

0

I'm creating a game engine and I'm at the point where I would want to draw wireframe shapes for debugging (colliders, triggers, raycasts, etc.). I was previously using meshes with a wireframe rasterizer state, but now I want to migrate the system over to DirectXTK's example using their PrimitiveBatch class so I can draw rays.

However, I am running into a problem when I call the batch's DrawIndexed() method, where everything I draw that frame, including objects from an ID3D11DeviceContext's DrawIndexed(), becomes wireframe.

Here's what the scene is supposed to look like.

Here's what it looks like after activating the debug drawing.

My debug drawing method:

void Renderer::DrawDebugShapes(ID3D11DeviceContext* context, Camera* camera, float deltaTime)
{
    if (debugObjs[0].size() < 1 && debugObjs[1].size() < 1 && debugObjs[2].size() < 1
        && debugObjs[3].size() < 1)
        return;

    db_effect->SetView(XMLoadFloat4x4(&camera->GetViewMatrix()));
    db_effect->SetProjection(XMLoadFloat4x4(&camera->GetProjectionMatrix()));

    context->OMSetBlendState(db_states->Opaque(), nullptr, 0xFFFFFFFF);
    context->OMSetDepthStencilState(db_states->DepthNone(), 0);
    context->RSSetState(db_states->CullNone());

    db_effect->Apply(context);

    context->IASetInputLayout(db_inputLayout.Get());

    db_batch->Begin();

    //Loop through all debug objs
    //0 = cubes, 1 = spheres, 2 = cylinders, 3 = rays
    for (short i = 0; i < 4; i++)
    {
        if (debugObjs[i].size() > 0)
        {
            for (auto iter = debugObjs[i].end() - 1; iter > debugObjs[i].begin(); iter--)
            {
                switch (i)
                {
                    case 0:
                        DrawShape(db_batch.get(), iter->world);
                        break;
                    case 1: 
                        //Draw sphere
                        break;
                    case 2:
                        //Draw capsule
                        break;
                    case 3:
                        //Draw rays
                        break;
                    default: break;
                }
                if (iter->type == DebugDrawType::ForDuration)
                    iter->duration -= deltaTime;

                //Erase objs that need to be
                if (iter->type == DebugDrawType::SingleFrame ||
                    (iter->type == DebugDrawType::ForDuration && iter->duration <= 0))
                    iter = debugObjs[i].erase(iter);
            }
        }
    }

    db_batch->End();
    context->RSSetState(0);
    context->OMSetDepthStencilState(0, 0);
    context->OMSetBlendState(0, 0, 0xFFFFFFFF);
    context->IASetInputLayout(0);
}

The DrawShape() call in case 0 can be commented out to remove the problem. My normal rendering is basic mesh forward rendering with a call to context->DrawIndexed() for each object and swapChain->Present(0,0) once everything (including debug shapes) are drawn. I've tried moving the debug drawing to after the swap chain, but that did not fix it.

DrawShape() just sends a world matrix to a function that draws cubes using the primitive batch (from the DirectXTK wiki):

inline void XM_CALLCONV DrawCube(PrimitiveBatch<VertexPositionColor>* batch,
    CXMMATRIX matWorld,
    FXMVECTOR color)
{
    static const XMVECTORF32 s_verts[8] =
    {
        { -1.f, -1.f, -1.f, 0.f },
        {  1.f, -1.f, -1.f, 0.f },
        {  1.f, -1.f,  1.f, 0.f },
        { -1.f, -1.f,  1.f, 0.f },
        { -1.f,  1.f, -1.f, 0.f },
        {  1.f,  1.f, -1.f, 0.f },
        {  1.f,  1.f,  1.f, 0.f },
        { -1.f,  1.f,  1.f, 0.f }
    };

    static const WORD s_indices[] =
    {
        0, 1,
        1, 2,
        2, 3,
        3, 0,
        4, 5,
        5, 6,
        6, 7,
        7, 4,
        0, 4,
        1, 5,
        2, 6,
        3, 7
    };

    VertexPositionColor verts[8];
    for (size_t i = 0; i < 8; ++i)
    {
        XMVECTOR v = XMVector3Transform(s_verts[i], matWorld);
        XMStoreFloat3(&verts[i].position, v);
        XMStoreFloat4(&verts[i].color, color);
    }

    batch->DrawIndexed(D3D_PRIMITIVE_TOPOLOGY_LINELIST, s_indices, _countof(s_indices), verts, 8);
}

I've also tried resetting states after debug drawing is finished to no avail. Does anyone have any ideas?

c++
graphics
game-engine
directx-11
directxtk
asked on Stack Overflow Jan 3, 2020 by Jomsss

1 Answer

-1

D3D_PRIMITIVE_TOPOLOGY_LINELIST draws lines, not triangles. If you want triangles, use D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST.

answered on Stack Overflow Jan 4, 2020 by Chuck Walbourn

User contributions licensed under CC BY-SA 3.0