根据3D模型的世界矩阵遵循的DirectX相机? [英] DirectX Camera to follow based on 3D Model's world Matrix?

查看:59
本文介绍了根据3D模型的世界矩阵遵循的DirectX相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个对象在3D空间中移动,我正在寻找一种方法,只需按一下按钮,即可捕捉相机并跟踪所选对象。

I have multiple objects moving about in a 3D space and am looking for ways to, on button press, have the camera snap and follow the object chosen.

是有没有办法利用每个对象的worldMatrix? (下面是一个对象的示例。这是一个自转&轨道的行星)

Is there a way to make use of each object's worldMatrix? (Below is an example of one object. This one is a planet that spins & orbits)

//set up matrices for rendering
D3DXMATRIX worldMatrixMer, viewMatrixMer, projectionMatrixMer;
m_Camera->GetViewMatrix(viewMatrixMer);
m_D3D->GetWorldMatrix(worldMatrixMer);
m_D3D->GetProjectionMatrix(projectionMatrixMer);
D3DXMatrixRotationX( &matRotateX, rx/65.0f);
//Rotate about Y axis
D3DXMatrixRotationY( &matRotateY, rotation * 15.0f);
D3DXMatrixRotationZ( &matRotateZ, rz/65.0f); 
//Collate Rot Matrices
D3DXMATRIX rotMatrixMer = matRotateX * matRotateY * matRotateZ;
D3DXVECTOR3 newVecDirMer;
D3DXVec3TransformCoord(&newVecDirMer, &initVecDirMer, &rotMatrixMer);
D3DXVec3Normalize( &currentVecDirMer, &newVecDirMer );
//Create the size of the object
D3DXMATRIX matScaleMer;
D3DXMatrixScaling(&matScaleMer, 0.1f, 0.1f, 0.1f);
//Starting position of object
D3DXMatrixTranslation(&matTranslateMer, 0.0f, 0.0f, 3.5983f * 3);
//Rotate about it's own axis
D3DXMatrixRotationY(&worldMatrixMer, rotation);
worldMatrixMer *= rotMatrixMer * matScaleMer * matTranslateMer;
//'Orbit'
D3DXMatrixRotationY( &matOrbit, (-1000.0f * rotation) / 88);
worldMatrixMer *= matOrbit;

我真的是想找到一种实现此目标的优雅方法,因此任何建议都将是极大的赞赏。

I'm really trying to find an elegant way of making this happen, so any suggestions would be greatly appreciated.

谢谢

推荐答案


一种利用每个对象的worldMatrix的方法?

Is there a way to make use of each object's worldMatrix?

对象矩阵可以这样表示:

Object matrix can be represented this way:

objx.x     objx.y     objx.z  0 //m[0][0]..m[0][3] or _11, _12, _13, _14
objy.x     objy.y     objy.z  0 //m[1][0]..m[1][3] or _21, _22, _23, _24
objz.x     objz.y     objz.z  0 //m[2][0]..m[2][3] or _31, _32, _33, _34
objpos.x   objpos.y   objpos.z  1 //m[3][0]..m[3][3] or _41, _42, _43, _44

其中 m [] [] _11 .. _44 D3DMATRIX ,objpos-对象位置矢量,objx-对象x(将本地x转换为世界空间)矢量的相应元素,等等。

Where m[][] and _11.._44 are corresponding elements of D3DMATRIX, objpos - object position vector, objx - object x ('local x" transformed to world space) vector, etc.

只要最后一列(m [0..3] [3])为0、0、0、1,就可以提取对象位置及其 x, y, z个向量(侧面,上,前-这取决于根据应用)。如果最后一列不是 0、0、0、1,则它是投影矩阵,因此您不能轻易地从中提取对象数据。

So as long as the last column (m[0..3][3]) is 0, 0, 0, 1 you can extract object position and its "x", "y", "z" vectors ("side", "up", "front" - which is which depends on application) from matrix. If last column is not "0, 0, 0, 1", then it is projection matrix and you can't extract object data from it this easily.

从那里你可以用相机做任何你想做的事。

From there you could do anything you want with your camera.

这篇关于根据3D模型的世界矩阵遵循的DirectX相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