Game Development Reference
In-Depth Information
Assuming the X and Y members of the viewport are 0, which is usually
the case, we can simply go a step further and get:
2
s
p
1
x
x
Width
2
s
y
p
1
y
Height
p
1
z
By definition, the projection window coincides with the z = 1 plane;
therefore p z =1.
However, we are not done. The projection matrix scales the points
on the projection window to simulate different fields of views. To
reclaim the point values before this scaling, we must transform the
points by the inverse of the scaling operations. Let P be the projection
matrix, and since entries P 00 and P 11 of a transformation matrix scale
the x and y coordinates of a point, we get:
2
x
1
p
1
x
viewportWi
dth
P
00
2
y
1
p
1
y
viewPortHe
ight
P
11
p
1
z
15.2 Computing the Picking Ray
Recall that a ray can be represented by the parametric equation p ( t )=
p 0 + t u , where p 0 is the origin of the ray describing its position and u
is a vector describing its direction.
From Figure 15.2 we can see that the origin of the ray is also the
origin of the view space, so p 0 = (0, 0, 0). If p is the point on the projec-
tion window to shoot the ray through, the direction vector u is given
by: u = p - p 0 =( p x , p y , 1) - (0, 0, 0) = p .
The following method computes the picking ray in view space
given the x and y coordinates of the clicked point from screen space:
d3d::Ray CalcPickingRay(int x, int y)
{
float px = 0.0f;
float py = 0.0f;
D3DVIEWPORT9 vp;
Device->GetViewport(&vp);
Search WWH ::




Custom Search