Android uses OpenGL ES to define image shapes

Source: Internet
Author: User

Defining shapes is often the first step to develop high-end graphics. You do not need to know how OpenGL ES wants you to define image objects when Drawing Images Using OpenGL ES. The following describes the coordination system related and screens, and defines shapes, such as triangles and circles.

Define a triangle:

OpenGL ES allows you to define objects in a 3D space.

Class Triangle {

Private FloatBuffer vertexBuffer;

// Number of coordinates per 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 the device hardware's native byte order
Bb. order (ByteOrder. nativeOrder ());

// Create a floating point buffer from the ByteBuffer
VertexBuffer = bb. asFloatBuffer ();
// Add the coordinates to the FloatBuffer
VertexBuffer. put (triangleCoords );
// Set the buffer to read the first coordinate
VertexBuffer. position (0 );
}


Define a rectangle:

Class Square {

Private FloatBuffer vertexBuffer;
Private writable buffer drawListBuffer;

// Number of coordinates per 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 (
// (# Of coordinate 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 (
// (# Of coordinate values * 2 bytes per short)
DrawOrder. length * 2 );
Dlb. order (ByteOrder. nativeOrder ());
DrawListBuffer = dlb. asw.buffer ();
DrawListBuffer. put (drawOrder );
DrawListBuffer. position (0 );
}
}

 

 

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.