// get the current transform matrices
D3DXMATRIX matProjection, matView, matWorld, matInverse;
ENGINE.GetDevice()->GetDevice()->GetTransform(D3DTS_PROJECTION, &matProjection);
ENGINE.GetDevice()->GetDevice()->GetTransform(D3DTS_VIEW, &matView);
ENGINE.GetDevice()->GetDevice()->GetTransform(D3DTS_WORLD, &matWorld);
// use the mouse coordinates to get the mouse angle
float xAngle = (((2.0f * ENGINE.GetInputManager()->GetMousePos()->x) / WINDOW_WIDTH) - 1.0f) / matProjection(0, 0);
float yAngle = (((-2.0f * ENGINE.GetInputManager()->GetMousePos()->y) / WINDOW_HEIGHT) + 1.0f) / matProjection(1, 1);
D3DXVECTOR3 origin, direction;
origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
direction = D3DXVECTOR3(xAngle, yAngle, 1.0f);
// find the inverse matrix
D3DXMatrixInverse(&matInverse, NULL, &(matWorld * matView));
// convert origin and direction into model space
D3DXVec3TransformCoord(&origin, &origin, &matInverse);
D3DXVec3TransformNormal(&direction, &direction, &matInverse);
D3DXVec3Normalize(&direction, &direction);
// detect picking
BOOL hit;
std :: list<CEntity*> :: iterator iter = MAINMANAGER.GetLegoObjectManager()->GetObjList().begin();
CResource* g_pResource = NULL;
while(iter != MAINMANAGER.GetLegoObjectManager()->GetObjList().end())
{
g_pResource = static_cast<CEntity*>(*iter)->GetResource();
D3DXIntersect( static_cast<PassiveMesh*>(g_pResource)->GetMesh(), &origin, &direction, &hit, NULL, NULL, NULL, NULL, NULL, NULL);
if(hit)
{
ENGINE.GetDevice()->GetDevice()->SetRenderState(D3DRS_LIGHTING, FALSE);
break;
}
else
{
ENGINE.GetDevice()->GetDevice()->SetRenderState(D3DRS_LIGHTING, TRUE);
}
++iter;
}
这是我挑选的示例源代码。我有个问题。
如果我创建三个 3D 对象。所以挑选第三个对象。第一个,第二个对象不起作用。
如果我只创建一个对象。采摘效果很好!
如果我创建两个对象。拾取不是第一个对象,而是第二个对象。
只有创建最后一个对象才能很好地工作。
我不明白为什么会出现这个问题。
请给我一些建议。