Class VertexArray

  • All Implemented Interfaces:
    VertexData, Disposable

    public class VertexArray
    extends java.lang.Object
    implements VertexData

    Convenience class for working with OpenGL vertex arrays. It interleaves all data in the order you specified in the constructor via VertexAttribute.

    This class is not compatible with OpenGL 3+ core profiles. For this VertexBufferObjects are needed.

    • Constructor Summary

      Constructors 
      Constructor Description
      VertexArray​(int numVertices, VertexAttribute... attributes)
      Constructs a new interleaved VertexArray
      VertexArray​(int numVertices, VertexAttributes attributes)
      Constructs a new interleaved VertexArray
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void bind​(ShaderProgram shader)
      Binds this VertexData for rendering via glDrawArrays or glDrawElements.
      void bind​(ShaderProgram shader, int[] locations)
      Binds this VertexData for rendering via glDrawArrays or glDrawElements.
      void dispose()
      Disposes this VertexData and all its associated OpenGL resources.
      VertexAttributes getAttributes()  
      java.nio.FloatBuffer getBuffer()
      Deprecated.
      use getBuffer(boolean) instead
      java.nio.FloatBuffer getBuffer​(boolean forWriting)
      Returns the underlying FloatBuffer for reading or writing.
      int getNumMaxVertices()  
      int getNumVertices()  
      void invalidate()
      Invalidates the VertexData if applicable.
      void setVertices​(float[] vertices, int offset, int count)
      Sets the vertices of this VertexData, discarding the old vertex data.
      void unbind​(ShaderProgram shader)
      Unbinds this VertexBufferObject.
      void unbind​(ShaderProgram 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

      • VertexArray

        public VertexArray​(int numVertices,
                           VertexAttribute... attributes)
        Constructs a new interleaved VertexArray
        Parameters:
        numVertices - the maximum number of vertices
        attributes - the VertexAttributes
      • VertexArray

        public VertexArray​(int numVertices,
                           VertexAttributes attributes)
        Constructs a new interleaved VertexArray
        Parameters:
        numVertices - the maximum number of vertices
        attributes - the VertexAttributes
    • Method Detail

      • dispose

        public void dispose()
        Description copied from interface: VertexData
        Disposes this VertexData and all its associated OpenGL resources.
        Specified by:
        dispose in interface Disposable
        Specified by:
        dispose in interface VertexData
      • getBuffer

        @Deprecated
        public java.nio.FloatBuffer getBuffer()
        Deprecated.
        use getBuffer(boolean) instead
        Description copied from interface: VertexData
        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 VertexData.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 VertexData
        Returns:
        the underlying FloatBuffer holding the vertex data.
      • getBuffer

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

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

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

        public void setVertices​(float[] vertices,
                                int offset,
                                int count)
        Description copied from interface: VertexData
        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 VertexData
        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: VertexData
        Update (a portion of) the vertices. Does not resize the backing buffer.
        Specified by:
        updateVertices in interface VertexData
        vertices - the vertex data
        sourceOffset - the offset to start copying the data from
        count - the number of floats to copy
      • bind

        public void bind​(ShaderProgram shader)
        Description copied from interface: VertexData
        Binds this VertexData for rendering via glDrawArrays or glDrawElements.
        Specified by:
        bind in interface VertexData
      • bind

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

        public void unbind​(ShaderProgram shader)
        Unbinds this VertexBufferObject.
        Specified by:
        unbind in interface VertexData
        Parameters:
        shader - the shader
      • unbind

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

        public void invalidate()
        Description copied from interface: VertexData
        Invalidates the VertexData if applicable. Use this in case of a context loss.
        Specified by:
        invalidate in interface VertexData