Android stereo graphics-Triangle

Source: Internet
Author: User

 

In fact, it is very easy to draw a triangle, that is, there are four triangles, so the triangle will be painted, it is best to study it first and then look at the Code, it is not difficult, try it

 

 

 

 

 

 

 

Package wyf. swq;

Import java. nio. ByteBuffer; // bring the relevant package

Import java. nio. ByteOrder; // bring the relevant package

Import java. nio. IntBuffer; // bring the relevant package

Import javax. microedition. khronos. opengles. GL10; // bring the relevant package

Public class TrianglePair {

Private IntBuffer myVertexBuffer; // vertex coordinate data buffer

Private IntBuffer myColorBuffer; // The buffer of vertex coloring data.

Private ByteBuffer myIndexBuffer; // data buffer of vertex Indexes

Int vCount = 0; // Number of vertices

Int iCount = 0; // Number of Indexes

Float yAngle = 0; // The angle of rotation around the Y axis

Float zAngle = 0; // The rotation angle around the Z axis

Public TrianglePair (){

VCount = 4; // a triangle with three vertices

Final int UNIT_SIZE = 10000; // scaling ratio

Int [] vertices = new int [] {

0, 0, 0,

8 * UNIT_SIZE, 0, 0,

0, 8 * UNIT_SIZE, 0,

0, 0, 8 * UNIT_SIZE

// 8 * UNIT_SIZE, 10 * UNIT_SIZE, 0,

// 2 * UNIT_SIZE, 10 * UNIT_SIZE, 0

};

// Create the vertex coordinate data cache. Because the byte sequence is different on different platforms, the data unit is not byte (the above integer cache) and must undergo ByteBuffer conversion, the key is to set nativeOrder () through ByteOrder ()

ByteBuffer vbb = ByteBuffer. allocateDirect (vertices. length * 4); // allocated memory block

Vbb. order (ByteOrder. nativeOrder (); // sets the byte sequence of the local platform.

MyVertexBuffer = vbb. asIntBuffer (); // convert to int Type Buffer

MyVertexBuffer. put (vertices); // place the vertex coordinate data into the buffer.

MyVertexBuffer. position (0); // you can specify the starting position of the buffer.

Final int one = 65535; // supports 65535 Gbit/s.

Int [] colors = new int [] {// an array of vertex color values. Each vertex has four color values, RGBA.

One, 0,

0, 0, one, 0,

0, 0, one, 0,

One, 0,

One, 0, 0, 0,

One, 0, 0

};

ByteBuffer cbb = ByteBuffer. allocateDirect (colors. length * 4); // allocated memory block

Cbb. order (ByteOrder. nativeOrder (); // sets the byte sequence of the local platform.

MyColorBuffer = cbb. asIntBuffer (); // convert to int Type Buffer

MyColorBuffer. put (colors); // Add the vertex color data to the buffer.

MyColorBuffer. position (0); // you can specify the starting position of the buffer.

// Initialize the Triangle Index data

ICount = 12;

Byte [] indices = new byte [] {

0, 1, 2,

0, 1, 3,

0, 2, 3,

1, 2, 3

};

// Create a triangle to construct the index data buffer

MyIndexBuffer = ByteBuffer. allocateDirect (indices. length); // The allocated memory block.

MyIndexBuffer. put (indices); // Insert the vertex index data to the buffer.

MyIndexBuffer. position (0); // you can specify the starting position of the buffer.

}

Public void drawSelf (GL10 gl ){

Gl. glableclientstate (GL10.GL _ VERTEX_ARRAY); // enables the vertex coordinate array.

Gl. glableclientstate (GL10.GL _ COLOR_ARRAY); // enables the vertex color array.

Gl. glRotatef (yAngle, 0); // rotate yAngle around the Y axis according to the yAngle value.

Gl. glRotatef (zAngle, 0, 0, 1 );

Gl. glVertexPointer (// specify vertex coordinate data for the paint brush

3, // The number of coordinates for each vertex is 3

GL10.GL _ FIXED, // The vertex coordinate value type is GL_FIXED, integer

0, // The interval between consecutive vertex coordinate data

MyVertexBuffer // Number of vertex coordinates

);

Gl. glColorPointer (// specify vertex color data for the paint brush

4,

GL10.GL _ FIXED,

0,

MyColorBuffer

);

Gl. glDrawElements (// draw a graph

GL10.GL _ TRIANGLES, // fill mode, which is filled in TRIANGLES

ICount ,//

GL10.GL _ UNSIGNED_BYTE, // start point number

MyIndexBuffer // Number of vertices

);

}}

From: dlnuchunge's column

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.