Android provides two basic classes for us to use OpenGL ES APIs to create and manipulate graphics: GLSurfaceView and GLSurfaceView. Renderer. Therefore, we need to understand these two classes first.
1. GLSurfaceView:
This is a view class. You can call the OpenGL API to draw images and manipulate objects on it. The function is similar to SurfaceView. We can create a GLSurfaceView class instance and add our own Renderer. If we want to implement some touch screen operations by ourselves, we must extend this class to implement the touch listener.
2. GLSurfaceView. Renderer
This interface defines the method required to draw a graph in GLSurfaceView of OpenGL. We must provide implementation for these interfaces in a separate class, and use the GLSurfaceView. setRenderer () method to attach it to the GLSurfaceView instance object.
We need to implement GLSurfaceView. Renderer in the following ways:
A) onSurfaceCreated (): The system calls it once when creating GLSurfaceView. We can use it to set OpenGL environment variables or initialize OpenGL graphics objects.
B) onDrawFrame (): This method is called every time the GLSurfaceView is re-painted. This method is used to draw images.
C) onSurfaceChanged (): This method is called when the Geometric Properties of GLSurfaceView are changed, including the size or the orientation of the device screen. For example, the system changes from upright to horizontal on the screen so that it can be called. This method is mainly used to respond to changes in the GLSurfaceView container.
Lab procedure
1. Add a new project
2. Add a new class myGLRenderer to implement the GLSurfaceView. Renderer interface.
The Code is as follows:
[Java] <span style = "font-size: 16px;"> public class myGLRenderer implements Renderer {
@ Override
Public void onDrawFrame (GL10 gl ){
// TODO Auto-generated method stub
Gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT); // clear the cache
}
@ Override
Public void onSurfaceChanged (GL10 gl, int width, int height ){
// TODO Auto-generated method stub
Gl. glViewport (0, 0, width, height); // you can specify
}
@ Override
Public void onSurfaceCreated (GL10 gl, EGLConfig config ){
// TODO Auto-generated method stub
Gl. glClearColor (0.5f, 0.5f, 0.5f, 1.0f); // sets the clear color.
}
}
</Span>
<Span style = "font-size: 16px;"> public class myGLRenderer implements Renderer {
@ Override
Public void onDrawFrame (GL10 gl ){
// TODO Auto-generated method stub
Gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT); // clear the cache
}
@ Override
Public void onSurfaceChanged (GL10 gl, int width, int height ){
// TODO Auto-generated method stub
Gl. glViewport (0, 0, width, height); // you can specify
}
@ Override
Public void onSurfaceCreated (GL10 gl, EGLConfig config ){
// TODO Auto-generated method stub
Gl. glClearColor (0.5f, 0.5f, 0.5f, 1.0f); // sets the clear color.
}
}
</Span>
3. Add a new class myGLSurfaceView. The parent class is GLSurfaceView.
The Code is as follows:
[Java] <span style = "font-size: 16px;"> public class myGLSurfaceView extends GLSurfaceView {
Public myGLSurfaceView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
Mrender = new myGLRenderer ();
SetRenderer (mrender );
}
Private myGLRenderer mrender;
}
</Span>
<Span style = "font-size: 16px;"> public class myGLSurfaceView extends GLSurfaceView {
Public myGLSurfaceView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
Mrender = new myGLRenderer ();
SetRenderer (mrender );
}
Private myGLRenderer mrender;
}
</Span>
4. The main program code is as follows:
[Java] <span style = "font-size: 16px;"> public class HelloOpenGLActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MGLSurfaceView = new myGLSurfaceView (this );
SetContentView (mGLSurfaceView); // here we use mGLSurfaceView to replace the previously used R. layout. main
}
Private myGLSurfaceView mGLSurfaceView;
}
</Span>
<Span style = "font-size: 16px;"> public class HelloOpenGLActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MGLSurfaceView = new myGLSurfaceView (this );
SetContentView (mGLSurfaceView); // here we use mGLSurfaceView to replace the previously used R. layout. main
}
Private myGLSurfaceView mGLSurfaceView;
}
</Span>
In this way, we have completed the application of OpenGL to draw a gray background. Of course, this is the most basic function. We will explore how to use OpenGL to draw simple ry.
From Peking University-Google Android lab