Advertisement

Bullet Physics Debug Drawer

Started by June 06, 2013 01:01 AM
23 comments, last by xexuxjy 11 years, 8 months ago

You could approximate number of vertices as you do with particle system. Fill VB as much as it can hold then if you have more vertices to process unlock/lock/refill/draw and repeat process as needed.

Bullet might have some methods to obtain number of triangles so you could base your number on that.

You don't need index buffer for lines.

I'm guessing that could cause memory leak...? Since I will be having unused vertices in memory.

If it will not cause memory leak, take a look at my code, I am getting messy lines:


void DrawAll()
{
device->SetFVF(D3DFVF_XYZ);
D3DXMATRIX worldMatrix;
D3DXMatrixIdentity(&worldMatrix);
device->SetTransform(D3DTS_WORLD, &worldMatrix);

D3DXVECTOR3* pVertices = NULL;
vbuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
DWORD numVert = 0;
DWORD numVertices = numLines * 2;
for (DWORD i = 0; i < linesList.size(); i++)
{
pVertices = linesList.lFrom;
pVertices[i+1] = linesList.lTo;
numVert += 2;

if (numVert > (numVertices - 2))
{
vbuffer->Unlock();
device->SetStreamSource(0, vbuffer, 0, sizeof(LINE_VERTEX));
device->DrawPrimitive(D3DPT_LINELIST, 0, linesList.size());
pVertices = 0;
vbuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
numVert = 0;
}
}
vbuffer->Unlock();
device->SetStreamSource(0, vbuffer, 0, sizeof(LINE_VERTEX));
device->DrawPrimitive(D3DPT_LINELIST, 0, linesList.size());
linesList.clear();
}

Advertisement

What memory leak you talking about?

You made some mistakes in your code, here is correction:


void DrawAll()
{
    device->SetFVF(D3DFVF_XYZ);
    device->SetStreamSource(0, vbuffer, 0, sizeof(LINE_VERTEX));
    D3DXMATRIX worldMatrix;
    D3DXMatrixIdentity(&worldMatrix);
    device->SetTransform(D3DTS_WORLD, &worldMatrix);

    D3DXVECTOR3* pVertices = NULL;
    vbuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
    DWORD numVert = 0;
    DWORD numVertices = numLines * 2;
    for (DWORD i = 0; i < linesList.size(); i++)
    {
        pVertices[numVert]   = linesList.lFrom;
        pVertices[numVert+1] = linesList.lTo;
        numVert += 2;

        if (numVert > (numVertices - 2))
        {
            vbuffer->Unlock();
            device->DrawPrimitive(D3DPT_LINELIST, 0, numVert / 2);
            pVertices = 0;
            vbuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
            numVert = 0;
        }
    }
    vbuffer->Unlock();

    if(numVert > 0)
    {
        device->DrawPrimitive(D3DPT_LINELIST, 0, numVert / 2);
    }
    linesList.clear();
}

assuming that you have properly created VB with "numVertices" as number of vertices?

EDIT: Moved linesList.clear(); out of if block as it might not get called. Now it is correct.

Does this work?

EDIT2: Additionally you can count how many draw calls you have now (add variable that increments when DrawPrimitive is called and display it to see) and resize your VB verts count if necessary.

Someone told me before creating vertex buffer with for example size of 10,000 vertices and then drawing less (200 vertices) cause memory leak, I'm not sure.

Is there is a way to dynamically change the size of the vertex buffer during rendering?

BTW, the code works now :)

Another question, should I override the following methods?

void btIDebugDraw::drawBox(const btVector3& bbMin, const btVector3& bbMax, const btVector3& color);

void btIDebugDraw::drawSphere (const btVector3& p, btScalar radius, const btVector3& color);

Any other method I should override other than drawLine?

drawing less vertices then you have allocated will not cause a memory leak - not sure who mentioned that.

you still might get difficulties drawing a large mesh like that just using drawline calls as you're not able to do any real culling of tri's that you can't see. I'll try and have a look if theres any way of querying the bvh mesh shape to get the various bounding boxes or the tree as that would reduce the draw calls. You could also have a fixed size buffer for lines and just ignore any calls after it's full - will look a bit patchy but may help you figure stuff out.

This topic is closed to new replies.

Advertisement