public class Mesh extends Object implements Disposable
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.
Modifier and Type | Class and Description |
---|---|
static class |
Mesh.VertexDataType |
Constructor and Description |
---|
Mesh(boolean staticVertices,
boolean staticIndices,
int maxVertices,
int maxIndices,
VertexAttributes attributes)
by jw: 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.
|
Mesh(boolean isStatic,
int maxVertices,
int maxIndices,
VertexAttributes attributes)
Creates a new Mesh with the given attributes.
|
Mesh(Mesh.VertexDataType type,
boolean isStatic,
int maxVertices,
int maxIndices,
VertexAttribute... attributes)
Creates a new Mesh with the given attributes.
|
Modifier and Type | Method and Description |
---|---|
void |
bind(ShaderProgram shader)
Binds the underlying
VertexBufferObject and IndexBufferObject if indices where given. |
void |
bind(ShaderProgram shader,
int[] locations)
Binds the underlying
VertexBufferObject and IndexBufferObject if indices where given. |
BoundingBox |
calculateBoundingBox()
Calculates the
BoundingBox of the vertices contained in this mesh. |
void |
calculateBoundingBox(BoundingBox bbox)
Calculates the
BoundingBox of the vertices contained in this mesh. |
BoundingBox |
calculateBoundingBox(BoundingBox out,
int offset,
int count)
Calculate the
BoundingBox of the specified part. |
BoundingBox |
calculateBoundingBox(BoundingBox out,
int offset,
int count,
Matrix4 transform)
Calculate the
BoundingBox of the specified part. |
float |
calculateRadius(float centerX,
float centerY,
float centerZ)
Calculates the squared radius of the bounding sphere around the specified center for the specified part.
|
float |
calculateRadius(float centerX,
float centerY,
float centerZ,
int offset,
int count)
Calculates the squared radius of the bounding sphere around the specified center for the specified part.
|
float |
calculateRadius(float centerX,
float centerY,
float centerZ,
int offset,
int count,
Matrix4 transform)
Calculates the radius of the bounding sphere around the specified center for the specified part.
|
float |
calculateRadius(Vector3 center)
Calculates the squared radius of the bounding sphere around the specified center for the specified part.
|
float |
calculateRadius(Vector3 center,
int offset,
int count)
Calculates the squared radius of the bounding sphere around the specified center for the specified part.
|
float |
calculateRadius(Vector3 center,
int offset,
int count,
Matrix4 transform)
Calculates the squared radius of the bounding sphere around the specified center for the specified part.
|
float |
calculateRadiusSquared(float centerX,
float centerY,
float centerZ,
int offset,
int count,
Matrix4 transform)
Calculates the squared radius of the bounding sphere around the specified center for the specified part.
|
static void |
clearAllMeshes(Application app)
Will clear the managed mesh cache.
|
Mesh |
copy(boolean isStatic)
Copies this mesh.
|
Mesh |
copy(boolean isStatic,
boolean removeDuplicates,
int[] usage)
Copies this mesh optionally removing duplicate vertices and/or reducing the amount of attributes.
|
static Mesh |
create(boolean isStatic,
Mesh[] meshes)
Create a new Mesh that is a combination of the supplied meshes.
|
static Mesh |
create(boolean isStatic,
Mesh[] meshes,
Matrix4[] transformations)
Create a new Mesh that is a combination of the supplied meshes.
|
static Mesh |
create(boolean isStatic,
Mesh base,
Matrix4[] transformations)
Create a new Mesh that is a combination of transformations of the supplied base mesh.
|
void |
dispose()
Frees all resources associated with this Mesh
|
BoundingBox |
extendBoundingBox(BoundingBox out,
int offset,
int count)
Extends the specified
BoundingBox with the specified part. |
BoundingBox |
extendBoundingBox(BoundingBox out,
int offset,
int count,
Matrix4 transform)
Extends the specified
BoundingBox with the specified part. |
void |
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.
|
ShortBuffer |
getIndicesBuffer() |
static String |
getManagedStatus() |
int |
getMaxIndices() |
int |
getMaxVertices() |
int |
getNumIndices() |
int |
getNumVertices() |
VertexAttribute |
getVertexAttribute(int usage)
Returns the first
VertexAttribute having the given VertexAttributes.Usage . |
VertexAttributes |
getVertexAttributes() |
int |
getVertexSize() |
float[] |
getVertices(float[] vertices)
Copies the vertices from the Mesh to the float array.
|
float[] |
getVertices(int srcOffset,
float[] vertices)
Copies the the remaining vertices from the Mesh to the float array.
|
float[] |
getVertices(int srcOffset,
int count,
float[] vertices)
Copies the specified vertices from the Mesh to the float array.
|
float[] |
getVertices(int srcOffset,
int count,
float[] vertices,
int destOffset)
Copies the specified vertices from the Mesh to the float array.
|
FloatBuffer |
getVerticesBuffer() |
static void |
invalidateAllMeshes(Application app)
Invalidates all meshes so the next time they are rendered new VBO handles are generated.
|
void |
render(ShaderProgram shader,
int primitiveType)
Renders the mesh using the given primitive type.
|
void |
render(ShaderProgram shader,
int primitiveType,
int offset,
int count)
Renders the mesh using the given primitive type.
|
void |
render(ShaderProgram shader,
int primitiveType,
int offset,
int count,
boolean autoBind)
Renders the mesh using the given primitive type.
|
void |
scale(float scaleX,
float scaleY,
float scaleZ)
Method to scale the positions in the mesh.
|
void |
setAutoBind(boolean autoBind)
Sets whether to bind the underlying
VertexArray or VertexBufferObject automatically on a call to one of the
render methods. |
Mesh |
setIndices(short[] indices)
Sets the indices of this Mesh
|
Mesh |
setIndices(short[] indices,
int offset,
int count)
Sets the indices of this Mesh.
|
Mesh |
setVertices(float[] vertices)
Sets the vertices of this Mesh.
|
Mesh |
setVertices(float[] vertices,
int offset,
int count)
Sets the vertices of this Mesh.
|
void |
transform(Matrix4 matrix)
Method to transform the positions in the mesh.
|
static void |
transform(Matrix4 matrix,
float[] vertices,
int vertexSize,
int offset,
int dimensions,
int start,
int count)
Method to transform the positions in the float array.
|
void |
transform(Matrix4 matrix,
int start,
int count) |
void |
transformUV(Matrix3 matrix)
Method to transform the texture coordinates in the mesh.
|
static void |
transformUV(Matrix3 matrix,
float[] vertices,
int vertexSize,
int offset,
int start,
int count)
Method to transform the texture coordinates (UV) in the float array.
|
void |
unbind(ShaderProgram shader)
Unbinds the underlying
VertexBufferObject and IndexBufferObject is indices were given. |
void |
unbind(ShaderProgram shader,
int[] locations)
Unbinds the underlying
VertexBufferObject and IndexBufferObject is indices were given. |
Mesh |
updateVertices(int targetOffset,
float[] source)
Update (a portion of) the vertices.
|
Mesh |
updateVertices(int targetOffset,
float[] source,
int sourceOffset,
int count)
Update (a portion of) the vertices.
|
public Mesh(boolean isStatic, int maxVertices, int maxIndices, VertexAttribute... attributes)
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
- the VertexAttribute
s. Each vertex attribute defines one property of a vertex such as position,
normal or texture coordinatepublic Mesh(boolean isStatic, int maxVertices, int maxIndices, VertexAttributes attributes)
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
- the VertexAttributes
. Each vertex attribute defines one property of a vertex such as position,
normal or texture coordinatepublic Mesh(boolean staticVertices, boolean staticIndices, int maxVertices, int maxIndices, VertexAttributes attributes)
staticVertices
- whether vertices of this mesh are static or not. Allows for internal optimizations.staticIndices
- whether indices of this mesh are 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
- the VertexAttributes
. Each vertex attribute defines one property of a vertex such as position,
normal or texture coordinatepublic Mesh(Mesh.VertexDataType type, boolean isStatic, int maxVertices, int maxIndices, VertexAttribute... attributes)
type
- the Mesh.VertexDataType
to be used, VBO or VA.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
- the VertexAttribute
s. Each vertex attribute defines one property of a vertex such as position,
normal or texture coordinatepublic static Mesh create(boolean isStatic, Mesh base, Matrix4[] transformations)
isStatic
- whether this mesh is static or not. Allows for internal optimizations.transformations
- the transformations to apply to the meshespublic static Mesh create(boolean isStatic, Mesh[] meshes)
isStatic
- whether this mesh is static or not. Allows for internal optimizations.meshes
- the meshes to combinepublic static Mesh create(boolean isStatic, Mesh[] meshes, Matrix4[] transformations)
isStatic
- whether this mesh is static or not. Allows for internal optimizations.meshes
- the meshes to combinetransformations
- the transformations to apply to the meshespublic Mesh setVertices(float[] vertices)
vertices
- the vertices.public Mesh setVertices(float[] vertices, int offset, int count)
vertices
- the vertices.offset
- the offset into the vertices arraycount
- the number of floats to usepublic Mesh updateVertices(int targetOffset, float[] source)
targetOffset
- the offset in number of floats of the mesh part.source
- the vertex data to update the mesh part withpublic Mesh updateVertices(int targetOffset, float[] source, int sourceOffset, int count)
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 updatepublic float[] getVertices(float[] vertices)
vertices
- the array to copy the vertices topublic float[] getVertices(int srcOffset, float[] vertices)
srcOffset
- the offset (in number of floats) of the vertices in the mesh to copyvertices
- the array to copy the vertices topublic float[] getVertices(int srcOffset, int count, float[] vertices)
srcOffset
- the offset (in number of floats) of the vertices in the mesh to copycount
- the amount of floats to copyvertices
- the array to copy the vertices topublic float[] getVertices(int srcOffset, int count, float[] vertices, int destOffset)
srcOffset
- the offset (in number of floats) of the vertices in the mesh to copycount
- the amount of floats to copyvertices
- the array to copy the vertices todestOffset
- the offset (in floats) in the vertices array to start copyingpublic Mesh setIndices(short[] indices)
indices
- the indicespublic Mesh setIndices(short[] indices, int offset, int count)
indices
- the indicesoffset
- the offset into the indices arraycount
- the number of indices to copypublic void getIndices(short[] indices)
indices
- the array to copy the indices topublic void getIndices(short[] indices, int destOffset)
indices
- the array to copy the indices todestOffset
- the offset in the indices array to start copyingpublic void getIndices(int srcOffset, short[] indices, int destOffset)
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 copyingpublic void getIndices(int srcOffset, int count, short[] indices, int destOffset)
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 copyingpublic int getNumIndices()
public int getNumVertices()
public int getMaxVertices()
public int getMaxIndices()
public int getVertexSize()
public void setAutoBind(boolean autoBind)
VertexArray
or VertexBufferObject
automatically on a call to one of the
render methods. Usually you want to use autobind. Manual binding is an expert functionality. There is a driver bug on the
MSM720xa chips that will fuck up memory if you manipulate the vertices and indices of a Mesh multiple times while it is
bound. Keep this in mind.autoBind
- whether to autobind meshes.public void bind(ShaderProgram shader)
VertexBufferObject
and IndexBufferObject
if indices where given. Use this with OpenGL
ES 2.0 and when auto-bind is disabled.shader
- the shader (does not bind the shader)public void bind(ShaderProgram shader, int[] locations)
VertexBufferObject
and IndexBufferObject
if indices where given. Use this with OpenGL
ES 2.0 and when auto-bind is disabled.shader
- the shader (does not bind the shader)locations
- array containing the attribute locations.public void unbind(ShaderProgram shader)
VertexBufferObject
and IndexBufferObject
is indices were given. Use this with OpenGL
ES 1.x and when auto-bind is disabled.shader
- the shader (does not unbind the shader)public void unbind(ShaderProgram shader, int[] locations)
VertexBufferObject
and IndexBufferObject
is indices were given. Use this with OpenGL
ES 1.x and when auto-bind is disabled.shader
- the shader (does not unbind the shader)locations
- array containing the attribute locations.public void render(ShaderProgram shader, int primitiveType)
Renders the mesh using the given primitive type. If indices are set for this mesh then getNumIndices() / #vertices per primitive primitives are rendered. If no indices are set then getNumVertices() / #vertices per primitive are rendered.
This method will automatically bind each vertex attribute as specified at construction time via VertexAttributes
to
the respective shader attributes. The binding is based on the alias defined for each VertexAttribute.
This method must only be called after the ShaderProgram.begin()
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.
primitiveType
- the primitive typepublic void render(ShaderProgram shader, int primitiveType, int offset, int count)
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 via VertexAttributes
to
the respective shader attributes. The binding is based on the alias defined for each VertexAttribute.
This method must only be called after the ShaderProgram.begin()
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.
shader
- the shader to be usedprimitiveType
- the primitive typeoffset
- the offset into the vertex or index buffercount
- number of vertices or indices to usepublic void render(ShaderProgram shader, int primitiveType, int offset, int count, boolean autoBind)
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 via VertexAttributes
to
the respective shader attributes. The binding is based on the alias defined for each VertexAttribute.
This method must only be called after the ShaderProgram.begin()
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.
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 Meshpublic void dispose()
dispose
in interface Disposable
public VertexAttribute getVertexAttribute(int usage)
VertexAttribute
having the given VertexAttributes.Usage
.usage
- the Usage.public VertexAttributes getVertexAttributes()
public FloatBuffer getVerticesBuffer()
public BoundingBox calculateBoundingBox()
BoundingBox
of the vertices contained in this mesh. In case no vertices are defined yet a
GdxRuntimeException
is thrown. This method creates a new BoundingBox instance.public void calculateBoundingBox(BoundingBox bbox)
BoundingBox
of the vertices contained in this mesh. In case no vertices are defined yet a
GdxRuntimeException
is thrown.bbox
- the bounding box to store the result in.public BoundingBox calculateBoundingBox(BoundingBox out, int offset, int count)
BoundingBox
of the specified part.out
- the bounding box to store the result in.offset
- the start index of the part.count
- the amount of indices the part contains.public BoundingBox calculateBoundingBox(BoundingBox out, int offset, int count, Matrix4 transform)
BoundingBox
of the specified part.out
- the bounding box to store the result in.offset
- the start index of the part.count
- the amount of indices the part contains.public BoundingBox extendBoundingBox(BoundingBox out, int offset, int count)
BoundingBox
with the specified part.out
- the bounding box to store the result in.offset
- the start index of the part.count
- the amount of indices the part contains.public BoundingBox extendBoundingBox(BoundingBox out, int offset, int count, Matrix4 transform)
BoundingBox
with the specified part.out
- the bounding box to store the result in.offset
- the start index of the part.count
- the amount of indices the part contains.public float calculateRadiusSquared(float centerX, float centerY, float centerZ, int offset, int count, Matrix4 transform)
centerX
- The X coordinate of the center of the bounding spherecenterY
- The Y coordinate of the center of the bounding spherecenterZ
- The Z coordinate of the center of the bounding sphereoffset
- the start index of the part.count
- the amount of indices the part contains.public float calculateRadius(float centerX, float centerY, float centerZ, int offset, int count, Matrix4 transform)
centerX
- The X coordinate of the center of the bounding spherecenterY
- The Y coordinate of the center of the bounding spherecenterZ
- The Z coordinate of the center of the bounding sphereoffset
- the start index of the part.count
- the amount of indices the part contains.public float calculateRadius(Vector3 center, int offset, int count, Matrix4 transform)
center
- The center of the bounding sphereoffset
- the start index of the part.count
- the amount of indices the part contains.public float calculateRadius(float centerX, float centerY, float centerZ, int offset, int count)
centerX
- The X coordinate of the center of the bounding spherecenterY
- The Y coordinate of the center of the bounding spherecenterZ
- The Z coordinate of the center of the bounding sphereoffset
- the start index of the part.count
- the amount of indices the part contains.public float calculateRadius(Vector3 center, int offset, int count)
center
- The center of the bounding sphereoffset
- the start index of the part.count
- the amount of indices the part contains.public float calculateRadius(float centerX, float centerY, float centerZ)
centerX
- The X coordinate of the center of the bounding spherecenterY
- The Y coordinate of the center of the bounding spherecenterZ
- The Z coordinate of the center of the bounding spherepublic float calculateRadius(Vector3 center)
center
- The center of the bounding spherepublic ShortBuffer getIndicesBuffer()
public static void invalidateAllMeshes(Application app)
app
- public static void clearAllMeshes(Application app)
public static String getManagedStatus()
public void scale(float scaleX, float scaleY, float scaleZ)
scaleX
- scale on xscaleY
- scale on yscaleZ
- scale on zpublic void transform(Matrix4 matrix)
matrix
- the transformation matrixpublic void transform(Matrix4 matrix, int start, int count)
public static void transform(Matrix4 matrix, float[] vertices, int vertexSize, int offset, int dimensions, int start, int count)
matrix
- the transformation matrixvertices
- the float arrayvertexSize
- the number of floats in each vertexoffset
- the offset within a vertex to the positiondimensions
- the size of the positionstart
- the vertex to start withcount
- the amount of vertices to transformpublic void transformUV(Matrix3 matrix)
matrix
- the transformation matrixpublic static void transformUV(Matrix3 matrix, float[] vertices, int vertexSize, int offset, int start, int count)
matrix
- the transformation matrixvertices
- the float arrayvertexSize
- the number of floats in each vertexoffset
- the offset within a vertex to the texture locationstart
- the vertex to start withcount
- the amount of vertices to transformpublic Mesh copy(boolean isStatic, boolean removeDuplicates, int[] usage)
isStatic
- whether the new mesh is static or not. Allows for internal optimizations.removeDuplicates
- whether to remove duplicate vertices if possible. Only the vertices specified by usage are checked.usage
- which attributes (if available) to copypublic Mesh copy(boolean isStatic)
isStatic
- whether the new mesh is static or not. Allows for internal optimizations.Copyright © 2015. All rights reserved.