Android OpenGL Tutorial 22 D shape Draw

Source: Internet
Author: User

First, draw a triangle.

    private float[] mTriangleArray={            0.0f,1.0f,0.0f,            1.0f,-1.0f,0.0f,            -1.0f,-1.0f,0.0f    };

Gl.gldrawarrays (gl10.gl_triangles, 0, MTRIANGLEARRAY.LENGTH/3);

Then draw a two triangle to make a square, and change the coordinates before it.

    private float[] mTriangleArray={            1.0f,1.0f,0.0f,            1.0f,-1.0f,0.0f,            -1.0f,-1.0f,0.0f,            -1.0f,-1.0f,0.0f,            -1.0f,1.0f,0.0f,            1.0f,1.0f,0.0f    };

Looks like Gldrawarrays was drawn in three three coordinates.

I now use the gldrawelements function to draw a square, which can not only draw a square, but also can draw 3D

    private float[] mTriangleArray={            1.0f,1.0f,0.0f,            1.0f,-1.0f,0.0f,            -1.0f,-1.0f,0.0f,            -1.0f,1.0f,0.0f    };    private float[] mColor ={            1.0f,0.0f,0.0f,1.0f,            1.0f,0.0f,0.0f,1.0f,            1.0f,0.0f,0.0f,1.0f,            1.0f,0.0f,0.0f,1.0f    };    private byte[] cubeFacets = new byte[]{            0, 1, 2,            0, 2, 3    };        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTriangleBuffer);        gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);        gl.glDrawElements(GL10.GL_TRIANGLE_STRIP                , cubeFacetsBuffer.remaining(),                GL10.GL_UNSIGNED_BYTE, cubeFacetsBuffer);

This function means that you preach the position of the coordinate group where he draws three points (first starting from 0, a coordinate group has three float), such as this cubefacets means 0, 1, 23 coordinates to draw a triangle, then 0, 2, 3 coordinates to draw a triangle.

The following is the complete code

public class Mainactivity extends Activity {
Private Glsurfaceview Mglview;

public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Mglview = new Glsurfaceview (this);    The subclass of the custom Glsurfaceview mglview.setrenderer (new Glrenderer ()) is used here; Setcontentview (Mglview);}    public void OnPause () {super.onpause (); Mglview.onpause ();}    public void Onresume () {super.onresume (); Mglview.onresume ();}             public class Glrenderer implements Glsurfaceview.renderer {private float[] mtrianglearray={1.0f,1.0f,0.0f,    1.0f,-1.0f,0.0f, -1.0f,-1.0f,0.0f, -1.0f,1.0f,0.0f};            Private float[] Mcolor ={1.0f,0.0f,0.0f,1.0f, 1.0f,0.0f,0.0f,1.0f, 1.0f,0.0f,0.0f,1.0f,    1.0F,0.0F,0.0F,1.0F};    Private byte[] cubefacets = new byte[]{0, 1, 2, 0, 2, 3};    Private Floatbuffer Mtrianglebuffer;    Private Floatbuffer Mcolorbuffer;    Bytebuffer Cubefacetsbuffer; Public Glrenderer () {mtrianglebuffer = Floatbufferutil (MtriAnglearray);        Mcolorbuffer = Floatbufferutil (Mcolor);    Cubefacetsbuffer = Bytebuffer.wrap (cubefacets); } @Override public void Ondrawframe (GL10 gl) {//clear screen and depth cache Gl.glclear (Gl10.gl_color_buffer_bit |        Gl10.gl_depth_buffer_bit);        Sets the current matrix mode to the Model view.        Gl.glmatrixmode (Gl10.gl_modelview);        Allows setting vertex//gl10.gl_vertex_array vertex array gl.glenableclientstate (Gl10.gl_vertex_array);        Allows setting color//gl10.gl_color_array color array gl.glenableclientstate (Gl10.gl_color_array);        Resets the current model observation matrix gl.glloadidentity ();        Move the triangle on the z axis gl.gltranslatef (0f, 0.0f, -2.0f);        Set triangle Gl.glvertexpointer (3, gl10.gl_float, 0, Mtrianglebuffer);        Sets the triangle color Gl.glcolorpointer (4, gl10.gl_float, 0, Mcolorbuffer); Draw Triangle Gl.gldrawelements (Gl10.gl_triangle_strip, cubefacetsbuffer.remaining (), GL1        0.gl_unsigned_byte, Cubefacetsbuffer);        Cancel color settingsGl.gldisableclientstate (Gl10.gl_color_array);        Cancel vertex settings gl.gldisableclientstate (Gl10.gl_vertex_array);    Draw End Gl.glfinish (); } @Override public void onsurfacechanged (GL10 gl, int width, int height) {float ratio = (float) Width/heig        Ht        Sets the size of the OpenGL scene, (0,0) represents the lower-left corner of the viewport inside the window, (w,h) specifies the size of the viewport gl.glviewport (0, 0, width, height);        Set the projection matrix Gl.glmatrixmode (gl10.gl_projection);        Resets the projection matrix gl.glloadidentity ();        Set the size of the viewport gl.glfrustumf (-ratio, ratio,-1, 1, 1, 10);        The following two sentences declare that all subsequent transformations are for the model (i.e. we draw the graph) Gl.glmatrixmode (Gl10.gl_modelview);    Gl.glloadidentity ();  } @Override public void onsurfacecreated (GL10 gl, EGLConfig config) {//Set white for Clear screen gl.glclearcolor (1, 1,    1, 1); }}//define a tool method to convert the float[] array to OpenGL ES required floatbufferprivate floatbuffer floatbufferutil (float[] arr) {Floatbuffer    Mbuffer; Initializes the bytebuffer, length of the arr array, because an int occupies 4 bytes Bytebuffer qbb = BytebuFfer.allocatedirect (Arr.length * 4);    Array arrangement with Nativeorder Qbb.order (Byteorder.nativeorder ());    Mbuffer = Qbb.asfloatbuffer ();    Mbuffer.put (arr);    Mbuffer.position (0); return mbuffer;}

}

Android OpenGL Tutorial 22 D shape Draw

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.