Native I/O

You can use the java.nio package to read and write data using block I/O operations. These operations let you send native, contiguous data to OpenGL® ES. In contrast, arrays in Java® are not contiguous and require a lot of copying. The java.nio package provides direct buffers to communicate with OpenGL® pointer functions, and wrapped buffers to access underlying Java arrays that cannot be passed to OpenGL pointer functions. Data types that are supported by the java.nio package include byte, short, int, and float.

Code sample: Defining the geometry for a 3-D cube using the java.nio package

final class Cube
{
    static FloatBuffer createVertexBuffer()
    {
        FloatBuffer buffer = ByteBuffer.allocateDirect
         (vertices.length * 4).asFloatBuffer();
        buffer.put(vertices);
        buffer.rewind();
        return buffer;
    }
    
    static FloatBuffer createNormalBuffer()
    {
        FloatBuffer buffer = ByteBuffer.allocateDirect
          (normals.length * 4).asFloatBuffer();
        buffer.put(normals);
        buffer.rewind();
        return buffer;
    }
    
    static FloatBuffer createTexCoordBuffer()
    {
        FloatBuffer buffer = ByteBuffer.allocateDirect
          (texCoords.length * 4).asFloatBuffer();
        buffer.put(texCoords);
        buffer.rewind();
        return buffer;
    }

    private static float[] vertices =
    {
        // front
        -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f,
        0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f,
        
        // right
        0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, -0.5f,
        0.5f, 0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f,
        
        // back
        0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f,
        -0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
        
        // left
        -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, 0.5f,
        -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f,
        
        // top
        -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f,
        0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f,
        
        // bottom
        -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f,
        0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f
    };
    
    private static float[] normals =
    {
        /* front */ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
        /* right */ 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
        /* back */ 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1,
        /* left */ -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0,
        /* top */ 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
        /* bottom */ 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0
    };
    
    private static float[] texCoords =
    {
        /* front */ 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1,
        /* right */ 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1,
        /* back */ 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1,
        /* left */ 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1,
        /* top */ 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1,
        /* bottom */ 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1
    };
}
Previous topic: Set up EGL

Was this information helpful? Send us your comments.