Android-OpenGL ES Shape Definition

Source: Internet
Author: User

This article translated from: http://developer.android.com/training/graphics/opengl/shapes.html

The first step to create an advanced image is to define the image in the context of the view of OpenGL ES to be drawn. If you do not understand some basic requirements of OpenGL ES to define graphical objects, it is difficult to use OpenGL ES to draw graphics.

This article describes the definition of OpenGL ES, the coordinate system, graphics and appearance, and the definition of triangles and rectangles relative to the Android device screen.

Define triangle

OpenGL ES allows you to use 3D space coordinates to define the graphic objects to be drawn. Therefore, before creating a triangle, you must define its coordinates. In OpenGL, the common practice of defining coordinates is to define a floating point vertex array for coordinates. To get the highest efficiency, you need to write these coordinates into a bytebutter, which will be passed to the image processing channel of opengles.

Classtriangle {

Private floatbuffervertexbuffer;

// Number of coordinatesper vertex in this array
Static final int coords_per_vertex = 3;
Static float trianglecoords [] = {// in counterclockwise order:
0.0f, 0.622008459f, 0.0f, // top
-0.5f,-0.3110000003f, 0.0f, // bottom left
0.5f,-0.3110000003f, 0.0f // bottom right
};

// Set color with red, green, blue and alpha (opacity) Values
Float color [] = {0.64251875f, 0.76953125f, 0.22265625f, 1.0f };

Public triangle (){
// Initialize vertex byte buffer for shape coordinates
Bytebuffer BB = bytebuffer. allocatedirect (
// (Number of coordinate values * 4 bytes per float)
Trianglecoords. length * 4 );
// Use thedevice hardware's native byte order
BB. Order (byteorder. nativeorder ());

// Create afloating point buffer from the bytebuffer
Vertexbuffer = BB. asfloatbuffer ();
// Add thecoordinates to the floatbuffer
Vertexbuffer. Put (trianglecoords );
// Set thebuffer to read the first coordinate
Vertexbuffer. Position (0 );
}
}

By default, OpenGL ES assumes that the origin of the coordinate system [0, 0] (x, y, z) is in the center of the glsurfaceview frame, and [, 0] is the upper right corner of the frame, [-1,-] is the lower left corner of the framework. For examples of the cut coordinate system, see OpenGL
Es developer guide.

Note that the coordinates of the image are defined in a counter-clockwise order. The order of drawing is very important, because it defines which side is the graphic surface to be drawn, and uses OpenGL ES to select the surface features, can choose not to draw the back. For more information about the surface and selection, see OpenGL
Es developer guide.

Define rectangle

It is quite easy to define the triangle form in OpenGL, but if you want to obtain a slightly more complex image, such as a rectangle. There are many ways to do this, but in OpenGL ES, the typical path for drawing such a graph is to use two triangles drawn together:

Figure 1. Draw a rectangle using two triangles

Also, you should define the vertices for the two triangles representing the rectangle in a counter-clockwise order and put these vertices in a bytebuffer. To avoid repeated definitions of the coordinates of two triangles, we used a drawing list to tell OpenGL ES how the graphic pipelines draw these vertices. The following is the drawing code:

Classsquare {

Private floatbuffer vertexbuffer;
Private writable bufferdrawlistbuffer;

// Number of coordinatesper vertex in this array
Static final int coords_per_vertex = 3;
Static float squarecoords [] = {-0.5f, 0.5f, 0.0f, // top left
-0.5f,-0.5f, 0.0f, // bottom left
0.5f,-0.5f, 0.0f, // bottom right
0.5f, 0.5f, 0.0f}; // top right

Private short draworder [] = {0, 1, 2, 0, 2, 3}; // order to draw vertices

Public Square (){
// Initialize vertex byte buffer for shape coordinates
Bytebuffer BB = bytebuffer. allocatedirect (
// (# Ofcoordinate values * 4 bytes per float)
Squarecoords. length * 4 );
BB. Order (byteorder. nativeorder ());
Vertexbuffer = BB. asfloatbuffer ();
Vertexbuffer. Put (squarecoords );
Vertexbuffer. Position (0 );

// Initialize byte buffer for the draw list
Bytebuffer DLB = bytebuffer. allocatedirect (
// (# Ofcoordinate values * 2 bytes per Short)
Draworder. length * 2 );
DLB. Order (byteorder. nativeorder ());
Drawlistbuffer = DLB. asw.buffer ();
Drawlistbuffer. Put (draworder );
Drawlistbuffer. Position (0 );
}
}

This example shows how to use OpenGL to draw complex graphics. Generally, you need to use a set of triangles to draw objects.

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.