com.jme3.scene
Class Mesh

java.lang.Object
  extended by com.jme3.scene.Mesh
All Implemented Interfaces:
Savable, java.lang.Cloneable
Direct Known Subclasses:
AbstractBox, Arrow, Curve, Cylinder, Dome, Grid, Line, ParticleMesh, PQTorus, Quad, SkeletonPoints, SkeletonWire, Sphere, Surface, Torus, WireBox, WireFrustum, WireSphere

public class Mesh
extends java.lang.Object
implements Savable, java.lang.Cloneable

Mesh is used to store rendering data.

All visible elements in a scene are represented by meshes. Meshes may contain three types of geometric primitives:


Nested Class Summary
static class Mesh.Mode
          The mode of the Mesh specifies both the type of primitive represented by the mesh and how the data should be interpreted.
 
Constructor Summary
Mesh()
          Creates a new mesh with no vertex buffers.
 
Method Summary
 void clearBuffer(VertexBuffer.Type type)
          Unsets the VertexBuffer set on this mesh with the given type.
 Mesh clone()
          Create a shallow clone of this Mesh.
 Mesh cloneForAnim()
          Clone the mesh for animation use.
 int collideWith(Collidable other, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results)
          Handles collision detection, internal use only.
 void createCollisionData()
          Generates a collision tree for the mesh.
 Mesh deepClone()
          Creates a deep clone of this mesh.
 void extractVertexData(Mesh other)
          Extracts the vertex attributes from the given mesh into this mesh, by using this mesh's index buffer to index into the attributes of the other mesh.
 void generateBindPose(boolean forSoftwareAnim)
          Generates the VertexBuffer.Type.BindPosePosition, VertexBuffer.Type.BindPoseNormal, and VertexBuffer.Type.BindPoseTangent buffers for this mesh by duplicating them based on the position and normal buffers already set on the mesh.
 BoundingVolume getBound()
          Returns the BoundingVolume of this Mesh.
 VertexBuffer getBuffer(VertexBuffer.Type type)
          Get the VertexBuffer stored on this mesh with the given type.
 SafeArrayList<VertexBuffer> getBufferList()
          Returns a list of all vertex buffers on this Mesh.
 IntMap<VertexBuffer> getBuffers()
          Returns a map of all vertex buffers on this Mesh.
 int[] getElementLengths()
          Get the element lengths for Mesh.Mode.Hybrid mesh mode.
 java.nio.FloatBuffer getFloatBuffer(VertexBuffer.Type type)
          Get the VertexBuffer data stored on this mesh in float format.
 int getId()
          Returns the mesh's VAO ID.
 IndexBuffer getIndexBuffer()
          Get the index buffer for this mesh.
 IndexBuffer getIndicesAsList()
          Acquires an index buffer that will read the vertices on the mesh as a list.
 float getLineWidth()
          Returns the line width for line meshes.
 VertexBuffer getLodLevel(int lod)
          Returns the lod level at the given index.
 int getMaxNumWeights()
          Returns the maximum number of weights per vertex on this mesh.
 Mesh.Mode getMode()
          Returns the mesh mode
 int[] getModeStart()
          Set the mode start indices for Mesh.Mode.Hybrid mesh mode.
 int getNumLodLevels()
           
 float getPointSize()
          Returns the size of points for point meshes
 java.nio.ShortBuffer getShortBuffer(VertexBuffer.Type type)
          Get the VertexBuffer data stored on this mesh in short format.
 void getTriangle(int index, int[] indices)
          Gets the triangle vertex indices at the given triangle index and stores them into the given int array.
 void getTriangle(int index, Triangle tri)
          Gets the triangle vertex positions at the given triangle index and stores them into the Triangle argument.
 void getTriangle(int index, Vector3f v1, Vector3f v2, Vector3f v3)
          Gets the triangle vertex positions at the given triangle index and stores them into the v1, v2, v3 arguments.
 int getTriangleCount()
          Returns how many triangles or elements are on this Mesh.
 int getTriangleCount(int lod)
          Returns the triangle count for the given LOD level.
 int getVertexCount()
          Returns the number of vertices on this mesh.
 void prepareForAnim(boolean forSoftwareAnim)
          Prepares the mesh for software skinning by converting the bone index and weight buffers to heap buffers.
 void read(JmeImporter im)
           
 void scaleTextureCoordinates(Vector2f scaleFactor)
          Scales the texture coordinate buffer on this mesh by the given scale factor.
 void setBound(BoundingVolume modelBound)
          Sets the BoundingVolume for this Mesh.
 void setBuffer(VertexBuffer.Type type, int components, byte[] buf)
           
 void setBuffer(VertexBuffer.Type type, int components, java.nio.ByteBuffer buf)
           
 void setBuffer(VertexBuffer.Type type, int components, float[] buf)
           
 void setBuffer(VertexBuffer.Type type, int components, java.nio.FloatBuffer buf)
          Set a floating point VertexBuffer on the mesh.
 void setBuffer(VertexBuffer.Type type, int components, int[] buf)
           
 void setBuffer(VertexBuffer.Type type, int components, java.nio.IntBuffer buf)
           
 void setBuffer(VertexBuffer.Type type, int components, short[] buf)
           
 void setBuffer(VertexBuffer.Type type, int components, java.nio.ShortBuffer buf)
           
 void setBuffer(VertexBuffer.Type type, int components, VertexBuffer.Format format, java.nio.Buffer buf)
          Creates a VertexBuffer for the mesh or modifies the existing one per the parameters given.
 void setBuffer(VertexBuffer vb)
          Sets the VertexBuffer on the mesh.
 void setDynamic()
          Indicates to the GPU that this mesh will be modified occasionally (a hint).
 void setElementLengths(int[] elementLengths)
          Set the element lengths for Mesh.Mode.Hybrid mesh mode.
 void setId(int id)
          Sets the mesh's VAO ID.
 void setInterleaved()
          Deprecated. 
 void setLineWidth(float lineWidth)
          Specify the line width for meshes of the line modes, such as Mesh.Mode.Lines.
 void setLodLevels(VertexBuffer[] lodLevels)
          Set the LOD (level of detail) index buffers on this mesh.
 void setMaxNumWeights(int maxNumWeights)
          Set the maximum number of weights per vertex on this mesh.
 void setMode(Mesh.Mode mode)
          Change the Mesh's mode.
 void setModeStart(int[] modeStart)
          Get the mode start indices for Mesh.Mode.Hybrid mesh mode.
 void setPointSize(float pointSize)
          Set the size of points for meshes of mode Mesh.Mode.Points.
 void setStatic()
          Indicates to the GPU that this mesh will not be modified (a hint).
 void setStreamed()
          Indicates to the GPU that this mesh will be modified every frame (a hint).
 void updateBound()
          Updates the bounding volume of this mesh.
 void updateCounts()
          Update the vertex and triangle counts for this mesh based on the current data.
 void write(JmeExporter ex)
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Mesh

