mercator  0.4.0
A terrain generation library for the Worldforge system.
Terrain.h
1 // This file may be redistributed and modified only under the terms of
2 // the GNU General Public License (See COPYING for details).
3 // Copyright (C) 2003 Alistair Riddoch, Damien McGinnes
4 
5 #ifndef MERCATOR_TERRAIN_H
6 #define MERCATOR_TERRAIN_H
7 
8 #include "Mercator.h"
9 #include "BasePoint.h"
10 
11 #include <wfmath/axisbox.h>
12 #include <wfmath/point.h>
13 
14 #include <map>
15 #include <set>
16 #include <list>
17 #include <cmath>
18 #include <tuple>
19 #include <functional>
20 
21 namespace Mercator {
22 
23 class Segment;
24 class Shader;
25 class TerrainMod;
26 class Area;
27 class Effector;
28 
37 class Terrain {
38  public:
40  typedef WFMath::AxisBox<2> Rect;
41 
43  typedef std::map<int, BasePoint> Pointcolumn;
45  typedef std::map<int, std::unique_ptr<Segment>> Segmentcolumn;
46 
48  typedef std::map<int, Pointcolumn > Pointstore;
50  typedef std::map<int, Segmentcolumn > Segmentstore;
51 
53  typedef std::map<int, const Shader *> Shaderstore;
54 
56  static const unsigned int DEFAULT = 0x0000;
58  static const unsigned int SHADED = 0x0001;
59  // More options go here as bit flags, and below should be a private
60  // test function
61  private:
63  const unsigned int m_options;
65  const int m_res;
67  const float m_spacing;
68 
70  Pointstore m_basePoints;
72  Segmentstore m_segments;
74  Shaderstore m_shaders;
75 
76  struct TerrainModEntry {
80  std::unique_ptr<TerrainMod> terrainMod;
81 
85  Rect rect;
86  };
87 
95  std::map<long, TerrainModEntry> m_terrainMods;
96 
97  struct TerrainAreaEntry {
101  std::unique_ptr<Area> terrainArea;
102 
106  Rect rect;
107  };
108 
112  std::map<long, TerrainAreaEntry> m_terrainAreas;
113 
123  void addSurfaces(Segment &);
124 
130  void shadeSurfaces(Segment &);
131 
135  bool isShaded() const {
136  return ((m_options & SHADED) == SHADED);
137  }
138  public:
140  static constexpr float defaultLevel = 8.f;
141 
148  explicit Terrain(unsigned int options = DEFAULT,
149  int resolution = defaultResolution);
150 
155  ~Terrain();
156 
167  float get(float x, float z) const;
168 
184  bool getHeight(float x, float z, float& h) const;
185 
204  bool getHeightAndNormal(float x, float z, float& h, WFMath::Vector<3>& n) const;
205 
216  bool getBasePoint(int x, int z, BasePoint& y) const;
217 
230  void setBasePoint(int x, int z, const BasePoint& y);
231 
233  void setBasePoint(int x, int y, float z) {
234  BasePoint bp(z);
235  setBasePoint(x, y, bp);
236  }
237 
242  Segment * getSegmentAtPos(float x, float z) const;
243 
253  Segment * getSegmentAtIndex(int x, int z) const;
254 
256  int getResolution() const {
257  return m_res;
258  }
259 
261  float getSpacing() const {
262  return m_spacing;
263  }
264 
266  const Segmentstore & getTerrain() const {
267  return m_segments;
268  }
269 
271  const Pointstore & getPoints() const {
272  return m_basePoints;
273  }
274 
276  const Shaderstore & getShaders() const {
277  return m_shaders;
278  }
279 
284  void addShader(const Shader * t, int id);
285 
289  void removeShader(const Shader * t, int id);
290 
296  Rect updateMod(long id, std::unique_ptr<TerrainMod> mod);
297 
302  bool hasMod(long id) const;
303 
304  const TerrainMod* getMod(long id) const;
305 
310  Rect updateArea(long id, std::unique_ptr<Area> a);
311 
312 
313  const Area* getArea(long id) const;
314 
320  int posToIndex(float pos) const;
321 
327  void processSegments(const WFMath::AxisBox<2>& area, const std::function<void(Segment&, int, int)>& func) const;
328 };
329 
330 inline int Terrain::posToIndex(float pos) const {
331  return (int)std::lround(std::floor(pos / m_spacing));
332 }
333 
334 inline Segment * Terrain::getSegmentAtPos(float x, float z) const
335 {
336  return getSegmentAtIndex(posToIndex(x), posToIndex(z));
337 }
338 
339 
340 } // namespace Mercator
341 
342 #endif // MERCATOR_TERRAIN_H
Mercator::Terrain
Class storing centrally all data about an instance of some terrain.
Definition: Terrain.h:37
Mercator::Terrain::DEFAULT
static const unsigned int DEFAULT
value provided for no flags set.
Definition: Terrain.h:56
Mercator::Terrain::defaultLevel
static constexpr float defaultLevel
Height value used when no data is available.
Definition: Terrain.h:140
Mercator::Terrain::getSegmentAtIndex
Segment * getSegmentAtIndex(int x, int z) const
Get the Segment at a given index.
Definition: Terrain.cpp:208
Mercator::Terrain::getTerrain
const Segmentstore & getTerrain() const
Accessor for 2D sparse array of Segment pointers.
Definition: Terrain.h:266
Mercator::Terrain::getHeightAndNormal
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.
Definition: Terrain.cpp:133
Mercator::Terrain::hasMod
bool hasMod(long id) const
Checks if a mod with the supplied id has been registered with the terrain.
Definition: Terrain.cpp:326
Mercator::Terrain::Pointstore
std::map< int, Pointcolumn > Pointstore
STL map to store sparse array of BasePoint columns.
Definition: Terrain.h:48
Mercator::Segment
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:37
Mercator::Terrain::updateArea
Rect updateArea(long id, std::unique_ptr< Area > a)
Updates the terrain affected by an area.
Definition: Terrain.cpp:350
Mercator::Terrain::Segmentstore
std::map< int, Segmentcolumn > Segmentstore
STL map to store sparse array of Segment pointer columns.
Definition: Terrain.h:50
Mercator::Terrain::getPoints
const Pointstore & getPoints() const
Accessor for 2D sparse array of BasePoint objects.
Definition: Terrain.h:271
Mercator::Terrain::Segmentcolumn
std::map< int, std::unique_ptr< Segment > > Segmentcolumn
STL map to store sparse array of Segments.
Definition: Terrain.h:45
Mercator::Terrain::setBasePoint
void setBasePoint(int x, int y, float z)
Set the height of the basepoint at x,y to z.
Definition: Terrain.h:233
Mercator::Terrain::updateMod
Rect updateMod(long id, std::unique_ptr< TerrainMod > mod)
Updates the terrain with a mod.
Definition: Terrain.cpp:241
Mercator::Terrain::Pointcolumn
std::map< int, BasePoint > Pointcolumn
STL map to store sparse array of BasePoints.
Definition: Terrain.h:43
Mercator::Terrain::getHeight
bool getHeight(float x, float z, float &h) const
Get an accurate height at a given coordinate x,z.
Definition: Terrain.cpp:123
Mercator::Terrain::getBasePoint
bool getBasePoint(int x, int z, BasePoint &y) const
Get the BasePoint at a given base point coordinate.
Definition: Terrain.cpp:144
Mercator::Terrain::setBasePoint
void setBasePoint(int x, int z, const BasePoint &y)
Set the BasePoint value at a given base point coordinate.
Definition: Terrain.cpp:158
Mercator::Area
Region of terrain surface which is modified.
Definition: Area.h:28
Mercator::Terrain::getSpacing
float getSpacing() const
Accessor for base point spacing.
Definition: Terrain.h:261
Mercator::BasePoint
Point on the fundamental grid that is used as the basis for terrain.
Definition: BasePoint.h:19
Mercator::Terrain::Shaderstore
std::map< int, const Shader * > Shaderstore
STL map to store sparse array of Shader pointers.
Definition: Terrain.h:53
Mercator::Terrain::getShaders
const Shaderstore & getShaders() const
Accessor for list of Shader pointers.
Definition: Terrain.h:276
Mercator::Terrain::processSegments
void processSegments(const WFMath::AxisBox< 2 > &area, const std::function< void(Segment &, int, int)> &func) const
Definition: Terrain.cpp:221
Mercator::Shader
Base class for Shader objects which create surface data for use when rendering terrain.
Definition: Shader.h:25
Mercator::Terrain::removeShader
void removeShader(const Shader *t, int id)
remove a Shader from the list for this terrain.
Definition: Terrain.cpp:62
Mercator::Terrain::Terrain
Terrain(unsigned int options=DEFAULT, int resolution=defaultResolution)
Construct a new Terrain object with optional options and resolution.
Definition: Terrain.cpp:32
Mercator::Terrain::getSegmentAtPos
Segment * getSegmentAtPos(float x, float z) const
Get a pointer to the segment which contains the coord x,y.
Definition: Terrain.h:334
Mercator::Terrain::get
float get(float x, float z) const
Get the height value at a given coordinate x,z.
Definition: Terrain.cpp:114
Mercator::Terrain::~Terrain
~Terrain()
Destroy Terrain object, deleting contained objects.
Mercator::TerrainMod
Base class for modifiers to the procedurally generated terrain.
Definition: TerrainMod.h:20
Mercator::Terrain::posToIndex
int posToIndex(float pos) const
Converts the supplied position into a segment index.
Definition: Terrain.h:330
Mercator::Terrain::Rect
WFMath::AxisBox< 2 > Rect
Bounding box.
Definition: Terrain.h:40
Mercator::Terrain::SHADED
static const unsigned int SHADED
set if shaders are going to be used on this terrain.
Definition: Terrain.h:58
Mercator::Terrain::addShader
void addShader(const Shader *t, int id)
Add a new Shader to the list for this terrain.
Definition: Terrain.cpp:40
Mercator::Terrain::getResolution
int getResolution() const
Accessor for base point resolution.
Definition: Terrain.h:256