Android draws triangles on Glsurfaceview based on OpenGL and uses the projection and camera view methods sample _android

Source: Internet
Author: User
Tags stub

This article illustrates the Android triangle on Glsurfaceview based on OpenGL and the use of projection and camera view methods. Share to everyone for your reference, specific as follows:

Defining triangles

OpenGL allows us to use three-dimensional coordinates to define an object. Before we draw a triangle, we need to define the coordinates of its various points. We typically use arrays to store the coordinates of each vertex.

The OpenGL ES default [0,0,0] (x,y,z) is in the center of the Glsurfaceview, [1,1,0] in the upper-right corner, [ -1,-1,0] in the lower-left corner.

Draw triangles

Before we draw a triangle, we have to tell OpenGL that we are using a vertex array. Then we use the drawing function to draw the triangle.

Experiment steps:

1. Add a new class triangle

The code is as follows:

 public class Triangle {public triangle () {float trianglecoords[] = { 
        X, Y, Z This is a equilateral -0.5f, -0.25f, 0, 0.5f, -0.25f, 0, 0.0f, 0.559016994f, 0}; Initialize the vertex cache of the triangle Bytebuffer VBB = Bytebuffer.allocatedirect (//(# of coordinate values * 4 byt
        Es per float) trianglecoords.length * 4); Vbb.order (Byteorder.nativeorder ())//using the byte sequence of the device hardware itself Trianglevb = Vbb.asfloatbuffer (); Create a floating-point cache Trianglevb.put (trianglecoords) from the Bytebuffer; Add vertex coordinates to the floating-point cache trianglevb.position (0);
    Make cache read first coordinate} public void Draw (GL10 gl) {gl.glcolor4f (0.63671875f, 0.76953125f, 0.22265625f, 0.0f);//Set Current color Gl.glvertexpointer (3, gl10.gl_float, 0, Trianglevb);/set vertex gl.gldrawarrays (gl10.gl_triangles, 0, 3);//Draw triangle} p
Rivate Floatbuffer Trianglevb; }

2. Add member Privatetriangle Mtriangle in the Myglrenderer class and initialize in the constructor.

The code is as follows:

Public Myglrenderer ()
{
    mtriangle = new triangle ();
}

3. Add the Glenableclientstate () method to the onsurfacecreated () function at the end of the Myglrenderer class to enable the vertex array.

The code is as follows:

@Override public
void onsurfacecreated (GL10 gl, eglconfig config) {
    //TODO auto-generated stub
    Gl.glclearcolor (0.5f, 0.5f, 0.5f, 1.0f);
    Gl.glenableclientstate (Gl10.gl_vertex_array);
}

4. At the end of the Myglrenderer class, the Ondrawframe () function adds a triangular rendering method.

The code is as follows:

@Override public
void Ondrawframe (GL10 gl) {
    //TODO auto-generated method Stub
    gl.glclear (Gl10.gl_ Color_buffer_bit | Gl10.gl_depth_buffer_bit);
    Mtriangle.draw (GL);
}

In this way, we have completed the drawing of a planar triangle. But we found that this triangle is not a equilateral as we define it, because OpenGL always assumes that our screen is a square, so that when we draw the final shape will be stretched as the width of the screen is different. In order to get the correct display, we need to project the graph to the correct position. This feature is implemented in the next section.

The Android device screen is usually not square, and OpenGL always projects the square coordinate system to the device by default, which causes the graphic to not be displayed in real proportions. To solve this problem, we can use OpenGL's projection mode and camera view to convert the coordinates of the graph to accommodate different device displays.

Experiment steps:

1. Modify the onsurfacecreated () function of the Myglrenderer class to enable Gl10.gl_projection mode, calculate the aspect ratio of the screen and use this ratio to convert the coordinates of the object.

The code is as follows:

@Override public
void onsurfacechanged (GL10 gl, int width, int height) {
    //TODO auto-generated method stub
    GL . Glviewport (0, 0, width, height);
    float ratio = (float) width/height;
    Gl.glmatrixmode (gl10.gl_projection); Set the current matrix to the projection matrix
    gl.glloadidentity ();//reset matrix to initial value
    gl.glfrustumf (-ratio, ratio,-1, 1, 3, 7)


2. Modify the Myglrenderer Ondrawframe () method, enable Modelview mode, and use Glu.glulookat () to set the viewpoint.

The code is as follows:

@Override public
void Ondrawframe (GL10 gl) {
  //TODO auto-generated method Stub
    gl.glclear (Gl10.gl_color_ Buffer_bit | Gl10.gl_depth_buffer_bit);
    Sets the current matrix to Model view mode
    Gl.glmatrixmode (gl10.gl_modelview);
    Gl.glloadidentity ();  Reset the matrix to it default state
    //Set Viewpoint
    Glu.glulookat (GL, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    Mtriangle.draw (GL);
}

In this way, we draw the proportion of the graph is always correct, no longer affected by the device is stretched and distorted.

For more information on Android-related content readers can view the site: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

Related Article

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.