An example of drawing rotating cube with OpenGL

In the last section, we learned how to use OpenGL to draw 2D graphics. Now let’s learn how to use OpenGL to draw 3D graphics. Let’s really feel the power of OpenGL
In the last video we drew a rotating triangle and square, today we’re going to use OpenGL to draw a rotating cube.
Drawing cubes is basically the same as drawing triangles and squares. The key is to build the coordinates of cubes. When building these vertex coordinates, to rotate the object around its own axis, the object’s center coordinates must always be (0.0f, 0.0f, 0.0f) and drawn in counterclockwise order.
Without further ado, here is an example:
Start by creating an OpenGLView class that inherits GLSurfaceView
public class OpenGLView extends GLSurfaceView{ private GLReader glReader; public OpenGLView(Context context) { super(context); // TODO Auto-generated constructor stub glReader=new GLReader(); setRenderer(glReader); }}
Next is our Activity class:
public class SimpleOpenGLActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); OpenGLView view=new OpenGLView(this); setContentView(view); }}
Finally, our core class, the GlReader class for rendering 3D graphics, implements the Renderer interface:
package cn.com.karl.opengl; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL11; import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLU; Public class GLReader implements the Renderer {float box [] = new float [] {//FRONT – 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f,// BACK to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f, 0.5 f, f, 0.5-0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f,// LEFT – 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f to 0.5 f,// RIGHT f 0.5, – 0.5 f to 0.5 f, f 0.5, 0.5 f to 0.5 f, 0.5 f to 0.5 f, 0.5 0.5 f, f, f 0.5, 0.5, f// TOP – 0.5 f, 0.5 f, f, 0.5 0.5 f, f 0.5, 0.5 f to 0.5 f, 0.5 f to 0.5 f, f 0.5, 0.5 f to 0.5 f,// BOTTOM – 0.5 f, 0.5 f, f, 0.5-0.5, f – 0.5 f to 0.5 f, 0.5 f to 0.5 f, f 0.5, 0.5, f – 0.5 f to 0.5 f,}; FloatBuffer cubeBuff; Float xrot = 0.0 f; Float yrot = 0.0 f;/* * * to convert a float array is stored in the byte array buffer * @ param arr * @ return */public FloatBuffer makeFloatBuffer (float [] arr) {ByteBuffer bb = ByteBuffer. AllocateDirect (arr. Length * 4); B.order (byteOrder.nativeOrder ()); b.order(); // Set ByteOrder, where byteOrder.nativeOrder () is to get the native ByteOrder FloatBuffer fb = bb.asFloatBuffer(); // Convert to float fb.put(arr); // Add data fb.position(0); Return fb; return fb; } public GLReader(){ cubeBuff = makeFloatBuffer(box); }//// conversion float array all drawing operations are carried out in this method the public void onDrawFrame GL10 (gl) {//TODO Auto – generated method stub gl. 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.glLodIdentity (); // Reset the current model observation matrix Glu. GluLookAt (GL, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0); // Set the viewpoint and model center position gl.glTexPointer (3, gl10.gl_float, 0, cubeBuff); // Set vertex data gl. glenableClientState (gl10.gl_vertex_array); gl.glRotatef(xrot, 1, 0, 0);// around the (0, 0) and (0, 1). The x axis rotation gl glRotatef (yrot, 0, 1, 0). GlColor4f (1.0f, 0, 0, 1.0f); glColor4f(1.0f, 0, 0, 1.0f); // Set color, red gl.glDraWarRays (gl10.gl_triangle_strip, 0, 4); // draw square FRONT gl.glColor4f(1.0f, 1.0f, 0, 1.0f); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4); Gl. GlColor4f (0, 1.0 f, 0, 1.0 f); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4); Gl. GlColor4f (0, 1.0 f, f 1.0, 1.0 f); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4); Gl. GlColor4f (0, 0, 1.0 f, 1.0 f); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4); Gl. GlColor4f (1.0 f, f, 1.0 1.0 f, 1.0 f); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4); Xrot + = 1.0 f; Yrot + = 0.5 f; } public void onSurfaceChanged(GL10 gl, int width, int height) {// TODO auto-generated method stub // set the scene size of OpenGL. GlViewport (0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); // Set the projection matrix gl.glLodIdentity (); Glu.gluPerspective (gl, 45.0f, ((float) width)/height, 0.1f, 10f); Public void onSurfaceCreated(gl10gl, eGLConfig config) {// TODO Auto-generated method stub gl.glclearColor (0.0f, 0.0f, 0.0f, 1.0f); // Set the background color to R, G, B, A gl.glEnable(gl10.gl_depth_test); // Enable depth cache gl.glEnable(gl10.gl_cull_face); // Enable backside clipping gl.glclearDepthf (1.0f); // Set the depth cache value gl.glDepthFunc(gl10.gl_lequal); GL_LEQUAL (gl.glclearDepthf (1.0f)); gl.glshaDemodel (gl10.gl_smooth); gl.glclearDepthf (1.0f); gl.glclearDepthf (1.0f);// set the shadow model GL_SMOOTH}}
the upper portion of the more important is to do the comments, so here don’t do too much explanation, finally see how some after the operation effect.

Read More: