Rotation trouble with HLSL Billboard shader

Started by
0 comments, last by Tsus 12 years, 3 months ago
I am attempting to billboard instanced quads on GPU side, but I believe the rotation is being applied incorrectly on the shader side. Here is my shader code:
float4x4 CreateWorldMatrix( float3 pos, float3 rot, float2 scl )
{
float4x4 rotate;

float4x4 translate = { 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
pos.x, pos.y, pos.z, 1.0 };

float4x4 rotateX = { 1.0, 0.0, 0.0, 0.0,
0.0, cos(rot.x), -sin(rot.x), 0.0,
0.0, sin(rot.x), cos(rot.x), 0.0,
0.0, 0.0, 0.0, 1.0 };

float4x4 rotateY = { cos(rot.y), 0.0, sin(rot.y), 0.0,
0.0, 1.0, 0.0, 0.0,
-sin(rot.y), 0.0, cos(rot.y), 0.0,
0.0, 0.0, 0.0, 1.0 };

float4x4 rotateZ = { cos(rot.z), -sin(rot.z), 0.0, 0.0,
sin(rot.z), cos(rot.z), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };

float4x4 scale = { scl.x, 0.0, 0.0, 0.0,
0.0, scl.y, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };

rotate = mul( rotateY, mul( rotateX, rotateZ ));
return mul( scale, mul( rotate, mul( gCamera, translate ) ) );
}

OutputVS TransformVS( InputVS input )
{
// output structure
OutputVS outVS = (OutputVS)0;

//Creates the world matrix and applies the camera's view for billboarding
float4x4 world = CreateWorldMatrix( input.transPos, input.rot, input.scale );
float4 worldPos = mul( float4(input.vertPos, 1.0f), world );
outVS.worldPos = mul( worldPos, gVP );
outVS.color = input.color;
outVS.texCoord = input.texCoord;

return outVS;
}


The gCamera is a float4x4 that orients the quads to the inverse facing of the camera (hence facing the camera) calculated here:
D3DXMATRIX vp = CCamera::GetInstance()->GetViewMatrix() * CCamera::GetInstance()->GetProjectionMatrix();
D3DXMATRIX camMat = CCamera::GetInstance()->GetViewMatrix();
D3DXMATRIX identity;
D3DXMatrixIdentity(&identity);
camMat._41 = camMat._42 = camMat._43 = 0.0f; camMat._44 = 1.0f;
camMat._13 *= -1.0f; camMat._23 *= -1.0f; camMat._33 *= -1.0f;


The problem I'm having is that the quads are billboarding in every direction as they should except in the local X. As the camera goes down the quad rotates up. I have changed around the order of rotation to every possible order I can think of to no avail. If more information is need just ask. Thanks.
Advertisement
Hi, you can make your life much easier.
You can transform your billboard center to view space and then add offsets in the xy-plane. Afterwards you multiply with the projection matrix and you’re done. So there is no need to compute any complicated transformation matrix at all.

In GPU Gems 2 is an article that shows how this is achieved without geometry shaders.
If you can use geometry shaders it is easier to accomplish and fairly straight-forward to adapt.

Hope that helps you a little! :)

This topic is closed to new replies.

Advertisement