## Projections

This 2D mapping is done via something called projection. We already mentioned that OpenGL ES is mainly concerned with triangles. A single triangle has three points defined in 3D space. To render such a triangle to the framebuffer, OpenGL ES needs to know the coordinates of these 3D points within the pixel-based coordinate system of the framebuffer. Once it knows those three corner-point coordinates, it can simply draw the pixels in the framebuffer that are inside the triangle. We could even write our own little OpenGL ES implementation by projecting 3D points to 2D, and simply draw lines between them via the Canvas.

There are two kinds of projections that are commonly used in 3D graphics. :

Parallel, or orthographic, projection: If you have ever played with a CAD application you might already know about these. A parallel projection doesn't care how far an object is away from the camera; the object will always have the same size in the final image. This type of projection is typically used for rendering 2D graphics in OpenGL ES.

Perspective projections: These are what we are used to when using our eyes. Objects further away from us will appear smaller on our retina. This type of projection is typically used when we do 3D graphics with OpenGL ES.

In both cases we need something called a projection plane. This is nearly exactly the same as the retina of our eyes. It's where the light is actually registered to form the final image. While a mathematical plane is infinite, our retina is limited in area. Our OpenGL

ES "retina" is equal to the rectangle at the top of the view frustum in Figure 7-1. This part of the view frustum is where OpenGL ES will project the points to. It is called the near clipping plane and has its own little 2D coordinate system. Figure 7-2 shows that near clipping plane again, from the point of view of the camera, with the coordinate system superimposed.

I \ | ||

(-1,1) |
<1.1)1 | |

I | ||

1 Q | ||

(-1,-1; |
(1.-1), |

Figure 7-2. The near clipping plane (also known as the projection plane) and its coordinate system.

Figure 7-2. The near clipping plane (also known as the projection plane) and its coordinate system.

Note that the coordinate system is by no means fixed. We can manipulate it so that we can work in any projected coordinate system we like (e.g., we could instruct OpenGL ESto let the origin be in the bottom-left corner, and let the visible area of the "retina" be 480 units on the x-axis and 320 units on the y-axis). Sounds familiar? Yes, OpenGL ES allows us to specify any coordinate system we want for the projected points.

Once we specify our view frustum, OpenGL ES then takes each point of a triangle and shoots a ray from it through the projection plane. The difference between a parallel and a perspective projection is how the direction of those rays is constructed. Figure 7-3 shows the difference between the two, viewed from above.

A perspective projection shoots the rays from the triangle points through the camera (or eye, in this case). Objects further away will thus appear smaller on the projection plane. When we use a parallel projection, the rays are shot perpendicular to the projection plane. In this case an object will keep its size on the projection plane no matter how far away it is.

Our projection plane is called a near clipping plane in OpenGL ES lingo, as pointed out earlier. All of the sides of the view frustum have similar names. The one furthest away from the camera is called the far clipping plane. The others are called the left, right, top, and bottom clipping planes. Anything outside or behind those planes will not be rendered. Objects that are partially within the view frustum will be clipped from these planes, meaning that the parts outside the view frustum get cut away. That's where the name clipping plane comes from.

You might be wondering why the view frustum of the parallel projection case in Figure 7-3 is rectangular. It turns out that the projection is actually governed by how we define our clipping planes. In the case of a perspective projection, the left, right, top, and bottom clipping planes are not perpendicular to the near and far planes (see Figure 7-3, which only shows the left and right clipping planes. In the case of the parallel projection, these planes are perpendicular, which tells OpenGL ES to render everything at the same size no matter how far away it is from the camera.

## Post a comment