Android graphics --- OpenGL (3)

Source: Internet
Author: User

Coordinate ing of the Drawing Object

One of the basic problems in displaying images on Android devices is that their screens may be of various sizes and shapes. OpenGL assumes a Square area, a uniform coordinate area, and by default, the coordinates can be perfectly drawn on some Square screens like in a square area.

 

Figure 1. The default OpenGL coordinate system (left) is mapped to a typical Android device screen (right ).

The illustration above demonstrates how to map the central Coordinate System of OpenGL on the left to the actual coordinates of the horizontal screen device on the right. Using the GpenGL projection mode can solve this problem, and the Camera View will change coordinates so that the graphic object can display the image according to the correct proportion.

To apply projection and photo views, we need to create a projection matrix and a photo view matrix, and apply them to the rendering pipeline of OpenGL. The projection matrix recalculates the coordinates of the image so that the image can be correctly mapped to the screen of the Android device. The photo view matrix creates a transformation of the object to be presented from a special perspective.

Projection and photography views in OpenGL ES 1.0

In ES 1.0 API, you need to create projection and photo views respectively and add them to the OpenGL environment.

1. Projection Matrix-to recalculate the coordinates of graphical objects so that they can be drawn according to the correct ratio, you must create a projection matrix using the geometric shape of the device screen. The following sample code demonstrates how to edit GLSurfaceView. renderer's onSurfaceChanged () method is used to create a projection matrix based on the screen appearance ratio and apply this projection matrix to the OpenGL rendering environment:

Public void onSurfaceChanged (GL10 gl, int width, int height ){

Gl. glViewport (0, 0, width, height );

 

// Make adjustments for screen ratio

Float ratio = (float) width/height;

Gl. glMatrixMode (GL10.GL _ PROJECTION); // set matrix to projection mode

Gl. glLoadIdentity (); // reset the matrix to its default state

Gl. glFrustumf (-ratio, ratio,-1, 1, 3, 7); // apply the projection matrix

}

2. Photographic transformation matrix-once a coordinate system is adjusted using a projection matrix, a photographic view must be used. The following code shows how to use the GLSurfaceView. Renderer onDrawFrame () method to implement a model view, and use the GLU. gluLookAt () tool to create a view transformation that simulates the camera's perspective:

Publicvoid onDrawFrame (GL10 gl ){... // Set GL_MODELVIEW transformation mode gl. glMatrixMode (GL10.GL _ MODELVIEW); gl. glLoadIdentity (); // reset the matrix to its default state // When using GL_MODELVIEW, you must set the camera view GLU. gluLookAt (gl, 0, 0,-5, 0f, 0f, 0f, 0f, 1.0f, 0.0f );...} for a complete example of how to use OpenGL ES 1.0 projection and photographic views, see OpenGL ES 1.0 Guide (www.2cto.com)

By FireOfStar
 

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.