mercator  0.4.0
A terrain generation library for the Worldforge system.
Mercator::Terrain Class Reference

Class storing centrally all data about an instance of some terrain. More...

#include <Terrain.h>

Public Types

typedef WFMath::AxisBox< 2 > Rect
 Bounding box. More...
 
typedef std::map< int, BasePointPointcolumn
 STL map to store sparse array of BasePoints. More...
 
typedef std::map< int, std::unique_ptr< Segment > > Segmentcolumn
 STL map to store sparse array of Segments. More...
 
typedef std::map< int, PointcolumnPointstore
 STL map to store sparse array of BasePoint columns. More...
 
typedef std::map< int, SegmentcolumnSegmentstore
 STL map to store sparse array of Segment pointer columns. More...
 
typedef std::map< int, const Shader * > Shaderstore
 STL map to store sparse array of Shader pointers. More...
 

Public Member Functions

 Terrain (unsigned int options=DEFAULT, int resolution=defaultResolution)
 Construct a new Terrain object with optional options and resolution. More...
 
 ~Terrain ()
 Destroy Terrain object, deleting contained objects. More...
 
float get (float x, float z) const
 Get the height value at a given coordinate x,z. More...
 
bool getHeight (float x, float z, float &h) const
 Get an accurate height at a given coordinate x,z. More...
 
bool getHeightAndNormal (float x, float z, float &h, WFMath::Vector< 3 > &n) const
 Get an accurate height and normal vector at a given coordinate x,z. More...
 
bool getBasePoint (int x, int z, BasePoint &y) const
 Get the BasePoint at a given base point coordinate. More...
 
void setBasePoint (int x, int z, const BasePoint &y)
 Set the BasePoint value at a given base point coordinate. More...
 
void setBasePoint (int x, int y, float z)
 Set the height of the basepoint at x,y to z. More...
 
SegmentgetSegmentAtPos (float x, float z) const
 Get a pointer to the segment which contains the coord x,y. More...
 
SegmentgetSegmentAtIndex (int x, int z) const
 Get the Segment at a given index. More...
 
int getResolution () const
 Accessor for base point resolution. More...
 
float getSpacing () const
 Accessor for base point spacing. More...
 
const SegmentstoregetTerrain () const
 Accessor for 2D sparse array of Segment pointers. More...
 
const PointstoregetPoints () const
 Accessor for 2D sparse array of BasePoint objects. More...
 
const ShaderstoregetShaders () const
 Accessor for list of Shader pointers. More...
 
void addShader (const Shader *t, int id)
 Add a new Shader to the list for this terrain. More...
 
void removeShader (const Shader *t, int id)
 remove a Shader from the list for this terrain. More...
 
Rect updateMod (long id, std::unique_ptr< TerrainMod > mod)
 Updates the terrain with a mod. More...
 
bool hasMod (long id) const
 Checks if a mod with the supplied id has been registered with the terrain. More...
 
const TerrainModgetMod (long id) const
 
Rect updateArea (long id, std::unique_ptr< Area > a)
 Updates the terrain affected by an area. More...
 
const AreagetArea (long id) const
 
int posToIndex (float pos) const
 Converts the supplied position into a segment index. More...
 
void processSegments (const WFMath::AxisBox< 2 > &area, const std::function< void(Segment &, int, int)> &func) const
 

Static Public Attributes

static const unsigned int DEFAULT = 0x0000
 value provided for no flags set. More...
 
static const unsigned int SHADED = 0x0001
 set if shaders are going to be used on this terrain. More...
 
static constexpr float defaultLevel = 8.f
 Height value used when no data is available. More...
 

Detailed Description

Class storing centrally all data about an instance of some terrain.

All the data includes:

  • Base Points which define the terrain.
  • Segment data which is the actual heightfields.
  • Shaders which control how the surface appears.
  • Surfaces which are generated by the Shaders (delegated to Segment).
  • TerrainMods which modify the terrain data (delegated to Segment).

Definition at line 37 of file Terrain.h.

Member Typedef Documentation

◆ Pointcolumn

typedef std::map<int, BasePoint> Mercator::Terrain::Pointcolumn

STL map to store sparse array of BasePoints.

Definition at line 43 of file Terrain.h.

◆ Pointstore

typedef std::map<int, Pointcolumn > Mercator::Terrain::Pointstore

STL map to store sparse array of BasePoint columns.

Definition at line 48 of file Terrain.h.

◆ Rect

typedef WFMath::AxisBox<2> Mercator::Terrain::Rect

Bounding box.

Definition at line 40 of file Terrain.h.

◆ Segmentcolumn

typedef std::map<int, std::unique_ptr<Segment> > Mercator::Terrain::Segmentcolumn

STL map to store sparse array of Segments.

Definition at line 45 of file Terrain.h.

◆ Segmentstore

STL map to store sparse array of Segment pointer columns.

Definition at line 50 of file Terrain.h.

◆ Shaderstore

typedef std::map<int, const Shader *> Mercator::Terrain::Shaderstore

