Evo i koda za "buduca pokoljenja". Davno sam ovo pisao tako da mi sad i nije bas sve jasno, ali radi (verovatno je opisano u tom pdf-u, zvuci mi jako poznato)
Code:
// informations about the intersection between picking ray and mesh
struct INTERSECTION {
// face in the mesh
DWORD dwFace;
// distance
float fDist;
// barycentric coordinates
float fBary1, fBary2;
// position of intersection
D3DXVECTOR3 vPos;
};
bool CMyObject::IsClicked(int nMouseX, int nMouseY, int nWidth, int nHeight, INTERSECTION* pIntersection) {
// transform the screen into the world coordinates
D3DXMATRIX mxTemp;
m_pD3DDevice->GetTransform(D3DTS_PROJECTION, &mxTemp);
float fNear = - mxTemp._43 / mxTemp._33;
float fNewX = (2.0f * nMouseX / nWidth - 1) / mxTemp._11;
float fNewY = -(2.0f * nMouseY / nHeight - 1) / mxTemp._22;
// calculate the inverse of the object's world transformation matrix
D3DXMATRIX mxInvWorld;
D3DXMatrixInverse(&mxInvWorld, NULL, &m_mxWorld);
// calculate the inverse of the view transformation matrix
D3DXMATRIX mxInvView;
m_pD3DDevice->GetTransform(D3DTS_VIEW, &mxTemp);
memcpy(&mxInvView, &mxTemp, sizeof(D3DXMATRIX));
D3DXMatrixInverse(&mxInvView, NULL, &mxInvView);
// calculate the ray direction vector
D3DXVECTOR3 vRayDir;
vRayDir.x = fNewX * mxInvView._11 + fNewY * mxInvView._21 + fNear * mxInvView._31;
vRayDir.y = fNewX * mxInvView._12 + fNewY * mxInvView._22 + fNear * mxInvView._32;
vRayDir.z = fNewX * mxInvView._13 + fNewY * mxInvView._23 + fNear * mxInvView._33;
D3DXVec3Normalize(&vRayDir, &vRayDir);
// we must rememeber this direction, before transforming it into object's space
D3DXVECTOR3 vOldDir;
memcpy(&vOldDir, &vRayDir, sizeof(D3DXVECTOR3));
// transform the direction vector
D3DXVec3TransformNormal(&vRayDir, &vRayDir, &mxInvWorld);
// calculate the ray position vector
D3DXVECTOR3 vRayPos = D3DXVECTOR3(mxInvView._41, mxInvView._42, mxInvView._43);
// we must remember this vector, before transforming it into object's space
pIntersection->vPos = vRayPos;
// transform the position vector (transform coordinates of this point)
D3DXVec3TransformCoord(&vRayPos, &vRayPos, &mxInvWorld);
BOOL bHit;
// check to see if the ray intersects the mesh
D3DXIntersect(m_pMesh, &vRayPos, &vRayDir, &bHit, &pIntersection->dwFace, &pIntersection->fBary1,
&pIntersection->fBary2, &pIntersection->fDist, NULL, NULL);
if (bHit) {
D3DXVec3Scale(&vOldDir, &vOldDir, pIntersection->fDist);
D3DXVec3Add(&pIntersection->vPos, &pIntersection->vPos, &vOldDir);
}
return bHit;
}