Android ApiDemos example resolution (200): Graphics-& gt; OpenGL ES-& gt

Source: Internet
Author: User

The previous section introduced Android ApiDemos to skip the examples related to OpenGL, but to write a tutorial for OpenGL programming. Here we will add the parsing of OpenGL ES examples in Android ApiDemos.

Textured Triangle describes how to add materials to a graph. For detailed steps, see Android OpenGL ES concise development tutorial 7: Material rendering.

In this example, the corresponding Activity is TriangleActivity, which uses a StaticTriangleRenderer to draw a triangle. In this example, the res. raw. robot image is used as the material to add materials to the triangle. A key step for adding a material is UV coordinate ing, which maps the coordinates of a two-dimensional image to the image to be drawn.

A simple understanding can be understood in this way, such as making a kite, using bamboo to make a frame (ry), and then how to paste the kite paper (image) to the frame, how to fly a kite paper to the frame is similar to how OpenGL maps Texture coordinates to geometric coordinates.

The coordinates of the Two-dimensional image (material) are always as follows: the UV coordinates are defined as the upper left corner () and the lower right corner () (because the 2D Texture is used)

In this example, how can we map the coordinates of this material to triangles. Defines the coordinates of triangles and materials in the StaticTriangleRenderer example:

The corresponding code is as follows:


[Java]
// A unit-sided equilateral triangle centered on the origin.
Float [] coords = {
// X, Y, Z
-0.5f,-0.25f, 0,
0.5f,-0.25f, 0,
0.0f, 0.559016994f, 0
};
 
For (int I = 0; I <VERTS; I ++ ){
For (int j = 0; j <3; j ++ ){
MFVertexBuffer. put (coords [I * 3 + j] * 2.0f );
}
}
 
For (int I = 0; I <VERTS; I ++ ){
For (int j = 0; j <2; j ++ ){
MTexBuffer. put (coords [I * 3 + j] * 2.0f + 0.5f );
}
}
 
For (int I = 0; I <VERTS; I ++ ){
MIndexBuffer. put (short) I );
}

// A unit-sided equilateral triangle centered on the origin.
Float [] coords = {
// X, Y, Z
-0.5f,-0.25f, 0,
0.5f,-0.25f, 0,
0.0f, 0.559016994f, 0
};

For (int I = 0; I <VERTS; I ++ ){
For (int j = 0; j <3; j ++ ){
MFVertexBuffer. put (coords [I * 3 + j] * 2.0f );
}
}

For (int I = 0; I <VERTS; I ++ ){
For (int j = 0; j <2; j ++ ){
MTexBuffer. put (coords [I * 3 + j] * 2.0f + 0.5f );
}
}

For (int I = 0; I <VERTS; I ++ ){
MIndexBuffer. put (short) I );
}

 

We can see the correspondence between triangle vertices and Texture coordinates. Therefore, the result in this example (without rotation angle) is.

The triangle is a positive triangle, but because of the Texture ing relationship, Android robot images are inverted.

 

 

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.