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 <unordered_map>
19 #include <tuple>
20 #include <functional>
21 
22 namespace Mercator {
23 
24 class Segment;
25 class Shader;
26 class TerrainMod;
27 class Area;
28 class Effector;
29 
38 class Terrain {
39  public:
41  typedef WFMath::AxisBox<2> Rect;
42 
44  typedef std::map<int, BasePoint> Pointcolumn;
46  typedef std::map<int, std::unique_ptr<Segment>> Segmentcolumn;
47 
49  typedef std::map<int, Pointcolumn > Pointstore;
51  typedef std::map<int, Segmentcolumn > Segmentstore;
52 
54  typedef std::map<int, const Shader *> Shaderstore;
55 
57  static const unsigned int DEFAULT = 0x0000;
59  static const unsigned int SHADED = 0x0001;
60  // More options go here as bit flags, and below should be a private
61  // test function
62  private:
64  const unsigned int m_options;
66  const int m_res;
68  const float m_spacing;
69 
71  Pointstore m_basePoints;
73  Segmentstore m_segments;
75  Shaderstore m_shaders;
76 
77  struct TerrainModEntry {
81  std::unique_ptr<TerrainMod> terrainMod;
82 
86  Rect rect;
87  };
88 
96  std::map<long, TerrainModEntry> m_terrainMods;
97 
98  struct TerrainAreaEntry {
102  std::unique_ptr<Area> terrainArea;
103 
107  Rect rect;
108  };
109 
113  std::map<long, TerrainAreaEntry> m_terrainAreas;
114 
124  void addSurfaces(Segment &);
125 
131  void shadeSurfaces(Segment &);
132 
136  bool isShaded() const {
137  return ((m_options & SHADED) == SHADED);
138  }
139  public:
141  static constexpr float defaultLevel = 8.f;
142 
149  explicit Terrain(unsigned int options = DEFAULT,
150  int resolution = defaultResolution);
151 
156  ~Terrain();
157 
168  float get(float x, float z) const;
169 
185  bool getHeight(float x, float z, float& h) const;
186 
205  bool getHeightAndNormal(float x, float z, float& h, WFMath::Vector<3>& n) const;
206 
217  bool getBasePoint(int x, int z, BasePoint& y) const;
218 
231  void setBasePoint(int x, int z, const BasePoint& y);
232 
234  void setBasePoint(int x, int y, float z) {
235  BasePoint bp(z);
236  setBasePoint(x, y, bp);
237  }
238 
243  Segment * getSegmentAtPos(float x, float z) const;
244 
254  Segment * getSegmentAtIndex(int x, int z) const;
255 
257  int getResolution() const {
258  return m_res;
259  }
260 
262  float getSpacing() const {
263  return m_spacing;
264  }
265 
267  const Segmentstore & getTerrain() const {
268  return m_segments;
269  }
270 
272  const Pointstore & getPoints() const {
273  return m_basePoints;
274  }
275 
277  const Shaderstore & getShaders() const {
278  return m_shaders;
279  }
280 
285  void addShader(const Shader * t, int id);
286 
290  void removeShader(const Shader * t, int id);
291 
297  Rect updateMod(long id, std::unique_ptr<TerrainMod> mod);
298 
303  bool hasMod(long id) const;
304 
305  const TerrainMod* getMod(long id) const;
306 
311  Rect updateArea(long id, std::unique_ptr<Area> a);
312 
313 
314  const Area* getArea(long id) const;
315 
321  int posToIndex(float pos) const;
322 
328  void processSegments(const WFMath::AxisBox<2>& area, const std::function<void(Segment&, int, int)>& func) const;
329 };
330 
331 inline int Terrain::posToIndex(float pos) const {
332  return (int)std::lround(std::floor(pos / m_spacing));
333 }
334 
335 inline Segment * Terrain::getSegmentAtPos(float x, float z) const
336 {
337  return getSegmentAtIndex(posToIndex(x), posToIndex(z));
338 }
339 
340 
341 } // namespace Mercator
342 
343 #endif // MERCATOR_TERRAIN_H
void removeShader(const Shader *t, int id)
remove a Shader from the list for this terrain.
Definition: Terrain.cpp:62
std::map< int, Pointcolumn > Pointstore
STL map to store sparse array of BasePoint columns.
Definition: Terrain.h:49
bool hasMod(long id) const
Checks if a mod with the supplied id has been registered with the terrain.
Definition: Terrain.cpp:326
void setBasePoint(int x, int z, const BasePoint &y)
Set the BasePoint value at a given base point coordinate.
Definition: Terrain.cpp:158
static const unsigned int DEFAULT
value provided for no flags set.
Definition: Terrain.h:57
const Shaderstore & getShaders() const
Accessor for list of Shader pointers.
Definition: Terrain.h:277
~Terrain()
Destroy Terrain object, deleting contained objects.
void setBasePoint(int x, int y, float z)
Set the height of the basepoint at x,y to z.
Definition: Terrain.h:234
Terrain(unsigned int options=DEFAULT, int resolution=defaultResolution)
Construct a new Terrain object with optional options and resolution.
Definition: Terrain.cpp:32
bool getBasePoint(int x, int z, BasePoint &y) const
Get the BasePoint at a given base point coordinate.
Definition: Terrain.cpp:144
WFMath::AxisBox< 2 > Rect
Bounding box.
Definition: Terrain.h:41
static constexpr float defaultLevel
Height value used when no data is available.
Definition: Terrain.h:141
const Segmentstore & getTerrain() const
Accessor for 2D sparse array of Segment pointers.
Definition: Terrain.h:267
void addShader(const Shader *t, int id)
Add a new Shader to the list for this terrain.
Definition: Terrain.cpp:40
std::map< int, const Shader * > Shaderstore
STL map to store sparse array of Shader pointers.
Definition: Terrain.h:54
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:37
Base class for Shader objects which create surface data for use when rendering terrain.
Definition: Shader.h:25
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
int getResolution() const
Accessor for base point resolution.
Definition: Terrain.h:257
Point on the fundamental grid that is used as the basis for terrain.
Definition: BasePoint.h:19
bool getHeight(float x, float z, float &h) const
Get an accurate height at a given coordinate x,z.
Definition: Terrain.cpp:123
int posToIndex(float pos) const
Converts the supplied position into a segment index.
Definition: Terrain.h:331
Segment * getSegmentAtIndex(int x, int z) const
Get the Segment at a given index.
Definition: Terrain.cpp:208
const Pointstore & getPoints() const
Accessor for 2D sparse array of BasePoint objects.
Definition: Terrain.h:272
std::map< int, Segmentcolumn > Segmentstore
STL map to store sparse array of Segment pointer columns.
Definition: Terrain.h:51
Class storing centrally all data about an instance of some terrain.
Definition: Terrain.h:38
Segment * getSegmentAtPos(float x, float z) const
Get a pointer to the segment which contains the coord x,y.
Definition: Terrain.h:335
std::map< int, BasePoint > Pointcolumn
STL map to store sparse array of BasePoints.
Definition: Terrain.h:44
void processSegments(const WFMath::AxisBox< 2 > &area, const std::function< void(Segment &, int, int)> &func) const
Definition: Terrain.cpp:221
Rect updateMod(long id, std::unique_ptr< TerrainMod > mod)
Updates the terrain with a mod.
Definition: Terrain.cpp:241
static const unsigned int SHADED
set if shaders are going to be used on this terrain.
Definition: Terrain.h:59
std::map< int, std::unique_ptr< Segment > > Segmentcolumn
STL map to store sparse array of Segments.
Definition: Terrain.h:46
float getSpacing() const
Accessor for base point spacing.
Definition: Terrain.h:262
Region of terrain surface which is modified.
Definition: Area.h:28
Rect updateArea(long id, std::unique_ptr< Area > a)
Updates the terrain affected by an area.
Definition: Terrain.cpp:350
Base class for modifiers to the procedurally generated terrain.
Definition: TerrainMod.h:20