Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

How to convert D3DXMATRIX into eye, lookat and up vectors?

This topic is 3167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to convert a D3DXMATRIX into eye, lookat and up vectors...
D3DXMatrixLookAtLH(&matView,
                     &D3DXVECTOR3(0.0f, 200.0f, -2000.0f),
                     &D3DXVECTOR3(0.0f, 0.0f, 0.0f),
                     &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
 g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);

Thanks Jack

Share this post


Link to post
Share on other sites
Advertisement
This can certainly be done, but why not just invert the matrix directly? That would be more straightforward, IMO. (Unless you need those parameters for more than you've shown in your code example.)

Share this post


Link to post
Share on other sites
Hey lucky!

A matrix decribes an origin and offset of a 3-space coordinate system. Let's look at a 4-square matrix like D3DXMATRIX in world space.


R.x R.y R.z 0
U.x U.y U.z 0
L.x L.y L.z 0
P.x P.y P.z 1


R = Right
U = Up
L = Look
P = Position (Or as I call it, offset. Synonymous to "Eye" vector)
I picture Scooby-Doo screaming "RULP!" to remember the setup. [smile]
RUL are the basis vectors for the space and describe orientation, as you may have already known. I showed you this to get you set up for the view matrix, where you seem to be.


R.x U.x L.x 0
R.y U.y L.y 0
R.z U.z L.z 0
-(P dot R), -(P dot U), -(P dot L), 1


It's just an inverted world matrix. To extract the vectors you want, just pull them from the matrix elements according to this mapping. Note you would be extracting them in view space in your case. I won't go into much more detail here unless I have to. [smile]

I hope this helps.

Cheers!
-Zyro

Share this post


Link to post
Share on other sites
Ahh. Thanks
One additional thing
Don't know you guys know the 3ds max sdk.
If I just extract the camera's transformation matrix,
would that turn out to be the correct view matrix in direct3d when export it to a file?
Thats what exactly i'm doing... It might be wrong...

Matrix3 mat = aNode->GetObjectTM(ip->GetTime());
ConvertTo4x4Matrix();
SaveAway();



Then I saved that to a 4x4 matrix and to disk.
And read it back to D3DXMATRIX in my game (directX)


D3DXMATRIX m_matView;
///.... read the variable in file
m_pDevice->SetTransform(D3DTS_VIEW, &m_matView);



I know this looks like trial and error... But I can't think of a better solution
Thanks
Jack

Share this post


Link to post
Share on other sites
Hi
So What I do is to invert the transformation matrix obtained from MAX
and do an inverse, and extract P,U,R and L respectively and then plug them into D3DXMatrixLookAtLH.. is it what you meant?
Thanks
Jack

Share this post


Link to post
Share on other sites
Quote:
So What I do is to invert the transformation matrix obtained from MAX
and do an inverse, and extract P,U,R and L respectively and then plug them into D3DXMatrixLookAtLH.. is it what you meant?
If the only reason you're extracting the position, look-at, and up vectors is to use them as arguments to D3DXMatrixLookAtLH(), then there's absolutely no point in doing so. (Not that I can think of at least.)

Seriously, if you need the inverse of the matrix, just invert it! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
So What I do is to invert the transformation matrix obtained from MAX
and do an inverse, and extract P,U,R and L respectively and then plug them into D3DXMatrixLookAtLH.. is it what you meant?
If the only reason you're extracting the position, look-at, and up vectors is to use them as arguments to D3DXMatrixLookAtLH(), then there's absolutely no point in doing so. (Not that I can think of at least.)

Seriously, if you need the inverse of the matrix, just invert it! :)


At first blush, jyk, could you give an example? can't visualize on top of my head. Sorry for the stupidity..Thanks
Jack

Share this post


Link to post
Share on other sites
Quote:
At first blush, jyk, could you give an example? can't visualize on top of my head.
Can't guarantee I'll get the DX syntax exactly right, but:
D3DXMATRIX matView;
D3DXMatrixInverse(matView, NULL, myMatrix);
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
At first blush, jyk, could you give an example? can't visualize on top of my head.
Can't guarantee I'll get the DX syntax exactly right, but:
D3DXMATRIX matView;
D3DXMatrixInverse(matView, NULL, myMatrix);
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);


Thanks jyk,
I'll try that right away
Hv a nice day
Jack

Share this post


Link to post
Share on other sites

if (strcmp(strNodeName, "Camera01") == 0)
{
CameraObject *cam = (CameraObject*) aNode->EvalWorldState(ip->GetTime()).obj;
Matrix3 atm = aNode->GetObjectTM(ip->GetTime()); // 4x3 matrix
float fov = cam->GetFOV(ip->GetTime());
WriteCam_File(atm, fov);
continue;
}




Whoops doesn't work, could you please check this also?


oid WriteCam_File(const Matrix3& tm, float fov)
{
FILE* fpc;
float zero = 0;
float one = 1;
fpc = fopen("Demo.cam", "wb");
float NewMat[4][4];
for (int i = 0; i < 4; i++)
{
for (int j = 0 ; j < 4; j++)
{
if (j == 3 && i != 3) { NewMat[j] = 0; continue; }
if (i == 3 && j == 3) { NewMat[j] = 1; continue; }
NewMat[j] = tm[j];
}
}
fwrite((const void *) NewMat, sizeof(float), 16, fpc);
fwrite((const void*)&fov, sizeof(float), 1, fpc);
fclose(fpc);

}






and finally


D3DXMATRIX mat1;
D3DXMatrixInverse(&mat1, NULL, &g_Cam);
g_pD3DDevice->SetTransform(D3DTS_VIEW, &mat1);// &g_Cam);




Original Transformation of the camera in max
Original Camera Transform======================
0.909856 0.355519 -0.213933 0.000000
-0.159671 0.775895 0.610322 0.000000
0.382971 -0.521146 0.762719 0.000000
439.998199 -144.352707 557.162292 1.000000


my View's transformation after the inverse is
View Transformation=======================
0.909856 -0.159671 0.382971 0.000000
0.355519 0.775895 -0.521146 0.000000
-0.213933 0.610322 0.762719 0.000000
-229.819366 -157.791046 -668.693604 1.000000

While the object is
Building Transform=======================
1.000000 0.000000 0.000000 0.000000
0.000000 1.000000 0.000000 0.000000
0.000000 0.000000 1.000000 0.000000
190.088028 182.825699 107.369308 1.000000


Are these correct?
I can't see anything in the viewport
Thanks
Jack

[Edited by - lucky6969b on October 14, 2009 4:11:29 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!