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.