Mercator
0.4.0
|
Class storing centrally all data about an instance of some terrain. More...
#include <Terrain.h>
Public Types | |
typedef WFMath::AxisBox< 2 > | Rect |
Bounding box. | |
typedef std::map< int, BasePoint > | Pointcolumn |
STL map to store sparse array of BasePoints. | |
typedef std::map< int, Segment * > | Segmentcolumn |
STL map to store sparse array of Segment pointers. | |
typedef std::map< int, Pointcolumn > | Pointstore |
STL map to store sparse array of BasePoint columns. | |
typedef std::map< int, Segmentcolumn > | Segmentstore |
STL map to store sparse array of Segment pointer columns. | |
typedef std::map< int, const Shader * > | Shaderstore |
STL map to store sparse array of Shader pointers. | |
Public Member Functions | |
Terrain (unsigned int options=DEFAULT, unsigned 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 &) const |
bool | getHeightAndNormal (float x, float z, float &, WFMath::Vector< 3 > &) 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. | |
Segment * | getSegmentAtPos (float x, float z) const |
Get a pointer to the segment which contains the coord x,y. More... | |
Segment * | getSegmentAtIndex (int x, int z) const |
Get a pointer to the segment at index x,y. More... | |
int | getResolution () const |
Accessor for base point resolution. | |
float | getSpacing () const |
Accessor for base point spacing. | |
const Segmentstore & | getTerrain () const |
Accessor for 2D sparse array of Segment pointers. | |
const Pointstore & | getPoints () const |
Accessor for 2D sparse array of BasePoint objects. | |
const Shaderstore & | getShaders () const |
Accessor for list of Shader pointers. | |
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, const 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 TerrainMod * | getMod (long id) const |
void | addArea (const Area *a) |
Add an area modifier to the terrain. More... | |
Rect | updateArea (const Area *a) |
Updates the terrain affected by an area. More... | |
void | removeArea (const Area *a) |
Remove an area modifier from the terrain. More... | |
bool | hasArea (const Area *a) const |
Checks if the supplied area has been registered with the terrain. More... | |
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. | |
static const unsigned int | SHADED = 0x0001 |
set if shaders are going to be used on this terrain. | |
static constexpr float | defaultLevel = 8.f |
Height value used when no data is available. | |
Class storing centrally all data about an instance of some terrain.
All the data includes:
|
explicit |
Construct a new Terrain object with optional options and resolution.
options | a bitfield of option flags. Defaults to no flags set.
|
resolution | the spacing between adjacent base points. Defaults to 64. |
Mercator::Terrain::~Terrain | ( | ) |
void Mercator::Terrain::addArea | ( | const Area * | area | ) |
Add an area modifier to the terrain.
Add a new Area object to the terrain, which defines a modification to the surface.
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.
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.
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.
x | coordinate on the base point grid. |
z | coordinate on the base point grid. |
y | reference to variable which will be used to store the BasePoint data. |
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.
x | coordinate of point to be returned. |
z | coordinate of point to be returned. |
h | reference to variable which will be used to store the resulting height value. |
n | reference to variable which will be used to store the resulting normal value. |
Segment * Mercator::Terrain::getSegmentAtIndex | ( | int | x, |
int | z | ||
) | const |
Get a pointer to the segment at index x,y.
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.
x | coordinate on the base point grid. |
z | coordinate on the base point grid. |
|
inline |
Get a pointer to the segment which contains the coord x,y.
bool Mercator::Terrain::hasArea | ( | const Area * | a | ) | const |
Checks if the supplied area has been registered with the terrain.
a | The area to check for. |
bool Mercator::Terrain::hasMod | ( | long | id | ) | const |
Checks if a mod with the supplied id has been registered with the terrain.
id | The id of the mod to check for. |
|
inline |
Converts the supplied position into a segment index.
pos | A position, either x or y. |
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.
area | An area. |
func | Function called for each segment. X and Y index are submitted as second and third arguments. |
void Mercator::Terrain::removeArea | ( | const Area * | area | ) |
Remove an area modifier from the terrain.
Remove an existing Area object from the terrain, and mark all the affected terrain surfaces as invalid.
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
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.
x | coordinate on the base point grid. |
z | coordinate on the base point grid. |
y | BasePoint value to be used at the given coordinate. |
Terrain::Rect Mercator::Terrain::updateArea | ( | const Area * | a | ) |
Updates the terrain affected by an area.
Apply changes to an area modifier to the terrain.
Call this when an already added terrain area has changed.
a | The terrain area which has changed. |
Terrain::Rect Mercator::Terrain::updateMod | ( | long | id, |
const TerrainMod * | mod | ||
) |
Updates the terrain with a mod.
id | The id of the mod, which is also used for ordering. |
mod | The terrain mod, or null if the entry for the id should be removed. |