Class VertexBufferObjectWithVAO
- java.lang.Object
-
- com.badlogic.gdx.graphics.glutils.VertexBufferObjectWithVAO
-
- All Implemented Interfaces:
VertexData
,Disposable
public class VertexBufferObjectWithVAO extends java.lang.Object implements VertexData
A
VertexData
implementation that uses vertex buffer objects and vertex array objects. (This is required for OpenGL 3.0+ core profiles. In particular, the default VAO has been deprecated, as has the use of client memory for passing vertex attributes.) Use of VAOs should give a slight performance benefit since you don't have to bind the attributes on every draw anymore.If the OpenGL ES context was lost you can call
invalidate()
to recreate a new OpenGL vertex buffer object.VertexBufferObjectWithVAO objects must be disposed via the
Code adapted fromdispose()
method when no longer neededVertexBufferObject
.
-
-
Constructor Summary
Constructors Constructor Description VertexBufferObjectWithVAO(boolean isStatic, int numVertices, VertexAttribute... attributes)
Constructs a new interleaved VertexBufferObjectWithVAO.VertexBufferObjectWithVAO(boolean isStatic, int numVertices, VertexAttributes attributes)
Constructs a new interleaved VertexBufferObjectWithVAO.VertexBufferObjectWithVAO(boolean isStatic, java.nio.ByteBuffer unmanagedBuffer, VertexAttributes attributes)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
bind(ShaderProgram shader)
Binds this VertexBufferObject for rendering via glDrawArrays or glDrawElementsvoid
bind(ShaderProgram shader, int[] locations)
Binds this VertexData for rendering via glDrawArrays or glDrawElements.void
dispose()
Disposes of all resources this VertexBufferObject 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
getNumMaxVertices()
int
getNumVertices()
void
invalidate()
Invalidates the VertexBufferObject so a new OpenGL buffer handle is created.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.
-
-
-
Constructor Detail
-
VertexBufferObjectWithVAO
public VertexBufferObjectWithVAO(boolean isStatic, int numVertices, VertexAttribute... attributes)
Constructs a new interleaved VertexBufferObjectWithVAO.- Parameters:
isStatic
- whether the vertex data is static.numVertices
- the maximum number of verticesattributes
- theVertexAttribute
s.
-
VertexBufferObjectWithVAO
public VertexBufferObjectWithVAO(boolean isStatic, int numVertices, VertexAttributes attributes)
Constructs a new interleaved VertexBufferObjectWithVAO.- Parameters:
isStatic
- whether the vertex data is static.numVertices
- the maximum number of verticesattributes
- theVertexAttributes
.
-
VertexBufferObjectWithVAO
public VertexBufferObjectWithVAO(boolean isStatic, java.nio.ByteBuffer unmanagedBuffer, VertexAttributes attributes)
-
-
Method Detail
-
getAttributes
public VertexAttributes getAttributes()
- Specified by:
getAttributes
in interfaceVertexData
- Returns:
- the
VertexAttributes
as specified during construction.
-
getNumVertices
public int getNumVertices()
- Specified by:
getNumVertices
in interfaceVertexData
- Returns:
- the number of vertices this VertexData stores
-
getNumMaxVertices
public int getNumMaxVertices()
- Specified by:
getNumMaxVertices
in interfaceVertexData
- Returns:
- the number of vertices this VertedData can store
-
getBuffer
@Deprecated public java.nio.FloatBuffer getBuffer()
Deprecated.usegetBuffer(boolean)
insteadDescription 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 useVertexData.setVertices(float[], int, int)
; Any modifications made to the Buffer *after* the call to bind will not automatically be uploaded.- Specified by:
getBuffer
in interfaceVertexData
- 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 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.
-
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 viaVertexAttributes
.This can be called in between calls to bind and unbind. The vertex data will be updated instantly.
- Specified by:
setVertices
in 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:VertexData
Update (a portion of) the vertices. Does not resize the backing buffer.- Specified by:
updateVertices
in interfaceVertexData
vertices
- the vertex datasourceOffset
- the offset to start copying the data fromcount
- the number of floats to copy
-
bind
public void bind(ShaderProgram shader)
Binds this VertexBufferObject for rendering via glDrawArrays or glDrawElements- Specified by:
bind
in interfaceVertexData
- Parameters:
shader
- the shader
-
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 interfaceVertexData
locations
- array containing the attribute locations.
-
unbind
public void unbind(ShaderProgram shader)
Unbinds this VertexBufferObject.- Specified by:
unbind
in interfaceVertexData
- Parameters:
shader
- the shader
-
unbind
public void unbind(ShaderProgram shader, int[] locations)
Description copied from interface:VertexData
Unbinds this VertexData.- Specified by:
unbind
in interfaceVertexData
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 interfaceVertexData
-
dispose
public void dispose()
Disposes of all resources this VertexBufferObject uses.- Specified by:
dispose
in interfaceDisposable
- Specified by:
dispose
in interfaceVertexData
-
-