STL map to store sparse array of Shader pointers.

Definition at line 53 of file Terrain.h.

Constructor & Destructor Documentation

◆ Terrain()

Mercator::Terrain::Terrain ( unsigned int  options = DEFAULT,
int  resolution = defaultResolution 
)
explicit

Construct a new Terrain object with optional options and resolution.

Parameters
optionsa bitfield of option flags. Defaults to no flags set.
  • DEFAULT value provided for no flags set.
  • SHADED is set if shaders are going to be used on this terrain.
resolutionthe spacing between adjacent base points. Defaults to 64.

Definition at line 32 of file Terrain.cpp.

◆ ~Terrain()

Mercator::Terrain::~Terrain ( )
default

Destroy Terrain object, deleting contained objects.

All Segment objects are deleted, but Shader objects are not yet deleted. Probably need to be fixed.

Member Function Documentation

◆ addShader()

void Mercator::Terrain::addShader ( const Shader t,
int  id 
)

Add a new Shader to the list for this terrain.

As each shader is added, surfaces are added to all existing segments to store the result of the shader.

Definition at line 40 of file Terrain.cpp.

References Mercator::Shader::newSurface().

◆ get()

float Mercator::Terrain::get ( float  x,
float  z 
) const

Get the height value at a given coordinate x,z.

This is a convenience function provided to quickly get a height value at a given point. It always succeeds, as if no height data is available it just returns the default value. If a Segment does exist in the right place and is populated, the coords within that Segment are determined, and the heightfield queried. This function does not cause any data to be populated, and does not attempt to do any interpolation to get an accurate height figure. For more accurate results see Terrain::getHeightAndNormal.

Definition at line 114 of file Terrain.cpp.

References defaultLevel, Mercator::Segment::get(), getSegmentAtIndex(), Mercator::Segment::getXRef(), Mercator::Segment::getZRef(), Mercator::Segment::isValid(), and posToIndex().

◆ getBasePoint()

bool Mercator::Terrain::getBasePoint ( int  x,
int  z,
BasePoint y 
) const

Get the BasePoint at a given base point coordinate.

Get the BasePoint value for the given coordinate on the base point grid.

Parameters
xcoordinate on the base point grid.
zcoordinate on the base point grid.
yreference to variable which will be used to store the BasePoint data.
Returns
true if a BasePoint is defined at the given coordinate, false otherwise.

Definition at line 144 of file Terrain.cpp.

Referenced by setBasePoint().

◆ getHeight()

bool Mercator::Terrain::getHeight ( float  x,
float  z,
float &  h 
) const

Get an accurate height at a given coordinate x,z.

This is a more expensive function that Terrain::get() for getting an accurate height value. The main body of functionality is in the Segment::getHeight() function called from here. The height is interpolated based on a model where each tile of the heightfield comprises two triangles. If no heightfield data is available for the given location, this function returns false, and no data is returned.

Parameters
xcoordinate of point to be returned.
zcoordinate of point to be returned.
hreference to variable which will be used to store the resulting height value.
Returns
true if heightdata was available, false otherwise.

Definition at line 123 of file Terrain.cpp.

References getSegmentAtIndex(), Mercator::Segment::getXRef(), Mercator::Segment::getZRef(), Mercator::Segment::isValid(), and posToIndex().

◆ getHeightAndNormal()

bool Mercator::Terrain::getHeightAndNormal ( float  x,
float  z,
float &  h,
WFMath::Vector< 3 > &  n 
) const

Get an accurate height and normal vector at a given coordinate x,z.

This is a more expensive function that Terrain::get() for getting an accurate height value and surface normal at a given point. The main body of functionality is in the Segment::getHeightAndNormal() function called from here. The height and normal are interpolated based on a model where each tile of the heightfield comprises two triangles. If no heightfield data is available for the given location, this function returns false, and no data is returned.

Parameters
xcoordinate of point to be returned.
zcoordinate of point to be returned.
hreference to variable which will be used to store the resulting height value.
nreference to variable which will be used to store the resulting normal value.
Returns
true if heightdata was available, false otherwise.

Definition at line 133 of file Terrain.cpp.

References Mercator::Segment::getHeightAndNormal(), getSegmentAtIndex(), Mercator::Segment::getXRef(), Mercator::Segment::getZRef(), Mercator::Segment::isValid(), and posToIndex().

◆ getPoints()

const Pointstore& Mercator::Terrain::getPoints ( ) const
inline

Accessor for 2D sparse array of BasePoint objects.

Definition at line 271 of file Terrain.h.

◆ getResolution()

int Mercator::Terrain::getResolution ( ) const
inline

Accessor for base point resolution.

Definition at line 256 of file Terrain.h.

◆ getSegmentAtIndex()

Segment * Mercator::Terrain::getSegmentAtIndex ( int  x,
int  z 
) const

Get the Segment at a given index.

Get the Segment pointer for the given coordinate on the base point grid. The Segment in question may not have been populated with heightfield or surface data.

