E/S nativo
Puede utilizar el paquete java.nio para leer y escribir los datos mediante las operaciones de E/S de bloque. Estas operaciones le permiten enviar datos nativos y contiguos a OpenGL ES. Por el contrario, las matrices de Java no son contiguas y requieren de un proceso de copiado elevado. El paquete java.nio ofrece búferes directos para establecer la comunicación con las funciones de puntero de OpenGL y búferes envueltos para acceder a matrices de Java subyacentes que no puedan pasarse a las funciones de puntero de OpenGL. El paquete java.nio es compatible con los siguientes tipos de datos: byte, short, int y float.
Ejemplo de código: definición de la geometría de un cubo 3-D mediante el paquete java.nio
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
};
}
¿Le ha resultado útil esta información? Envíenos sus comentarios.