Advertisement Jump to content
Sign in to follow this  
neroziros

DX11 Billboard Matrix Generation C++, DX11

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

If you intended to correct an error in the post then please contact us.

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]
    float age = input.TimerLifetime.x / input.TimerLifetime.y;


    ParticleVertexGsOut v;


    // Determine the particle's color based on its age.
    v.Color = input.Color;
    bool fastFade = (input.Flags & FLAG_FAST_FADE) > 0;
    if (fastFade)
        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 this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!