Open Graphics Library (OpenGL)
按照Google官方文檔:
首先 OpenGL is a cross-platform graphics API that specifies a standard software interface for 3D graphics processing hardware.
There are two foundational classes in the Android framework that let you create and manipulate graphics with the OpenGL ES API:GLSurfaceView and GLSurfaceView.Renderer.
GLSurfaceView
This class is a View where
you can draw and manipulate objects using OpenGL API calls and is similar in function to aSurfaceView.
You can use this class by creating an instance ofGLSurfaceView and
adding your Renderer to
it. However, if you want to capture touch screen events, you should extend theGLSurfaceView class
to implement the touch listeners, as shown in OpenGL Tutorials for ES 1.0, ES
2.0 and theTouchRotateActivity sample.
GLSurfaceView.Renderer
This interface defines the methods required for drawing graphics in an OpenGL GLSurfaceView.
You must provide an implementation of this interface as a separate class and attach it to your GLSurfaceView instance
using GLSurfaceView.setRenderer().
GLSurfaceView view = new GLSurfaceView(this); view.setRenderer(new OpenGLRenderer()); setContentView(view);
The GLSurfaceView.Renderer interface
requires that you implement the following methods:
三種方法(此段英文簡單,還是能看懂)
onSurfaceCreated():
The system calls this method once, when creating the GLSurfaceView.
Use this method to perform actions that need to happen only once, such as setting OpenGL environment parameters or initializing OpenGL graphic objects.
onDrawFrame():
The system calls this method on each redraw of the GLSurfaceView.
Use this method as the primary execution point for drawing (and re-drawing) graphic objects.
onSurfaceChanged():
The system calls this method when the GLSurfaceView geometry
changes, including changes in size of the GLSurfaceView or
orientation of the device screen. For example, the system calls this method when the device changes from portrait to landscape orientation. Use this method to respond to changes in the GLSurfaceView container.
public class OpenGLRenderer implements Renderer {/* * (non-Javadoc) * * @see android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax. * microedition.khronos.opengles.GL10, javax.microedition.khronos. * egl.EGLConfig) */// 當 GLSurfaceView geometry changes(// GLSurfaceView形狀改變),大小,尺寸什麼的,螢幕方向landscape--->portraitpublic void onSurfaceCreated(GL10 gl, EGLConfig config) {// Set the background color to black ( rgba ).gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // OpenGL docs.// Enable Smooth Shading, default not really needed.gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs.// Depth buffer setup.gl.glClearDepthf(1.0f);// OpenGL docs.// Enables depth testing.gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs.// The type of depth testing to do.gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs.// Really nice perspective calculations.gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs.GL10.GL_NICEST);}/* * (non-Javadoc) * * @see android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax. * microedition.khronos.opengles.GL10) */public void onDrawFrame(GL10 gl) {// Clears the screen and depth buffer.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs.GL10.GL_DEPTH_BUFFER_BIT);}/* * (non-Javadoc) * * @see android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax. * microedition.khronos.opengles.GL10, int, int) */public void onSurfaceChanged(GL10 gl, int width, int height) {// Sets the current view port to the new size.gl.glViewport(0, 0, width, height);// OpenGL docs.// Select the projection matrixgl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs.// Reset the projection matrixgl.glLoadIdentity();// OpenGL docs.// Calculate the aspect ratio of the windowGLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,100.0f);// Select the modelview matrixgl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs.// Reset the modelview matrixgl.glLoadIdentity();// OpenGL docs.}
Once you have established a container view for OpenGL using GLSurfaceView andGLSurfaceView.Renderer,
you can begin calling OpenGL APIs using the following classes:
建立好container view後 就可以通過下面這些類調用API了:
OpenGL ES 1.0/1.1 API Packages
OpenGL ES 2.0 API Class
Lessons
-
Building an OpenGL ES Environment
-
Learn how to set up an Android application to be able to draw OpenGL graphics.
-
Defining Shapes
-
Learn how to define shapes and why you need to know about faces and winding.
-
Drawing Shapes
-
Learn how to draw OpenGL shapes in your application.
-
Applying Projection and Camera Views
-
Learn how to use projection and camera views to get a new perspective on your drawn objects.
-
Adding Motion
-
Learn how to do basic movement and animation of drawn objects with OpenGL.
-
Responding to Touch Events
-
Learn how to do basic interaction with OpenGL graphics.