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 );
}
}