= Polygon Meshes
:author:
:revnumber:
:revdate: 2016/03/17 20:48
:keywords: spatial, node, mesh, geometry, scenegraph
:relfileprefix: ../../
:imagesdir: ../..
ifdef::env-github,env-browser[:outfilesuffix: .adoc]



image::jme3/dolphin-mesh.png[dolphin-mesh.png,width="",height="",align="right"]


All visible game elements in a scene, whether it is a Model or a Shape, are made up of polygon meshes. JME3 has a link:{link-javadoc}/com/jme3/scene/Mesh.html[com.jme3.scene.Mesh] class that represents all meshes.

*  Meshes are made up of triangles: `getTriangleCount(…)` and `getTriangle(…)`
*  Each mesh has a unique ID: `getId()`
*  Meshes have transformations: Location (local translation), rotation, scale.
*  Meshes have a bounding volume. jME3 can detect intersections (that is, non-physical collisions) between meshes, or between meshes and 2D elements such as rays: `collideWith()`.
*  Meshes are locked with `setStatic()` and unlocked with `setDynamic()`.
**  Static Meshes cannot be modified, but are more optimized and faster (they can be precalculated).
**  Dynamic Meshes can be modified live, but are not optimized and slower.


You have several options when <<jme3/advanced/spatial#,creating Geometries from meshes>>:

*  Use built-in <<jme3/advanced/shape#,Shape>>s as meshes;
*  Load <<jme3/advanced/3d_models#,3D models>> (that is, meshes created in external applications); or
*  Create free-form <<jme3/advanced/custom_meshes#,custom meshes>> programmatically.


== Vertex Buffer

The VertexBuffer contains a particular type of geometry data used by Meshes. Every VertexBuffer set on a Mesh is sent as an attribute to the vertex shader to be processed.


=== Mesh Vertex Buffers

Here is the list of link:{link-javadoc}/com/jme3/scene/VertexBuffer.Type.html[VertexBuffer] types.

[cols="2", options="header"]
|===

a|Vertex Buffer Type
a|Description

a|Type.Position
a|Position of the vertex (3 floats)

a|Type.Index
a| Specifies the index buffer, must contain integer data.

a|Type.TexCoord
a| Texture coordinate

a|Type.TexCoord2
a| Texture coordinate #2

a|Type.Normal
a| Normal vector, normalized.

a|Type.Tangent
a| Tangent vector, normalized.

a|Type.Binormal
a| Binormal vector, normalized.

a|Type.Color
a| Color and Alpha (4 floats)

a|Type.Size
a|The size of the point when using point buffers.

a|Type.InterleavedData
a| Specifies the source data for various vertex buffers when interleaving is used.

a|Type.BindPosePosition
a| Inital vertex position, used with animation.

a|Type.BindPoseNormal
a| Inital vertex normals, used with animation

a|Type.BoneWeight
a| Bone weights, used with animation

a|Type.BoneIndex
a| Bone indices, used with animation

|===


=== Mesh Properties

Some Mesh properties from the link:{link-javadoc}/com/jme3/scene/Mesh.html[Mesh] class.

[cols="2", options="header"]
|===

a|Mesh method
a|Description

a|setBound(boundingVolume)
a|if you need to specifiy a custom optimized bounding volume

a|setStatic()
a|Locks the mesh so you cannot modify it anymore, thus optimizing its data (faster).

a|setDynamic()
a|Unlocks the mesh so you can modified it, but this will un-optimize the data (slower).

a|setMode(Mesh.Mode.Points)
a|Used to set mesh rendering modes, see below.

a|getId()
a|returns the Mesh ID, default value is -1

a|getTriangle(int,tri)
a|returns data of triangle number `int` into variable `tri`

a|scaleTextureCoordinates(Vector2f)
a|How the texture will be stretched over the whole mesh.

|===


=== Mesh Rendering Modes

Here is the list of link:{link-javadoc}/com/jme3/scene/Mesh.Mode.html[Mesh rendering modes].

[cols="2", options="header"]
|===

a|Mesh Mode
a|Description

a|Mesh.Mode.Points
a|Show only corner points (vertices) of mesh

a|Mesh.Mode.Lines
a|Show lines (edges) of mesh

a|Mesh.Mode.LineLoop
a|?

a|Mesh.Mode.LineStrip
a|?

a|Mesh.Mode.Triangles
a|?

a|Mesh.Mode.TriangleStrip
a|?

a|Mesh.Mode.TriangleFan
a|?

a|Mesh.Mode.Hybrid
a|?

|===


=== Level of Detail

Optionally, custom meshes can have a LOD (level of detail optimization) that renders more or less detail, depending on the distance of the mesh from the camera. You have to specify several vertex buffers, one for each level of detail you want (very far away with few details, close up with all details, and something in the middle). Use `setLodLevels(VertexBuffer[] lodLevels)`.
