[Solved]View matrix from direction and position

Started by
4 comments, last by PSS 15 years, 1 month ago
I'm attempting to create a view matrix from a d3dlight9 object. I have a position, and a direction vector. Can I create a view matrix from this, assuming that (0.0, 1.0, 0.0) is up? Thank you [Edited by - PSS on February 25, 2009 10:01:02 AM]
Advertisement
Yes, that's sufficient information to create a look-at matrix.
You can use the unit-y vector as a starting point and you can then refine it to get a more accurate right and up vector:

if (Math.Abs(Vector3.Dot(Up, L)) < .5f){    R = Vector3.Cross(Up, L);    U = Vector3.Cross(L, R);}else{    U = Vector3.Cross(L, Right);    R = Vector3.Cross(U, L);}
Thanks for the confirmation.

I tried the following before posting, though when rotating the light, the scene just moves very slightly in an almost circular fashion.

D3DXVECTOR3 tvUp( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &m_viewMatrix, &tvLight.Position, &tvLight.Direction, &tvUp );

Note: This is calculating the camera's view matrix, and tvLight is a D3DLIGHT9 object

Should I be taking the light direction as the 'at' vector?

[Edited by - PSS on February 24, 2009 11:47:34 PM]
You're using a direction vector where you should have a target.

D3DXMatrixLookAtLH( pOut, pEye, pAt, pUp );

In your case, you're passing in pOut, pEye, pDir, pUp.

To compute pAt, just add your position and direction vectors to get a point along the line of your direction.

D3DXVECTOR3 at;
D3DXVec3Add( &at, &tvLight.Position, &tvLight.Direction );

D3DXMatrixLookAtLH(&m_viewMatrix, &tvLight.Position, &at, &tvUp);

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Thanks JWalsh!

Thank you for clarifying that for me, that did the trick!

This topic is closed to new replies.

Advertisement