public class TerrainGrid extends TerrainQuad
TerrainGrid itself is an actual TerrainQuad. Its four children are the visible four tiles.
The grid is indexed by cells. Each cell has an integer XZ coordinate originating at 0,0. TerrainGrid will piggyback on the TerrainLodControl so it can use the camera for its updates as well. It does this in the overwritten update() method.
It uses an LRU (Least Recently Used) cache of 16 terrain tiles (full TerrainQuadTrees). The center 4 are the ones that are visible. As the camera moves, it checks what camera cell it is in and will attach the now visible tiles.
The 'quadIndex' variable is a 4x4 array that represents the tiles. The center four (index numbers: 5, 6, 9, 10) are what is visible. Each quadIndex value is an offset vector. The vector contains whole numbers and represents how many tiles in offset this location is from the center of the map. So for example the index 11 [Vector3f(2, 0, 1)] is located 2*terrainSize in X axis and 1*terrainSize in Z axis.
As the camera moves, it tests what cameraCell it is in. Each camera cell covers four quad tiles and is half way inside each one.
+-------+-------+ | 1 | 3 | Four terrainQuads that make up the grid | *..|..* | with the cameraCell in the middle, covering |----|--|--|----| all four quads. | *..|..* | | 2 | 4 | +-------+-------+
This results in the effect of when the camera gets half way across one of the sides of a quad to an empty (non-loaded) area, it will trigger the system to load in the next tiles.
The tile loading is done on a background thread, and once the tile is loaded, then it is attached to the grid quad tree, back on the OGL thread. It will grab the terrain quad from the LRU cache if it exists. If it does not exist, it will load in the new TerrainQuad tile.
The loading of new tiles triggers events for any TerrainGridListeners. The events are:
These allow physics to update, and other operation (often needed for loading the terrain) to occur at the right time.
Modifier and Type | Class and Description |
---|---|
protected class |
TerrainGrid.UpdateQuadCache |
TerrainQuad.LocationHeight
Spatial.BatchHint, Spatial.CullHint, Spatial.DFSMode
Modifier and Type | Field and Description |
---|---|
protected LRUCache<Vector3f,TerrainQuad> |
cache |
protected java.util.concurrent.ExecutorService |
cacheExecutor |
protected int |
cellsLoaded |
protected Vector3f |
currentCamCell |
protected int[] |
gridOffset |
protected java.util.Set<TerrainGridListener> |
listeners |
protected static java.util.logging.Logger |
log |
protected Material |
material |
protected Vector3f[] |
quadIndex |
protected int |
quadSize |
protected int |
quarterSize |
protected boolean |
runOnce |
neighbourFinder, offset, offsetAmount, patchSize, quadrant, size, stepScale, totalSize
batchHint, controls, cullHint, frustrumIntersects, key, localLights, localOverrides, localTransform, name, parent, queueBucket, queueDistance, refreshFlags, RF_BOUND, RF_CHILD_LIGHTLIST, RF_LIGHTLIST, RF_MATPARAM_OVERRIDE, RF_TRANSFORM, shadowMode, userData, worldBound, worldLights, worldOverrides, worldTransform
Constructor and Description |
---|
TerrainGrid() |
TerrainGrid(java.lang.String name,
int patchSize,
int maxVisibleSize,
TerrainGridTileLoader terrainQuadGrid) |
TerrainGrid(java.lang.String name,
int patchSize,
int maxVisibleSize,
Vector3f scale,
TerrainGridTileLoader terrainQuadGrid) |
TerrainGrid(java.lang.String name,
int patchSize,
int maxVisibleSize,
Vector3f scale,
TerrainGridTileLoader terrainQuadGrid,
Vector2f offset,
float offsetAmount) |
Modifier and Type | Method and Description |
---|---|
void |
addListener(TerrainGridListener listener) |
void |
adjustHeight(java.util.List<Vector2f> xz,
java.util.List<java.lang.Float> height)
Raise/lower the height at many points.
|
protected void |
attachQuadAt(TerrainQuad q,
int quadrant,
Vector3f quadCell,
boolean shifted)
Runs on the rendering thread
|
protected java.util.concurrent.ExecutorService |
createExecutorService()
This will print out any exceptions from the thread
|
Vector3f |
getCamCell(Vector3f location)
Get the location in cell-coordinates of the specified location.
|
Vector3f |
getCurrentCell() |
TerrainGridTileLoader |
getGridTileLoader() |
protected float |
getHeightmapHeight(int x,
int z)
This will just get the heightmap value at the supplied point,
not an interpolated (actual) height value.
|
Material |
getMaterial(Vector3f worldLocation)
Returns the material that this terrain uses.
|
int |
getNumMajorSubdivisions() |
protected int |
getQuadrant(int quadIndex) |
Terrain |
getTerrainAt(Vector3f worldLocation)
Get the terrain tile at the specified world location, in XZ coordinates.
|
Terrain |
getTerrainAtCell(Vector3f cellCoordinate)
Get the terrain tile at the specified XZ cell coordinate (not world coordinate).
|
Vector3f |
getTileCell(Vector3f location)
Centered at 0,0.
|
protected boolean |
isCenter(int quadIndex) |
void |
read(JmeImporter im) |
void |
removeListener(TerrainGridListener listener) |
protected void |
removeQuad(TerrainQuad q) |
void |
setMaterial(Material mat)
Applies the given material to the Spatial, this will propagate the
material down to the geometries in the scene graph.
|
void |
setQuadSize(int quadSize) |
Vector3f |
toCellSpace(Vector3f worldLocation)
Convert the world location into a cell location (integer coordinates)
|
Vector3f |
toWorldSpace(Vector3f cellLocation)
Convert the cell coordinate (integer coordinates) into world coordinates.
|
protected void |
updateChildren(Vector3f camCell)
Called when the camera has moved into a new cell.
|
void |
write(JmeExporter ex) |
adjustHeight, attachBoundChildren, cacheTerrainTransforms, calculateLod, clearCaches, clone, clone, cloneFields, collideWith, createHeightSubBlock, createQuad, createQuadPatch, findDownPatch, findDownQuad, findLeftPatch, findLeftQuad, findNeighboursLod, findPick, findRightPatch, findRightQuad, findTopPatch, findTopQuad, fixEdges, fixNormalEdges, fixNormals, generateDebugTangents, generateEntropy, getAllTerrainPatches, getAllTerrainPatchesWithTranslation, getHeight, getHeight, getHeightMap, getHeightmapHeight, getMaterial, getMaxLod, getMeshNormal, getNormal, getNormal, getPatch, getPatchSize, getQuad, getQuadrant, getTerrainSize, getTotalSize, isPointOnTerrain, isRootQuad, needToRecalculateNormals, recalculateAllNormals, reIndexPages, resetCachedNeighbours, setHeight, setHeight, setHeight, setHeight, setLocked, setNeedToRecalculateNormals, setNeighbourFinder, setNormalRecalcNeeded, setParent, setQuadrant, setSupportMultipleCollisions, split, updateNormals
attachChild, attachChildAt, breadthFirstTraversal, deepClone, depthFirstTraversal, descendantMatches, descendantMatches, descendantMatches, detachAllChildren, detachChild, detachChildAt, detachChildNamed, getChild, getChild, getChildIndex, getChildren, getQuantity, getTriangleCount, getVertexCount, hasChild, oldDeepClone, setLightListRefresh, setLodLevel, setMatParamOverrideRefresh, setModelBound, setTransformRefresh, swapChildren, updateGeometricState, updateLogicalState, updateModelBound, updateWorldBound
addControl, addLight, addMatParamOverride, breadthFirstTraversal, center, checkCulling, clearMatParamOverrides, depthFirstTraversal, forceRefresh, getBatchHint, getControl, getControl, getCullHint, getKey, getLastFrustumIntersection, getLocalBatchHint, getLocalCullHint, getLocalLightList, getLocalMatParamOverrides, getLocalQueueBucket, getLocalRotation, getLocalScale, getLocalShadowMode, getLocalToWorldMatrix, getLocalTransform, getLocalTranslation, getName, getNumControls, getParent, getQueueBucket, getShadowMode, getUserData, getUserDataKeys, getWorldBound, getWorldLightList, getWorldMatParamOverrides, getWorldRotation, getWorldScale, getWorldTransform, getWorldTranslation, hasAncestor, jmeClone, localToWorld, lookAt, matches, move, move, oldClone, removeControl, removeControl, removeFromParent, removeLight, removeMatParamOverride, rotate, rotate, rotateUpTo, runControlRender, scale, scale, setBatchHint, setBoundRefresh, setCullHint, setKey, setLastFrustumIntersection, setLocalRotation, setLocalRotation, setLocalScale, setLocalScale, setLocalScale, setLocalTransform, setLocalTranslation, setLocalTranslation, setName, setQueueBucket, setRequiresUpdates, setShadowMode, setUserData, toString, updateMatParamOverrides, updateWorldLightList, updateWorldTransforms, worldToLocal
protected static final java.util.logging.Logger log
protected Vector3f currentCamCell
protected int quarterSize
protected int quadSize
protected Vector3f[] quadIndex
protected java.util.Set<TerrainGridListener> listeners
protected Material material
protected LRUCache<Vector3f,TerrainQuad> cache
protected int cellsLoaded
protected int[] gridOffset
protected boolean runOnce
protected java.util.concurrent.ExecutorService cacheExecutor
public TerrainGrid(java.lang.String name, int patchSize, int maxVisibleSize, Vector3f scale, TerrainGridTileLoader terrainQuadGrid, Vector2f offset, float offsetAmount)
public TerrainGrid(java.lang.String name, int patchSize, int maxVisibleSize, Vector3f scale, TerrainGridTileLoader terrainQuadGrid)
public TerrainGrid(java.lang.String name, int patchSize, int maxVisibleSize, TerrainGridTileLoader terrainQuadGrid)
public TerrainGrid()
protected boolean isCenter(int quadIndex)
protected int getQuadrant(int quadIndex)
public Vector3f getCamCell(Vector3f location)
public Vector3f getTileCell(Vector3f location)
location
- world coordinatepublic TerrainGridTileLoader getGridTileLoader()
public Terrain getTerrainAt(Vector3f worldLocation)
public Terrain getTerrainAtCell(Vector3f cellCoordinate)
cellCoordinate
- integer cell coordinatespublic Vector3f toCellSpace(Vector3f worldLocation)
public Vector3f toWorldSpace(Vector3f cellLocation)
protected void removeQuad(TerrainQuad q)
protected void attachQuadAt(TerrainQuad q, int quadrant, Vector3f quadCell, boolean shifted)
shifted
- quads are still attached to the parent and don't need to re-loadprotected void updateChildren(Vector3f camCell)
camCell
- the cell the camera is inpublic void addListener(TerrainGridListener listener)
public Vector3f getCurrentCell()
public void removeListener(TerrainGridListener listener)
public void setMaterial(Material mat)
Spatial
setMaterial
in class Node
mat
- The material to set.public void setQuadSize(int quadSize)
public void adjustHeight(java.util.List<Vector2f> xz, java.util.List<java.lang.Float> height)
Terrain
adjustHeight
in interface Terrain
adjustHeight
in class TerrainQuad
xz
- a list of coordinates where the height will be adjustedheight
- +- value to adjust the height by, that matches the xz coordinatesprotected float getHeightmapHeight(int x, int z)
TerrainQuad
getHeightmapHeight
in class TerrainQuad
public int getNumMajorSubdivisions()
getNumMajorSubdivisions
in interface Terrain
getNumMajorSubdivisions
in class TerrainQuad
public Material getMaterial(Vector3f worldLocation)
Terrain
getMaterial
in interface Terrain
getMaterial
in class TerrainQuad
worldLocation
- the location, in world coordinates, of where
we are interested in the underlying texture.protected java.util.concurrent.ExecutorService createExecutorService()
public void read(JmeImporter im) throws java.io.IOException
read
in interface Savable
read
in class TerrainQuad
java.io.IOException
public void write(JmeExporter ex) throws java.io.IOException
write
in interface Savable
write
in class TerrainQuad
java.io.IOException