The following code is used in the book for Android OpenGL es:
1) Create three vertices
Private Intbuffer Triggerbuffer = Intbuffer.wrap (new int[]{
0,one,0,
-one,-one,0,
One,-one,0,
});
2) then use Triggerbuffer to draw vertices
For example: Gl.glvertexpointer (3, gl10.gl_fixed, 0,triggerbuffer);
Often appears:must use a native error in order direct buffer .
/*
* OpenGL is a very low-level drawing interface that uses a buffer storage structure that is not the same as in our Java program.
* Java is a big-endian byte-order (Bigedian), and OpenGL requires data that is a small-endian byte order (Littleedian).
* Therefore, we need to do some work when converting Java buffers into OpenGL-usable buffers. Here's how to build a buff
* */
To solve this problem we can create a tool class:
Import Java.nio.bytebuffer;import java.nio.byteorder;import java.nio.floatbuffer;import java.nio.IntBuffer;/** * Created by Lenovo on 2016/9/6. */public class Bufferutil {public static Floatbuffer Floattobuffer (float[] a) {//Initialize buffer first, the length of the array is, because a float takes up 4 bytes Bytebuffer MBB = Bytebuffer.allocatedirect (A.length * 4); Array sorting with Nativeorder, according to the local arrangement order, the specified storage mode, is 1. Little endian (small head): Stores low-order bytes at the start address//2. Big endian: High-order bytes are stored in the start Address Mbb.order (Byteorder.nativeorder ()); Floatbuffer Mbuffer = Mbb.asfloatbuffer (); Mbuffer.put (a); Mbuffer.position (0); return mbuffer; }//convert array A to intbuffer public static Intbuffer Inttobuffer (int[] a) {//Initialize buffer first, the length of the array as a float takes 4 bytes Bytebuffer MBB = Bytebuffer.allocatedirect (A.length * 4); Array sorting with Nativeorder Mbb.order (Byteorder.nativeorder ()); Intbuffer mBuffer2 = Mbb.asintbuffer (); Mbuffer2.put (a); Mbuffer2.position (0); return mBuffer2; }//creates a length of floatbuffer, which is stored in OpenGL mode, and position adds 1 after each call to the put join point, so the join point resets position to 0 public static at the time of the drawing Floatbuffer getfloatbuffer (int length) {Bytebuffer MBB = bytebuffer.allocatedirect (length * 4); Mbb.order (Byteorder.nativeorder ()); Floatbuffer Mbuffer = Mbb.asfloatbuffer (); Mbuffer.position (0); return mbuffer; public static Intbuffer getintbuffer (int length) {Bytebuffer MBB = bytebuffer.allocatedirect (length * 4); Mbb.order (Byteorder.nativeorder ()); Intbuffer Mbuffer = Mbb.asintbuffer (); Mbuffer.position (0); return mbuffer; }}
PS: Create a length of floatbuffer time, the storage mode is OpenGL storage mode, after each call put join point position will add 1, so join point after the drawing time to reset the position to 0
Floatbuffer and Intbuffer in OpenGL and how data in Java is stored differently