Class InstanceBufferObject
- java.lang.Object
-
- com.badlogic.gdx.graphics.glutils.InstanceBufferObject
-
- All Implemented Interfaces:
InstanceData,Disposable
public class InstanceBufferObject extends java.lang.Object implements InstanceData
Modification of theVertexBufferObjectclass. Sets the glVertexAttribDivisor for everyVertexAttributeautomatically.
-
-
Constructor Summary
Constructors Constructor Description InstanceBufferObject(boolean isStatic, int numVertices, VertexAttribute... attributes)InstanceBufferObject(boolean isStatic, int numVertices, VertexAttributes instanceAttributes)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidbind(ShaderProgram shader)Binds this InstanceBufferObject for rendering via glDrawArraysInstanced or glDrawElementsInstancedvoidbind(ShaderProgram shader, int[] locations)Binds this InstanceData for rendering via glDrawArraysInstanced or glDrawElementsInstanced.voiddispose()Disposes of all resources this InstanceBufferObject uses.VertexAttributesgetAttributes()java.nio.FloatBuffergetBuffer()Deprecated.usegetBuffer(boolean)insteadjava.nio.FloatBuffergetBuffer(boolean forWriting)Returns the underlying FloatBuffer for reading or writing.intgetNumInstances()intgetNumMaxInstances()protected intgetUsage()voidinvalidate()Invalidates the InstanceBufferObject 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.voidsetInstanceData(float[] data, int offset, int count)Sets the vertices of this InstanceData, discarding the old vertex data.voidsetInstanceData(java.nio.FloatBuffer data, int count)Sets the vertices of this InstanceData, discarding the old vertex data.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.voidunbind(ShaderProgram shader)Unbinds this InstanceBufferObject.voidunbind(ShaderProgram shader, int[] locations)Unbinds this InstanceData.voidupdateInstanceData(int targetOffset, float[] data, int sourceOffset, int count)Update (a portion of) the vertices.voidupdateInstanceData(int targetOffset, java.nio.FloatBuffer data, int sourceOffset, int count)Update (a portion of) the vertices.
-
-
-
Constructor Detail
-
InstanceBufferObject
public InstanceBufferObject(boolean isStatic, int numVertices, VertexAttribute... attributes)
-
InstanceBufferObject
public InstanceBufferObject(boolean isStatic, int numVertices, VertexAttributes instanceAttributes)
-
-
Method Detail
-
getAttributes
public VertexAttributes getAttributes()
- Specified by:
getAttributesin interfaceInstanceData- Returns:
- the
VertexAttributesas specified during construction.
-
getNumInstances
public int getNumInstances()
- Specified by:
getNumInstancesin interfaceInstanceData- Returns:
- the number of vertices this InstanceData stores
-
getNumMaxInstances
public int getNumMaxInstances()
- Specified by:
getNumMaxInstancesin interfaceInstanceData- Returns:
- the number of vertices this InstanceData can store
-
getBuffer
@Deprecated public java.nio.FloatBuffer getBuffer()
Deprecated.usegetBuffer(boolean)insteadDescription copied from interface:InstanceDataReturns 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 useInstanceData.setInstanceData(float[], int, int); Any modifications made to the Buffer *after* the call to bind will not automatically be uploaded.- Specified by:
getBufferin interfaceInstanceData- Returns:
- the underlying FloatBuffer holding the vertex data.
-
getBuffer
public java.nio.FloatBuffer getBuffer(boolean forWriting)
Description copied from interface:InstanceDataReturns the underlying FloatBuffer for reading or writing.- Specified by:
getBufferin interfaceInstanceData- Parameters:
forWriting- when true, the underlying buffer will be uploaded on the next call to bind. If you need immediate uploading useInstanceData.setInstanceData(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-
-
setInstanceData
public void setInstanceData(float[] data, int offset, int count)Description copied from interface:InstanceDataSets the vertices of this InstanceData, 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:
setInstanceDatain interfaceInstanceData- Parameters:
data- the instance dataoffset- the offset to start copying the data fromcount- the number of floats to copy
-
setInstanceData
public void setInstanceData(java.nio.FloatBuffer data, int count)Description copied from interface:InstanceDataSets the vertices of this InstanceData, 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 InstanceData. 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:
setInstanceDatain interfaceInstanceData- Parameters:
data- the instance datacount- the number of floats to copy
-
updateInstanceData
public void updateInstanceData(int targetOffset, float[] data, int sourceOffset, int count)Description copied from interface:InstanceDataUpdate (a portion of) the vertices. Does not resize the backing buffer.- Specified by:
updateInstanceDatain interfaceInstanceDatadata- the instance datasourceOffset- the offset to start copying the data fromcount- the number of floats to copy
-
updateInstanceData
public void updateInstanceData(int targetOffset, java.nio.FloatBuffer data, int sourceOffset, int count)Description copied from interface:InstanceDataUpdate (a portion of) the vertices. Does not resize the backing buffer.- Specified by:
updateInstanceDatain interfaceInstanceDatadata- 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 InstanceBufferObject for rendering via glDrawArraysInstanced or glDrawElementsInstanced- Specified by:
bindin interfaceInstanceData- Parameters:
shader- the shader
-
bind
public void bind(ShaderProgram shader, int[] locations)
Description copied from interface:InstanceDataBinds this InstanceData for rendering via glDrawArraysInstanced or glDrawElementsInstanced.- Specified by:
bindin interfaceInstanceDatalocations- array containing the attribute locations.
-
unbind
public void unbind(ShaderProgram shader)
Unbinds this InstanceBufferObject.- Specified by:
unbindin interfaceInstanceData- Parameters:
shader- the shader
-
unbind
public void unbind(ShaderProgram shader, int[] locations)
Description copied from interface:InstanceDataUnbinds this InstanceData.- Specified by:
unbindin interfaceInstanceDatalocations- array containing the attribute locations.
-
invalidate
public void invalidate()
Invalidates the InstanceBufferObject so a new OpenGL buffer handle is created. Use this in case of a context loss.- Specified by:
invalidatein interfaceInstanceData
-
dispose
public void dispose()
Disposes of all resources this InstanceBufferObject uses.- Specified by:
disposein interfaceDisposable- Specified by:
disposein interfaceInstanceData
-
-