Android OpenGL Tutorial 33 D casually draw

Source: Internet
Author: User

Draw a trigonous vertebra need four points, draw four faces

    private float[] mTriangleArray={            -1.0f,-1.0f,0.0f,            1.0f,-1.0f,0.0f,            0.0f,1.0f,0.0f,            0.0f,0.0f,1.0f    };    // 定义三棱椎的4个顶点的颜色    private float[] mColor={            1.0f,0.0f,0.0f,0.0f,            0.0f,1.0f,0.0f,0.0f,            0.0f,0.0f,1.0f,0.0f,            1.0f,1.0f,0.0f,0.0f,    };    private byte[] cubeFacets = new byte[]{            0, 1, 2,            0, 2, 3,            2, 1, 3,            0, 1, 3    };        gl.glDrawElements(GL10.GL_TRIANGLE_STRIP                , cubeFacetsBuffer.remaining(),                GL10.GL_UNSIGNED_BYTE, cubeFacetsBuffer);

If we're going to draw a square, we're going to draw eight dots, six faces, the other code doesn't change.
Defines the color of the 4 vertices of the trigonous vertebrae
Private float[] mcolor={
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f
};

    // 定义立方体的8个顶点    float[] cubeVertices = new float[] {            // 上顶面正方形的四个顶点            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, -0.5f,            0.5f, -0.5f, -0.5f,            -0.5f, -0.5f, -0.5f,            -0.5f, 0.5f, -0.5f    };    // 定义立方体所需要的6个面(一共是12个三角形所需的顶点)    private byte[] cubeFacets = new byte[]{            0, 2, 1,            0, 2, 3,            2, 7, 3,            2, 7, 6,            0, 7, 3,            0, 7, 4,            4, 6, 5,            4, 6, 7,            1, 4, 0,            1, 4, 5,            1, 6, 2,            1, 6, 5    };

Then add a spin to see the whole picture.
Gl.glrotatef (rotate, 1f, 0f, 0f);
rotate++;

The complete code is as follows

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 {//define the color of the 4 vertices of the trigonous vertebra private float[] mcolor={1.0            f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f,    1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f    }; Define the 8 vertices of the cube float[] cubevertices = new float[] {//top four vertices of the square 0.5f, 0.5f, 0.5f, 0. 5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f,//four vertices of the bottom square 0.5f, 0 .5f, -0.5f, 0.5F, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f};            Define the 6 faces required for the cube (total 12 triangles required) Private byte[] cubefacets = new byte[]{0, 2, 1, 0, 2, 3,            2, 7, 3, 2, 7, 6, 0, 7, 3, 0, 7, 4, 4, 6, 5, 4, 6, 7,    1, 4, 0, 1, 4, 5, 1, 6, 2, 1, 6, 5};    Private Floatbuffer Mtrianglebuffer;    Bytebuffer Cubefacetsbuffer;    Floatbuffer Tapercolorsbuffer;        Public Glrenderer () {mtrianglebuffer = Floatbufferutil (cubevertices);        The four fixed-point color arrays of the trigonous vertebrae are packaged into intbuffer Tapercolorsbuffer = 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); Allow 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);        Rotate Gl.glrotatef along the x-axis (rotate, 1f, 0f, 0f);        Set triangle Gl.glvertexpointer (3, gl10.gl_float, 0, Mtrianglebuffer);        Sets the color data of the vertex gl.glcolorpointer (4, gl10.gl_fixed, 0, Tapercolorsbuffer); Draw Triangle Gl.gldrawelements (Gl10.gl_triangle_strip, cubefacetsbuffer.remaining (), GL1        0.gl_unsigned_byte, Cubefacetsbuffer);        Cancels the color setting gl.gldisableclientstate (Gl10.gl_color_array);        Cancel vertex settings gl.gldisableclientstate (Gl10.gl_vertex_array);        Draw End Gl.glfinish ();    rotate++;    } int rotate = 0; @Override public void onsurfacechanged (GL10 gl, int width, int height) {float ratio = (float) width/hEight        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 accounts for 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;} Define a tool method to convert the int[] array to OpenGL ES required intbufferprivate intbuffer intbufferutil (inT[] arr) {Intbuffer mbuffer;    Initializes the bytebuffer, length of the arr array, because an int accounts for 4 bytes Bytebuffer qbb = Bytebuffer.allocatedirect (Arr.length * 4);    Array arrangement with Nativeorder Qbb.order (Byteorder.nativeorder ());    Mbuffer = Qbb.asintbuffer ();    Mbuffer.put (arr);    Mbuffer.position (0); return mbuffer;}

}

Android OpenGL Tutorial 33 D casually 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.