Android draws triangles on glsurfaceview based on OpenGL and uses projection and camera view methods

This paper gives an example of how Android draws triangles on glsurfaceview based on OpenGL and uses projection and camera view. Share with you for your reference, as follows:

Define triangle

OpenGL allows us to define objects using three-dimensional coordinates. Before drawing a triangle, we need to define the coordinates of its points. We usually use arrays to store the coordinates of each vertex.

OpenGL es defaults to [0,0] (x, y, z) in the center of glsurfaceview, [1,1,0] in the upper right corner and [- 1, - 1,0] in the lower left corner.

Draw triangle

Before drawing triangles, we must tell OpenGL that we are using vertex arrays. Then we use the drawing function to draw the triangle.

Experimental steps:

1. Add a new class triangle

The code is as follows:

2. Add the member privatetrangle mtriangle in the myglrenderer class and initialize it in the constructor.

The code is as follows:

3. Add the glenableclientstate() method to the onsurfacecreated() function of myglrenderer class to enable the vertex array.

The code is as follows:

4. Add triangle drawing method at the end of ondrawframe() function of myglrenderer class.

The code is as follows:

In this way, we have completed the drawing of a plane triangle. However, we found that this triangle is not an equilateral triangle as we defined. This is because OpenGL always assumes that our screen is a square, so when drawing, the final graphics will be stretched with the different length width ratio of the screen. In order to get the correct display, we need to project the graphics to the correct position. We will implement this function in the next section.

The screen of Android devices is usually not square, and OpenGL always projects the square coordinate system onto this device by default, which makes the graphics unable to be displayed at real scale. To solve this problem, we can use OpenGL projection mode and camera view to convert the coordinates of graphics to adapt to different device displays.

Experimental steps:

1. Modify onsurfacecreated() function of myglrenderer class to enable gl10.gl_ Project mode, calculate the aspect ratio of the screen and use this ratio to convert the coordinates of the object.

The code is as follows:

2. Modify the ondrawframe() method of myglrenderer, enable the modelview mode, and use Glu. Glulookat() to set the viewpoint.

The code is as follows:

In this way, the scale of the graphics we draw is always correct and will no longer be stretched and deformed under the influence of the equipment.

More readers interested in Android related content can view the special topics of this site: summary of Android graphics and image processing skills, introduction and advanced tutorial of Android development, summary of Android debugging skills and solutions to common problems, summary of Android multimedia operation skills (audio, video, recording, etc.), summary of Android basic component usage Summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>