OpenGL ES learning notes and opengles learning notes
To use openGL:
1.Create a GLSurfaceView object
2.Create GLSurfaceView. renderer implementation class.
3.Set the contentView of the activity and the render object of the view.
4.The process of implementing the render class.
OnSurfaceCreate () method
Set screen color and enable vertex buffer
// Set the screen color
Gl. glClearColor (0, 0, 0, 1 );
// Enable vertex buffer
Gl. glableclientstate (GL10.GL _ VERTEX_ARRAY );
OnSurfaceChanged () method
1. Set the viewport (the area of the View/output screen)
gl.glViewport(0, 0, width, height);
2. manipulate the projection matrix and set the intercept (the ratio is usually the same as that of the viewport; otherwise, the output screen will be out of shape)
// Matrix mode, projection matrix, openGL based on the state machine gl. glMatrixMode (GL10.GL _ PROJECTION); // loads the unit matrix (return to zero) gl. glLoadIdentity (); // the parameter of the flat header (Visual Object) corresponds to the left, right, bottom, top, near plane, and far plane (the drawing must be within the range of these values, otherwise, the gl. glFrustumf (-ratio, ratio,-1f, 1f, 3, 7 );
OnDrawFrame () method
1. Clear the color Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
2. manipulate the Model View matrix and set eye Parameters
// The previously created projection matrix. If you want to set the eyeball parameters, change it to the model view matrix. Otherwise, you can leave gl unchanged. glMatrixMode (GL10.GL _ MODELVIEW); gl. glLoadIdentity (); // loads the unit matrix (GLU. gluLookAt (gl, 0, 0, 5, 0, 0, 0, 0, 1, 0); // After gl, the first group of three parameters are eye coordinates, the three parameters in the middle are eye observation points, and the last three parameters are eye-directed vectors.
3. Define an array of coordinate values of graphic vertices (floating point and integer can also be used)
float[] coords = { 0f,0.5f,0f, -0.5f,-0.5f,0f, 0.5f,-0.5f,0f };
4. Convert vertex coordinates into buffer data
// Allocate byte cache space (more efficient) and store the vertex coordinate data ByteBuffer ibb = ByteBuffer. allocateDirect (coords. length * 4); // a floating point number occupies 4 digits // The set order (Local Order) ibb. order (ByteOrder. nativeOrder (); // place the vertex coordinate array (the first step is to wrap it into a floating-point buffer) FloatBuffer fbb = ibb. asFloatBuffer (); fbb. put (coords); // locate the pointer from which to read the vertex data ibb. position (0 );
5. Set the drawing color
Gl. glColor4f (1f, 0f, 0f, 1f); // unlike Android, here is the native argb of rgba Android. // glColorXX. Here 4 is four floating point numbers. F is float
6. Specify the vertex buffer pointer
// 3-dimensional point. Three coordinate values are used to represent a point. // type: Data type of each point. // stride: 0, span. // ibb: Specify the vertex buffer gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, ibb );
7. Drawing (most important)
// 0: Start Point: // 3: number of points to be drawn gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3); // There are three modes in total: drawing points, draw lines, and draw TRIANGLES (elements ). The plane is also a triangle // the ball and the ring are countless triangles
The code for drawing a triangle is attached.
Opengl uses the Cartesian coordinate system, which is different from the screen coordinate system. The lower left corner is the origin.
Package com. example. administrator. myapplication; import android. content. context; import android. opengl. GLSurfaceView; import android. opengl. GLU; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. util. attributeSet; import java. nio. byteBuffer; import java. nio. byteOrder; import java. nio. floatBuffer; import javax. microedition. khronos. egl. EGLConfig; import javax. microedition. Khronos. opengles. GL10; public class MainActivity extends AppCompatActivity {float ratio; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); MyGLSurfaceView = new MyGLSurfaceView (this); view. setRenderer (new MyRenderer (); // Renderer setContentView (view);} class MyGLSurfaceView extends GLSurfaceView {public MyGLSurfaceView (Context context) {super (con Text);} public MyGLSurfaceView (Context context, AttributeSet attrs) {super (context, attrs) ;}} class MyRenderer implements android. opengl. GLSurfaceView. renderer {@ Override public void onSurfaceCreated (GL10 gl, EGLConfig config) {// sets the gl of the surface image. glClearColor (0, 0, 0, 1); gl. glableclientstate (GL10.GL _ VERTEX_ARRAY); // enable vertex cache} @ Override public void onSurfaceChanged (GL10 gl, int width, int height) {// Set the gl Of The viewport and image area. glViewport (0, 0, width, height); ratio = (float) width/(float) height; // matrix mode, projection matrix, opengGL based on the state machine gl. glMatrixMode (GL10.GL _ PROJECTION); // loads the unit matrix gl. glLoadIdentity (); // HTTP header gl. glFrustumf (-1f, 1f,-ratio, ratio, 3, 7);} @ Override public void onDrawFrame (GL10 gl) {gl. glClear (GL10.GL _ COLOR_BUFFER_BIT); // Model View matrix gl. glMatrixMode (GL10.GL _ MODELVIEW); // loads the unit matrix gl. glLoadIdentity (); GLU. gl ULookAt (gl, 0f, 0f, 5f, 0f, 0f, 0f, 0, 1, 0); // draw a triangle float [] coords = {0f, ratio, 0f, -1f,-ratio, 0f, 1f,-ratio, 0f}; // allocate byte cache space to store vertex data ByteBuffer ib = ByteBuffer. allocateDirect (coords. length * 4); // The set sequence (Local Order) ib. order (ByteOrder. nativeOrder (); // place the vertex data FloatBuffer fbb = ib. asFloatBuffer (); fbb. put (coords); // locate the correct position. Read the vertex data at this position ib. position (0); gl. glColor4f (1f, 0f, 0f, 1f); // three-dimensional vertex // stride // span gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, ib); gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3 );}}}