com.jmex.terrain
Class TerrainBlock

java.lang.Object
  extended by com.jme.scene.Spatial
      extended by com.jme.scene.Geometry
          extended by com.jme.scene.TriMesh
              extended by com.jmex.terrain.TerrainBlock
All Implemented Interfaces:
Savable, java.io.Serializable

public class TerrainBlock
extends TriMesh

TerrainBlock defines the lowest level of the terrain system. TerrainBlock is the actual part of the terrain system that renders to the screen. The terrain is built from a heightmap defined by a one dimenensional int array. The step scale is used to define the amount of units each block line will extend. By directly creating a TerrainBlock yourself, you can generate a brute force terrain. This is many times sufficient for small terrains on modern hardware. If terrain is to be large, it is recommended that you make use of the TerrainPage class.

Author:
Mark Powell, Joshua Slack
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class com.jme.scene.TriMesh
TriMesh.Mode
 
Nested classes/interfaces inherited from class com.jme.scene.Spatial
Spatial.CullHint, Spatial.LightCombineMode, Spatial.NormalsMode, Spatial.TextureCombineMode
 
Field Summary
 
Fields inherited from class com.jme.scene.TriMesh
indexBuffer, mode, triangleQuantity
 
Fields inherited from class com.jme.scene.Geometry
binormalBuf, bound, castsShadows, colorBuf, compVect, defaultColor, displayListID, enabled, fogBuf, hasDirtyVertices, normBuf, states, tangentBuf, texBuf, vboInfo, vertBuf, vertQuantity
 
Fields inherited from class com.jme.scene.Spatial
cullHint, frustrumIntersects, geometricalControllers, isCollidable, lightCombineMode, localRotation, localScale, localTranslation, LOCKED_BOUNDS, LOCKED_BRANCH, LOCKED_MESH_DATA, LOCKED_NONE, LOCKED_SHADOWS, LOCKED_TRANSFORMS, lockedMode, name, normalsMode, parent, queueDistance, renderQueueMode, renderStateList, textureCombineMode, worldBound, worldRotation, worldScale, worldTranslation, zOrder
 
Constructor Summary
TerrainBlock()
          Empty Constructor to be used internally only.
TerrainBlock(java.lang.String name)
          For internal use only.
TerrainBlock(java.lang.String name, int size, Vector3f stepScale, float[] heightMap, Vector3f origin)
          Constructor instantiates a new TerrainBlock object.
TerrainBlock(java.lang.String name, int size, Vector3f stepScale, float[] heightMap, Vector3f origin, int totalSize, Vector2f offset, float offsetAmount)
          Constructor instantiates a new TerrainBlock object.
 
Method Summary
 void addHeightMapValue(int x, int y, float toAdd)
          setHeightMapValue adds to the value of this block's height map at the given coords
 void buildTextureCoordinates()
          buildTextureCoordinates calculates the texture coordinates of the terrain.
 float getHeight(float x, float z)
          getHeight returns the height of an arbitrary point on the terrain.
 float getHeight(Vector2f position)
          getHeight returns the height of an arbitrary point on the terrain.
 float getHeight(Vector3f position)
          getHeight returns the height of an arbitrary point on the terrain.
 float getHeightFromWorld(Vector3f position)
          getHeightFromWorld returns the height of an arbitrary point on the terrain when given world coordinates.
 float[] getHeightMap()
          Returns the height map this terrain block is using.
 Vector2f getOffset()
          Returns the current offset amount.
 float getOffsetAmount()
          Returns the offset amount this terrain block uses for textures.
 short getQuadrant()
           
 int getSize()
          Returns the size of this terrain block.
 Vector3f getStepScale()
          Returns the step scale that stretches the height map.
 Vector3f getSurfaceNormal(float x, float z, Vector3f store)
          getSurfaceNormal returns the normal of an arbitrary point on the terrain.
 Vector3f getSurfaceNormal(Vector2f position, Vector3f store)
          getSurfaceNormal returns the normal of an arbitrary point on the terrain.
 Vector3f getSurfaceNormal(Vector3f position, Vector3f store)
          getSurfaceNormal returns the normal of an arbitrary point on the terrain.
 int getTotalSize()
          Returns the total size of the terrain.
 boolean hasChanged()
           
 void multHeightMapValue(int x, int y, float toMult)
          setHeightMapValue multiplies the value of this block's height map at the given coords by the value given.
 void read(JMEImporter e)
           
 void setDetailTexture(int unit, float repeat)
          setDetailTexture copies the texture coordinates from the first texture channel to another channel specified by unit, mulitplying by the factor specified by repeat so that the texture in that channel will be repeated that many times across the block.
 void setHeightMap(float[] heightMap)
          Sets the terrain's height map.
 void setHeightMapValue(int x, int y, float newVal)
          setHeightMapValue sets the value of this block's height map at the given coords
 void setOffset(Vector2f offset)
          Sets the value for the current offset amount to use when building texture coordinates.
 void setOffsetAmount(float offsetAmount)
          Sets the offset of this terrain texture map.
 void setQuadrant(short quadrant)
           
 void setSize(int size)
          Sets the size of this terrain block.
 void setStepScale(Vector3f stepScale)
          Sets the step scale of this terrain block's height map.
 void setTotalSize(int totalSize)
          Sets the total size of the terrain .
 void updateFromHeightMap()
          Updates the block's vertices and normals from the current height map values.
 void write(JMEExporter e)
           
 
