The support for OpenGl on Android is seamless, so there are a lot of games with such realistic 3D effects, and GLSurfaceView is also useful in some Camera processes. This article records OpenGL's entry-level example on Android to draw a triangle and a square. Despite its simple functionality, I have been playing around for several nights, and a lot of online articles have problems with the code.
First file: MainActivity. java
package com.example.learnopengl1;import android.opengl.GLSurfaceView;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);GLSurfaceView glSurfaceView = new GLSurfaceView(this);glSurfaceView.setRenderer(new OpenGLRender());setContentView(glSurfaceView);//setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
The second file: BufferUtil. java is responsible for converting the array into a buffer
Package com. example. learnopengl1; import java. nio. byteBuffer; import java. nio. byteOrder; import java. nio. floatBuffer; import java. nio. intBuffer; public class BufferUtil {public static FloatBuffer mBuffer; public static FloatBuffer floatToBuffer (float [] a) {// initialize the buffer first. The array length is * 4, because a float occupies 4 bytes ByteBuffer mbb = ByteBuffer. allocateDirect (. length * 4); // nativeOrdermbb is used for Array sorting. order (ByteOrder. nativeOrder (); mBuffer = mbb. asFloatBuffer (); mBuffer. put (a); mBuffer. position (0); return mBuffer;} public static IntBuffer intToBuffer (int [] a) {IntBuffer intBuffer; // initialize the buffer first. The length of the array is * 4, because a float occupies 4 bytes ByteBuffer mbb = ByteBuffer. allocateDirect (. length * 4); // nativeOrdermbb is used for Array sorting. order (ByteOrder. nativeOrder (); intBuffer = mbb. asIntBuffer (); intBuffer. put (a); intBuffer. position (0); return intBuffer ;}}
Third file: OpenGLRender. java, which is the core file and is responsible for drawing related GLSurfaceView objects.
Package com. example. learnopengl1; import java. nio. floatBuffer; import javax. microedition. khronos. egl. EGLConfig; import javax. microedition. khronos. opengles. GL10; import android. opengl. GLSurfaceView. renderer; public class OpenGLRender implements Renderer {private float [] mTriangleArray = {0f, 1f, 0f,-1f,-1f, 0f, 1f,-1f, 0f }; private FloatBuffer mTriangleBuffer; private float [] mColorArray = {1f, 0f, 0f, 1f, // red 0f, 1f, 0f, 1f, // green 0f, 0f, 1f, 1f // blue}; private FloatBuffer mColorBuffer; // four vertices of the square private FloatBuffer quateBuffer; private float [] mQuateArray = {-1f,-1f, 0f, 1f,-1f, 0f,-1f, 1f, 0f, 1f, 1f, 0f,}; @ Overridepublic void onDrawFrame (GL10 gl) {// TODO Auto-generated method stubgl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT); // use an array as the color gl. glColorPointer (4, GL10.GL _ FLOAT, 0, mColorBuffer); // draw a small triangle gl. glLoadIdentity (); gl. glTranslatef (-1.5f, 0.0f,-6.0f); gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, mTriangleBuffer); // The array points to the triangle vertex buffergl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3); // gl. glDisableClientState (GL10.GL _ COLOR_ARRAY); gl. glFinish (); // draw a square gl. glLoadIdentity (); gl. glTranslatef (1.5f, 0.0f,-6.0f); // gl. glColor4f (1.0f, 0.0f, 0.0f, 1.0f); gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, quateBuffer); gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 0, 4); gl. glFinish () ;}@ Overridepublic void onSurfaceChanged (GL10 gl, int w, int h) {// TODO Auto-generated method stubgl. glViewport (0, 0, w, h); float ratio = (float) w/h; gl. glMatrixMode (GL10.GL _ PROJECTION); gl. glLoadIdentity (); gl. glFrustumf (-ratio, ratio,-1, 1, 1, 10); gl. glMatrixMode (GL10.GL _ MODELVIEW); gl. glLoadIdentity () ;}@ Overridepublic void onSurfaceCreated (GL10 gl, EGLConfig config) {// TODO Auto-generated method stubgl. glShadeModel (GL10.GL _ SMOOTH); gl. glClearColor (1.0f, 1.0f, 1.0f, 0f); gl. glClearDepthf (1.0f); gl. glEnable (GL10.GL _ DEPTH_TEST); gl. glDepthFunc (GL10.GL _ LEQUAL); gl. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ NICEST); gl. glableclientstate (GL10.GL _ VERTEX_ARRAY); gl. glableclientstate (GL10.GL _ COLOR_ARRAY); mTriangleBuffer = BufferUtil. floatToBuffer (mTriangleArray); mColorBuffer = BufferUtil. floatToBuffer (mColorArray); quateBuffer = BufferUtil. floatToBuffer (mQuateArray );}}
Development highlights:
1. GLSurfaceView can be directly new or placed in the layout. This example uses the first method.
2. A GLSurfaceView must have a matching Renderer. This Renderer is an interface with three functions. This is similar to Surfaceview. In particular, onDrawFrame () can be analogous to the onDraw () function of View in Android.
3. Use the following code to draw a triangle in the onDrawFrame () function:
// Draw a small triangle
Gl. glLoadIdentity ();
Gl. glTranslatef (-1.5f, 0.0f,-6.0f );
Gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, mTriangleBuffer); // The array points to the triangle vertex buffer.
Gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3 );
// Gl. glDisableClientState (GL10.GL _ COLOR_ARRAY );
Gl. glFinish ();
Note that,In some tutorials, it indicates that gl. glDisableClientState (GL10.GL _ VERTEX_ARRAY) is required after painting; clearing the set vertex is an error!Once this sentence is called, nothing can be drawn !!!
Then draw a square:
// Draw a square
Gl. glLoadIdentity ();
Gl. glTranslatef (1.5f, 0.0f,-6.0f );
// Gl. glColor4f (1.0f, 0.0f, 0.0f, 1.0f );
Gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, quateBuffer );
Gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 0, 4 );
Gl. glFinish ();
Note that Color: gl. glColorPointer (4, GL10.GL _ FLOAT, 0, mColorBuffer) has been loaded before the triangle is drawn );
If you call gl. glDisableClientState (GL10.GL _ COLOR_ARRAY) after drawing, you can see that the triangle is flashing and the square is invisible.. This is also a misunderstanding! If gl. glColor4f (1.0f, 0.0f, 0.0f, 1.0f) is used to specify the color, the triangle and square are drawn based on the color. All in all, this onDrawFrame () is very similar to the onDraw () of the View. It is painted in onDraw without the paint color. Or after the painting, you can set the color to transparent, and the result is certainly invisible. I don't understand why the following must be taken after a small triangle is drawn in so many Tutorials:
Gl. glDisableClientState (GL10.GL _ VERTEX_ARRAY );
Gl. glDisableClientState (GL10.GL _ COLOR_ARRAY );
These two bugs !!!
The process involved in the Code itself is not explained, and the reference link is very clear.
4. If the coordinate sequence of the four vertices of a square is changed, it is not a square.
Source code link: http://download.csdn.net/detail/yanzi1225627/7484793
Reference: link 1 link 2
As follows: