Sign in to follow this  

D3D9 Picking

Recommended Posts

This is the code i am following


[code]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
    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);
        d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
        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?

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this