SoFunction
Updated on 2025-03-01

Example of Android drawing triangles on GLSurfaceView based on OpenGL and using projection and camera view method

This article describes the method of drawing triangles on GLSurfaceView based on OpenGL and using projection and camera view. Share it for your reference, as follows:

Defining a triangle

OpenGL allows us to define objects using three-dimensional coordinates. Before drawing a triangle, we need to define the coordinates of its points. We generally use arrays to store the coordinates of each vertex.

OpenGL ES Default [0,0,0] (X,Y,Z) is in the center of the GLSurfaceView, [1,1,0] is in the upper right corner, and [-1,-1,0] is in the lower left corner.

Draw a triangle

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

Experimental steps:

1. Add a new class Triangle

The code is as follows:

public class Triangle {
  public Triangle()
  {
     float triangleCoords[] = {
          // X, Y, Z This is an equilateral triangle          -0.5f, -0.25f, 0,
           0.5f, -0.25f, 0,
           0.0f, 0.559016994f, 0
        };
        // Initialize the vertex cache of the triangle        ByteBuffer vbb = (
            // (# of coordinate values * 4 bytes per float)
             * 4);
        (());// Use the byte order of the device hardware itself        triangleVB = (); // Create a floating point cache from ByteBuffer        (triangleCoords); // Add vertex coordinates to the floating point cache        (0); // Make the cache read the first coordinate  }
  public void draw(GL10 gl)
  {
    gl.glColor4f(0.63671875f, 0.76953125f, 0.22265625f, 0.0f); //Set the current color    (3, GL10.GL_FLOAT, 0, triangleVB);//Set vertex    (GL10.GL_TRIANGLES, 0, 3);//Draw triangle  }
  private FloatBuffer triangleVB;
}

2. Add the member privateTriangle in myGLRenderer class and initialize it in the constructor.

The code is as follows:

public myGLRenderer()
{
    mTriangle = new Triangle();
}

3. Add glEnableClientState() method to enable vertex array in the onSurfaceCreated() function of myGLRenderer class.

The code is as follows:

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub
    (0.5f, 0.5f, 0.5f, 1.0f);
    (GL10.GL_VERTEX_ARRAY);
}

4. Add a triangle drawing method to the onDrawFrame() function of myGLRenderer class.

The code is as follows:

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
    (GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    (gl);
}

In this way, we complete the drawing of a flat triangle. But we found that this triangle is not an equilateral triangle as we defined, because OpenGL generally assumes that our screen is a square so that when drawing, the final figure will be stretched according to the length and width ratio of the screen. To get the correct display, we need to project the graphics to the correct position. This function will be implemented in the next section.

Android device screens are usually not square, and OpenGL always project the square coordinate system onto this device by default, which causes the graphics 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 graphics to suit different devices' displays.

Experimental steps:

1. Modify the onSurfaceCreated() function of 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
    (0, 0, width, height);
    float ratio = (float) width / height;
    (GL10.GL_PROJECTION); // Set the current matrix as a projection matrix    (); // Reset the matrix to the initial value    (-ratio, ratio, -1, 1, 3, 7); // Set the projection matrix according to aspect ratio}

2. Modify the onDrawFrame() method of myGLRenderer, enable MODELVIEW mode, and use() to set the viewpoint.

The code is as follows:

@Override
public void onDrawFrame(GL10 gl) {
  // TODO Auto-generated method stub
    (GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    // Set the current matrix to model view mode    (GL10.GL_MODELVIEW);
    ();  // reset the matrix to its default state
    // Set viewpoint    (gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    (gl);
}

In this way, the scale of the graph we draw is always correct and is no longer stretched and deformed by the equipment.

For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.