Sign in to follow this  
3delite

D3DXMatrixLookAtLH alternative

Recommended Posts

Hi!

I was trying to build my Direct3D9 app with the new Delphi XE2 for x64 and got a crash at the D3DXMatrixLookAtLH line saying that a floating point division by zero occured. It is strange becouse in Win32 build mode everything is working fine.

So I decided to replace the D3DXMatrixLookAtLH routine with my own, found some links that helped, but I couldn't figure it all out. Can anybody help with this?

The original code:

[code]

       EyePointVector := D3DXVector3(300, -290 ,-723);
        LookAtPointVector := D3DXVector3(300, -290, 0.0);
        UpVector := D3DXVector3(0.0, 1.0, 0.0);
        D3DXMatrixLookAtLH(ViewMatrix, EyePointVector, LookAtPointVector, UpVector);
[/code]

Tryed achieving the same with:

[code]

        EyePointVector := D3DXVector3(300, -290 ,-723);
        LookAtPointVector := D3DXVector3(300, -290, 0.0);
        UpVector := D3DXVector3(0.0, 1.0, 0.0);
        RightVector := D3DXVector3(1.0, 0.0, 0.0);

        viewMatrix._11 := RightVector.x; viewMatrix._12 := UpVector.x; viewMatrix._13 := LookAtPointVector.x; viewMatrix._14 := 0.0;
        viewMatrix._21 := RightVector.y; viewMatrix._22 := UpVector.y; viewMatrix._23 := LookAtPointVector.y; viewMatrix._24 := 0.0;
        viewMatrix._31 := RightVector.z; viewMatrix._32 := UpVector.z; viewMatrix._33 := LookAtPointVector.z; viewMatrix._34 := 0.0;
        viewMatrix._41 := - D3DXVec3Dot(EyePointVector, RightVector);
        viewMatrix._42 := - D3DXVec3Dot(EyePointVector, UpVector);
        viewMatrix._43 := - D3DXVec3Dot(EyePointVector, LookAtPointVector);
        viewMatrix._44 := 1.0;

[/code]

But the 3D space is deformed.

Share this post


Link to post
Share on other sites
Hi,
you either found some badly misleading links or you didn't understand it properly. The matrix is built differently, it is a bit more complicated. Check the documentation to find out how to build it manually:
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb205342%28v=vs.85%29.aspx"]http://msdn.microsof...v=vs.85%29.aspx[/url]

You definitely cannot have large numbers (like 300 in your case) in the top-left 3x3 submatrix, it consists of 3 normalized (unit-lenght) vectors.

Share this post


Link to post
Share on other sites
[quote name='Tom KQT' timestamp='1317903297' post='4869751']
Hi,
you either found some badly misleading links or you didn't understand it properly. The matrix is built differently, it is a bit more complicated. Check the documentation to find out how to build it manually:
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb205342%28v=vs.85%29.aspx"]http://msdn.microsof...v=vs.85%29.aspx[/url]

You definitely cannot have large numbers (like 300 in your case) in the top-left 3x3 submatrix, it consists of 3 normalized (unit-lenght) vectors.
[/quote]

Thanx a lot!!! Got it working!!!


The code that works:

[code] EyePointVector := D3DXVector3(300, -290 ,-723);
LookAtPointVector := D3DXVector3(300, -290, 0.0);
UpVector := D3DXVector3(0.0, 1.0, 0.0);
//D3DXMatrixLookAtLH(ViewMatrix, EyePointVector, LookAtPointVector, UpVector);

RightVector := D3DXVector3(1.0, 0.0, 0.0);

D3DXVec3Subtract(ZAxis, LookAtPointVector, EyePointVector);
D3DXVec3Normalize(ZAxis, ZAxis);

D3DXVec3Cross(XAxis, UpVector, ZAxis);
D3DXVec3Normalize(XAxis, XAxis);

D3DXVec3Cross(YAxis, ZAxis, XAxis);

viewMatrix._11 := XAxis.x; viewMatrix._12 := YAxis.x; viewMatrix._13 := ZAxis.x; viewMatrix._14 := 0.0;
viewMatrix._21 := XAxis.y; viewMatrix._22 := YAxis.y; viewMatrix._23 := ZAxis.y; viewMatrix._24 := 0.0;
viewMatrix._31 := XAxis.z; viewMatrix._32 := YAxis.z; viewMatrix._33 := ZAxis.z; viewMatrix._34 := 0.0;

viewMatrix._41 := - D3DXVec3Dot( XAxis, EyePointVector );
viewMatrix._42 := - D3DXVec3Dot( YAxis, EyePointVector );
viewMatrix._43 := - D3DXVec3Dot( ZAxis, EyePointVector );
viewMatrix._44 := 1.0;[/code]

Couldn't get this without your help! Thanx!  :)

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