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 theVertexBufferObject
class. Sets the glVertexAttribDivisor for everyVertexAttribute
automatically.
-
-
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 void
bind(ShaderProgram shader)
Binds this InstanceBufferObject for rendering via glDrawArraysInstanced or glDrawElementsInstancedvoid
bind(ShaderProgram shader, int[] locations)
Binds this InstanceData for rendering via glDrawArraysInstanced or glDrawElementsInstanced.void
dispose()
Disposes of all resources this InstanceBufferObject uses.VertexAttributes
getAttributes()
java.nio.FloatBuffer
getBuffer()
Deprecated.usegetBuffer(boolean)
insteadjava.nio.FloatBuffer
getBuffer(boolean forWriting)
Returns the underlying FloatBuffer for reading or writing.int
getNumInstances()
int
getNumMaxInstances()
protected int
getUsage()
void
invalidate()
Invalidates the InstanceBufferObject so a new OpenGL buffer handle is created.protected void
setBuffer(java.nio.Buffer data, boolean ownsBuffer, VertexAttributes value)
Low level method to reset the buffer and attributes to the specified values.void
setInstanceData(float[] data, int offset, int count)
Sets the vertices of this InstanceData, discarding the old vertex data.void
setInstanceData(java.nio.FloatBuffer data, int count)
Sets the vertices of this InstanceData, discarding the old vertex data.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.void
unbind(ShaderProgram shader)
Unbinds this InstanceBufferObject.void
unbind(ShaderProgram shader, int[] locations)
Unbinds this InstanceData.void
updateInstanceData(int targetOffset, float[] data, int sourceOffset, int count)
Update (a portion of) the vertices.void
updateInstanceData(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:
getAttributes
in interfaceInstanceData
- Returns:
- the
VertexAttributes
as specified during construction.
-
getNumInstances
public int getNumInstances()
- Specified by:
getNumInstances
in interfaceInstanceData
- Returns:
- the number of vertices this InstanceData stores
-
getNumMaxInstances
public int getNumMaxInstances()
- Specified by:
getNumMaxInstances
in interfaceInstanceData
- Returns:
- the number of vertices this InstanceData can store
-
getBuffer
@Deprecated public java.nio.FloatBuffer getBuffer()
Deprecated.usegetBuffer(boolean)
insteadDescription copied from interface:InstanceData
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 useInstanceData.setInstanceData(float[], int, int)
; Any modifications made to the Buffer *after* the call to bind will not automatically be uploaded.- Specified by:
getBuffer
in interfaceInstanceData
- Returns:
- the underlying FloatBuffer holding the vertex data.
-
getBuffer
public java.nio.FloatBuffer getBuffer(boolean forWriting)
Description copied from interface:InstanceData
Returns the underlying FloatBuffer for reading or writing.- Specified by:
getBuffer
in 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:InstanceData
Sets 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:
setInstanceData
in 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:InstanceData
Sets 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:
setInstanceData
in 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:InstanceData
Update (a portion of) the vertices. Does not resize the backing buffer.- Specified by:
updateInstanceData
in interfaceInstanceData
data
- 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:InstanceData
Update (a portion of) the vertices. Does not resize the backing buffer.- Specified by:
updateInstanceData
in interfaceInstanceData
data
- 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:
bind
in interfaceInstanceData
- Parameters:
shader
- the shader
-
bind
public void bind(ShaderProgram shader, int[] locations)
Description copied from interface:InstanceData
Binds this InstanceData for rendering via glDrawArraysInstanced or glDrawElementsInstanced.- Specified by:
bind
in interfaceInstanceData
locations
- array containing the attribute locations.
-
unbind
public void unbind(ShaderProgram shader)
Unbinds this InstanceBufferObject.- Specified by:
unbind
in interfaceInstanceData
- Parameters:
shader
- the shader
-
unbind
public void unbind(ShaderProgram shader, int[] locations)
Description copied from interface:InstanceData
Unbinds this InstanceData.- Specified by:
unbind
in interfaceInstanceData
locations
- 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:
invalidate
in interfaceInstanceData
-
dispose
public void dispose()
Disposes of all resources this InstanceBufferObject uses.- Specified by:
dispose
in interfaceDisposable
- Specified by:
dispose
in interfaceInstanceData
-
-