By he minggui (http://blog.csdn.net/hmg25) reprint please indicate the source
Through the previous article-concepts, we will write a simple getting started program today to implement a color cube that continuously rotates around the X axis and Y axis. The effect is as follows:
In Android, we use GLSurfaceView to display OpenGL views. GLSurfaceView is a very important class, which is located in android. the opengl package is used to manage a special surface that can be the memory of a composite view robot system. Manage An EGL display that visualizes OpenGL. Allows a user to provide input Render objects for display. A dedicated thread rendering interface is implemented from the UI thread to achieve 3D performance. Supports on-demand and continuous rendering. Package, track, and check OpenGL
An error occurred while calling the Renderer. First, we need to create a GLSurfaceView.
Public class mainactivity extends activity {cuberenderer mcuberenderer; // our custom cube Renderer @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); // remove the title glsurfaceview glview = new glsurfaceview (this); // create a glsurfaceview mcuberenderer = new cuberenderer (); glview. setrenderer (mcuberenderer); setcontentview (glview );}}
Next, we will create a cuberenderer that inherits the Renderer interface. Renderer is an interface dedicated to 3D rendering. To inherit from it, we need to overload the following methods:
Public void ondrawframe (gl10 GL)
{
// The rendering operation called during repainting.
}
Public
Void onsurfacechanged (gl10 GL, int width, int height)
{
// Called when the window is changed. The window range, perspective, and projection range are usually set here.
}
Public
Void onsurfacecreated (gl10 GL, eglconfig config)
{
// Called at creation, usually initialized here
}
The complete code of cuberenderer is as follows:
Public class CubeRenderer implements Renderer {float box [] = new float [] {// FRONT-0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, // BACK-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f, 0.5f, 0.5f,-0.5f, // LEFT-0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f, // RIGHT 0.5f, -0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, // TOP-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f, // BOTTOM-0.5f,-0.5f, 0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f, 0.5f, 0.5f,-0.5f,-0.5f,}; FloatBuffer cubeBuff; float xrot = 0.0f; float yrot = 0.0f; /*** convert the float array and store it in the byte buffer array * @ param arr * @ return */public FloatBuffer makeFloatBuffer (float [] arr) {ByteBuffer bb = ByteBuffer. allocateDirect (arr. length * 4); // allocate the buffer space. A float occupies 4 byte bb. order (ByteOrder. nativeOrder (); // you can specify ByteOrder. nativeOrder () is to obtain the local byte order FloatBuffer fb = bb. asFloatBuffer (); // convert to float type fb. put (arr); // Add data to fb. position (0); // set the initial position of the array return fb;} public CubeRenderer () {// TODO Auto-generated constructor stubcubeBuff = makeFloatBuffer (box ); // convert the float array} protected void init (GL10 gl) {gl. glClearColor (0.0f, 0.0f, 0.0f, 1.0f); // set the background color, R, G, B, And Agl. glable (GL10.GL _ DEPTH_TEST); // enable the deep cache gl. glable (GL10.GL _ CULL_FACE); // enable back-side cropping gl. glClearDepthf (1.0f); // sets the deep cache value gl. glDepthFunc (GL10.GL _ LEQUAL); // sets the depth cache comparison function. GL_LEQUAL indicates that the depth cache value of the new Pixel is smaller than or equal to the depth cache value of the current pixel (through gl. glClearDepthf (1.0f) is used to test gl in depth. glShadeModel (GL10.GL _ SMOOTH); // sets the shadow mode GL_SMOOTH} @ Overridepublic void onSurfaceCreated (GL10 gl, EGLConfig config) {// TODO Auto-generated method stubinit (gl );} @ Overridepublic void onSurfaceChanged (GL10 gl, int w, int h) {// TODO Auto-generated method stubgl. glViewport (0, 0, w, h); // set the Windows gl. glMatrixMode (GL10.GL _ PROJECTION); // sets the PROJECTION matrix gl. glLoadIdentity (); // sets the matrix as the unit matrix, which is equivalent to Resetting the matrix. gluPerspective (gl, 45.0f, (float) w)/h, 0.1f, 10f); // sets the perspective range} @ Overridepublic void onDrawFrame (GL10 gl) {// TODO Auto-generated method stubgl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT); // clear the screen and depth cache gl. glMatrixMode (GL10.GL _ MODELVIEW); // switch to the model observation matrix gl. glLoadIdentity (); // reset the current model observation matrix (GLU. gluLookAt (gl, 0, 0, 3, 0, 0, 0, 0, 1, 0); // set the viewpoint and model center position gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, cubeBuff); // sets the vertex data gl. glableclientstate (GL10.GL _ VERTEX_ARRAY); gl. glRotatef (xrot, 1, 0, 0); // rotate gl around (0, 0) and (1, 0, 0) on the X axis. glRotatef (yrot, 0, 1, 0); gl. glColor4f (1.0f, 0, 0, 1.0f); // set the color and red gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 0, 4); // draw the positive FRONT gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 4, 4); gl. glColor4f (0, 1.0f, 0, 1.0f); gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 8, 4); gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 12, 4); gl. glColor4f (0, 0, 1.0f, 1.0f); gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 16, 4); gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 20, 4); xrot + = 1.0f; yrot + = 0.5f ;}}
Source code project,: http://download.csdn.net/source/3566635