D3D9 Picking

Started by
0 comments, last by Jason Z 11 years, 3 months ago

This is the code i am following

void detect_picking()
{
// get the current transform matrices
D3DXMATRIX matProjection, matView, matWorld, matInverse;
d3ddev->GetTransform(D3DTS_PROJECTION, &matProjection);
d3ddev->GetTransform(D3DTS_VIEW, &matView);
d3ddev->GetTransform(D3DTS_WORLD, &matWorld);

// use the mouse coordinates to get the mouse angle
GetCursorPos(&MousePos);
float xAngle = (((2.0f * MousePos.x) / SCREEN_WIDTH) - 1.0f) / matProjection(0, 0);
float yAngle = (((-2.0f * MousePos.y) / SCREEN_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;
D3DXIntersect(meshTeapot, &origin, &direction, &hit, NULL, NULL, NULL, NULL, NULL, NULL);
if(hit)
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
else
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
}

now this is the line i am trying to understand

D3DXMatrixInverse(&matInverse, NULL, &(matWorld * matView));

the inverse of matView goes to WorldSpace Cordinates and the inverse of matWorld go to the cordinates of the meshs local cordinates, can anyone tell me if i am right with this?

:)
Advertisement

That sounds correct - the code sample is finding the 'angles' in view space based on the projection parameters (I use quotes because they don't actually look like angles...), and then it converts from view space vector to object space. Then the intersection test is carried out on the object. That seems like a perfectly rational way to do picking.

This topic is closed to new replies.

Advertisement