D3DXMatrixLookAtLH vs Custom View Matrix

Started by
11 comments, last by Buckeye 9 years, 12 months ago

Hi,

I am quite sure am doing something wrong here but not able to figure it out. Basically,


D3DXMatrixLookAtLH(&m_matView, &m_vecPosition, &m_vecLookAtPoint, &m_WorldUp);

results in : d3dview.png

Whereas. custom view matrix calculation :


m_vecLookAtPoint = m_vecLookAtPoint - m_vecPosition;
    D3DXVec3Normalize(&m_vecLookAtPoint, &m_vecLookAtPoint);
    
    D3DXVec3Cross(&m_vecRight, &m_vecLookAtPoint, &m_WorldUp);

    D3DXVec3Normalize(&m_vecRight, &m_vecRight);

    D3DXVec3Cross(&m_WorldUp, &m_vecLookAtPoint, &m_vecRight);
    D3DXVec3Normalize(&m_WorldUp, &m_WorldUp);

    m_matView._11 = m_vecRight.x; m_matView._12 = m_WorldUp.x; m_matView._13 = m_vecLookAtPoint.x; m_matView._14 = 0;
    m_matView._21 = m_vecRight.y; m_matView._22 = m_WorldUp.y; m_matView._23 = m_vecLookAtPoint.y; m_matView._24 = 0;
    m_matView._31 = m_vecRight.z; m_matView._32 = m_WorldUp.z; m_matView._33 = m_vecLookAtPoint.z; m_matView._34 = 0;
    
    m_matView._41 = -D3DXVec3Dot(&m_vecPosition, &m_vecRight);
    m_matView._42 = -D3DXVec3Dot(&m_vecPosition, &m_WorldUp);
    m_matView._43 = -D3DXVec3Dot(&m_vecPosition, &m_vecLookAtPoint);
    m_matView._44 = 1.0f;

results in :

customview.png

If i manually change the WorldUp vector to (0,-1,0) instead of (0,1,0) i get the default output as in first image.

These are my vectors used for the matrix calculations :


    m_vecPosition    = D3DXVECTOR3(3.0f, 3.0f, -10.0f);
    m_WorldUp        = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
    m_vecRight        = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
    m_vecLookAtPoint= D3DXVECTOR3(0.0f, 0.0f, 0.0f);  

The question is, why I have to invert my World up vector during custom view matrix calculation? what is it that am doing wrong?

Cheers.

Advertisement

You're modifying m_vecLookAtPoint, but according to the formula from MSDN, you need it to be unmodified for later.

I think the first two lines should be:

m_vecRight = m_vecLookAtPoint - m_vecPosition;
D3DXVec3Normalize(&m_vecRight, &m_vecRight);

There might be problems later in your code. When you try to put formulas into code, you should always double-check that you're using the correct variables everywhere (especially on the first few lines), although it sounds to me more like you copy-pasted that code from somewhere and don't care enough to fix it yourself smile.png .

What happens when you swap m_vecLookAtPoint and m_WorldUp in D3DXVec3Cross(&m_vecRight, &m_vecLookAtPoint, &m_WorldUp); ?

Hi tonemgub,

Thanks for the reply. The code is not copied from anywhere, but rather I am referring to the book by Frank luna on DirectX9 using Shader approach.


I think the first two lines should be:

m_vecRight = m_vecLookAtPoint - m_vecPosition;
D3DXVec3Normalize(&m_vecRight, &m_vecRight);

I am not quite sure how this will result in camera right vector as the calculation should give you camera lookat vector itself. unless its a typo. Right vector will be obtained only when you take the cross product of lookat vector & reference up vector.

I got the right output after changing the order of cross product. As per msdn,


zaxis = normal(At - Eye)
xaxis = normal(cross(Up, zaxis))
yaxis = cross(zaxis, xaxis)

Whereas I was doing it cross(zaxis, up) thinking it follows right hand thumb rule ( cross(zaxis, Up) will point to the right side) for the cross product but being Left handed view matrix its the other way around ?

Once again thanks to your input, I started looking vars more carefully smile.png

Cheers.

What happens when you swap m_vecLookAtPoint and m_WorldUp in D3DXVec3Cross(&m_vecRight, &m_vecLookAtPoint, &m_WorldUp); ?

Yup!

Thats exactly what i did & it worked. So basically the cross product for LH matrix doesnt seem to follow right hand thumb rule but rather left hand convention? As conventionally in right hand thumb rule.... "LookAt X Up" should point to right hand side... and "Up X LookAt" should point to left side ....


So basically the cross product for LH matrix doesnt seem to follow right hand thumb rule but rather left hand convention?

Yup. m_vecLookAtPoint faces away from you, so cross(LookAt, Up) would point to the left.


So basically the cross product for LH matrix doesnt seem to follow right hand thumb rule but rather left hand convention?

With your setup, yes, you should be using the left-hand rule for all your rotations and crosses, etc.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Thank you so much guys! smile.png

Appreciate all the help...

Cheers.


So basically the cross product for LH matrix doesnt seem to follow right hand thumb rule but rather left hand convention?

With your setup, yes, you should be using the left-hand rule for all your rotations and crosses, etc.

Correct me if i'm wrong here but should it matter all that much? x cross y = z, regardless of left hand or right hand. regardless if X points to the right or left, you would get it via Y cross Z, and not Z cross Y as the OP attempted to do. A positive rotation on X, will rotate Y to Z, regardless of the handedness as well. The handedness only matters with projection. In fact looking at this vs this the only thing different between the two is that Z is eye-lookat in the right handed(-z forward) and lookat-eye in the left(+z forward), as we want the Y to point up and X to point right regardless of the handedness, so that the coordinates match screen space, so Z is inverted from lh to rh systems.

looking through the d3dx versions(lookatLH, lookatRH) it seems that there is an error in the docs for the right hand version that was never addressed.

The formula used by D3DXMatrixLookAtLH is right there on it's MSDN page, in the Remarks section. Isn't that what you are trying to do?

Actually, I just looked at it again and realized they were not re-using the At vector later in the formula, and got confused by your re-use of it (which, as I realize now, is actually correct). Sorry about that.

This topic is closed to new replies.

Advertisement