# DX11 Billboard Matrix Generation C++, DX11

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there,

I was wondering if someone knows a way to create Billboard matrix in C++. I am trying to port the particle system posted here https://github.com/sharpdx/SharpDX/commit/71d289e05a0c383e5bc0680333150f06c25197d9.  However I have only found built in functions for C#, like the ones in SharpDX:

up = Vector3.UnitY;
cameraForward = Vector3.Normalize(cameraTarget - cameraPosition);
cameraForward = Vector3.Normalize(cameraForward);
Matrix.Billboard(Vector3.Zero, -cameraForward, up, cameraForward)

What would be the proper way to replicate this in C++? I need the Billboard matrix to generate the billboard quads for a Particle System inside a Geometry shader.

Cheers and thanks for any help!

// Geometry shader to expand the vertex into a quad.
[maxvertexcount(4)]
void GS_Render(point ParticleVertex inputArray[1], inout TriangleStream<ParticleVertexGsOut> stream) {

ParticleVertex input = inputArray[0];

// Calculate the particles age in [0..1]

ParticleVertexGsOut v;

// Determine the particle's color based on its age.
v.Color = input.Color;
v.Color.a *= (-(256 * 256) * pow(age - 0.5f, 16) + 1);
else
v.Color.a *= (-4 * (age - 0.5f) * (age - 0.5f) + 1);

// Calculate the particle's current size
float2 size = lerp(input.SizeStartEnd.x, input.SizeStartEnd.y, age);

// Check if one of the quad's axes should be constrained to the particle's velocity.
bool constrained = (input.Flags & FLAG_CONSTRAINED) > 0;
float3 right, up;
if(constrained)
{
right = normalize(input.Velocity);
up = cross(_camDir, right) * size.y;
right *= size.x;
}
else
{
float2 xr = float4(size.x, 0, 0, 1);
float2 yr = float4(0, size.y, 0, 1);
right = mul(xr, _lookAtMatrix).xyz; // <- BILLBOARD MATRIX USE
up = mul(yr, _lookAtMatrix).xyz;// <- BILLBOARD MATRIX USE
}

// Create and append four vertices to form a quad.
float4 positionWS = float4(input.Position + right + up, 1.f);
v.PositionVS = mul(positionWS, _view);
v.Position = mul(v.PositionVS, _proj);
v.TexCoord = float2(1, 1);
stream.Append(v);

positionWS = float4(input.Position - right + up, 1.f);
v.PositionVS = mul(positionWS, _view);
v.Position = mul(v.PositionVS, _proj);
v.TexCoord = float2(0, 1);
stream.Append(v);

positionWS = float4(input.Position + right - up, 1.f);
v.PositionVS = mul(positionWS, _view);
v.Position = mul(v.PositionVS, _proj);
v.TexCoord = float2(1, 0);
stream.Append(v);

positionWS = float4(input.Position - right - up, 1.f);
v.PositionVS = mul(positionWS, _view);
v.Position = mul(v.PositionVS, _proj);
v.TexCoord = float2(0, 0);
stream.Append(v);

stream.RestartStrip();
}

##### Share on other sites

http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/

be for warned that the article above uses column vectors, and so the matrices may need to be flipped along the diagonal

EDIT: Nehe's tutorial might be a better fit for you: http://nehe.gamedev.net/article/billboarding_how_to/18011/

Edited by Burnt_Fyr

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 23
• 42
• 75