Class IndexBufferObject

  • All Implemented Interfaces:
    IndexData, Disposable

    public class IndexBufferObject
    extends java.lang.Object
    implements IndexData

    In IndexBufferObject wraps OpenGL's index buffer functionality to be used in conjunction with VBOs. This class can be seamlessly used with OpenGL ES 1.x and 2.0.

    Uses indirect Buffers on Android 1.5/1.6 to fix GC invocation due to leaking PlatformAddress instances.

    You can also use this to store indices for vertex arrays. Do not call bind() or unbind() in this case but rather use getBuffer() to use the buffer directly with glDrawElements. You must also create the IndexBufferObject with the second constructor and specify isDirect as true as glDrawElements in conjunction with vertex arrays needs direct buffers.

    VertexBufferObjects must be disposed via the dispose() method when no longer needed

    • Constructor Summary

      Constructors 
      Constructor Description
      IndexBufferObject​(boolean isStatic, int maxIndices)
      Creates a new IndexBufferObject.
      IndexBufferObject​(boolean isStatic, java.nio.ByteBuffer data)  
      IndexBufferObject​(int maxIndices)
      Creates a new static IndexBufferObject to be used with vertex arrays.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void bind()
      Binds this IndexBufferObject for rendering with glDrawElements.
      void dispose()
      Disposes this IndexBufferObject and all its associated OpenGL resources.
      java.nio.ShortBuffer getBuffer()
      Deprecated.
      use getBuffer(boolean) instead
      java.nio.ShortBuffer getBuffer​(boolean forWriting)
      Returns the underlying ShortBuffer for reading or writing.
      int getNumIndices()  
      int getNumMaxIndices()  
      void invalidate()
      Invalidates the IndexBufferObject so a new OpenGL buffer handle is created.
      void setIndices​(short[] indices, int offset, int count)
      Sets the indices of this IndexBufferObject, discarding the old indices.
      void setIndices​(java.nio.ShortBuffer indices)
      Copies the specified indices to the indices of this IndexBufferObject, discarding the old indices.
      void unbind()
      Unbinds this IndexBufferObject.
      void updateIndices​(int targetOffset, short[] indices, int offset, int count)
      Update (a portion of) the indices.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IndexBufferObject

        public IndexBufferObject​(int maxIndices)
        Creates a new static IndexBufferObject to be used with vertex arrays.
        Parameters:
        maxIndices - the maximum number of indices this buffer can hold
      • IndexBufferObject

        public IndexBufferObject​(boolean isStatic,
                                 int maxIndices)
        Creates a new IndexBufferObject.
        Parameters:
        isStatic - whether the index buffer is static
        maxIndices - the maximum number of indices this buffer can hold
      • IndexBufferObject

        public IndexBufferObject​(boolean isStatic,
                                 java.nio.ByteBuffer data)
    • Method Detail

      • getNumIndices

        public int getNumIndices()
        Specified by:
        getNumIndices in interface IndexData
        Returns:
        the number of indices currently stored in this buffer
      • getNumMaxIndices

        public int getNumMaxIndices()
        Specified by:
        getNumMaxIndices in interface IndexData
        Returns:
        the maximum number of indices this IndexBufferObject can store.
      • setIndices

        public void setIndices​(short[] indices,
                               int offset,
                               int count)

        Sets the indices of this IndexBufferObject, discarding the old indices. The count must equal the number of indices to be copied to this IndexBufferObject.

        This can be called in between calls to bind() and unbind(). The index data will be updated instantly.

        Specified by:
        setIndices in interface IndexData
        Parameters:
        indices - the vertex data
        offset - the offset to start copying the data from
        count - the number of shorts to copy
      • setIndices

        public void setIndices​(java.nio.ShortBuffer indices)
        Description copied from interface: IndexData
        Copies the specified indices to the indices of this IndexBufferObject, discarding the old indices. Copying start at the current Buffer.position() of the specified buffer and copied the Buffer.remaining() amount of indices. This can be called in between calls to IndexData.bind() and IndexData.unbind(). The index data will be updated instantly.
        Specified by:
        setIndices in interface IndexData
        Parameters:
        indices - the index data to copy
      • updateIndices

        public void updateIndices​(int targetOffset,
                                  short[] indices,
                                  int offset,
                                  int count)
        Description copied from interface: IndexData
        Update (a portion of) the indices.
        Specified by:
        updateIndices in interface IndexData
        Parameters:
        targetOffset - offset in indices buffer
        indices - the index data
        offset - the offset to start copying the data from
        count - the number of shorts to copy
      • getBuffer

        public java.nio.ShortBuffer getBuffer​(boolean forWriting)
        Description copied from interface: IndexData
        Returns the underlying ShortBuffer for reading or writing.
        Specified by:
        getBuffer in interface IndexData
        Parameters:
        forWriting - when true, the underlying buffer will be uploaded on the next call to IndexData.bind(). If you need immediate uploading use IndexData.setIndices(short[], int, int).
        Returns:
        the underlying short buffer.
      • bind

        public void bind()
        Binds this IndexBufferObject for rendering with glDrawElements.
        Specified by:
        bind in interface IndexData
      • unbind

        public void unbind()
        Unbinds this IndexBufferObject.
        Specified by:
        unbind in interface IndexData
      • invalidate

        public void invalidate()
        Invalidates the IndexBufferObject so a new OpenGL buffer handle is created. Use this in case of a context loss.
        Specified by:
        invalidate in interface IndexData
      • dispose

        public void dispose()
        Disposes this IndexBufferObject and all its associated OpenGL resources.
        Specified by:
        dispose in interface Disposable
        Specified by:
        dispose in interface IndexData