This demo shows how to to select objects from screen coordinates, accounting for the viewport scaling and transformation through the world, view, and projection matrices. This improves upon the the Pick sample provided with the DirectX SDK, which does not allow for world transformations to be applied on a per-object basis.
The application and source code also demonstrate a method for setting up hierarchies of objects, with parent / child transformation dependency.
Ray Picking with Object Transformations
In the SDK's Pick sample, a ray is calculated that passes through the frustum under the selected pixel, in world space. This ray is then tested for intersection against the the geometry of a mesh rendered to the screen, to determine if and where the user has clicked on the object.
That will work fine as long as the world transformation is an identity matrix. However, if a world transformation is applied to the objects in the scene, then we can no longer compare a world space ray directly against the mesh in model space. There are basically two ways to handle this:
In the sample provided on this page, the second method is utilized. Once the ray is back-transformed into model space, then intersection testing can be performed using the object mesh without per-vertex transformation.
Visitors Since 1/1/2000: