APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.graphics

public Class Mesh

extends Object
implements Disposable
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.utils.Disposable
Imports
java.nio.Buffer, .FloatBuffer, .ShortBuffer, java.util.HashMap, .Map, com.badlogic.gdx.Application, .Gdx, com.badlogic.gdx.graphics.VertexAttributes.Usage, com.badlogic.gdx.graphics.glutils.IndexArray, .IndexBufferObject, .IndexBufferObjectSubData, .IndexData, .InstanceBufferObject, .InstanceData, .ShaderProgram, .VertexArray, .VertexBufferObject, .VertexBufferObjectSubData, .VertexBufferObjectWithVAO, .VertexData, com.badlogic.gdx.math.Matrix3, .Matrix4, .Vector2, .Vector3, com.badlogic.gdx.math.collision.BoundingBox, com.badlogic.gdx.utils.Array, .Disposable, .GdxRuntimeException

A Mesh holds vertices composed of attributes specified by a VertexAttributes instance. 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.

Author
mzechner, Dave Clayton , Xoppa

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
protected boolean
protected final IndexData
protected InstanceData
protected boolean
protected final boolean
pack-priv static final Map<Application, Array<Mesh>>
meshes

list of all meshes

private final Vector3
protected final VertexData

Constructor Summary

AccessConstructor and Description
protected
Mesh(VertexData vertices, IndexData indices, boolean isVertexArray)

public
Mesh(boolean
whether this mesh is static or not. Allows for internal optimizations.
isStatic
,
int
the maximum number of vertices this mesh can hold
maxVertices
,
int
the maximum number of indices this mesh can hold
maxIndices
,
VertexAttribute...
the VertexAttributes. Each vertex attribute defines one property of a vertex such as position, normal or texture coordinate
attributes
)

Creates a new Mesh with the given attributes.

public
Mesh(boolean
whether this mesh is static or not. Allows for internal optimizations.
isStatic
,
int
the maximum number of vertices this mesh can hold
maxVertices
,
int
the maximum number of indices this mesh can hold
maxIndices
,
VertexAttributes
the VertexAttributes. Each vertex attribute defines one property of a vertex such as position, normal or texture coordinate
attributes
)

Creates a new Mesh with the given attributes.

public
Mesh(boolean
whether vertices of this mesh are static or not. Allows for internal optimizations.
staticVertices
,
boolean
whether indices of this mesh are static or not. Allows for internal optimizations.
staticIndices
,
int
the maximum number of vertices this mesh can hold
maxVertices
,
int
the maximum number of indices this mesh can hold
maxIndices
,
VertexAttributes
the VertexAttributes. Each vertex attribute defines one property of a vertex such as position, normal or texture coordinate
attributes
)

Creates a new Mesh with the given attributes.

public
Mesh(Mesh.VertexDataType
the VertexDataType to be used, VBO or VA.
type
,
boolean
whether this mesh is static or not. Allows for internal optimizations.
isStatic
,
int
the maximum number of vertices this mesh can hold
maxVertices
,
int
the maximum number of indices this mesh can hold
maxIndices
,
VertexAttribute...
the VertexAttributes. Each vertex attribute defines one property of a vertex such as position, normal or texture coordinate
attributes
)

Creates a new Mesh with the given attributes.

public
Mesh(Mesh.VertexDataType
the VertexDataType to be used, VBO or VA.
type
,
boolean
whether this mesh is static or not. Allows for internal optimizations.
isStatic
,
int
the maximum number of vertices this mesh can hold
maxVertices
,
int
the maximum number of indices this mesh can hold
maxIndices
,
VertexAttributes attributes)

Creates a new Mesh with the given attributes.

Method Summary

Modifier and TypeMethod and Description
private static void
public void
bind(final ShaderProgram
the shader (does not bind the shader)
shader
)

Binds the underlying VertexBufferObject and IndexBufferObject if indices where given.

public void
bind(final ShaderProgram
the shader (does not bind the shader)
shader
,
final int[]
array containing the vertex attribute locations.
locations
,
final int[]
array containing the instance attribute locations.
instanceLocations
)

Binds the underlying VertexBufferObject and IndexBufferObject if indices where given.

public BoundingBox

Returns:

the bounding box.
calculateBoundingBox
()

Calculates the BoundingBox of the vertices contained in this mesh.

public void
calculateBoundingBox(BoundingBox
the bounding box to store the result in.
bbox
)

Calculates the BoundingBox of the vertices contained in this mesh.

public BoundingBox

Returns:

the value specified by out.
calculateBoundingBox
(final BoundingBox
the bounding box to store the result in.
out
,
int
the start index of the part.
offset
,
int
the amount of indices the part contains.
count
)

Calculate the BoundingBox of the specified part.

public BoundingBox

Returns:

the value specified by out.
calculateBoundingBox
(final BoundingBox
the bounding box to store the result in.
out
,
int
the start index of the part.
offset
,
int
the amount of indices the part contains.
count
,
final Matrix4 transform)

Calculate the BoundingBox of the specified part.

public float

Returns:

the radius of the bounding sphere.
calculateRadius
(final float
The X coordinate of the center of the bounding sphere
centerX
,
final float
The Y coordinate of the center of the bounding sphere
centerY
,
final float
The Z coordinate of the center of the bounding sphere
centerZ
,
int
the start index of the part.
offset
,
int
the amount of indices the part contains.
count
,
final Matrix4 transform)

Calculates the radius of the bounding sphere around the specified center for the specified part.

public float

Returns:

the squared radius of the bounding sphere.
calculateRadius
(final Vector3
The center of the bounding sphere
center
,
int
the start index of the part.
offset
,
int
the amount of indices the part contains.
count
,
final Matrix4 transform)

Calculates the squared radius of the bounding sphere around the specified center for the specified part.

public float

Returns:

the squared radius of the bounding sphere.
calculateRadius
(final float
The X coordinate of the center of the bounding sphere
centerX
,
final float
The Y coordinate of the center of the bounding sphere
centerY
,
final float
The Z coordinate of the center of the bounding sphere
centerZ
,
int
the start index of the part.
offset
,
int
the amount of indices the part contains.
count
)

Calculates the squared radius of the bounding sphere around the specified center for the specified part.

public float

Returns:

the squared radius of the bounding sphere.
calculateRadius
(final Vector3
The center of the bounding sphere
center
,
int
the start index of the part.
offset
,
int
the amount of indices the part contains.
count
)

Calculates the squared radius of the bounding sphere around the specified center for the specified part.

public float

Returns:

the squared radius of the bounding sphere.
calculateRadius
(final float
The X coordinate of the center of the bounding sphere
centerX
,
final float
The Y coordinate of the center of the bounding sphere
centerY
,
final float
The Z coordinate of the center of the bounding sphere
centerZ
)

Calculates the squared radius of the bounding sphere around the specified center for the specified part.

public float

Returns:

the squared radius of the bounding sphere.
calculateRadius
(final Vector3
The center of the bounding sphere
center
)

Calculates the squared radius of the bounding sphere around the specified center for the specified part.

public float

Returns:

the squared radius of the bounding sphere.
calculateRadiusSquared
(final float
The X coordinate of the center of the bounding sphere
centerX
,
final float
The Y coordinate of the center of the bounding sphere
centerY
,
final float
The Z coordinate of the center of the bounding sphere
centerZ
,
int
the start index of the part.
offset
,
int
the amount of indices the part contains.
count
,
final Matrix4 transform)

Calculates the squared radius of the bounding sphere around the specified center for the specified part.

public static void
clearAllMeshes(Application app)

Will clear the managed mesh cache.

public Mesh

Returns:

the copy of this mesh
copy
(boolean
whether the new mesh is static or not. Allows for internal optimizations.
isStatic
,
boolean
whether to remove duplicate vertices if possible. Only the vertices specified by usage are checked.
removeDuplicates
,
final int[]
which attributes (if available) to copy
usage
)

Copies this mesh optionally removing duplicate vertices and/or reducing the amount of attributes.

public Mesh

Returns:

the copy of this mesh
copy
(boolean
whether the new mesh is static or not. Allows for internal optimizations.
isStatic
)

Copies this mesh.

public Mesh
public void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Frees all resources associated with this Mesh

public Mesh
enableInstancedRendering(boolean isStatic, int maxInstances, VertexAttribute... attributes)

public BoundingBox

Returns:

the value specified by out.
extendBoundingBox
(final BoundingBox
the bounding box to store the result in.
out
,
int
the start index of the part.
offset
,
int
the amount of indices the part contains.
count
)

Extends the specified BoundingBox with the specified part.

public BoundingBox

Returns:

the value specified by out.
extendBoundingBox
(final BoundingBox
the bounding box to store the result in.
out
,
int
the start of the part.
offset
,
int
the size of the part.
count
,
final Matrix4 transform)

Extends the specified BoundingBox with the specified part.

public IndexData
public void
getIndices(short[]
the array to copy the indices to
indices
)

Copies the indices from the Mesh to the short array.

public void
getIndices(short[]
the array to copy the indices to
indices
,
int
the offset in the indices array to start copying
destOffset
)

Copies the indices from the Mesh to the short array.

public void
getIndices(int
the zero-based offset of the first index to fetch
srcOffset
,
short[]
the array to copy the indices to
indices
,
int
the offset in the indices array to start copying
destOffset
)

Copies the remaining indices from the Mesh to the short array.

public void
getIndices(int
the zero-based offset of the first index to fetch
srcOffset
,
int
the total amount of indices to copy
count
,
short[]
the array to copy the indices to
indices
,
int
the offset in the indices array to start copying
destOffset
)

Copies the indices from the Mesh to the short array.

public ShortBuffer
getIndicesBuffer()
Deprecated use getIndicesBuffer(boolean) instead

Returns: the backing shortbuffer holding the indices.

public ShortBuffer
getIndicesBuffer(boolean forWriting)

public VertexAttributes
getInstancedAttributes()

Returns: the instanced attributes of this Mesh if any

public static String
public int
getMaxIndices()

Returns: the maximum number of indices this mesh can hold

public int
getMaxVertices()

Returns: the maximum number of vertices this mesh can hold

public int
getNumIndices()

Returns: the number of defined indices

public int
getNumVertices()

Returns: the number of defined vertices

public VertexAttribute

Returns:

the VertexAttribute or null if no attribute with that usage was found.
getVertexAttribute
(int
the Usage.
usage
)

Returns the first VertexAttribute having the given Usage.

public VertexAttributes
getVertexAttributes()

Returns: the vertex attributes of this Mesh

public int
getVertexSize()

Returns: the size of a single vertex in bytes

public float[]
getVertices(float[]
the array to copy the vertices to
vertices
)

Copies the vertices from the Mesh to the float array.

public float[]
getVertices(int
the offset (in number of floats) of the vertices in the mesh to copy
srcOffset
,
float[]
the array to copy the vertices to
vertices
)

Copies the the remaining vertices from the Mesh to the float array.

public float[]
getVertices(int
the offset (in number of floats) of the vertices in the mesh to copy
srcOffset
,
int
the amount of floats to copy
count
,
float[]
the array to copy the vertices to
vertices
)

Copies the specified vertices from the Mesh to the float array.

public float[]
getVertices(int
the offset (in number of floats) of the vertices in the mesh to copy
srcOffset
,
int
the amount of floats to copy
count
,
float[]
the array to copy the vertices to
vertices
,
int
the offset (in floats) in the vertices array to start copying
destOffset
)

Copies the specified vertices from the Mesh to the float array.

public FloatBuffer
getVerticesBuffer()
Deprecated use getVerticesBuffer(boolean) instead

Returns: the backing FloatBuffer holding the vertices.

public FloatBuffer
getVerticesBuffer(boolean forWriting)

public static void
invalidateAllMeshes(Application app)

Invalidates all meshes so the next time they are rendered new VBO handles are generated.

public boolean
isInstanced()

Returns: Indicates whether this mesh uses instancing.

private VertexData
makeVertexBuffer(boolean isStatic, int maxVertices, VertexAttributes vertexAttributes)

public void
render(ShaderProgram shader, int
the primitive type
primitiveType
)

Renders the mesh using the given primitive type.

public void
render(ShaderProgram
the shader to be used
shader
,
int
the primitive type
primitiveType
,
int
the offset into the vertex or index buffer
offset
,
int
number of vertices or indices to use
count
)

Renders the mesh using the given primitive type.

public void
render(ShaderProgram
the shader to be used
shader
,
int
the primitive type
primitiveType
,
int
the offset into the vertex or index buffer
offset
,
int
number of vertices or indices to use
count
,
boolean
overrides the autoBind member of this Mesh
autoBind
)

Renders the mesh using the given primitive type.

public void
scale(float
scale on x
scaleX
,
float
scale on y
scaleY
,
float
scale on z
scaleZ
)

Method to scale the positions in the mesh.

public void
setAutoBind(boolean
whether to autobind meshes.
autoBind
)

Sets whether to bind the underlying VertexArray or VertexBufferObject automatically on a call to one of the render methods.

public Mesh

Returns:

the mesh for invocation chaining.
setIndices
(short[]
the indices
indices
)

Sets the indices of this Mesh

public Mesh

Returns:

the mesh for invocation chaining.
setIndices
(short[]
the indices
indices
,
int
the offset into the indices array
offset
,
int
the number of indices to copy
count
)

Sets the indices of this Mesh.

public Mesh

Returns:

the mesh for invocation chaining.
setInstanceData
(float[]
the instance data.
instanceData
,
int
the offset into the vertices array
offset
,
int
the number of floats to use
count
)

Sets the instance data of this Mesh.

public Mesh

Returns:

the mesh for invocation chaining.
setInstanceData
(float[]
the instance data.
instanceData
)

Sets the instance data of this Mesh.

public Mesh

Returns:

the mesh for invocation chaining.
setInstanceData
(FloatBuffer
the instance data.
instanceData
,
int
the number of floats to use
count
)

Sets the instance data of this Mesh.

public Mesh

Returns:

the mesh for invocation chaining.
setInstanceData
(FloatBuffer
the instance data.
instanceData
)

Sets the instance data of this Mesh.

public Mesh

Returns:

the mesh for invocation chaining.
setVertices
(float[]
the vertices.
vertices
)

Sets the vertices of this Mesh.

public Mesh

Returns:

the mesh for invocation chaining.
setVertices
(float[]
the vertices.
vertices
,
int
the offset into the vertices array
offset
,
int
the number of floats to use
count
)

Sets the vertices of this Mesh.

public void
transform(final Matrix4
the transformation matrix
matrix
)

Method to transform the positions in the mesh.

public void
transform(final Matrix4 matrix, final int start, final int count)

public static void
transform(final Matrix4
the transformation matrix
matrix
,
final float[]
the float array
vertices
,
int
the number of floats in each vertex
vertexSize
,
int
the offset within a vertex to the position
offset
,
int
the size of the position
dimensions
,
int
the vertex to start with
start
,
int
the amount of vertices to transform
count
)

Method to transform the positions in the float array.

public void
transformUV(final Matrix3
the transformation matrix
matrix
)

Method to transform the texture coordinates in the mesh.

protected void
transformUV(final Matrix3 matrix, final int start, final int count)

public static void
transformUV(final Matrix3
the transformation matrix
matrix
,
final float[]
the float array
vertices
,
int
the number of floats in each vertex
vertexSize
,
int
the offset within a vertex to the texture location
offset
,
int
the vertex to start with
start
,
int
the amount of vertices to transform
count
)

Method to transform the texture coordinates (UV) in the float array.

public void
unbind(final ShaderProgram
the shader (does not unbind the shader)
shader
)

Unbinds the underlying VertexBufferObject and IndexBufferObject is indices were given.

public void
unbind(final ShaderProgram
the shader (does not unbind the shader)
shader
,
final int[]
array containing the vertex attribute locations.
locations
,
final int[]
array containing the instance attribute locations.
instanceLocations
)

Unbinds the underlying VertexBufferObject and IndexBufferObject is indices were given.

public Mesh
updateInstanceData(int
the offset in number of floats of the mesh part.
targetOffset
,
float[]
the instance data to update the mesh part with
source
)

Update (a portion of) the instance data.

public Mesh
updateInstanceData(int
the offset in number of floats of the mesh part.
targetOffset
,
float[]
the instance data to update the mesh part with
source
,
int
the offset in number of floats within the source array
sourceOffset
,
int
the number of floats to update
count
)

Update (a portion of) the instance data.

public Mesh
updateInstanceData(int
the offset in number of floats of the mesh part.
targetOffset
,
FloatBuffer
the instance data to update the mesh part with
source
)

Update (a portion of) the instance data.

public Mesh
updateInstanceData(int
the offset in number of floats of the mesh part.
targetOffset
,
FloatBuffer
the instance data to update the mesh part with
source
,
int
the offset in number of floats within the source array
sourceOffset
,
int
the number of floats to update
count
)

Update (a portion of) the instance data.

public Mesh
updateVertices(int
the offset in number of floats of the mesh part.
targetOffset
,
float[]
the vertex data to update the mesh part with
source
)

Update (a portion of) the vertices.

public Mesh
updateVertices(int
the offset in number of floats of the mesh part.
targetOffset
,
float[]
the vertex data to update the mesh part with
source
,
int
the offset in number of floats within the source array
sourceOffset
,
int
the number of floats to update
count
)

Update (a portion of) the vertices.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait