Game Development Reference
In-Depth Information
D3DXMATRIX proj;
Device->GetTransform(D3DTS_PROJECTION, &proj);
px = ((( 2.0f*x) / vp.Width) - 1.0f) / proj(0, 0);
py = (((-2.0f*y) / vp.Height) + 1.0f) / proj(1, 1);
d3d::Ray ray;
ray._origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
ray._direction = D3DXVECTOR3(px, py, 1.0f);
return ray;
}
where Ray is defined as:
struct Ray
{
D3DXVECTOR3 _origin;
D3DXVECTOR3 _direction;
};
We update the d3dUtility.h file and d3d namespace by adding Ray to it.
15.3 Transforming Rays
The picking ray we computed in the previous section is described in
view space. In order to perform a ray-object intersection test, the ray
and the objects must be in the same coordinate system. Rather than
transform all the objects into view space, it is often easier to transform
the picking ray into world space or even an object's local space.
We can transform a ray r ( t )= p 0 + t u by transforming its origin p 0
and direction u by a transformation matrix. Note that the origin is
transformed as a point and the direction is treated as a vector. The pick-
ing sample for this chapter implements the following function to
transform a ray:
void TransformRay(d3d::Ray* ray, D3DXMATRIX* T)
{
// transform the ray's origin,w=1.
D3DXVec3TransformCoord(
&ray->_origin,
&ray->_origin,
T);
// transform the ray's direction,w=0.
D3DXVec3TransformNormal(
&ray->_direction,
&ray->_direction,
T);
// normalize the direction
D3DXVec3Normalize(&ray->_direction, &ray->_direction);
}
Search WWH ::




Custom Search