OpenGlES, finally able to draw this thing
In fact, both methods draw 12 triangles. In OpenGL ES, only triangles are supported, so any complex polygon is drawn by triangles.
The first: vertex method
:
Think of a quad as a face, and a face consists of two triangles. Does a triangle have 3 vertices? , so a face has 3+3 vertices, a cube has 6 faces, 6*6 vertices
The color of the cube is also rendered according to the vertices, just like the vertices that define the cube, but its parameters are not the same as those that define the vertices. Its parameter types are: R, G, B, A, which represent the color value
public class OpenGlEsDemoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); GLSurfaceView glview = new GLSurfaceView(this); glview.setRenderer(new OpenGlRender()); setContentView(glview); } }
public class OpenGlRender implements GLSurfaceView.Renderer{ //Draw two triangles per face, the cube has 6 faces private float[] vertices={ -1.0f,1.0f,1f, // top left -1.0f,-1.0f,1f, // bottom left 1.0f,-1.0f,1f, //top right -1.0f,1.0f,1f, //bottom left 1.0f,-1.0f,1f, //bottom right 1.0f,1.0f,1f, //top right //front 1.0f,1.0f,1f, 1.0f,-1.0f,1f, 1.0f,-1.0f,-1f, 1.0f,1.0f,1f, 1.0f,-1.0f,-1.0f, 1.0f,1.0f,-1f, //right -1.0f,1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f,1.0f,1.0f, -1.0f,-1.0f,-1.0f, -1.0f,-1.0f,1.0f, -1.0f,1.0f,1.0f, //left 1.0f,1.0f,-1.0f, 1.0f,-1.0f,-1.0f, -1.0f,-1.0f,-1.0f, 1.0f,1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f,1.0f,-1.0f, //behind -1.0f,1.0f,-1.0f, // top left -1.0f,1.0f,1.0f, //bottom left 1.0f,1.0f,-1.0f, //top right -1.0f,1.0f,1.0f, //bottom left 1.0f,1.0f,1.0f, //top right 1.0f,1.0f,-1.0f, // -top right above -1.0f,-1.0f,1.0f, -1.0f,-1.0f,-1.0f, 1.0f,-1.0f,-1.0f, -1.0f,-1.0f,1.0f, 1.0f,-1.0f,-1.0f, 1.0f,-1.0f,1.0f, //below }; //The vertex color of the cube private float[] colors={ 1f,0f,0f,1f, 1f,0f,0f,1f, 1f,0f,0f,1f, 1f,0f,0f,1f, 1f,0f,0f,1f, 1f,0f,0f,1f, 1f,0f,1f,1f, 1f,0f,1f,1f, 1f,0f,1f,1f, 1f,0f,1f,1f, 1f,0f,1f,1f, 1f,0f,1f,1f, 0f,1f,0f,1f, 0f,1f,0f,1f, 0f,1f,0f,1f, 0f,1f,0f,1f, 0f,1f,0f,1f, 0f,1f,0f,1f, 0f,0f,1f,1f, 0f,0f,1f,1f, 0f,0f,1f,1f, 0f,0f,1f,1f, 0f,0f,1f,1f, 0f,0f,1f,1f, 0.5f,0f,1f,1f, 0.5f,0f,1f,1f, 0.5f,0f,1f,1f, 0.5f,0f,1f,1f, 0.5f,0f,1f,1f, 0.5f,0f,1f,1f, 1f,0f,0.5f,1f, 1f,0f,0.5f,1f, 1f,0f,0.5f,1f, 1f,0f,0.5f,1f, 1f,0f,0.5f,1f, 1f,0f,0.5f,1f, }; FloatBuffer vertBuffer; //Vertex buffer FloatBuffer colorBuffer; //color buffer float rx=-70f; //Rotation angle public OpenGlRender(){ ByteBuffer vbb=ByteBuffer.allocateDirect(vertices.length*4); vbb.order(ByteOrder.nativeOrder()); vertBuffer=vbb.asFloatBuffer(); vertBuffer.put(vertices); vertBuffer.position(0); ByteBuffer cbb= ByteBuffer.allocateDirect(colors.length*4); cbb.order (ByteOrder.nativeOrder ()); colorBuffer = cbb.asFloatBuffer(); colorBuffer.put(colors); colorBuffer.position(0); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { //enable depth test gl.glEnable(GL10.GL_DEPTH_TEST); // type of depth test done gl.glDepthFunc(GL10.GL_DITHER); //black background gl.glClearColor(0f, 0f, 0f, 0.5f); //Enable shadow smoothing gl.glShadeModel(GL10.GL_SMOOTH); //Clear the depth buffer gl.glClearDepthf(1.0f); gl.glEnable(GL10.GL_TEXTURE_2D); //Tell the system to correct the perspective gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); } public void draw(GL10 gl){ gl.glFrontFace(GL10.GL_CCW); gl.glEnable(GL10.GL_CULL_FACE); gl.glCullFace(GL10.GL_BACK); //Open vertex and texture buffers gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuffer); gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); gl.glLoadIdentity (); gl.glTranslatef (0, 0, -5); gl.glRotatef(45f, 0f, 1f, 0f); //tilt 45 degrees to the right (y axis) C gl.glRotatef(rx,1f, 0f, 0f); //Incline to the top (x axis), according to the angle obtained each time gl.glDrawArrays(GL10.GL_TRIANGLES, 0,vertices.length); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisable(GL10.GL_CULL_FACE); rx--; //The rotation angle minus 1 } public void onDrawFrame(GL10 gl) { // clear depth and color buffers gl.glClearColor(0f, 0f, 0f, 0.5f); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL10.GL_MODELVIEW); //Set the matrix mode draw(gl); } public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity (); GLU.gluPerspective(gl, 45.0f, (float)width/(float)height, 0.1f, 100.f); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity (); }
The second: index method
The effect drawn is the same as above, but it is drawn by index
Or divide it into 6 faces to draw, and draw 2 triangles on one face.
-1f,1f,1f, //0 -1f,-1f,1f, //1 1f,-1f,1f, //2 1f,1f,1f, //3What is drawn here is the front of the cube, which corresponds to the x, y, and z axes.
private short[] indices={ 0,1,2, 0,2,3,}It's just that its index position is specified in this indices, so it will obediently draw the quadrilateral
0 is the coordinate of the upper left corner, 1 is the lower left corner, 2 is the lower right corner, and 3 is the upper right corner
public class Demo extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate (savedInstanceState); GLSurfaceView v = new GLSurfaceView(this); v.setRenderer(new OpenGlRender2(null)); setContentView(v); } }
public class OpenGlRender2 implements Renderer{ //vertex coordinates x, y, z private float[] vertices= { -1f,1f,1f, -1f,-1f,1f, 1f,-1f,1f, 1f,1f,1f, //front 1f,1f,1f, 1f,-1f,1f, 1f,-1f,-1f, 1f,1f,-1f, //right 1f,1f,-1f, 1f,-1f,-1f, -1f,-1f,-1f, -1f,1f,-1f, //behind -1f,1f,-1f, -1f,-1f,-1f, -1f,-1f,1f, -1f,1f,1f, //left -1f,1f,-1f, -1f,1f,1f, 1f,1f,1f, 1f,1f,-1f, //above -1f,-1f,1f, -1f,-1f,-1f, 1f,-1f,-1f, 1f,-1f,1f, //below }; //Vertex color, R, G, B, A private float[] colors={ 0.2f,0f,0.7f,1f, 0f,0.4f,0.3f,1f, 0.8f,0.1f,0.1f,1f, 1f,1f,1f,1f, //front 0f,1f,0f,1f, 0f,1f,0f,1f, 0f,1f,0f,1f, 0f,1f,0f,1f, //behind 0f,0f,1f,1f, 0f,0f,1f,1f, 0f,0f,1f,1f, 0f,0f,1f,1f, //left 0.3f,0.5f,1f,1f, 0.3f,0.5f,1f,1f, 0.3f,0.5f,1f,1f, 0.3f,0.5f,1f,1f, //above 0f,0.2f,0.3f,1f, 0f,0.4f,0.3f,1f, 0f,0.3f,0.3f,1f, 0f,0.4f,0.3f,1f, //below }; private short[] indices={ 0,1,2, 0,2,3, 4,5,6, 4,6,7, 8,9,10, 8,10,11, 12,13,14, 12,14,15, 16,17,18, 16,18,19, 20,21,22, 20,22,23, }; private FloatBuffer mVertexBuffer,mColorBuffer; private ShortBuffer mIndixBuffer; private float rx=45.0f; public OpenGlRender2(Bitmap bitmap){ ByteBuffer vbb=ByteBuffer.allocateDirect(vertices.length*4); vbb.order(ByteOrder.nativeOrder()); mVertexBuffer=vbb.asFloatBuffer(); mVertexBuffer.put(vertices); mVertexBuffer.position(0); ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length*2); ibb.order(ByteOrder.nativeOrder()); mIndixBuffer=ibb.asShortBuffer(); mIndixBuffer.put(indices); mIndixBuffer.position(0); ByteBuffer cbb=ByteBuffer.allocateDirect(colors.length*4); cbb.order (ByteOrder.nativeOrder ()); mColorBuffer = cbb.asFloatBuffer (); mColorBuffer.put(colors); mColorBuffer.position(0); } public void onDrawFrame(GL10 gl) { gl.glClearColor(0f, 0f, 0f, 0.5f); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glFrontFace(GL10.GL_CCW); gl.glEnable(GL10.GL_CULL_FACE); gl.glCullFace(GL10.GL_BACK); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3,GL10.GL_FLOAT, 0, mVertexBuffer); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer); gl.glLoadIdentity (); // gl.glColor4f(1f, 0f, 0f, 1f); gl.glTranslatef (0f, 0f, -5f); gl.glRotatef(-45f, 0f, 1f, 0f); gl.glRotatef(rx, 1f, 0f, 0f); gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, mIndixBuffer); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisable(GL10.GL_CULL_FACE); rx++; } public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity (); GLU.gluPerspective(gl, 45.0f, (float)width/(float)height, 0.1f, 100.f); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity (); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { //enable depth test gl.glEnable(GL10.GL_DEPTH_TEST); // type of depth test done gl.glDepthFunc(GL10.GL_DITHER); //black background gl.glClearColor(1f, 0f, 0f, 1f); //Enable shadow smoothing gl.glShadeModel(GL10.GL_SMOOTH); //Clear the depth buffer gl.glClearDepthf(1.0f); }