Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Am I doing it right? I converted this.

m_ViewMatrix = XMMatrixLookAtLH(XMLoadFloat3(&m_Position), lookAtVector, upVector);

to this one.

XMVECTOR vz = XMVector3Normalize( lookAtVector - XMLoadFloat3(&m_Position) );
XMVECTOR vx = XMVector3Normalize( XMVector3Cross( upVector, vz ) );
XMVECTOR vy = XMVector3Cross( vz, vx );

m_ViewMatrix.r[0] = vx;
m_ViewMatrix.r[1] = vy;
m_ViewMatrix.r[2] = vz;
m_ViewMatrix.r[3] = XMLoadFloat3(&m_Position);

m_ViewMatrix.r[0].m128_f32[3] = 0.0f;
m_ViewMatrix.r[1].m128_f32[3] = 0.0f;
m_ViewMatrix.r[2].m128_f32[3] = 0.0f;
m_ViewMatrix.r[3].m128_f32[3] = 1.0f;

m_ViewMatrix = XMMatrixInverse( &XMMatrixDeterminant(m_ViewMatrix), m_ViewMatrix );

Everything looks fine when I run it. Another question is, I saw on this site(http://webglfactory.blogspot.com/2011/06/how-to-create-view-matrix.html) that he subtracted lookat from position in his vector vz. I tried it but gave me wrong view matrix. Can anyone check my code. I'm studying linear algebra right now. Sucks my course doesn't have one.

Thank you, Balls

share|improve this question
add comment

1 Answer

First, XMMatrixInverse() first parameter is annotated as output, so for XNAMath it should be:

XMVECTOR vDeterminant;
m_ViewMatrix = XMMatrixInverse( &vDeterminant, m_ViewMatrix );

or for DirectxMath:

m_ViewMatrix = XMMatrixInverse( nullptr, m_ViewMatrix );

Second, if you look at implementation of XMMatrixLookAtLH(), you will find something like this:

XMMATRIX XMMatrixLookAtLH( FXMVECTOR EyePosition, FXMVECTOR FocusPosition, FXMVECTOR UpDirection )
{
    XMVECTOR EyeDirection = XMVectorSubtract( FocusPosition, EyePosition );

    XMVECTOR R2 = XMVector3Normalize( EyeDirection );

    XMVECTOR R0 = XMVector3Cross( UpDirection, R2 );
    R0 = XMVector3Normalize( R0 );

    XMVECTOR R1 = XMVector3Cross( R2, R0 );

    XMVECTOR NegEyePosition = XMVectorNegate( EyePosition );

    XMVECTOR D0 = XMVector3Dot( R0, NegEyePosition );
    XMVECTOR D1 = XMVector3Dot( R1, NegEyePosition );
    XMVECTOR D2 = XMVector3Dot( R2, NegEyePosition );

    XMMATRIX M;
    M.r[0] = XMVectorSelect( D0, R0, g_XMSelect1110.v );
    M.r[1] = XMVectorSelect( D1, R1, g_XMSelect1110.v );
    M.r[2] = XMVectorSelect( D2, R2, g_XMSelect1110.v );
    M.r[3] = g_XMIdentityR3.v;

    M = XMMatrixTranspose( M );

    return M;
}

Your link refer to OpenGL matrix, but you are using DirectX functions, look at this for differences between them.

EDIT: Implementation is in DirectXMathMatrix.inl or xnamathmatrix.inl,you can find them in DirectX SDK, or Windows 8.0 SDK, or download it here.

share|improve this answer
    
Thanks!! May I know where did you find the implementations? I can't find it on the link you provided. Thank you again. –  Balls Oct 14 '12 at 7:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.