我有一段时间以来一直试图在我的游戏编辑器中实现基于点击的模型插入,但遇到了很多问题。最近的是模型放置的偏移量。到目前为止,我已经将这些代码放在一起了:
D3DXVECTOR3 vPickRayDir;
D3DXVECTOR3 vPickRayOrig;
float width = (float)d3d->scd.BufferDesc.Width;
float height = (float)d3d->scd.BufferDesc.Height;
const D3DXMATRIX* pmatProj = &d3d->matProjection;
POINT ptCursor;
GetCursorPos(&ptCursor);
ScreenToClient(&ptCursor);
D3DXVECTOR3 v;
v.x = (((2.0f * ptCursor.x) / (d3d->viewport.Width)) - 1.0f) / pmatProj->_11;
v.y = -(((2.0f * ptCursor.y) / (d3d->viewport.Height)) - 1.0f) / pmatProj->_22;
v.z = -1.0f;
const D3DXMATRIX matView = d3d->matView;
const D3DXMATRIX _matWorld = matWorld;
D3DXMATRIX matRotateX, matRotateY, matRotateZ, matTranslate;
D3DXMatrixTranslation(&matTranslate, 0, 0, 0);
D3DXMatrixRotationX(&matRotateX, 0);
D3DXMatrixRotationY(&matRotateY, 0);
D3DXMatrixRotationZ(&matRotateZ, 0);
matWorld = (matRotateX * matRotateY * matRotateZ) * matTranslate;
D3DXMATRIX mWorldView = matWorld * matView;
D3DXMATRIX m;
D3DXMatrixInverse(&m, NULL, &mWorldView);
vPickRayDir.x = v.x * m._11 + v.y * m._21 + v.z * m._31;
vPickRayDir.y = v.x * m._12 + v.y * m._22 + v.z * m._32;
vPickRayDir.z = v.x * m._13 + v.y * m._23 + v.z * m._33;
vPickRayOrig.x = m._41;
vPickRayOrig.y = m._42;
vPickRayOrig.z = m._43;
ModelInstance mi(GetDocument()->GetModel(m_wndModels.m_nodeTree.selectID));
mi.Translation.at(0).x = (1 * vPickRayDir.x + vPickRayOrig.x);
mi.Translation.at(0).y = (1 * vPickRayDir.y + vPickRayOrig.y);
mi.Translation.at(0).z = (1 * vPickRayDir.z + vPickRayOrig.z);
mi.Translation.at(0).z *= -1;
为什么会有偏移量?如何删除它?
P.S。 Here's a link the complied program。单击“打开”并打开随附的fbx文件。打开后,双击模型列表中的模型,然后单击要插入的视图空间。