Parameters
xcoordinate on the base point grid.
zcoordinate on the base point grid.
Returns
a valid pointer if a Segment is defined at the given coordinate, zero otherwise.

Definition at line 208 of file Terrain.cpp.

Referenced by get(), getHeight(), getHeightAndNormal(), getSegmentAtPos(), processSegments(), setBasePoint(), updateArea(), and updateMod().

◆ getSegmentAtPos()

Segment * Mercator::Terrain::getSegmentAtPos ( float  x,
float  z 
) const
inline

Get a pointer to the segment which contains the coord x,y.

Returns
Null if no segment is defined at that location, or a pointer to a Segment otherwise.

Definition at line 334 of file Terrain.h.

References getSegmentAtIndex(), and posToIndex().

◆ getShaders()

const Shaderstore& Mercator::Terrain::getShaders ( ) const
inline

Accessor for list of Shader pointers.

Definition at line 276 of file Terrain.h.

◆ getSpacing()

float Mercator::Terrain::getSpacing ( ) const
inline

Accessor for base point spacing.

Definition at line 261 of file Terrain.h.

◆ getTerrain()

const Segmentstore& Mercator::Terrain::getTerrain ( ) const
inline

Accessor for 2D sparse array of Segment pointers.

Definition at line 266 of file Terrain.h.

◆ hasMod()

bool Mercator::Terrain::hasMod ( long  id) const

Checks if a mod with the supplied id has been registered with the terrain.

Parameters
idThe id of the mod to check for.
Returns
True if the mod is added to the terrain.

Definition at line 326 of file Terrain.cpp.

◆ posToIndex()

int Mercator::Terrain::posToIndex ( float  pos) const
inline

Converts the supplied position into a segment index.

Parameters
posA position, either x or y.
Returns
The index

Definition at line 330 of file Terrain.h.

Referenced by get(), getHeight(), getHeightAndNormal(), and getSegmentAtPos().

◆ processSegments()

void Mercator::Terrain::processSegments ( const WFMath::AxisBox< 2 > &  area,
const std::function< void(Segment &, int, int)> &  func 
) const

Processes all segments within the supplied area.

Parameters
areaAn area.
funcFunction called for each segment. X and Y index are submitted as second and third arguments.

Definition at line 221 of file Terrain.cpp.

References getSegmentAtIndex().

◆ removeShader()

void Mercator::Terrain::removeShader ( const Shader t,
int  id 
)

remove a Shader from the list for this terrain.

As each shader is removed, surfaces are removed from existing segments

Definition at line 62 of file Terrain.cpp.

◆ setBasePoint() [1/2]

void Mercator::Terrain::setBasePoint ( int  x,
int  y,
float  z 
)
inline

Set the height of the basepoint at x,y to z.

Definition at line 233 of file Terrain.h.

References setBasePoint().

◆ setBasePoint() [2/2]

void Mercator::Terrain::setBasePoint ( int  x,
int  z,
const BasePoint y 
)

Set the BasePoint value at a given base point coordinate.

Set the BasePoint value for the given coordinate on the base point grid. If inserting this BasePoint completes the set of points required to define one or more Segment objects which were not yet defined, new Segment objects are created. If this replaces a point for one or more Segment objects that were already defined, the contents of those Segment objects are invalidated.

Parameters
xcoordinate on the base point grid.
zcoordinate on the base point grid.
yBasePoint value to be used at the given coordinate.

Definition at line 158 of file Terrain.cpp.

References getBasePoint(), getSegmentAtIndex(), and Mercator::Segment::setCornerPoint().

Referenced by setBasePoint().

◆ updateArea()

Terrain::Rect Mercator::Terrain::updateArea ( long  id,
std::unique_ptr< Area a 
)

Updates the terrain affected by an area.

Parameters
aThe terrain area which has changed, or null if the entry should be removed.
Returns
The area affected by the terrain area before it was updated.

Definition at line 350 of file Terrain.cpp.

References getSegmentAtIndex().

◆ updateMod()

Terrain::Rect Mercator::Terrain::updateMod ( long  id,
std::unique_ptr< TerrainMod mod 
)

Updates the terrain with a mod.

Parameters
idThe id of the mod, which is also used for ordering.
modThe terrain mod, or null if the entry for the id should be removed.
Returns
The area affected by the terrain mod before it was updated.

Definition at line 241 of file Terrain.cpp.

References getSegmentAtIndex().

Member Data Documentation

◆ DEFAULT

const unsigned int Mercator::Terrain::DEFAULT = 0x0000
static

value provided for no flags set.

Definition at line 56 of file Terrain.h.

◆ defaultLevel

constexpr float Mercator::Terrain::defaultLevel = 8.f
staticconstexpr

Height value used when no data is available.

Definition at line 140 of file Terrain.h.

Referenced by get().

◆ SHADED

const unsigned int Mercator::Terrain::SHADED = 0x0001
static

set if shaders are going to be used on this terrain.

Definition at line 58 of file Terrain.h.


The documentation for this class was generated from the following files: