Draw a red triangle with OpenGL ES (1)

Source: Internet
Author: User

The following are all converted from the android game programming entry-level classic. For more information, see the source.

OpenGL ES converts the coordinates of the points projected to the near-cropping surface into the pixel coordinates of the frame buffer using the viewport. You can use part or all of the frame buffer in the following method:

Gl10.glviewport (int x, int y, int width, int height)

The X and Y coordinates indicate the lower left corner of the video interface in the frame buffer, and the width and height indicate the size of the view, in pixels. Usually, set X and Y to 0, width and height to the screen resolution, that is, full screen mode. Of course, you can also operate OpenGL ES so that this method only uses part of the frame buffer, and it automatically scales the rendering output to adapt to this part of the area.

Note: although it seems that this method sets the 2D Coordinate System for rendering, this is not actually the case. It only defines the frame buffer used by OpenGL ES to output the final image. 2d coordinate systems are defined by projection and Model-View matrices.

OpenGL ES records three matrices: projection matrix, Model-View matrix, and texture matrix. Before using a matrix, you must tell OpenGL ES which matrix to operate.

Gl10.glmatrixmode (INT Mode)

The mode parameter can be gl10.gl _ projection, gl10.gl _ modelview, or gl10.gl _ texture.

OpenGL ES provides a way to set an active matrix to an orthogonal projection matrix:

Gl10.glorthof (INT left, int right, int bottom, int top, int near, int far)

You can use your space imagination to imagine that the video cone with parallel projection has a box shape. The glorthof () parameter can be interpreted as specifying two corners of the cone box.

Because 2D image processing is required, you must specify the corner points (left, bottom, front) and (right, top, and back) to work in a pixel coordinate system, A 2D Coordinate System of 480*320 will be created as follows:

Gl. glorthof (0,480, 0,320, 1,-1 );

Although the cone is very thin, it is okay in 2D mode. The Visual Range of the coordinate system is from (0, 0, 1) to (480,320,-1 ). Any point in this box area is visible on the screen. These points are projected to the front of the box area, that is, the near cropping area. Regardless of the size of these projections, they are stretched to the viewport.

In order to use the cone completely and reliably, some buffers need to be allocated to its Z axis. The near and far parameters cannot be set to 0.

The following code uses the black color to clear the screen, sets the viewport to extend it to the entire frame buffer, and sets the projection matrix. Through these settings, the coordinate origin of the coordinate system is set in the lower left corner of the screen, and the Y axis is pointed up.

Gl. glclearcolor (0, 0, 0, 1 );

Gl. glclear (gl10.gl _ color_buffer_bit );

Gl. glviewport (0, 0, glgraphics. getwidth (), glgraphics. getheight ());

Gl. glmatrixmode (gl10.gl _ projection );

Gl. glloadidentity ();

Gl. glorthof (0,320, 0,480, 1,-1 );

In fact, most of the methods provided by OpenGL ES for operating the activity matrix are not directly set the activity matrix. In practice, these methods construct a temporary matrix based on the parameters they carry and multiply the matrix with the current one. The glorthof () method is no exception. Therefore, if glorthof () is called in each frame, the projection matrix is multiplied by itself every time it is called, which is quite bad. To avoid this problem, make sure that a clean unit matrix is available in a proper place before multiplying the projection matrix. A matrix is multiplied by a unit matrix, which remains unchanged. This is the purpose of calling glloadidentity.

Note that the coordinate system is changed from (0, 0, 1) to (320,480,-1)-this is a portrait rendering.

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.