Sign in to follow this  
sparrow666

reopen: D3DXMatrixLookAtRH & D3DXMatrixLookAtLH

Recommended Posts

link to the closed thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=505156 I am trying to get for every moment in the game the vector of the camera position and the vector where camera is looking at. And i would also like to set those parameters. For the games that use D3DXMatrixLookAtRH or D3DXMatrixLookAtLH that should be possible.

Share this post


Link to post
Share on other sites
Those two functions are defined as WINAPI so I'd imagine you can hook them using normal DLL injection stuff. Must admit to not having bothered with this myself so I'll refrain from commenting!

Doing a little digging around it wasn't too hard to find the definitions for those two functions:

Right-Handed:
zaxis = normal(cameraPosition - cameraTarget)
xaxis = normal(cross(cameraUpVector, zaxis))
yaxis = cross(zaxis, xaxis)

xaxis.x yaxis.x zaxis.x 0
xaxis.y yaxis.y zaxis.y 0
xaxis.z yaxis.z zaxis.z 0
-dot(xaxis, cameraPosition) -dot(yaxis, cameraPosition) -dot(zaxis, cameraPosition) 1


Left-Handed:
zaxis = normal(cameraTarget - cameraPosition)
xaxis = normal(cross(cameraUpVector, zaxis))
yaxis = cross(zaxis, xaxis)

xaxis.x yaxis.x zaxis.x 0
xaxis.y yaxis.y zaxis.y 0
xaxis.z yaxis.z zaxis.z 0
-dot(xaxis, cameraPosition) -dot(yaxis, cameraPosition) -dot(zaxis, cameraPosition) 1


Obviously, finding the camera direction is trivial from either of the above matrices.

Deriving the camera position shouldn't be too hard, but probably just requires solving a three-way simultaneous equation:


M41 = -(xaxis.x * cameraPosition.x + xaxis.y * cameraPosition.y + xaxis.z *
cameraPosition.z)


M42 = -(yaxis.x * cameraPosition.x + yaxis.y * cameraPosition.y + yaxis.z * cameraPosition.z)


M43 = -(zaxis.x * cameraPosition.x + zaxis.y * cameraPosition.y + zaxis.z * cameraPosition.z)

cameraPosition.x = (-M41 - xaxis.y * cameraPosition.y - xaxis.z *
cameraPosition.z) / xaxis.x

cameraPosition.y = (-M42 - yaxis.x * cameraPosition.x - yaxis.z * cameraPosition.z) / yaxis.y

cameraPosition.z = (-M43 - zaxis.x * cameraPosition.x - zaxis.y * cameraPosition.y) / zaxis.z

=> cameraPosition.x = (-M41 - xaxis.y * ((-M42 - yaxis.x * cameraPosition.x - yaxis.z * ((-M43 - zaxis.x * cameraPosition.x - zaxis.y * cameraPosition.y) / zaxis.z)) / yaxis.y) - xaxis.z *
((-M43 - zaxis.x * cameraPosition.x - zaxis.y * cameraPosition.y) / zaxis.z)) / xaxis.x


etc...etc... [smile]

Probably a much easier/simpler way of doing it, but i'm a little rusty on my linear algebra [sad]


hth
Jack

Share this post


Link to post
Share on other sites
Jack thank you very much for the reply!

I am sorry I was not clear on what my problem is. The thing is that I would like to get this for all games that exist out there and all games do not use D3DXMatrixLookAtRH or D3DXMatrixLookAtLH functions. For the ones that use these functions the problem is trivial.

But there are some games, that have custom written transform functions and do not use D3DXMatrixLookAtRH or D3DXMatrixLookAtLH. So I was wondering how could I get that information from there.

Share this post


Link to post
Share on other sites
Quote:
Original post by sparrow666
But there are some games, that have custom written transform functions and do not use D3DXMatrixLookAtRH or D3DXMatrixLookAtLH. So I was wondering how could I get that information from there.
With difficulty and no guarantees I would imagine!

Much like Steve commented in your previous thread - if a game uses old fixed function routines you can just hook ::SetTransform(), but if shaders are involved you can only really make a guess as to what to hook. Complex reflection via D3D10 might well yield some good hints as to which registers/variables the matrices are stored in, but there is no guarantee of reliability here.

In particular, most games will pass in world*view*proj as a single matrix into a shader. This just makes it a royal PITA to try and decompose back to the basic inputs!


hth
Jack

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