Class VertexBufferObject

  • All Implemented Interfaces:
    com.badlogic.gdx.utils.Disposable, IntVertexData

    public class VertexBufferObject
    extends java.lang.Object
    implements IntVertexData

    A VertexData implementation based on OpenGL vertex buffer objects.

    If the OpenGL ES context was lost you can call invalidate() to recreate a new OpenGL vertex buffer object.

    The data is bound via glVertexAttribPointer() according to the attribute aliases specified via VertexAttributes in the constructor.

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

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        VertexBufferObject​(boolean isStatic, int numVertices, com.badlogic.gdx.graphics.VertexAttribute... attributes)
      Constructs a new interleaved VertexBufferObject.
        VertexBufferObject​(boolean isStatic, int numVertices, com.badlogic.gdx.graphics.VertexAttributes attributes)
      Constructs a new interleaved VertexBufferObject.
      protected VertexBufferObject​(int usage, java.nio.ByteBuffer data, boolean ownsBuffer, com.badlogic.gdx.graphics.VertexAttributes attributes)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void bind​(ExtShaderProgram shader)
      Binds this VertexBufferObject for rendering via glDrawArrays or glDrawElements
      void bind​(ExtShaderProgram shader, int[] locations)
      Binds this VertexData for rendering via glDrawArrays or glDrawElements.
      void dispose()
      Disposes of all resources this VertexBufferObject uses.
      com.badlogic.gdx.graphics.VertexAttributes getAttributes()  
      java.nio.FloatBuffer getBuffer()
      Returns the underlying FloatBuffer and marks it as dirty, causing the buffer contents to be uploaded on the next call to bind.
      int getNumMaxVertices()  
      int getNumVertices()  
      protected int getUsage()  
      void invalidate()
      Invalidates the VertexBufferObject so a new OpenGL buffer handle is created.
      protected void setBuffer​(java.nio.Buffer data, boolean ownsBuffer, com.badlogic.gdx.graphics.VertexAttributes value)
      Low level method to reset the buffer and attributes to the specified values.
      protected void setUsage​(int value)
      Set the GL enum used in the call to GL20.glBufferData(int, int, Buffer, int), can only be called when the VBO is not bound.
      void setVertices​(float[] vertices, int offset, int count)
      Sets the vertices of this VertexData, discarding the old vertex data.
      void unbind​(ExtShaderProgram shader)
      Unbinds this VertexBufferObject.
      void unbind​(ExtShaderProgram shader, int[] locations)
      Unbinds this VertexData.
      void updateVertices​(int targetOffset, float[] vertices, int sourceOffset, int count)
      Update (a portion of) the vertices.
      • Methods inherited from class java.lang.Object

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

      • VertexBufferObject

        public VertexBufferObject​(boolean isStatic,
                                  int numVertices,
                                  com.badlogic.gdx.graphics.VertexAttribute... attributes)
        Constructs a new interleaved VertexBufferObject.
        Parameters:
        isStatic - whether the vertex data is static.
        numVertices - the maximum number of vertices
        attributes - the VertexAttributes.
      • VertexBufferObject

        public VertexBufferObject​(boolean isStatic,
                                  int numVertices,
                                  com.badlogic.gdx.graphics.VertexAttributes attributes)
        Constructs a new interleaved VertexBufferObject.
        Parameters:
        isStatic - whether the vertex data is static.
        numVertices - the maximum number of vertices
        attributes - the VertexAttributes.
      • VertexBufferObject

        protected VertexBufferObject​(int usage,
                                     java.nio.ByteBuffer data,
                                     boolean ownsBuffer,
                                     com.badlogic.gdx.graphics.VertexAttributes attributes)
    • Method Detail

      • getAttributes

        public com.badlogic.gdx.graphics.VertexAttributes getAttributes()
        Specified by:
        getAttributes in interface IntVertexData
        Returns:
        the VertexAttributes as specified during construction.
      • getNumVertices

        public int getNumVertices()
        Specified by:
        getNumVertices in interface IntVertexData
        Returns:
        the number of vertices this VertexData stores
      • getNumMaxVertices

        public int getNumMaxVertices()
        Specified by:
        getNumMaxVertices in interface IntVertexData
        Returns:
        the number of vertices this VertedData can store
      • getBuffer

        public java.nio.FloatBuffer getBuffer()
        Description copied from interface: IntVertexData
        Returns the underlying FloatBuffer and marks it as dirty, causing the buffer contents to be uploaded on the next call to bind. If you need immediate uploading use IntVertexData.setVertices(float[], int, int); Any modifications made to the Buffer *after* the call to bind will not automatically be uploaded.
        Specified by:
        getBuffer in interface IntVertexData
        Returns:
        the underlying FloatBuffer holding the vertex data.
      • setBuffer

        protected void setBuffer​(java.nio.Buffer data,
                                 boolean ownsBuffer,
                                 com.badlogic.gdx.graphics.VertexAttributes value)
        Low level method to reset the buffer and attributes to the specified values. Use with care!
        Parameters:
        data -
        ownsBuffer -
        value -
      • setVertices

        public void setVertices​(float[] vertices,
                                int offset,
                                int count)
        Description copied from interface: IntVertexData
        Sets the vertices of this VertexData, discarding the old vertex data. The count must equal the number of floats per vertex times the number of vertices to be copied to this VertexData. The order of the vertex attributes must be the same as specified at construction time via VertexAttributes.

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

        Specified by:
        setVertices in interface IntVertexData
        Parameters:
        vertices - the vertex data
        offset - the offset to start copying the data from
        count - the number of floats to copy
      • updateVertices

        public void updateVertices​(int targetOffset,
                                   float[] vertices,
                                   int sourceOffset,
                                   int count)
        Description copied from interface: IntVertexData
        Update (a portion of) the vertices. Does not resize the backing buffer.
        Specified by:
        updateVertices in interface IntVertexData
        vertices - the vertex data
        sourceOffset - the offset to start copying the data from
        count - the number of floats to copy
      • getUsage

        protected int getUsage()
        Returns:
        The GL enum used in the call to GL20.glBufferData(int, int, Buffer, int), e.g. GL_STATIC_DRAW or GL_DYNAMIC_DRAW
      • setUsage

        protected void setUsage​(int value)
        Set the GL enum used in the call to GL20.glBufferData(int, int, Buffer, int), can only be called when the VBO is not bound.
      • bind

        public void bind​(ExtShaderProgram shader)
        Binds this VertexBufferObject for rendering via glDrawArrays or glDrawElements
        Specified by:
        bind in interface IntVertexData
        Parameters:
        shader - the shader
      • bind

        public void bind​(ExtShaderProgram shader,
                         int[] locations)
        Description copied from interface: IntVertexData
        Binds this VertexData for rendering via glDrawArrays or glDrawElements.
        Specified by:
        bind in interface IntVertexData
        locations - array containing the attribute locations.
      • unbind

        public void unbind​(ExtShaderProgram shader,
                           int[] locations)
        Description copied from interface: IntVertexData
        Unbinds this VertexData.
        Specified by:
        unbind in interface IntVertexData
        locations - array containing the attribute locations.
      • invalidate

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

        public void dispose()
        Disposes of all resources this VertexBufferObject uses.
        Specified by:
        dispose in interface com.badlogic.gdx.utils.Disposable
        Specified by:
        dispose in interface IntVertexData