Android opengles2.0 drawing triangles (II)

Drawing triangles is selected as the first instance of OpenGL es 2.0 because, as mentioned earlier, points, lines and triangles are the graphic basis of the OpenGL es world. No matter how complex geometric objects are, they can be assembled with triangles in the world of OpenGL es. For the drawing of Android OpenGL es triangles, there are detailed instructions and steps in the Android official document. This example is also a triangle drawn according to the steps in the official document.

step

According to the instructions in the official documents, the steps of drawing triangles using OpenGL es 2.0 in Android are as follows:

1. Set the version of OpenGL es used in the androidmanifest.xml file:

The version of 3.0 is 0x00030000, and the version of 3.1 is 0x00030001. It should be noted that the support of various Android versions mentioned in the previous blog for OpenGL es should not be less than the minimum Android SDK version supporting OpenGL es.

2. There is no doubt that displaying triangles requires a carrier. Create an activity to display triangles, and use glsurfaceview as the view to display triangles. The specific rendering of graphics is completed in render.

3. Implement the render of glsurfaceview, and complete the rendering of triangles in render. The specific behaviors are as follows:

Concrete implementation

After setting the OpenGL es version, creating the portal activity and setting glsurfaceview as the display carrier, we enter our main work.

First step

First, we need to write a simple vertex shader and a simple slice shader: vertex shader:

Slice shader:

gl_ Position and GL_ Fragcolor is a built-in variable of shader, which is fixed point position and slice color respectively.

Step 2

Then, we determine the vertex coordinates and colors of the graph we want to draw: what we need to draw now is to draw a triangle in a three-dimensional space. Of course, the triangle is three vertices. Because our triangle is just a plane figure, for convenience, we don't set the camera (the camera will be explained in the later blog), and the triangle is presented to us. So we set the Z coordinates of the three vertices to 0. It was also mentioned in the previous blog that OpenGL es coordinates are mapped to the screen. The distance from the vertical center of the screen to the upper, lower, left and right edges is 1.0, so the results of (- 1.0,0) and (0,1.0,0) distances to the origin are different on the screen. The diagram is as follows (the left is the ideal state and the right is the actual state):

Therefore, in order not to exceed the screen, our coordinate data is set as:

Color data, we set it to a single color:

Step 3

Then we began to implement our triangle drawing in render. The render interface has three methods: onsurfacecreated, onsurfacechanged, and ondrawframe. In the onsurfacecreated method, we create a program object, connect vertices and slice shaders, and link the program object.

Step 4

To set the view window in onsurfacechanged:

Step 5

Finally, draw in ondrawframe:

Final effect

Source address

All the code is in one project and hosted on GitHub - the demo of Android OpenGLES 2.0 series blog

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>