Game Development Reference
In-Depth Information
The NDC is a value that remains the same and is independent of the view size. It ranges
from -1 to 1. Basically, the objective is that the coordinate that we want to convert to the
world coordinate has to first be independent of the canvas size. Hence, we divide it by
the height and width of the canvas. The conversion is shown in the following diagram:
Device Coordinate
Normalized Device Coordinate
World Coordinate
Unproject the vector
Our NDC vector's coordinates look something like this ( x1 , y1 , 1). This means that it
is one unit ahead of the eye vector ( x1 , y1 , 0). Basically in this case, the eye vector is
the click coordinate. Now, this NDC has to be converted to world coordinates.
Throughout the topic, we have discussed this formula:
v' = P * M * v
Each vertex is rendered after multiplying it with the ModelView matrix and then
with the projection matrix of the camera. The final values vector ( v' ) has three
components ( x , y , z ), where x and y denote the location on the screen and z is for
depth test. Now, we have a device coordinate, v' , and we want to convert it to the
world coordinate. So our formula should be as follows:
v = M -1 * P -1 * v'
This means that if we multiply the NDC with the inverse projection matrix, then
with the inverse ModelView matrix of the camera, we convert the NDC to the
world coordinate.
Our projection matrix and ModelView matrix are 4 x 4 matrices and our vector is a
1 x 3 vector. To multiply these matrices with our vector, we have to convert it to a
1 x 4 vector. Hence, we add a w component, 1. Now, our vec3 vector becomes vec4
( x1 , y1 , 1, 1). After multiplication with the matrices, we get the w component which
might not be equal to 1. We need to perform a perspective divide to normalize our
coordinates. Hence, perspective divide is division of all four coordinates ( x , y , z , w )
by w in order to normalize the w value to 1.
So to unproject the vector and convert it from screen coordinates to world
coordinates, we need to perform the following steps:
1.
Multiply the vector by the inverse camera projection matrix.
2.
Multiply the vector by the inverse camera ModelView matrix.
3.
Perform perspective divide to normalize the w component.
 
Search WWH ::




Custom Search