Code example for CharBuffer

Methods: capacity, limit, rewind

0
            if (gl instanceof GL11) {
                GL11 gl11 = (GL11)gl;
                int[] buffer = new int[1];
                 
                mVertices.rewind();
                mIndices.rewind();
                 
                // Allocate and fill the vertex buffer. 
                gl11.glGenBuffers(1, buffer, 0);
                mVertBufferIndex = buffer[0];
                gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertBufferIndex);
                final int vertexSize = mVertices.capacity() * 4;
                gl11.glBufferData(GL11.GL_ARRAY_BUFFER, vertexSize, 
                        mVertices, GL11.GL_STATIC_DRAW);
                 
                if(mNormals != null) {
                	mNormals.rewind();
                	gl11.glGenBuffers(1, buffer, 0);
                    mNormalBufferIndex = buffer[0];
                    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mNormalBufferIndex);
                    final int normalSize = mNormals.capacity() * 4;
                    gl11.glBufferData(GL11.GL_ARRAY_BUFFER, normalSize, 
                            mNormals, GL11.GL_STATIC_DRAW);
                } 
                if(mColors != null) {
                	mColors.rewind();
                	gl11.glGenBuffers(1, buffer, 0);
                    mColorBufferIndex = buffer[0];
                    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mColorBufferIndex);
                    final int colorSize = mColors.capacity();
                    gl11.glBufferData(GL11.GL_ARRAY_BUFFER, colorSize, 
                            mColors, GL11.GL_STATIC_DRAW);
                } 
                if(mTexcoords != null) {
                	mTexcoords.rewind();
                	gl11.glGenBuffers(1, buffer, 0);
                    mTexCoordsBufferIndex = buffer[0];
                    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTexCoordsBufferIndex);
                    final int texcoordSize = mTexcoords.capacity() * 4;
                    gl11.glBufferData(GL11.GL_ARRAY_BUFFER, texcoordSize, 
                            mTexcoords, GL11.GL_STATIC_DRAW);
                } 
                 
                // Unbind the array buffer. 
                gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
                 
                // Allocate and fill the index buffer. 
                gl11.glGenBuffers(1, buffer, 0);
                mIndexBufferIndex = buffer[0];
                gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 
                        mIndexBufferIndex);
                // A char is 2 bytes. 
                final int indexSize = mIndices.capacity() * 2;
                gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, indexSize, mIndices, GL11.GL_STATIC_DRAW);
                 
                // Unbind the element array buffer. 
                gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
                 
                mIndexCount = mIndices.limit();
                 
                // TODO are we safe so delete the java.nio.buffers now?? 
                // Probably not... since we can lose context every now and then. 
            } 
        }