Methods inherited from class com.jme.scene.TriMesh
clearBuffers, draw, findCollisions, findTriangleCollision, findTrianglePick, getIndexBuffer, getMaxIndex, getMeshAsTriangles, getMeshAsTrianglesVertices, getMode, getTriangle, getTriangle, getTriangleCount, getTriangleIndices, getVertIndex, hasCollision, hasTriangleCollision, randomPointOnTriangles, recalcTriangleQuantity, reconstruct, setIndexBuffer, setMode, setTriangleQuantity
 
Methods inherited from class com.jme.scene.Geometry
addTextureCoordinates, addTextureCoordinates, applyRenderState, checkTextureCoordinates, clearTextureBuffers, copyTextureCoordinates, copyTextureCoordinates, findPick, getBinormalBuffer, getColorBuffer, getDefaultColor, getDisplayListID, getFogBuffer, getLightState, getModelBound, getNormalBuffer, getNumberOfUnits, getTangentBuffer, getTextureCoords, getTextureCoords, getVBOInfo, getVertexBuffer, getVertexCount, getWorldCoords, getWorldNormals, hasDirtyVertices, isCastsShadows, lockMeshes, postdraw, predraw, randomVertex, reconstruct, resizeTextureIds, rotateNormals, rotatePoints, scaleTextureCoordinates, scaleTextureCoordinates, setBinormalBuffer, setCastsShadows, setColorBuffer, setDefaultColor, setDisplayListID, setFogCoordBuffer, setHasDirtyVertices, setLightState, setModelBound, setNormalBuffer, setRandomColors, setSolidColor, setTangentBuffer, setTextureCoords, setTextureCoords, setTextureCoords, setVBOInfo, setVertexBuffer, setVertexCount, sortLights, translatePoints, translatePoints, unlockMeshes, updateModelBound, updateWorldBound
 
Methods inherited from class com.jme.scene.Spatial
addController, calculateCollisions, calculatePick, clearControllers, clearRenderState, clearRenderState, getClassTag, getController, getControllerCount, getControllers, getCullHint, getLastFrustumIntersection, getLightCombineMode, getLocalCullHint, getLocalLightCombineMode, getLocalNormalsMode, getLocalRenderQueueMode, getLocalRotation, getLocalScale, getLocalTextureCombineMode, getLocalToWorldMatrix, getLocalTranslation, getLocks, getName, getNormalsMode, getParent, getRenderQueueMode, getRenderState, getRenderState, getTextureCombineMode, getUserData, getWorldBound, getWorldRotation, getWorldScale, getWorldTranslation, getZOrder, hasAncestor, isCollidable, localToWorld, lock, lock, lockBounds, lockBranch, lockMeshes, lockShadows, lockTransforms, lookAt, onDraw, propagateBoundToRoot, propagateStatesFromRoot, removeController, removeController, removeFromParent, removeUserData, rotateUpTo, setCullHint, setIsCollidable, setLastFrustumIntersection, setLightCombineMode, setLocalRotation, setLocalRotation, setLocalScale, setLocalScale, setLocalTranslation, setLocalTranslation, setLocks, setLocks, setName, setNormalsMode, setParent, setRenderQueueMode, setRenderState, setTextureCombineMode, setUserData, setZOrder, setZOrder, toString, unlock, unlock, unlockBounds, unlockBranch, unlockMeshes, unlockShadows, unlockTransforms, updateGeometricState, updateRenderState, updateRenderState, updateWorldData, updateWorldRotation, updateWorldScale, updateWorldTranslation, updateWorldVectors, updateWorldVectors, worldToLocal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.jme.util.export.Savable
getClassTag
 

