b2Draw() for DirectX 11

Post your tutorials and samples here.
Arwic
Posts: 2
Joined: Tue Oct 28, 2014 10:06 am

b2Draw() for DirectX 11

Postby Arwic » Tue Oct 28, 2014 10:17 am

I'm new to c++ and box2d, from c# and farseer, and have recently got a little xna esc engine going with directxtk. I was wondering if anyone has a b2Draw() for d3d11 that I can incorporate into my engine to draw my physics worlds. Sadly I can't seem to find anything in directx and rendering anything b2draw needs is beyond my single white triangle I can render :(

Arwic
Posts: 2
Joined: Tue Oct 28, 2014 10:06 am

Re: b2Draw() for DirectX 11

Postby Arwic » Sat Nov 08, 2014 12:57 am

I have finished my basic debug draw methods!
Drawing solid shapes isn't complete but the outline works
(gfx is just an object that holds my Direct3D stuff, similar to that of ChiliTomatoNoodle's tutorials but in DirectX11)

Code: Select all

void DebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
   VERTEX verts[9];
   for (UINT i = 0; i < vertexCount; i++)
   {
      verts[i] = { vertices[i].x / gfx->SCREENWIDTH * 64, vertices[i].y / gfx->SCREENHEIGHT * 64, 0.0f, D3DXCOLOR(color.r, color.g, color.b, 1.0f) };
   }
   verts[vertexCount] = { vertices[0].x / gfx->SCREENWIDTH * 64, vertices[0].y / gfx->SCREENHEIGHT * 64, 0.0f, D3DXCOLOR(color.r, color.g, color.b, 1.0f) };

   gfx->RenderVerts(verts, vertexCount + 1, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP);
}

void DebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{
   gfx->DrawCircle(center.x, center.y, radius, color.r, color.g, color.b, 1.0f, 64, 0);
}


Code: Select all

void D3DGraphics::RenderVerts(VERTEX* verts, UINT count, D3D11_PRIMITIVE_TOPOLOGY type)
{
   D3D11_MAPPED_SUBRESOURCE ms;
   ZeroMemory(&ms, sizeof(ms));
   // map the buffer   
   m_pDeviceContext->Map(m_pVertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);
   // copy the data to it
   memcpy(ms.pData, verts, sizeof(VERTEX) * (count + 1));
   // unmap it
   m_pDeviceContext->Unmap(m_pVertexBuffer, NULL);

   UINT stride = sizeof(VERTEX);
   UINT offset = 0;
   m_pDeviceContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);
   m_pDeviceContext->IASetPrimitiveTopology(type);
   m_pDeviceContext->Draw(count, 0);
}

void D3DGraphics::DrawCircle(float x, float y, float radius, float r, float g, float b, float a, float scale, float rotation)
{
   // number of points, more->smoother+slower, requires highering the vertex buffer's byte width
   VERTEX verts[17];
   int points = 16;
   for (int i = 0; i < points; i++)
   {
      float theta = (2 * 3.14159265359) * i / points;
      verts[i] = { (x + radius * cos(theta)) / SCREENWIDTH * scale, (y + radius * sin(theta)) / SCREENHEIGHT * scale, 0.0f, D3DXCOLOR(r, g, b, a) };
   }
   verts[16] = verts[0];
   RenderVerts(verts, points + 1, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP);
}


Creating the buffer

Code: Select all

// create the vertex buffer
   D3D11_BUFFER_DESC bd;
   ZeroMemory(&bd, sizeof(bd));

   bd.Usage = D3D11_USAGE_DYNAMIC;                // write access access by CPU and GPU
   bd.ByteWidth = sizeof(VERTEX)* 17;             // size of the VERTEX struct * max num of verts in the array
   bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;       // use as a vertex buffer
   bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;    // allow CPU to write in buffer

   m_pDevice->CreateBuffer(&bd, NULL, &m_pVertexBuffer);       // create the buffer


Return to “Tutorials and Samples”



Who is online

Users browsing this forum: No registered users and 1 guest