OpenGL ES entry (3) Projection -- Translated from beginning Android games

Source: Internet
Author: User
Tags android games

Projection

The 2D plotting of OpenGL ES is mapped to the screen through "projection. We have already mentioned that OpenGL ES is mainly related to triangles. A triangle has three vertices in 3D space (remember that OpenGL ES performs operations on graphics in 3D space ). To draw such a triangle in framebuffer, OpenGL ES needs to know the coordinates of the three vertices of the triangle in 3D space. The coordinates are provided by the system based on pixels by framebuffer. When OpenGL
After es knows the coordinates of the three vertices of a triangle, it can easily draw the pixels contained in the triangle into framebuffer. By ing the coordinate of points in a 3D space to a 2-dimensional space, the canvas can be used to connect two points, and we can complete simple operations on OpenGL ES.

 

Usually there are two different projection modes in 3D drawing:

Parallel, or orthographic, projection (parallel, positive projection ):If you have used CAD, you may already know the mode. Parallel projection does not care about how far the camera (CAMERA) is in the object. In the final image, the object size is always the same as its actual size. This projection mode is usually used in open gl2d plotting. (2d actually uses OpenGL to project objects in 3D space to the screen ).

Perspective projections (Perspective Projection ):This projection mode is just like our eyes looking at things. Objects farther away from our retina look smaller. This projection mode is usually used in 3D drawing of OpenGL ES.

 

No matter which projection mode is used, a thing called a projection plane is required. The projection plane is very similar to our retina (in fact, it is similar to the projection plane ). The light shining on the projection plane finally projects the object to the plane to form the final picture. The scope of the plane in mathematics is infinite, but the scope of our retina is limited. The "retina" of OpenGL is equal to the rectangle in the head of the Face Image in Figure 7-1 (that is, the four pyramid chart without a sharp angle is shown in the previous section. OpenGL ES projects an object to this part of the view's head. This is the so-called cropping plane. The cropping plane has its own coordinate system. Figure 7-
2. It displays the cropping plane superimposed on the coordinate system from the camera's perspective.

Figure7-2. Crop a plane (also called a projection plane) and its coordinate system.

 

Note that the coordinate system of the plane is not fixed. We can operate on it to make our projection plane work in any coordinate system (e.g ., we can specify that the origin of OpenGL ES is in the lower left corner, and the X axis has 480 units, and the Y axis has 320 units. PS. Note that the unit is not pixel, and one unit may have multiple pixels ). Sounds familiar? Yes, OpenGL ES allows us to specify any coordinate system we want on the projection.

When we specify a horizontal view to intercept the body, OpenGL ES projects a ray of light and the light passes through the projection plane. The difference between parallel projection and projection lies in the direction in which these rays are projected. Figure 7-3 shows the differences between the two.

 

Figure 7-3. Perspective Projection (left) and line projection (right)

 

The light from the Perspective Projection starts from a triangle and passes through camera (or our eyes. PS: the projection plane is in the middle of 2 ). The farther the object is from the projection plane, the smaller it appears on the projection plane. When we use parallel projection, the light is perpendicular to our projection plane. In this case, regardless of the distance from the projection plane, the objects projected on the projection plane will keep the original size.

 

In OpenGL ES, the projection plane is strangely called a cropping plane. In the field of view, all planes have similar names. The plane farthest from camera is called the far-cropping plane. Others are called left, and top, and bottom cropping planes. Any objects not in these planes or after these planes will not be rendered. If only one part of the object is in these planes, the object will be cropped through the plane, meaning that the part of the object that is not in the front of the view will be removed. This is the origin of the crop plane name.

 

You may be worried about why the horizontal part of the view projected in Figure 7-3 is a rectangle. It reflects how we define the projection cropping plane. In the case of projection, the top, bottom, left, and right cropping planes are not vertical and near-cropping planes and far-cropping planes (see figure 7-3, it only shows the Left and Right cropping planes.) in this case, the normal projection is perpendicular to the far and near cropping planes, they will tell OpenGL ES to ignore the distance of objects from the camera and render them as the actual size of objects.

 

PS ~ : If this is helpful to you, leave a footprint here ~ Give me some motivation. You can also give me some suggestions ^. ^

 

Attached Original:

 

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
Framebuffer. Once it knows those three corner-point coordinates, it can simply draw
Pixels in the framebuffer that are inside the triangle. We cocould 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
CAD application you might already know about these. A parallel
Projection doesn't care how far an object is away from the camera;
Object will always have the same size in the final image. This type
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
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 opengles "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. 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.

 

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 cocould 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
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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.