public Mesh()
Creates a new mesh with no vertex buffers.

Method Detail

clone

public Mesh clone()
Create a shallow clone of this Mesh. The vertex buffers are shared between this and the clone mesh, the rest of the data is cloned.

Overrides:
clone in class java.lang.Object
Returns:
A shallow clone of the mesh

deepClone

public Mesh deepClone()
Creates a deep clone of this mesh. The vertex buffers and the data inside them is cloned.

Returns:
a deep clone of this mesh.

cloneForAnim

public Mesh cloneForAnim()
Clone the mesh for animation use. This creates a shallow clone of the mesh, sharing most of the vertex buffer data, however the VertexBuffer.Type.Position, VertexBuffer.Type.Normal, and VertexBuffer.Type.Tangent buffers are deeply cloned.

Returns:
A clone of the mesh for animation use.

generateBindPose

public void generateBindPose(boolean forSoftwareAnim)
Generates the VertexBuffer.Type.BindPosePosition, VertexBuffer.Type.BindPoseNormal, and VertexBuffer.Type.BindPoseTangent buffers for this mesh by duplicating them based on the position and normal buffers already set on the mesh. This method does nothing if the mesh has no bone weight or index buffers.

Parameters:
forSoftwareAnim - Should be true if the bind pose is to be generated.

prepareForAnim

public void prepareForAnim(boolean forSoftwareAnim)
Prepares the mesh for software skinning by converting the bone index and weight buffers to heap buffers.

Parameters:
forSoftwareAnim - Should be true to enable the conversion.

setLodLevels

public void setLodLevels(VertexBuffer[] lodLevels)
Set the LOD (level of detail) index buffers on this mesh.

Parameters:
lodLevels - The LOD levels to set

getNumLodLevels

public int getNumLodLevels()
Returns:
The number of LOD levels set on this mesh, including the main index buffer, returns zero if there are no lod levels.

getLodLevel

public VertexBuffer getLodLevel(int lod)
Returns the lod level at the given index.

Parameters:
lod - The lod level index, this does not include the main index buffer.
Returns:
The LOD index buffer at the index
Throws:
java.lang.IndexOutOfBoundsException - If the index is outside of the range [0, getNumLodLevels()].
See Also:
setLodLevels(com.jme3.scene.VertexBuffer[])

getElementLengths

public int[] getElementLengths()
Get the element lengths for Mesh.Mode.Hybrid mesh mode.

Returns:
element lengths

setElementLengths

public void setElementLengths(int[] elementLengths)
Set the element lengths for Mesh.Mode.Hybrid mesh mode.

Parameters:
elementLengths - The element lengths to set

getModeStart

public int[] getModeStart()
Set the mode start indices for Mesh.Mode.Hybrid mesh mode.

Returns:
mode start indices

setModeStart

public void setModeStart(int[] modeStart)
Get the mode start indices for Mesh.Mode.Hybrid mesh mode.


getMode

public Mesh.Mode getMode()
Returns the mesh mode

Returns:
the mesh mode
See Also:
setMode(com.jme3.scene.Mesh.Mode)

setMode

public void setMode(Mesh.Mode mode)
Change the Mesh's mode. By default the mode is Mesh.Mode.Triangles.

Parameters:
mode - The new mode to set
See Also:
Mesh.Mode

getMaxNumWeights

public int getMaxNumWeights()
Returns the maximum number of weights per vertex on this mesh.

Returns:
maximum number of weights per vertex
See Also:
setMaxNumWeights(int)

setMaxNumWeights

public void setMaxNumWeights(int maxNumWeights)
Set the maximum number of weights per vertex on this mesh. Only relevant if this mesh has bone index/weight buffers. This value should be between 0 and 4.

Parameters:
maxNumWeights -

getPointSize

public float getPointSize()
Returns the size of points for point meshes

Returns:
the size of points
See Also:
setPointSize(float)

setPointSize

public void setPointSize(float pointSize)
Set the size of points for meshes of mode Mesh.Mode.Points. The point size is specified as on-screen pixels, the default value is 1.0. The point size does nothing if point sprite render state is enabled, in that case, the vertex shader must specify the point size by writing to gl_PointSize.

Parameters:
pointSize - The size of points

getLineWidth

public float getLineWidth()
Returns the line width for line meshes.

Returns:
the line width

setLineWidth

public void setLineWidth(float lineWidth)
Specify the line width for meshes of the line modes, such as Mesh.Mode.Lines. The line width is specified as on-screen pixels, the default value is 1.0.

Parameters:
lineWidth - The line width

setStatic

public void setStatic()
Indicates to the GPU that this mesh will not be modified (a hint). Sets the usage mode to VertexBuffer.Usage.Static for all vertex buffers on this Mesh.


setDynamic

public void setDynamic()
Indicates to the GPU that this mesh will be modified occasionally (a hint). Sets the usage mode to VertexBuffer.Usage.Dynamic for all vertex buffers on this Mesh.


setStreamed

public void setStreamed()
Indicates to the GPU that this mesh will be modified every frame (a hint). Sets the usage mode to VertexBuffer.Usage.Stream for all vertex buffers on this Mesh.


setInterleaved

@Deprecated
public void setInterleaved()
Deprecated. 

Interleaves the data in this mesh. This operation cannot be reversed. Some GPUs may prefer the data in this format, however it is a good idea to avoid using this method as it disables some engine features.


updateCounts

public void updateCounts()
Update the vertex and triangle counts for this mesh based on the current data. This method should be called after the capacities of the mesh's vertex buffers has been altered.

Throws:
java.lang.IllegalStateException - If this mesh is in interleaved format.

getTriangleCount

public int getTriangleCount(int lod)
Returns the triangle count for the given LOD level.

Parameters:
lod - The lod level to look up
Returns:
The triangle count for that LOD level

getTriangleCount

public int getTriangleCount()
Returns how many triangles or elements are on this Mesh. This value is only updated when updateCounts() is called. If the mesh mode is not a triangle mode, then this returns the number of elements/primitives, e.g. how many lines or how many points, instead of how many triangles.

Returns:
how many triangles/elements are on this Mesh.

getVertexCount

public int getVertexCount()
Returns the number of vertices on this mesh. The value is computed based on the position buffer, which must be set on all meshes.

Returns:
Number of vertices on the mesh

getTriangle

public void getTriangle(int index,
                        Vector3f v1,
                        Vector3f v2,
                        Vector3f v3)
Gets the triangle vertex positions at the given triangle index and stores them into the v1, v2, v3 arguments.

Parameters:
index - The index of the triangle. Should be between 0 and getTriangleCount().
v1 - Vector to contain first vertex position
v2 - Vector to contain second vertex position
v3 - Vector to contain third vertex position

getTriangle

public void getTriangle(int index,
                        Triangle tri)
Gets the triangle vertex positions at the given triangle index and stores them into the Triangle argument. Also sets the triangle index to the index argument.

Parameters:
index - The index of the triangle. Should be between 0 and getTriangleCount().
tri - The triangle to store the positions in

getTriangle

public void getTriangle(int index,
                        int[] indices)
Gets the triangle vertex indices at the given triangle index and stores them into the given int array.

Parameters:
index - The index of the triangle. Should be between 0 and getTriangleCount().
indices - Indices of the triangle's vertices

getId

public int getId()
Returns the mesh's VAO ID. Internal use only.


setId

public void setId(int id)
Sets the mesh's VAO ID. Internal use only.


createCollisionData

public void createCollisionData()
Generates a collision tree for the mesh. Called automatically by collideWith(com.jme3.collision.Collidable, com.jme3.math.Matrix4f, com.jme3.bounding.BoundingVolume, com.jme3.collision.CollisionResults).


collideWith

public int collideWith(Collidable other,
                       Matrix4f worldMatrix,
                       BoundingVolume worldBound,
                       CollisionResults results)
Handles collision detection, internal use only. User code should only use collideWith() on scene graph elements such as Spatials.


setBuffer

public void setBuffer(VertexBuffer vb)
Sets the VertexBuffer on the mesh. This will update the vertex/triangle counts if needed.

Parameters:
vb - The buffer to set
Throws:
java.lang.IllegalArgumentException - If the buffer type is already set

clearBuffer

public void clearBuffer(VertexBuffer.Type type)
Unsets the VertexBuffer set on this mesh with the given type. Does nothing if the vertex buffer type is not set initially.

Parameters:
type - The buffer type to remove

setBuffer

public void setBuffer(VertexBuffer.Type type,
                      int components,
                      VertexBuffer.Format format,
                      java.nio.Buffer buf)
Creates a VertexBuffer for the mesh or modifies the existing one per the parameters given.

Parameters:
type - The type of the buffer
components - Number of components
format - Data format
buf - The buffer data
Throws:
java.lang.UnsupportedOperationException - If the buffer already set is incompatible with the parameters given.

setBuffer

public void setBuffer(VertexBuffer.Type type,
                      int components,
                      java.nio.FloatBuffer buf)
Set a floating point VertexBuffer on the mesh.

Parameters:
type - The type of VertexBuffer, e.g. VertexBuffer.Type.Position, VertexBuffer.Type.Normal, etc.
components - Number of components on the vertex buffer, should be between 1 and 4.
buf - The floating point data to contain

setBuffer

public void setBuffer(VertexBuffer.Type type,
                      int components,
                      float[] buf)

setBuffer

public void setBuffer(VertexBuffer.Type type,
                      int components,
                      java.nio.IntBuffer buf)

setBuffer

public void setBuffer(VertexBuffer.Type type,
                      int components,
                      int[] buf)

setBuffer

public void setBuffer(VertexBuffer.Type type,
                      int components,
                      java.nio.ShortBuffer buf)

setBuffer

public void setBuffer(VertexBuffer.Type type,
                      int components,
                      byte[] buf)

setBuffer

public void setBuffer(VertexBuffer.Type type,
                      int components,
                      java.nio.ByteBuffer buf)

setBuffer

public void setBuffer(VertexBuffer.Type type,
                      int components,
                      short[] buf)

getBuffer

public VertexBuffer getBuffer(VertexBuffer.Type type)
Get the VertexBuffer stored on this mesh with the given type.

Parameters:
type - The type of VertexBuffer
Returns:
the VertexBuffer data, or null if not set

getFloatBuffer

public java.nio.FloatBuffer getFloatBuffer(VertexBuffer.Type type)
Get the VertexBuffer data stored on this mesh in float format.

Parameters:
type - The type of VertexBuffer
Returns:
the VertexBuffer data, or null if not set

getShortBuffer

public java.nio.ShortBuffer getShortBuffer(VertexBuffer.Type type)
Get the VertexBuffer data stored on this mesh in short format.

Parameters:
type - The type of VertexBuffer
Returns:
the VertexBuffer data, or null if not set

getIndicesAsList

public IndexBuffer getIndicesAsList()
Acquires an index buffer that will read the vertices on the mesh as a list.

Returns:
A virtual or wrapped index buffer to read the data as a list

getIndexBuffer

public IndexBuffer getIndexBuffer()
Get the index buffer for this mesh. Will return null if no index buffer is set.

Returns:
The index buffer of this mesh.
See Also:
VertexBuffer.Type.Index

extractVertexData

public void extractVertexData(Mesh other)
Extracts the vertex attributes from the given mesh into this mesh, by using this mesh's index buffer to index into the attributes of the other mesh. Note that this will also change this mesh's index buffer so that the references to the vertex data match the new indices.

Parameters:
other - The mesh to extract the vertex data from

scaleTextureCoordinates

public void scaleTextureCoordinates(Vector2f scaleFactor)
Scales the texture coordinate buffer on this mesh by the given scale factor.

Note that values above 1 will cause the texture to tile, while values below 1 will cause the texture to stretch.

Parameters:
scaleFactor - The scale factor to scale by. Every texture coordinate is multiplied by this vector to get the result.
Throws:
java.lang.IllegalStateException - If there's no texture coordinate buffer on the mesh
java.lang.UnsupportedOperationException - If the texture coordinate buffer is not in 2D float format.

updateBound

public void updateBound()
Updates the bounding volume of this mesh. The method does nothing if the mesh has no VertexBuffer.Type.Position buffer. It is expected that the position buffer is a float buffer with 3 components.


getBound

public BoundingVolume getBound()
Returns the BoundingVolume of this Mesh. By default the bounding volume is a BoundingBox.

Returns:
the bounding volume of this mesh

setBound

public void setBound(BoundingVolume modelBound)
Sets the BoundingVolume for this Mesh. The bounding volume is recomputed by calling updateBound().

Parameters:
modelBound - The model bound to set

getBuffers

public IntMap<VertexBuffer> getBuffers()
Returns a map of all vertex buffers on this Mesh. The integer key for the map is the ordinal of the vertex buffer's VertexBuffer.Type. Note that the returned map is a reference to the map used internally, modifying it will cause undefined results.

Returns:
map of vertex buffers on this mesh.

getBufferList

public SafeArrayList<VertexBuffer> getBufferList()
Returns a list of all vertex buffers on this Mesh. Using a list instead an IntMap via the getBuffers() method is better for iteration as there's no need to create an iterator instance. Note that the returned list is a reference to the list used internally, modifying it will cause undefined results.

Returns:
list of vertex buffers on this mesh.

write

public void write(JmeExporter ex)
           throws java.io.IOException
Specified by:
write in interface Savable
Throws:
java.io.IOException

read

public void read(JmeImporter im)
          throws java.io.IOException
Specified by:
read in interface Savable
Throws:
java.io.IOException