Constructor Detail

TerrainBlock

public TerrainBlock()
Empty Constructor to be used internally only.


TerrainBlock

public TerrainBlock(java.lang.String name)
For internal use only. Creates a new TerrainBlock with the given name by simply calling super(name)

Parameters:
name - The name.

TerrainBlock

public TerrainBlock(java.lang.String name,
                    int size,
                    Vector3f stepScale,
                    float[] heightMap,
                    Vector3f origin)
Constructor instantiates a new TerrainBlock object. The parameters and heightmap data are then processed to generate a TriMesh object for rendering.

Parameters:
name - the name of the terrain block.
size - the size of the heightmap.
stepScale - the scale for the axes.
heightMap - the height data.
origin - the origin offset of the block.

TerrainBlock

public TerrainBlock(java.lang.String name,
                    int size,
                    Vector3f stepScale,
                    float[] heightMap,
                    Vector3f origin,
                    int totalSize,
                    Vector2f offset,
                    float offsetAmount)
Constructor instantiates a new TerrainBlock object. The parameters and heightmap data are then processed to generate a TriMesh object for renderering.

Parameters:
name - the name of the terrain block.
size - the size of the block.
stepScale - the scale for the axes.
heightMap - the height data.
origin - the origin offset of the block.
totalSize - the total size of the terrain. (Higher if the block is part of a TerrainPage tree.
offset - the offset for texture coordinates.
offsetAmount - the total offset amount. Used for texture coordinates.
Method Detail

setDetailTexture

public void setDetailTexture(int unit,
                             float repeat)
setDetailTexture copies the texture coordinates from the first texture channel to another channel specified by unit, mulitplying by the factor specified by repeat so that the texture in that channel will be repeated that many times across the block.

Parameters:
unit - channel to copy coords to
repeat - number of times to repeat the texture across and down the block

getHeight

public float getHeight(Vector2f position)
getHeight returns the height of an arbitrary point on the terrain. If the point is between height point values, the height is linearly interpolated. This provides smooth height calculations. If the point provided is not within the bounds of the height map, the NaN float value is returned (Float.NaN).

Parameters:
position - the vector representing the height location to check.
Returns:
the height at the provided location.

getHeight

public float getHeight(Vector3f position)
getHeight returns the height of an arbitrary point on the terrain. If the point is between height point values, the height is linearly interpolated. This provides smooth height calculations. If the point provided is not within the bounds of the height map, the NaN float value is returned (Float.NaN).

Parameters:
position - the vector representing the height location to check. Only the x and z values are used.
Returns:
the height at the provided location.

getHeight

public float getHeight(float x,
                       float z)
getHeight returns the height of an arbitrary point on the terrain. If the point is between height point values, the height is linearly interpolated. This provides smooth height calculations. If the point provided is not within the bounds of the height map, the NaN float value is returned (Float.NaN).

Parameters:
x - the x coordinate to check.
z - the z coordinate to check.
Returns:
the height at the provided location.

getHeightFromWorld

public float getHeightFromWorld(Vector3f position)
getHeightFromWorld returns the height of an arbitrary point on the terrain when given world coordinates. If the point is between height point values, the height is linearly interpolated. This provides smooth height calculations. If the point provided is not within the bounds of the height map, the NaN float value is returned (Float.NaN).

Parameters:
position - the vector representing the height location to check.
Returns:
the height at the provided location.

getSurfaceNormal

public Vector3f getSurfaceNormal(Vector2f position,
                                 Vector3f store)
getSurfaceNormal returns the normal of an arbitrary point on the terrain. The normal is linearly interpreted from the normals of the 4 nearest defined points. If the point provided is not within the bounds of the height map, null is returned.

Parameters:
position - the vector representing the location to find a normal at.
store - the Vector3f object to store the result in. If null, a new one is created.
Returns:
the normal vector at the provided location.

getSurfaceNormal

public Vector3f getSurfaceNormal(Vector3f position,
                                 Vector3f store)
getSurfaceNormal returns the normal of an arbitrary point on the terrain. The normal is linearly interpreted from the normals of the 4 nearest defined points. If the point provided is not within the bounds of the height map, null is returned.

Parameters:
position - the vector representing the location to find a normal at. Only the x and z values are used.
store - the Vector3f object to store the result in. If null, a new one is created.
Returns:
the normal vector at the provided location.

getSurfaceNormal

public Vector3f getSurfaceNormal(float x,
                                 float z,
                                 Vector3f store)
getSurfaceNormal returns the normal of an arbitrary point on the terrain. The normal is linearly interpreted from the normals of the 4 nearest defined points. If the point provided is not within the bounds of the height map, null is returned.

Parameters:
x - the x coordinate to check.
z - the z coordinate to check.
store - the Vector3f object to store the result in. If null, a new one is created.
Returns:
the normal unit vector at the provided location.

buildTextureCoordinates

public void buildTextureCoordinates()
buildTextureCoordinates calculates the texture coordinates of the terrain.


getHeightMap

public float[] getHeightMap()
Returns the height map this terrain block is using.

Returns:
This terrain block's height map.

getOffsetAmount

public float getOffsetAmount()
Returns the offset amount this terrain block uses for textures.

Returns:
The current offset amount.

getStepScale

public Vector3f getStepScale()
Returns the step scale that stretches the height map.

Returns:
The current step scale.

getTotalSize

public int getTotalSize()
Returns the total size of the terrain.

Returns:
The terrain's total size.

getSize

public int getSize()
Returns the size of this terrain block.

Returns:
The current block size.

getOffset

public Vector2f getOffset()
Returns the current offset amount. This is used when building texture coordinates.

Returns:
The current offset amount.

setOffset

public void setOffset(Vector2f offset)
Sets the value for the current offset amount to use when building texture coordinates. Note that this does NOT rebuild the terrain at all. This is mostly used for outside constructors of terrain blocks.

Parameters:
offset - The new texture offset.

setSize

public void setSize(int size)
Sets the size of this terrain block. Note that this does NOT rebuild the terrain at all. This is mostly used for outside constructors of terrain blocks.

Parameters:
size - The new size.

setTotalSize

public void setTotalSize(int totalSize)
Sets the total size of the terrain . Note that this does NOT rebuild the terrain at all. This is mostly used for outside constructors of terrain blocks.

Parameters:
totalSize - The new total size.

setStepScale

public void setStepScale(Vector3f stepScale)
Sets the step scale of this terrain block's height map. Note that this does NOT rebuild the terrain at all. This is mostly used for outside constructors of terrain blocks.

Parameters:
stepScale - The new step scale.

setOffsetAmount

public void setOffsetAmount(float offsetAmount)
Sets the offset of this terrain texture map. Note that this does NOT rebuild the terrain at all. This is mostly used for outside constructors of terrain blocks.

Parameters:
offsetAmount - The new texture offset.

setHeightMap

public void setHeightMap(float[] heightMap)
Sets the terrain's height map. Note that this does NOT rebuild the terrain at all. This is mostly used for outside constructors of terrain blocks.

Parameters:
heightMap - The new height map.

updateFromHeightMap

public void updateFromHeightMap()
Updates the block's vertices and normals from the current height map values.


setHeightMapValue

public void setHeightMapValue(int x,
                              int y,
                              float newVal)
setHeightMapValue sets the value of this block's height map at the given coords

Parameters:
x -
y -
newVal -

addHeightMapValue

public void addHeightMapValue(int x,
                              int y,
                              float toAdd)
setHeightMapValue adds to the value of this block's height map at the given coords

Parameters:
x -
y -
toAdd -

multHeightMapValue

public void multHeightMapValue(int x,
                               int y,
                               float toMult)
setHeightMapValue multiplies the value of this block's height map at the given coords by the value given.

Parameters:
x -
y -
toMult -

hasChanged

public boolean hasChanged()

getQuadrant

public short getQuadrant()
Returns:
Returns the quadrant.

setQuadrant

public void setQuadrant(short quadrant)
Parameters:
quadrant - The quadrant to set.

write

public void write(JMEExporter e)
           throws java.io.IOException
Specified by:
write in interface Savable
Overrides:
write in class TriMesh
Throws:
java.io.IOException

read

public void read(JMEImporter e)
          throws java.io.IOException
Specified by:
read in interface Savable
Overrides:
read in class TriMesh
Throws:
java.io.IOException