Sign in to follow this  
mrheisenberg

Can't properly produce a ViewProjection matrix

Recommended Posts

I have a camera class that uses the DirectXMath API:

__declspec(align(16)) class Camera
{
public:
XMVECTOR Translation;
XMMATRIX Rotation;
XMVECTOR Scale;
XMMATRIX Transform;


XMFLOAT3 RotAngles;


XMMATRIX ProjectionMatrix;


float Width;
float Height;


float NearZ;
float FarZ;
float AspectRatio;
float FieldOfView;


Camera()
{
Translation = XMVectorZero();
Rotation = XMMatrixIdentity();
Scale = XMVectorSplatOne();


Transform = XMMatrixIdentity();


Width = 800;
Height = 600;


NearZ = 0.1f;
FarZ = 100.0f;
AspectRatio = 800 / 600;
FieldOfView = (XM_PIDIV4);


ProjectionMatrix = XMMatrixPerspectiveFovLH(FieldOfView, AspectRatio, NearZ, FarZ);
}


void Update()
{
Rotation = XMMatrixRotationRollPitchYaw(RotAngles.x, RotAngles.y, RotAngles.z);
XMMATRIX scaleM = XMMatrixScalingFromVector(Scale);
XMMATRIX translationM = XMMatrixTranslationFromVector(Translation);


Transform = scaleM * Rotation * translationM;
}


XMMATRIX GetViewMatrix()
{
XMVECTOR Eye;
XMVECTOR At;
XMVECTOR Up;


Eye = Translation;
At = Translation + Transform.r[2];
Up = Transform.r[1];


return(XMMatrixLookAtLH(Eye, At, Up));
}


XMMATRIX GetViewProjectionMatrix()
{
return(XMMatrixTranspose(GetViewMatrix() * ProjectionMatrix));
}
};

When I store the result of GetViewProjectionMatrix() in a XMFLOAT4X4 and update it to the constant buffer, the geometry gets torn apart or doesn't show up at all when I move/rotate the camera with the keyboard.I have isolated the camera to be issue with the deforming/disappearing geometry, but I have no idea what the problem is.I mean the projection matrix can't be wrong, it's just 1 function call, so it's most likely the view matrix.Could someone tell me where the issue is?I tried different combinations of multiplication orders/transposing both/transposing only one/anything.It never works properly.

 

Share this post


Link to post
Share on other sites

The order of multiplications seems fine. Have you tried not transposing the matrices at all, because unless you've been messing with the row-column ordering in-shader, transposing shouldn't be necessary. Also check the debug runtime to see if anything is wrong with the cbuffer you've created.

Share this post


Link to post
Share on other sites

The order of multiplications seems fine. Have you tried not transposing the matrices at all, because unless you've been messing with the row-column ordering in-shader, transposing shouldn't be necessary. Also check the debug runtime to see if anything is wrong with the cbuffer you've created.

Maybe the issue is here?
 

XMVECTOR Eye;
XMVECTOR At;
XMVECTOR Up;


Eye = Translation;
At = Translation + Transform.r[2];
Up = Transform.r[1];


return(XMMatrixLookAtLH(Eye, At, Up));

I tried not transposing, transposing only one or the other, transposing both, it never works properly.

 

Share this post


Link to post
Share on other sites

At = Translation + Transform.r[2];

This doesn’t make sense.  “At” is supposed to be a position, Transform.r[2] is a direction.

Anyway, the solution is unrelated to that.
XMMATRIX GetViewMatrix()
{
    return(XMMatrixInverse(NULL, Transform));
}

L. Spiro

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this