Get the current viewpoint perspective matrix.
This function can be used to request the viewpoint perspective matrix. It will be computed only once, unless the viewpoint changes and forces the function to recompute a new frustum.
We compute the viewpoint perspective matrix ourselves (instead of using the gluPerspective() function which would do it for us) because we need it in order to sort the shapes in Z order from back to front. That is, we need the inverse matrix, but it is easier to compute the projection first and then compute its inverse.
Note since we already have this matrix available in all cases, you can get it to setup the projection matrix in OpenGL.
The matrix is defined as follow:
| f/aspect 0 0 0 | | 0 f 0 0 | | 0 0 p q | | 0 0 -1 0 |
with p = (zfar + znear) / (znear - zfar) q = 2 * zfar * znear / (znear - zfar) and f = 1 / tan(fovy / 2)
The fovy is the vertical field of view (when the Y coordinates go from the bottom of the screen to the top). far and near are computed internally. The aspect ratio is defined as:
screen width / screen height
For the aspect ratio to be computed properly, you need to setup the screen width and height with SetScreenSizes().
- The viewpoint can also be positioned. This is applied to the viewpoint position matrix instead. See the GetViewpointPositionMatrix() to get that matrix. The objects in our view will have their matrix multiplied by this viewpoint position matrix so they are placed properly.
The aspect is computed as width / height. But X3D gives us a field of view which is determined using the smallest of width or height. Thus in the code, there are two cases to defined entry 0,0 and 1,1 of the matrix depending of the width and height of the output screen.
- A matrix with the viewpoint perspective matrix
- See also: