Class VertexBufferObject
- java.lang.Object
-
- com.badlogic.gdx.graphics.glutils.VertexBufferObject
-
- All Implemented Interfaces:
VertexData,Disposable
public class VertexBufferObject extends java.lang.Object implements VertexData
A
VertexDataimplementation 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
VertexAttributesin 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, VertexAttribute... attributes)Constructs a new interleaved VertexBufferObject.VertexBufferObject(boolean isStatic, int numVertices, VertexAttributes attributes)Constructs a new interleaved VertexBufferObject.protectedVertexBufferObject(int usage, java.nio.ByteBuffer data, boolean ownsBuffer, VertexAttributes attributes)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidbind(ShaderProgram shader)Binds this VertexBufferObject for rendering via glDrawArrays or glDrawElementsvoidbind(ShaderProgram shader, int[] locations)Binds this VertexData for rendering via glDrawArrays or glDrawElements.voiddispose()Disposes of all resources this VertexBufferObject uses.VertexAttributesgetAttributes()java.nio.FloatBuffergetBuffer()Deprecated.usegetBuffer(boolean)insteadjava.nio.FloatBuffergetBuffer(boolean forWriting)Returns the underlying FloatBuffer for reading or writing.intgetNumMaxVertices()intgetNumVertices()protected intgetUsage()voidinvalidate()Invalidates the VertexBufferObject so a new OpenGL buffer handle is created.protected voidsetBuffer(java.nio.Buffer data, boolean ownsBuffer, VertexAttributes value)Low level method to reset the buffer and attributes to the specified values.protected voidsetUsage(int value)Set the GL enum used in the call toGL20.glBufferData(int, int, java.nio.Buffer, int), can only be called when the VBO is not bound.voidsetVertices(float[] vertices, int offset, int count)Sets the vertices of this VertexData, discarding the old vertex data.voidunbind(ShaderProgram shader)Unbinds this VertexBufferObject.voidunbind(ShaderProgram shader, int[] locations)Unbinds this VertexData.voidupdateVertices(int targetOffset, float[] vertices, int sourceOffset, int count)Update (a portion of) the vertices.
-
-
-
Constructor Detail
-
VertexBufferObject
public VertexBufferObject(boolean isStatic, int numVertices, VertexAttribute... attributes)Constructs a new interleaved VertexBufferObject.- Parameters:
isStatic- whether the vertex data is static.numVertices- the maximum number of verticesattributes- theVertexAttributes.
-
VertexBufferObject
public VertexBufferObject(boolean isStatic, int numVertices, VertexAttributes attributes)Constructs a new interleaved VertexBufferObject.- Parameters:
isStatic- whether the vertex data is static.numVertices- the maximum number of verticesattributes- theVertexAttributes.
-
VertexBufferObject
protected VertexBufferObject(int usage, java.nio.ByteBuffer data, boolean ownsBuffer, VertexAttributes attributes)
-
-
Method Detail
-
getAttributes
public VertexAttributes getAttributes()
- Specified by:
getAttributesin interfaceVertexData- Returns:
- the
VertexAttributesas specified during construction.
-
getNumVertices
public int getNumVertices()
- Specified by:
getNumVerticesin interfaceVertexData- Returns:
- the number of vertices this VertexData stores
-
getNumMaxVertices
public int getNumMaxVertices()
- Specified by:
getNumMaxVerticesin interfaceVertexData- Returns:
- the number of vertices this VertedData can store
-
getBuffer
@Deprecated public java.nio.FloatBuffer getBuffer()
Deprecated.usegetBuffer(boolean)insteadDescription copied from interface:VertexDataReturns 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 useVertexData.setVertices(float[], int, int); Any modifications made to the Buffer *after* the call to bind will not automatically be uploaded.- Specified by:
getBufferin interfaceVertexData- Returns:
- the underlying FloatBuffer holding the vertex data.
-
getBuffer
public java.nio.FloatBuffer getBuffer(boolean forWriting)
Description copied from interface:VertexDataReturns the underlying FloatBuffer for reading or writing.- Specified by:
getBufferin interfaceVertexData- Parameters:
forWriting- when true, the underlying buffer will be uploaded on the next call to bind. If you need immediate uploading useVertexData.setVertices(float[], int, int).- Returns:
- the underlying FloatBuffer holding the vertex data.
-
setBuffer
protected void setBuffer(java.nio.Buffer data, boolean ownsBuffer, 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:VertexDataSets 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 viaVertexAttributes.This can be called in between calls to bind and unbind. The vertex data will be updated instantly.
- Specified by:
setVerticesin interfaceVertexData- Parameters:
vertices- the vertex dataoffset- the offset to start copying the data fromcount- the number of floats to copy
-
updateVertices
public void updateVertices(int targetOffset, float[] vertices, int sourceOffset, int count)Description copied from interface:VertexDataUpdate (a portion of) the vertices. Does not resize the backing buffer.- Specified by:
updateVerticesin interfaceVertexDatavertices- the vertex datasourceOffset- the offset to start copying the data fromcount- the number of floats to copy
-
getUsage
protected int getUsage()
- Returns:
- The GL enum used in the call to
GL20.glBufferData(int, int, java.nio.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 toGL20.glBufferData(int, int, java.nio.Buffer, int), can only be called when the VBO is not bound.
-
bind
public void bind(ShaderProgram shader)
Binds this VertexBufferObject for rendering via glDrawArrays or glDrawElements- Specified by:
bindin interfaceVertexData- Parameters:
shader- the shader
-
bind
public void bind(ShaderProgram shader, int[] locations)
Description copied from interface:VertexDataBinds this VertexData for rendering via glDrawArrays or glDrawElements.- Specified by:
bindin interfaceVertexDatalocations- array containing the attribute locations.
-
unbind
public void unbind(ShaderProgram shader)
Unbinds this VertexBufferObject.- Specified by:
unbindin interfaceVertexData- Parameters:
shader- the shader
-
unbind
public void unbind(ShaderProgram shader, int[] locations)
Description copied from interface:VertexDataUnbinds this VertexData.- Specified by:
unbindin interfaceVertexDatalocations- 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:
invalidatein interfaceVertexData
-
dispose
public void dispose()
Disposes of all resources this VertexBufferObject uses.- Specified by:
disposein interfaceDisposable- Specified by:
disposein interfaceVertexData
-
-