Choose to draw the triangle as the first instance of OpenGL ES 2.0, because the dots, lines, and triangles are the graphical basis of the OpenGL ES World. No matter how complex the geometric objects, in the OpenGL ES of the world can be used to spell the triangle. The illustration of the Android OpenGL ES Triangle is detailed in the official Android documentation and is a triangle drawn in accordance with official document steps.
Steps
According to the instructions in the official documentation, the steps to draw a triangle using OpenGL ES 2.0 in Android are:
1. Set the version of OpenGL ES used in the Androidmanifest.xml file:
<!--tell the system this app requires OpenGL ES 2.0. -->
<uses-feature android:glesversion= "0x00020000" android:required= "true"/>
The version of 3.0 is 0x00030000,3.1 version for 0x00030001.
Note that the versions of Android that were mentioned in the previous blog support the OpenGL ES version, and the MINSDK for Android apps should not be smaller than the minimum version of the Android SDK that supports the OpenGL ES version.
2. There is no doubt that the display of triangles requires a carrier. Create an activity that shows triangles, using Glsurfaceview as the view to display triangles, the concrete rendering of graphics is done in render.
3. Implementation of Glsurfaceview render, in the render to complete the mapping of the triangle, the specific behavior are:
- Load vertex and slice shader
- Determine the coordinates and color data that you need to draw the graphic
- Create program objects, connect vertex and slice shaders, and Link program objects.
- Sets the view window (viewport).
- To pass the coordinate data color data into the OpenGL ES program
- Causes the contents of the color buffer to appear on the screen.
Concrete implementation
We set up the OpenGL ES version, create the entry activity and set the Glsurfaceview as a display carrier, it has entered our most important work.
First step
First, we need to write a simple vertex shader and a simple chip shader:
Vertex shader:
Attribute Vec4 vposition;
void Main () {
gl_position = vposition;
}
Slice-Element shader:
Precision Mediump float;
Uniform vec4 Vcolor;
void Main () {
gl_fragcolor = Vcolor;
}
Both Gl_position and Gl_fragcolor are shader built-in variables, which are fixed-point locations and piece-element color respectively.
Second Step
Then we determine the vertex coordinates and color of the graph we want to draw:
What we need to draw now is to draw a triangle in a three-dimensional space, and the triangle is of course three vertices. Because our triangle is just a flat graph, for convenience, we now do not set the camera (the camera in the back of the blog when used in the explanation), the triangle is to us to render. So we set the z coordinates of the three vertices to 0.
The previous blog also mentioned OpenGL ES coordinates mapped to the screen, the distance from the center of the screen to the left and right edges is 1.0, so ( -1.0,0,0) and (0,1.0,0) to the origin of the distance on the screen the result is not the same, the diagram below (left is the ideal state, On the right is the actual state):
So, in order to not exceed the screen, our coordinate data is set to:
Float trianglecoords[] = {
0.5f, 0.5f, 0.0f,//Top
-0.5f, -0.5f, 0.0f,//Bottom left
0.5f, -0.5f, 0.0f//Bo Ttom right
};
Color data, we set it to a single color:
Float color[] = {1.0f, 1.0f, 1.0f, 1.0f}; White
Third Step
Then we begin to implement our triangle drawing in the render. The render interface has three methods, namely, onsurfacecreated, onsurfacechanged, and Ondrawframe.
In the Onsurfacecreated method, we create program objects, connect vertex and slice shaders, and Link program objects.
Set the background to gray
Gles20.glclearcolor (0.5f,0.5f,0.5f,1.0f);
Apply for the underlying space
bytebuffer BB = bytebuffer.allocatedirect (
trianglecoords.length * 4);
Bb.order (Byteorder.nativeorder ());
The coordinate data is converted to Floatbuffer to pass to the OpenGL ES program
vertexbuffer = Bb.asfloatbuffer ();
Vertexbuffer.put (trianglecoords);
Vertexbuffer.position (0);
int vertexshader = Loadshader (Gles20.gl_vertex_shader,
vertexshadercode);
int fragmentshader = Loadshader (Gles20.gl_fragment_shader,
fragmentshadercode);
Create an empty opengles program
Mprogram = Gles20.glcreateprogram ();
Adding a vertex shader to the program
Gles20.glattachshader (Mprogram, vertexshader);
Adding a chip shader to the program
Gles20.glattachshader (Mprogram, fragmentshader);
Connect to the shader program
Gles20.gllinkprogram (Mprogram);
Fourth Step
To set the Settings view window in onsurfacechanged:
Gles20.glviewport (0,0,width,height);
Fifth Step
Finally, draw in the Ondrawframe:
Adding the program to the OPENGLES2.0 environment
Gles20.gluseprogram (mprogram);
Gets the vposition member handle of the vertex shader
mpositionhandle = gles20.glgetattriblocation (Mprogram, "vposition");
Handles
Gles20.glenablevertexattribarray (mpositionhandle) with triangle vertices enabled;
Prepare triangular coordinate data
gles20.glvertexattribpointer (Mpositionhandle, Coords_per_vertex,
gles20.gl_float, False,
vertexstride, vertexbuffer);
Gets the handle of the Vcolor member of the chip shader
mcolorhandle = gles20.glgetuniformlocation (Mprogram, "Vcolor");
Sets the color for drawing triangles
GLES20.GLUNIFORM4FV (mcolorhandle, 1, color, 0);
Draw triangular
gles20.gldrawarrays (gles20.gl_triangles, 0, vertexcount);
The handle
Gles20.gldisablevertexattribarray (mpositionhandle) of the vertex array is prohibited;
Final effect
Source Address
All the code is in one project, hosted on the GitHub--android Opengles 2.0 Series Blog Demo
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.