My Project
|
Manages a network of Terrain objects arranged in a grid pattern to model extremely large worlds that couldn't be handled by a single Terrain object. More...
#include <TerrainLattice.h>
Public Types | |
typedef map< int, TerrainTileCacheItem > | TerrainTileCacheMap_type |
![]() | |
typedef ParaEngine::weak_ptr< IObject, IAttributeFields > | WeakPtr_type |
![]() | |
typedef ParaEngine::weak_ptr< IObject > | WeakPtr_type |
Public Member Functions | |
TerrainLattice (TerrainLatticeLoader *pLoader, bool useGeoMipmap=false) | |
ATTRIBUTE_DEFINE_CLASS (TerrainLattice) | |
virtual IAttributeFields * | GetChildAttributeObject (const std::string &sName) |
get attribute by child object. More... | |
virtual int | GetChildAttributeObjectCount (int nColumnIndex=0) |
get the number of child objects (row count) in the given column. More... | |
virtual int | GetChildAttributeColumnCount () |
we support multi-dimensional child object. More... | |
virtual IAttributeFields * | GetChildAttributeObject (int nRowIndex, int nColumnIndex=0) |
void | Load (char *szBaseName, int maxNumTriangles, int maxBlockSize, float commonRepeats) |
void | SetTileSize (float fTileSize) |
Terrain * | GetTerrain (int positionX, int positionY) |
Get the terrain at the tile location. More... | |
Terrain * | GetTerrainAtPoint (float x, float y) |
void | SetCameraPosition (float x, float y, float z) |
void | ModelViewMatrixChanged () |
void | Render () |
float | GetElevation (float x, float y) |
DWORD | GetRegionValue (const string &sLayerName, float x, float y) |
get value of a given terrain region layer More... | |
void | GetNormal (float x, float y, float &normalX, float &normalY, float &normalZ) |
Returns the surface normal of the terrain at the specified point. More... | |
float | IntersectRay (float startX, float startY, float startZ, float dirX, float dirY, float dirZ, float &intersectX, float &intersectY, float &intersectZ, float fMaxDistance=INFINITY) |
void | SetLowestVisibleHeight (float fHeight) |
Set the height of the lowest visible terrain point. More... | |
float | GetLowestVisibleHeight () |
Get the height of the lowest visible terrain point. More... | |
void | Paint (ParaEngine::TextureEntity *detailTexture, float brushRadius, float brushIntensity, float maxIntensity, bool erase, float x, float y) |
paint detailed texture on the terrain surface | |
void | Flatten (float x, float y, float radius, int flatten_op, float elevation, float factor) |
Flatten the terrain both up and down to the specified elevation, using using the tightness parameter to determine how much the altered points are allowed to deviate from the specified elevation. More... | |
void | DigCircleFlat (float x, float y, float radius, float fFlatPercentage=0.7f, float factor=0.5f) |
flatten a land centered at x,y, with a specified radius. More... | |
void | GaussianHill (float x, float y, float radius, float hscale, float standard_deviation=0.1f, float smooth_factor=0.5f) |
void | Spherical (float x, float y, float radius, float offset) |
offset in a spherical region More... | |
void | RadialScale (float x, float y, float scale_factor, float min_dist, float max_dist, float smooth_factor=0.5f, int frequency=1) |
Note: terrain data should be in normalized space with height in the range [0,1]. More... | |
void | Roughen_Smooth (float x, float y, float radius, bool roughen=false, bool big_grid=false, float factor=0.5f) |
square filter for sharpening and smoothing. More... | |
void | AddHeightField (float x, float y, const char *filename, int nSmoothPixels=7) |
Add rectangular height field from file to the current terrain. More... | |
void | MergeHeightField (float x, float y, const char *filename, int mergeOperation=0, float weight1=1.0, float weight2=1.0, int nSmoothPixels=7) |
merge a rectangular height field from file to the current terrain. More... | |
void | Ramp (float x1, float y1, float x2, float y2, float radius, float borderpercentage=0.5f, float factor=1.0f) |
create a ramp (inclined slope) from (x1,y1) to (x2,y2). More... | |
void | UpdateHoles (float x, float y) |
Update all holes in the terrain tile that contains the input point. More... | |
bool | IsHole (float x, float y) |
Whether the terrain contains a hole at the specified location. More... | |
void | SetHole (float x, float y, bool bIsHold) |
set a new terrain hole at the specified location. More... | |
void | InitDeviceObjects () |
void | DeleteDeviceObjects () |
void | InvalidateDeviceObjects () |
float | GetVertexElevation (float x, float y) |
Returns the height of the vertex closest to the specified point. More... | |
void | SetVertexElevation (float x, float y, float newElevation, bool recalculate_geometry=true) |
Sets the elevation (z-coordinate) in real units of the nearest vertex to the specified point. More... | |
float | GetVertexSpacing () const |
bool | SnapPointToVertexGrid (float &x, float &y) |
snap the input point position to the nearest vertex gird point. More... | |
int | GetMaxTileCacheSize () |
set how many terrain tile(including height fields, etc) are cached in memory. More... | |
void | SetMaxTileCacheSize (int nNum) |
Get how many terrain tile(including height fields, etc) are cached in memory. More... | |
void | ResizeTextureMaskWidth (int nWidth) |
resize all texture mask width | |
void | SetGlobalConfigModified (bool bEnable=true) |
mark the global config as modified, so that when saving the world, the config file will be saved to disk. More... | |
const CShapeBox & | GetBoundingBox () const |
Get the bounding box from the last tessellation result. More... | |
void | GetBoundingBoxes (vector< ParaEngine::CShapeAABB > &boxes, const Matrix4 *modelView, const ParaEngine::CShapeFrustum &frustum, int nMaxBoxesNum=1024, int nSmallestBoxStride=4) |
Get the bounding boxes for a given view frustum. More... | |
void | SetAllLoadedModified (bool bIsModified=true, DWORD dwModifiedBits=16) |
set all loaded terrain tile content modified. More... | |
void | SaveTerrain (bool bHeightMap, bool bTextures) |
save modified terrain to disk. More... | |
bool | IsModified () |
return true, if the terrain is modified and should be saved. More... | |
void | SwitchLodStyle (bool useGeoMipmap) |
bool | IsWalkable (float x, float y, Vector3 &oNormal) |
void | SetTerrainInfo (float x, float y, float radius, uint32 data, uint32 bitMask, uint32 bitOffset) |
uint32 | GetTerrainInfo (float x, float y, uint32 bitMask, uint32 bitOffset) |
void | SetEditorMode (bool enable) |
void | SetVisibleDataMask (uint32 mask, uint8 bitOffset) |
![]() | |
virtual int | GetAttributeClassID () |
attribute class ID should be identical, unless one knows how overriding rules work. More... | |
virtual const char * | GetAttributeClassName () |
a static string, describing the attribute class object's name | |
virtual const char * | GetAttributeClassDescription () |
a static string, describing the attribute class object | |
virtual int | InstallFields (CAttributeClass *pClass, bool bOverride) |
this class should be implemented if one wants to add new attribute. More... | |
ATTRIBUTE_METHOD1 (IAttributeFields, GetName_s, const char **) | |
ATTRIBUTE_METHOD1 (IAttributeFields, SetName_s, const char *) | |
ATTRIBUTE_METHOD (IAttributeFields, PrintObject_s) | |
ATTRIBUTE_METHOD (IAttributeFields, AddRef_s) | |
ATTRIBUTE_METHOD1 (IAttributeFields, GetRefCount_s, int *) | |
ATTRIBUTE_METHOD1 (IAttributeFields, SetTime_s, int) | |
ATTRIBUTE_METHOD1 (IAttributeFields, GetTime_s, int *) | |
ATTRIBUTE_METHOD (IAttributeFields, Release_s) | |
virtual const std::string & | GetIdentifier () |
virtual void | SetIdentifier (const std::string &sID) |
virtual void | SetModified (bool bModified) |
set whether any field has been modified. More... | |
virtual bool | ValidateFields () |
validate all fields and return true if validation passed. More... | |
virtual string | GetValidationMessage () |
get the recent validation message due to the most recent call to ValidateFields() | |
virtual bool | ResetField (int nFieldID) |
Reset the field to its initial or default value. More... | |
virtual bool | InvokeEditor (int nFieldID, const std::string &sParameters) |
Invoke an (external) editor for a given field. More... | |
virtual bool | AddChildAttributeObject (IAttributeFields *pChild, int nRowIndex=-1, int nColumnIndex=0) |
add child object. More... | |
virtual void * | QueryObjectByName (const std::string &sObjectType) |
convert to object of a given type. More... | |
virtual void * | QueryObject (int nObjectType) |
convert to object of a given type. More... | |
virtual int | GetTime () |
get the current local time in case it is animated in milli seconds frames. More... | |
virtual void | SetTime (int nTime) |
virtual CDynamicAttributeField * | GetDynamicField (const std::string &sName) |
Get a dynamic field with a given name. More... | |
virtual CDynamicAttributeField * | GetDynamicField (int nIndex) |
Get a dynamic field with a given index. More... | |
virtual const char * | GetDynamicFieldNameByIndex (int nIndex) |
get field name by index | |
virtual int | GetDynamicFieldCount () |
how many dynamic field this object currently have. More... | |
virtual int | SetDynamicField (const std::string &sName, const CVariable &value) |
set a dynamic field with a given name. More... | |
virtual int | AddDynamicField (const std::string &sName, ATTRIBUTE_FIELDTYPE dwType) |
add dynamic field and return field index | |
virtual void | RemoveAllDynamicFields () |
remove all dynamic fields | |
virtual int | SaveDynamicFieldsToString (std::string &output) |
save only text dynamic fields to fieldname = value text strings. More... | |
virtual int | LoadDynamicFieldsFromString (const std::string &input) |
load only text dynamic fields from string More... | |
CAttributeClass * | GetAttributeClass () |
get the main attribute class object. More... | |
void | PrintObject () |
print the content of this object to a text file at temp/doc/[ClassName].txt. More... | |
![]() | |
virtual void | Clone (IObject *obj) const |
Clone the object's contains to a pointer. More... | |
virtual IObject * | Clone () const |
Clone the object's contains and return a pointer to the newly created object. More... | |
virtual bool | Equals (const IObject *obj) const |
Compare the object with another object. More... | |
virtual const IType * | GetType () const |
virtual std::string | ToString () const |
WeakPtr_type & | GetWeakReference () |
get weak reference object. More... | |
virtual int | ProcessObjectEvent (const ObjectEvent &event) |
this function is only used to backward compatibility of ParaObject:AddEvent() function. More... | |
![]() | |
void | addref () const |
add reference count of the object. More... | |
bool | delref () const |
decrease reference count of the object. More... | |
int | GetRefCount () const |
get the reference count | |
virtual int | Release () |
CRefCounted * | AddToAutoReleasePool () |
addref and releases the ownership sometime soon automatically (usually at the end of the current frame). More... | |
Static Public Member Functions | |
static int | GetTileIDFromXY (int X, int Y) |
get the tile ID which is the key in m_pCachedTerrains, from lattice coordinate | |
static bool | GetXYFromTileID (int nTileID, int *X, int *Y) |
get lattice coordinates, from the tile ID which is the key in m_pCachedTerrains More... | |
![]() | |
static HRESULT | GetAttributeClassID_s (IAttributeFields *cls, int *p1) |
static HRESULT | GetAttributeClassName_s (IAttributeFields *cls, const char **p1) |
static bool | OpenWithDefaultEditor (const char *sFilename, bool bWaitOnReturn=false) |
Open a given file with the default registered editor in the game engine. More... | |
Public Attributes | |
bool | m_useGeoMipmap |
Additional Inherited Members | |
![]() | |
virtual CAttributeClass * | CreateAttributeClass () |
initialize fields | |
virtual CDynamicAttributesSet * | GetDynamicAttributes (bool bCreateIfNotExist=false) |
![]() | |
WeakPtr_type | m_weak_reference |
![]() | |
int | m_refcount |
Manages a network of Terrain objects arranged in a grid pattern to model extremely large worlds that couldn't be handled by a single Terrain object.
TerrainLattice manages the seams between Terrain objects so that, even during dynamic tessellation, there are no visible cracks between neighboring Terrain objects in the lattice.
void TerrainLattice::AddHeightField | ( | float | x, |
float | y, | ||
const char * | filename, | ||
int | nSmoothPixels = 7 |
||
) |
Add rectangular height field from file to the current terrain.
This is just a shortcut for MergeHeightField()
x | center of the rect in world unit |
y | center of the rect in world unit |
filename | : the raw elevation or gray scale image file that contains the height field. |
nSmoothPixels | the number of pixels to smooth from the edge of the height field. if this is 0, the original height field will be loaded unmodified. if it is greater than 0, the loaded height field will be smoothed for nSmoothPixels from the edge, where the edge is always 0. |
void TerrainLattice::DigCircleFlat | ( | float | x, |
float | y, | ||
float | radius, | ||
float | fFlatPercentage = 0.7f , |
||
float | factor = 0.5f |
||
) |
flatten a land centered at x,y, with a specified radius.
Algorithm: (1) flatten a circle with radius same as fFlatPercentage*radius (2) smooth the entire region twice.
x | center of the circle in world unit |
y | center of the circle in world unit |
radius | radius of the inner circle in world unit |
fFlatPercentage | value is between [0,1]. fFlatPercentage*radius is the actual radius that got flattened. |
factor | value is between [0,1]. 1 means fully transformed; 0 means nothing is changed |
void ParaTerrain::TerrainLattice::Flatten | ( | float | x, |
float | y, | ||
float | radius, | ||
int | flatten_op, | ||
float | elevation, | ||
float | factor | ||
) |
Flatten the terrain both up and down to the specified elevation, using using the tightness parameter to determine how much the altered points are allowed to deviate from the specified elevation.
x | center of the circle in world unit |
y | center of the circle in world unit |
radius | radius of the inner circle in world unit |
flatten_op | enum FlattenOperation{ Fill_Op=0, //Flatten the terrain up to the specified elevation ShaveTop_Op=1, //Flatten the terrain down to the specified elevation Flatten_Op=2, //Flatten the terrain up and down to the specified elevation }; |
elevation | the desired height |
factor | value is between [0,1]. 1 means fully transformed; 0 means nothing is changed |
const CShapeBox & TerrainLattice::GetBoundingBox | ( | ) | const |
Get the bounding box from the last tessellation result.
The returned box is always in world coordinate.
void TerrainLattice::GetBoundingBoxes | ( | vector< ParaEngine::CShapeAABB > & | boxes, |
const Matrix4 * | modelView, | ||
const ParaEngine::CShapeFrustum & | frustum, | ||
int | nMaxBoxesNum = 1024 , |
||
int | nSmallestBoxStride = 4 |
||
) |
Get the bounding boxes for a given view frustum.
The returned boxes are always in world coordinate with render origin applied and transform by modelView
boxes | [in|out] |
modelView | if not null, all returned boxes will be transformed by it, before testing with the frustum |
frustum | the view frustum to test against. |
nMaxBoxesNum | maximum number of boxes returned. Since it internally uses a breadth first traversal of the terrain quad tree, larger boxes will return when there are more boxes in the frustum than nMaxBoxesNum |
nSmallestBoxStride | what is the stride size of the smallest boxes returned. |
|
virtual |
we support multi-dimensional child object.
by default objects have only one column.
Reimplemented from ParaEngine::IAttributeFields.
|
virtual |
get attribute by child object.
used to iterate across the attribute field hierarchy.
Reimplemented from ParaEngine::IAttributeFields.
|
virtual |
get the number of child objects (row count) in the given column.
please note different columns can have different row count.
Reimplemented from ParaEngine::IAttributeFields.
float TerrainLattice::GetLowestVisibleHeight | ( | ) |
Get the height of the lowest visible terrain point.
This may be used to render the ocean
int TerrainLattice::GetMaxTileCacheSize | ( | ) |
set how many terrain tile(including height fields, etc) are cached in memory.
The actual cached tile number may be smaller or larger than this value, since we ensure that a tile that is very recently used is never removed from cache. the default value is 18. The smallest value is 18, the largest value is 4000
void TerrainLattice::GetNormal | ( | float | x, |
float | y, | ||
float & | normalX, | ||
float & | normalY, | ||
float & | normalZ | ||
) |
Returns the surface normal of the terrain at the specified point.
x | The x location of the point on the Terrain's surface in world units. |
y | The y location of the point on the Terrain's surface in world units. |
normalX | Gets filled with the surface normal x component |
normalY | Gets filled with the surface normal y component |
normalZ | Gets filled with the surface normal z component |
DWORD TerrainLattice::GetRegionValue | ( | const string & | sLayerName, |
float | x, | ||
float | y | ||
) |
Terrain * TerrainLattice::GetTerrain | ( | int | positionX, |
int | positionY | ||
) |
Get the terrain at the tile location.
And load the terrain if it has been cached
float TerrainLattice::GetVertexElevation | ( | float | x, |
float | y | ||
) |
float TerrainLattice::GetVertexSpacing | ( | ) | const |
|
static |
get lattice coordinates, from the tile ID which is the key in m_pCachedTerrains
bool ParaTerrain::TerrainLattice::IsHole | ( | float | x, |
float | y | ||
) |
Whether the terrain contains a hole at the specified location.
Currently, we allow user to load a low resolution hole maps at the beginning of terrain creation.
x | The x location of the point on the Terrain's surface in world units. |
y | The y location of the point on the Terrain's surface in world units. |
|
virtual |
return true, if the terrain is modified and should be saved.
Reimplemented from ParaEngine::IAttributeFields.
void TerrainLattice::MergeHeightField | ( | float | x, |
float | y, | ||
const char * | filename, | ||
int | mergeOperation = 0 , |
||
float | weight1 = 1.0 , |
||
float | weight2 = 1.0 , |
||
int | nSmoothPixels = 7 |
||
) |
merge a rectangular height field from file to the current terrain.
x | center of the rect in world unit |
y | center of the rect in world unit |
filename | : the raw elevation or gray scale image file that contains the height field. |
MergeOperation | { Addition=0, Subtract=1, Multiplication=2, Division=3, Minimum=4, Maximum=5, }; |
weight1 | the destination merging weight |
weight2 | the source file merging weight |
nSmoothPixels | the number of pixels to smooth from the edge of the height field. if this is 0, the original height field will be loaded unmodified. if it is greater than 0, the loaded height field will be smoothed for nSmoothPixels from the edge, where the edge is always 0. |
void TerrainLattice::ModelViewMatrixChanged | ( | ) |
tessellation according to the new view matrix
Update neighbor, in case the shorelines are not uniformly tessellated.
repair cracks and rebuild render buffer
tessellation according to the new view matrix
void TerrainLattice::RadialScale | ( | float | x, |
float | y, | ||
float | scale_factor, | ||
float | min_dist, | ||
float | max_dist, | ||
float | smooth_factor = 0.5f , |
||
int | frequency = 1 |
||
) |
Note: terrain data should be in normalized space with height in the range [0,1].
Picks a point and scales the surrounding terrain in a circular manner. Can be used to make all sorts of circular shapes. Still needs some work. radial_scale: pick a point (center_x, center_y) and scale the points where distance is mindist<=distance<=maxdist linearly. The formula we'll use for a nice sloping smoothing factor is (-cos(x*3)/2)+0.5.
x | center of the circle in world unit |
y | center of the circle in world unit |
scale_factor | height of the scaled portion in world unit. This value can be negative. |
void ParaTerrain::TerrainLattice::Ramp | ( | float | x1, |
float | y1, | ||
float | x2, | ||
float | y2, | ||
float | radius, | ||
float | borderpercentage = 0.5f , |
||
float | factor = 1.0f |
||
) |
create a ramp (inclined slope) from (x1,y1) to (x2,y2).
The ramp's half width is radius. this is usually used to created a slope path connecting a high land with a low land.
radius | The ramp's half width |
borderpercentage | borderpercentage*radius is how long the ramp boarder is to linearly interpolate with the original terrain. specify 0 for sharp ramp border. |
factor | in range[0,1]. it is the smoothness to merge with other border heights.Specify 1.0 for a complete merge |
void TerrainLattice::Render | ( | void | ) |
generate terrain rendering report to console
void TerrainLattice::Roughen_Smooth | ( | float | x, |
float | y, | ||
float | radius, | ||
bool | roughen = false , |
||
bool | big_grid = false , |
||
float | factor = 0.5f |
||
) |
square filter for sharpening and smoothing.
Use neighbour-averaging to roughen or smooth the height field. The factor determines how much of the computed roughening is actually applied to the height field. In it's default invocation, the 4 directly neighboring squares are used to calculate the roughening. If you select big sampling grid, all 8 neighboring cells will be used.
x | center of the circle in world unit |
y | center of the circle in world unit |
radius | radius of the inner circle in world unit |
roughen | true for sharpening, false for smoothing. |
big_grid | true for 8 neighboring cells, false for 4. |
factor | value is between [0,1]. 1 means fully transformed; 0 means nothing is changed |
void TerrainLattice::SaveTerrain | ( | bool | bHeightMap, |
bool | bTextures | ||
) |
save modified terrain to disk.
bHeightMap | true to save height map |
bTextures | true to save textures: alpha maps, etc |
void TerrainLattice::SetAllLoadedModified | ( | bool | bIsModified = true , |
DWORD | dwModifiedBits = 16 |
||
) |
set all loaded terrain tile content modified.
void TerrainLattice::SetCameraPosition | ( | float | x, |
float | y, | ||
float | z | ||
) |
ensure that the 9 tiles near the eye position are always active.And increase their hit count
garbage collect other terrain tiles; while frame move all active tiles
use 0x3fffffff, instead of 0xffffffff prevents the current frame used terrain to be deleted.
void ParaTerrain::TerrainLattice::SetGlobalConfigModified | ( | bool | bEnable = true | ) |
mark the global config as modified, so that when saving the world, the config file will be saved to disk.
void ParaTerrain::TerrainLattice::SetHole | ( | float | x, |
float | y, | ||
bool | bIsHold | ||
) |
set a new terrain hole at the specified location.
Currently, we will allow user to dynamically dig holes in terrain. After calling this function, the user must manually Call UpdateHoles() to inform that the holes in the terrain has been updated.
x | The x location of the point on the Terrain's surface in world units. |
y | The y location of the point on the Terrain's surface in world units. |
void TerrainLattice::SetLowestVisibleHeight | ( | float | fHeight | ) |
Set the height of the lowest visible terrain point.
This may be used to render the ocean
void TerrainLattice::SetMaxTileCacheSize | ( | int | nNum | ) |
Get how many terrain tile(including height fields, etc) are cached in memory.
The actual cached tile number may be smaller or larger than this value, since we ensure that a tile that is very recently used is never removed from cache. the default value is 18. The smallest value is 18, the largest value is 4000
void TerrainLattice::SetVertexElevation | ( | float | x, |
float | y, | ||
float | newElevation, | ||
bool | recalculate_geometry = true |
||
) |
Sets the elevation (z-coordinate) in real units of the nearest vertex to the specified point.
bool TerrainLattice::SnapPointToVertexGrid | ( | float & | x, |
float & | y | ||
) |
snap the input point position to the nearest vertex gird point.
x | [in|out] |
y | [in|out] |
void ParaTerrain::TerrainLattice::Spherical | ( | float | x, |
float | y, | ||
float | radius, | ||
float | offset | ||
) |
offset in a spherical region
x | center of the circle in world unit |
y | center of the circle in world unit |
radius | radius of the inner circle in world unit |
void ParaTerrain::TerrainLattice::UpdateHoles | ( | float | x, |
float | y | ||
) |