Class Mesh
- All Implemented Interfaces:
Disposable
A Mesh holds vertices composed of attributes specified by an array of VertexAttribute
instances. The vertices are held either in
VRAM in form of vertex buffer objects or in RAM in form of vertex arrays. The former variant is more performant and is
preferred over vertex arrays if hardware supports it.
Meshes are automatically managed. If the OpenGL context is lost all vertex buffer objects get invalidated and must be reloaded when the context is recreated. This only happens on Android when a user switches to another application or receives an incoming call. A managed Mesh will be reloaded automagically so you don't have to do this manually.
A Mesh consists of vertices and optionally indices which specify which vertices define a triangle. Each vertex is composed of attributes such as position, normal, color or texture coordinate. Note that not all of this attributes must be given, except for position which is non-optional. Each attribute has an alias which is used when rendering a Mesh in OpenGL ES 2.0. The alias is used to bind a specific vertex attribute to a shader attribute. The shader source and the alias of the attribute must match exactly for this to work.
-
Field Summary
Modifier and TypeFieldDescriptionfinal VertexAttribute[]
Do not modify.final IndexData
final int
The size of one vertex, in bytes.final VertexData
-
Constructor Summary
ConstructorDescriptionMesh
(boolean useVertexArray, boolean isStatic, int maxVertices, int maxIndices, VertexAttribute... attributes) Creates a new Mesh with the given attributes.Mesh
(boolean isStatic, int maxVertices, int maxIndices, VertexAttribute... attributes) Creates a new Mesh with the given attributes. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Binds the underlyingVertexBufferObject
andIndexBufferObject
if indices where given.void
dispose()
Frees all resources associated with this Meshvoid
getIndices
(int srcOffset, int count, short[] indices, int destOffset) Copies the indices from the Mesh to the short array.void
getIndices
(int srcOffset, short[] indices, int destOffset) Copies the remaining indices from the Mesh to the short array.void
getIndices
(short[] indices) Copies the indices from the Mesh to the short array.void
getIndices
(short[] indices, int destOffset) Copies the indices from the Mesh to the short array.int
int
int
int
int
void
void
void
Renders the mesh using the given primitive type.void
setAutoBind
(boolean autoBind) Sets whether to bind the underlyingVertexArray
orVertexBufferObject
automatically on a call to one of the render methods.setIndices
(short[] indices) Sets the indices of this MeshsetIndices
(short[] indices, int offset, int count) Sets the indices of this Mesh.setVertices
(float[] vertices) Sets the vertices of this Mesh.setVertices
(float[] vertices, int offset, int count) Sets the vertices of this Mesh.void
Unbinds the underlyingVertexBufferObject
andIndexBufferObject
is indices were given.updateVertices
(int targetOffset, float[] source) Update (a portion of) the vertices.updateVertices
(int targetOffset, float[] source, 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
Methods inherited from interface arc.util.Disposable
isDisposed
-
Field Details
-
vertexSize
public final int vertexSizeThe size of one vertex, in bytes. -
attributes
Do not modify. -
vertices
-
indices
-
-
Constructor Details
-
Mesh
Creates a new Mesh with the given attributes.- Parameters:
isStatic
- whether this mesh is static or not. Allows for internal optimizations.maxVertices
- the maximum number of vertices this mesh can holdmaxIndices
- the maximum number of indices this mesh can holdattributes
- theVertexAttribute
s. Each vertex attribute defines one property of a vertex such as position, normal or texture coordinate
-
Mesh
public Mesh(boolean useVertexArray, boolean isStatic, int maxVertices, int maxIndices, VertexAttribute... attributes) Creates a new Mesh with the given attributes. This is an expert method with no error checking. Use at your own risk.- Parameters:
useVertexArray
- whether to use VBOs or VAOs. Note that the latter is not supported with OpenGL 3.0.isStatic
- whether this mesh is static or not. Allows for internal optimizations.maxVertices
- the maximum number of vertices this mesh can holdmaxIndices
- the maximum number of indices this mesh can hold
-
-
Method Details
-
setVertices
Sets the vertices of this Mesh. The attributes are assumed to be given in float format.- Parameters:
vertices
- the vertices.- Returns:
- the mesh for invocation chaining.
-
setVertices
Sets the vertices of this Mesh. The attributes are assumed to be given in float format.- Parameters:
vertices
- the vertices.offset
- the offset into the vertices arraycount
- the number of floats to use- Returns:
- the mesh for invocation chaining.
-
updateVertices
Update (a portion of) the vertices. Does not resize the backing buffer.- Parameters:
targetOffset
- the offset in number of floats of the mesh part.source
- the vertex data to update the mesh part with
-
updateVertices
Update (a portion of) the vertices. Does not resize the backing buffer.- Parameters:
targetOffset
- the offset in number of floats of the mesh part.source
- the vertex data to update the mesh part withsourceOffset
- the offset in number of floats within the source arraycount
- the number of floats to update
-
setIndices
Sets the indices of this Mesh- Parameters:
indices
- the indices- Returns:
- the mesh for invocation chaining.
-
setIndices
Sets the indices of this Mesh.- Parameters:
indices
- the indicesoffset
- the offset into the indices arraycount
- the number of indices to copy- Returns:
- the mesh for invocation chaining.
-
getIndices
public void getIndices(short[] indices) Copies the indices from the Mesh to the short array. The short array must be large enough to hold all the Mesh's indices.- Parameters:
indices
- the array to copy the indices to
-
getIndices
public void getIndices(short[] indices, int destOffset) Copies the indices from the Mesh to the short array. The short array must be large enough to hold destOffset + all the Mesh's indices.- Parameters:
indices
- the array to copy the indices todestOffset
- the offset in the indices array to start copying
-
getIndices
public void getIndices(int srcOffset, short[] indices, int destOffset) Copies the remaining indices from the Mesh to the short array. The short array must be large enough to hold destOffset + all the remaining indices.- Parameters:
srcOffset
- the zero-based offset of the first index to fetchindices
- the array to copy the indices todestOffset
- the offset in the indices array to start copying
-
getIndices
public void getIndices(int srcOffset, int count, short[] indices, int destOffset) Copies the indices from the Mesh to the short array. The short array must be large enough to hold destOffset + count indices.- Parameters:
srcOffset
- the zero-based offset of the first index to fetchcount
- the total amount of indices to copyindices
- the array to copy the indices todestOffset
- the offset in the indices array to start copying
-
getNumIndices
public int getNumIndices()- Returns:
- the number of defined indices
-
getNumVertices
public int getNumVertices()- Returns:
- the number of defined vertices
-
getMaxVertices
public int getMaxVertices()- Returns:
- the maximum number of vertices this mesh can hold
-
getMaxIndices
public int getMaxIndices()- Returns:
- the maximum number of indices this mesh can hold
-
getVertexSize
public int getVertexSize()- Returns:
- the size of a single vertex in bytes
-
setAutoBind
public void setAutoBind(boolean autoBind) Sets whether to bind the underlyingVertexArray
orVertexBufferObject
automatically on a call to one of the render methods. Usually you want to use autobind. Manual binding is an expert functionality.- Parameters:
autoBind
- whether to autobind meshes.
-
bind
Binds the underlyingVertexBufferObject
andIndexBufferObject
if indices where given. Use this with OpenGL ES 2.0 and when auto-bind is disabled.- Parameters:
shader
- the shader (does not bind the shader)
-
unbind
Unbinds the underlyingVertexBufferObject
andIndexBufferObject
is indices were given. Use this with OpenGL ES 1.x and when auto-bind is disabled.- Parameters:
shader
- the shader (does not unbind the shader)
-
render
- See Also:
-
render
- See Also:
-
render
Renders the mesh using the given primitive type. offset specifies the offset into either the vertex buffer or the index buffer depending on whether indices are defined. count specifies the number of vertices or indices to use thus count / #vertices per primitive primitives are rendered.
This method will automatically bind each vertex attribute as specified at construction time to the respective shader attributes. The binding is based on the alias defined for each VertexAttribute.
This method must only be called after the
Shader.bind()
method has been called!This method is intended for use with OpenGL ES 2.0 and will throw an IllegalStateException when OpenGL ES 1.x is used.
- Parameters:
shader
- the shader to be usedprimitiveType
- the primitive typeoffset
- the offset into the vertex or index buffercount
- number of vertices or indices to useautoBind
- overrides the autoBind member of this Mesh
-
dispose
public void dispose()Frees all resources associated with this Mesh- Specified by:
dispose
in interfaceDisposable
-
getVerticesBuffer
- Returns:
- the backing FloatBuffer holding the vertices. Does not have to be a direct buffer on Android!
-
getIndicesBuffer
- Returns:
- the backing shortbuffer holding the indices. Does not have to be a direct buffer on Android!
-