0

I am using direct 3d for the first time and i am looking for a way to represent bounding boxes (rectangles/cylinders/spheres). How do i outline the bounding box? Is this a shader, or do i use somthing else to create an outlined shape?

chasester

1 Answers1

3

The simplest way is to use a line list to create a unit wireframe bounding volume. using a transform matrix, you can scale, translate or rotate the volume around any object in your 3D world.

accomplishing this requires 3 main parts:

  1. A constant VB and IB with the linelist verts set up (remember it needs to be centered around the origin, and have a unit length of 1), plus a VB for per-instance data.
  2. An input layout that takes in a transform matrix as a per instance member.
  3. A vertex shader that applies the transform matrix to each vertex of the cube. (the pixel shader need only output the color as is supplied).

(It should be noted that the same principle applies to spheres, hulls etc. as well)

The shaders I use look like this:

struct WIRE_VS_INPUT
{
    float3 Position     : POSITION;
    float4 Color        : COLOR0;
    float3 Transform    : TRANSFORM;
    float3 Size         : SIZE;  
};

struct WIRE_VS_OUTPUT
{
    float4 Position     : SV_POSITION; 
    float4 Color        : COLOR0;
};

WIRE_VS_OUTPUT WireBoxVS( WIRE_VS_INPUT Input )
{
    WIRE_VS_OUTPUT Output;
    Output.Position = mul(float4((Input.Position * Input.Size) + Input.Transform,1),g_mWorldViewProjection);
    Output.Color = Input.Color;
    return Output;    
}

float4 WireBoxPS( WIRE_VS_OUTPUT Input ) : SV_TARGET
{ 
    return Input.Color;
}

The cube VB & IB setup:

const DirectX::XMFLOAT3 kWireCube[] =
{
    DirectX::XMFLOAT3(-1,-1,-1),
    DirectX::XMFLOAT3(1,-1,-1),
    DirectX::XMFLOAT3(1,1,-1),
    DirectX::XMFLOAT3(-1,1,-1),
    DirectX::XMFLOAT3(-1,-1,1),
    DirectX::XMFLOAT3(1,-1,1),
    DirectX::XMFLOAT3(1,1,1),
    DirectX::XMFLOAT3(-1,1,1)
};

const WORD kWireCubeIndices[] =
{
    0,1,
    1,2,
    2,3,
    3,0,

    4,5,
    5,6,
    6,7,
    7,4,

    0,4,
    1,5,
    2,6,
    3,7
};
Necrolis
  • 25,836
  • 3
  • 63
  • 101