j3d.org Code

org.j3d.loaders
Class HeightMapTerrainData

java.lang.Object
  extended by org.j3d.terrain.AbstractTerrainData
      extended by org.j3d.terrain.AbstractStaticTerrainData
          extended by org.j3d.loaders.HeightMapTerrainData
All Implemented Interfaces:
StaticTerrainData, TerrainData, HeightDataSource

public class HeightMapTerrainData
extends AbstractStaticTerrainData

Generalised implementation of the TerrainData for any file format or loader that supports a static grid based data structure.

Supporting the height data source methods requires a bit of assumption about the data. Because we have data in quads, and we don't know how the underlying terrain rendering code is triangulating the data, we have to punt and take a guess. To interpolate a height value for a point that is not directly on a grid position, the code will take the average height of the grid square. This is a really horrible algorithm, but is the fastest to implement currently. We need to re-visit this to get something a little more accurate and is proportional to the position in the cell.

The basic implementation here does not support a texture. If an application wishes to use a texture, they should extend this class and override the getTexture() method. If you wish to provide a pre-loaded texture, then you can use the setTexture(BufferedImage) method of this class to place one here.

If a color interpolator is not provided, then color is not supported in this terrain (unless set by some implementing class).

Version:
$Revision: 1.10 $
Author:
Justin Couch

Field Summary
 
Fields inherited from class org.j3d.terrain.AbstractStaticTerrainData
gridDepth, gridWidth
 
Fields inherited from class org.j3d.terrain.AbstractTerrainData
colorAvailable, gridStepX, gridStepY, textureAvailable
 
Fields inherited from interface org.j3d.terrain.TerrainData
FREEFORM_DATA, STATIC_DATA, TILED_DATA
 
Constructor Summary
HeightMapTerrainData(float[][] data, boolean mustCopy, javax.vecmath.Point2d stepDetails)
          Create a new instance that uses the passed height map data to this loader.
HeightMapTerrainData(HeightMapSource source)
          Create a new instance that sources the data from the given parser.
 
Method Summary
 void getCoordinate(float[] coord, float[] tex, float[] color, int gridX, int gridY)
          Get the coordinate with all the information - texture and colors.
 void getCoordinate(float[] coord, int gridX, int gridY)
          Get the coordinate of the point in the grid.
 void getCoordinateWithColor(float[] coord, float[] color, int gridX, int gridY)
          Get the coordinate of the point and the corresponding color value in the grid.
 void getCoordinateWithTexture(float[] coord, float[] textureCoord, int gridX, int gridY, int patchX, int patchY)
          Get the coordinate of the point and correspond texture coordinate in the grid.
 float getHeight(float x, float z)
          Get the height at the given X,Z coordinate in the local coordinate system.
 float getHeightFromGrid(int gridX, int gridY)
          Get the height at the specified grid position.
 java.awt.image.BufferedImage getTexture()
          Fetch the BufferedImage that is used to cover the entire terrain.
 void setColorInterpolator(ColorInterpolator interp)
          Set up a height color ramp to provide colour information.
 void setTexture(java.awt.image.BufferedImage tex)
          Set the texture to the new instance.
 
Methods inherited from class org.j3d.terrain.AbstractStaticTerrainData
getGridDepth, getGridWidth
 
Methods inherited from class org.j3d.terrain.AbstractTerrainData
getGridXStep, getGridYStep, getSourceDataType, hasColor, hasTexture
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.j3d.terrain.TerrainData
getGridXStep, getGridYStep, getSourceDataType, hasColor, hasTexture
 

Constructor Detail

HeightMapTerrainData

public HeightMapTerrainData(HeightMapSource source)
Create a new instance that sources the data from the given parser. Assumes that the parser has already fetched its information and has the height-grid available for use.

Parameters:
source - The place to source the data from

HeightMapTerrainData

public HeightMapTerrainData(float[][] data,
                            boolean mustCopy,
                            javax.vecmath.Point2d stepDetails)
Create a new instance that uses the passed height map data to this loader. The data passed can be either referenced or copied, depending on the value of the mustCopy parameter. If it is not copied, then the calling code should make sure that it does not change values in the array after calling this method. If copying, the code assumes a rectangular grid of points where the second dimension size is based on data[0].length.

Parameters:
data - The source data to use in [length][width] order
mustCopy - true to request an internal copy be made of the data false for it to just reference the data
stepDetails - The distance between each height value in the X and Z coordinates (Y in terrain parlance)
Method Detail

getHeight

public float getHeight(float x,
                       float z)
Get the height at the given X,Z coordinate in the local coordinate system. The

Parameters:
x - The x coordinate for the height sampling
z - The z coordinate for the height sampling
Returns:
The height at the current point or NaN

getCoordinate

public void getCoordinate(float[] coord,
                          int gridX,
                          int gridY)
Get the coordinate of the point in the grid.

Parameters:
coord - the x, y, and z coordinates will be placed in the first three elements of the array.
gridX - The X coordinate of the position in the grid
gridY - The Y coordinate of the position in the grid

getCoordinate

public void getCoordinate(float[] coord,
                          float[] tex,
                          float[] color,
                          int gridX,
                          int gridY)
Get the coordinate with all the information - texture and colors.

Parameters:
coord - he x, y, and z coordinates will be placed in the first three elements of the array.
tex - 2D coordinates are placed in the first two elements
color - 3 component colors are placed in the first 3 elements
gridX - The X coordinate of the position in the grid
gridY - The Y coordinate of the position in the grid

getCoordinateWithTexture

public void getCoordinateWithTexture(float[] coord,
                                     float[] textureCoord,
                                     int gridX,
                                     int gridY,
                                     int patchX,
                                     int patchY)
Get the coordinate of the point and correspond texture coordinate in the grid. Assumes that the grid covers a single large texture rather than multiple smaller textures.

Parameters:
coord - he x, y, and z coordinates will be placed in the first three elements of the array.
textureCoord - 2D coordinates are placed in the first two elements
gridX - The X coordinate of the position in the grid
gridY - The Y coordinate of the position in the grid

getCoordinateWithColor

public void getCoordinateWithColor(float[] coord,
                                   float[] color,
                                   int gridX,
                                   int gridY)
Get the coordinate of the point and the corresponding color value in the grid. Color values are used when there is no texture supplied, so this should always provide something useful.

Parameters:
coord - he x, y, and z coordinates will be placed in the first three elements of the array.
color - 3 component colors are placed in the first 3 elements
gridX - The X coordinate of the position in the grid
gridY - The Y coordinate of the position in the grid

getTexture

public java.awt.image.BufferedImage getTexture()
Fetch the BufferedImage that is used to cover the entire terrain. If no texture is used, then return null. Assumes a single large texture for the entire terrain.

Specified by:
getTexture in interface StaticTerrainData
Overrides:
getTexture in class AbstractStaticTerrainData
Returns:
The texture instance to use or null

getHeightFromGrid

public float getHeightFromGrid(int gridX,
                               int gridY)
Get the height at the specified grid position.

Parameters:
gridX - The X coordinate of the position in the grid
gridY - The Y coordinate of the position in the grid
Returns:
The height at the given grid position

setTexture

public void setTexture(java.awt.image.BufferedImage tex)
Set the texture to the new instance. Setting a value of null will clear the existing texture.

Overrides:
setTexture in class AbstractStaticTerrainData
Parameters:
tex - The new texture to use

setColorInterpolator

public void setColorInterpolator(ColorInterpolator interp)
Set up a height color ramp to provide colour information. This should be set before passing the terrain data to a rendering algorithm as it sets the hasColor() flag to true. Heights should be based on sea-level as value zero. A value of null clears the current reference.

Parameters:
interp - The interpolator instance to use

j3d.org Code

Latest Info from http://code.j3d.org/
Copyright © 2001 - j3d.org