Mercator  0.4.0
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, 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 unsigned int m_res;
68  const float m_spacing;
69 
71  Pointstore m_basePoints;
73  Segmentstore m_segments;
75  Shaderstore m_shaders;
76 
84  std::unordered_map<long, std::tuple<const TerrainMod *, Rect>> m_terrainMods;
85 
92  std::unordered_map<const Area *, Rect> m_terrainAreas;
93 
94  void addSurfaces(Segment &);
95  void shadeSurfaces(Segment &);
96 
100  bool isShaded() const {
101  return ((m_options & SHADED) == SHADED);
102  }
103  public:
105  static constexpr float defaultLevel = 8.f;
106 
107  explicit Terrain(unsigned int options = DEFAULT,
108  unsigned int resolution = defaultResolution);
109  ~Terrain();
110 
111  float get(float x, float z) const;
112  bool getHeight(float x, float z, float&) const;
113  bool getHeightAndNormal(float x, float z, float&, WFMath::Vector<3>&) const;
114 
115  bool getBasePoint(int x, int z, BasePoint& y) const;
116  void setBasePoint(int x, int z, const BasePoint& y);
117 
119  void setBasePoint(int x, int y, float z) {
120  BasePoint bp(z);
121  setBasePoint(x, y, bp);
122  }
123 
128  Segment * getSegmentAtPos(float x, float z) const;
129 
134  Segment * getSegmentAtIndex(int x, int z) const;
135 
137  int getResolution() const {
138  return m_res;
139  }
140 
142  float getSpacing() const {
143  return m_spacing;
144  }
145 
147  const Segmentstore & getTerrain() const {
148  return m_segments;
149  }
150 
152  const Pointstore & getPoints() const {
153  return m_basePoints;
154  }
155 
157  const Shaderstore & getShaders() const {
158  return m_shaders;
159  }
160 
162  void addShader(const Shader * t, int id);
163  void removeShader(const Shader * t, int id);
164 
170  Rect updateMod(long id, const TerrainMod * mod);
171 
176  bool hasMod(long id) const;
177 
178  const TerrainMod* getMod(long id) const;
179 
180  void addArea(const Area* a);
181 
188  Rect updateArea(const Area* a);
189  void removeArea(const Area* a);
190 
195  bool hasArea(const Area* a) const;
196 
202  int posToIndex(float pos) const;
203 
209  void processSegments(const WFMath::AxisBox<2>& area, const std::function<void(Segment&, int, int)>& func) const;
210 };
211 
212 inline int Terrain::posToIndex(float pos) const {
213  return std::lround(std::floor(pos / m_spacing));
214 }
215 
216 inline Segment * Terrain::getSegmentAtPos(float x, float z) const
217 {
218  return getSegmentAtIndex(posToIndex(x), posToIndex(z));
219 }
220 
221 
222 } // namespace Mercator
223 
224 #endif // MERCATOR_TERRAIN_H
Rect updateMod(long id, const TerrainMod *mod)
Updates the terrain with a mod.
Definition: Terrain.cpp:343
void removeShader(const Shader *t, int id)
remove a Shader from the list for this terrain.
Definition: Terrain.cpp:91
std::map< int, Pointcolumn > Pointstore
STL map to store sparse array of BasePoint columns.
Definition: Terrain.h:49
bool getHeightAndNormal(float x, float z, float &, WFMath::Vector< 3 > &) const
Get an accurate height and normal vector at a given coordinate x,z.
Definition: Terrain.cpp:205
void setBasePoint(int x, int z, const BasePoint &y)
Set the BasePoint value at a given base point coordinate.
Definition: Terrain.cpp:252
Terrain(unsigned int options=DEFAULT, unsigned int resolution=defaultResolution)
Construct a new Terrain object with optional options and resolution.
Definition: Terrain.cpp:37
static const unsigned int DEFAULT
value provided for no flags set.
Definition: Terrain.h:57
const Segmentstore & getTerrain() const
Accessor for 2D sparse array of Segment pointers.
Definition: Terrain.h:147
Rect updateArea(const Area *a)
Updates the terrain affected by an area.
Definition: Terrain.cpp:480
~Terrain()
Destroy Terrain object, deleting contained objects.
Definition: Terrain.cpp:48
void setBasePoint(int x, int y, float z)
Set the height of the basepoint at x,y to z.
Definition: Terrain.h:119
std::map< int, Segment * > Segmentcolumn
STL map to store sparse array of Segment pointers.
Definition: Terrain.h:46
Definition: Area.cpp:20
Segment * getSegmentAtIndex(int x, int z) const
Get a pointer to the segment at index x,y.
Definition: Terrain.cpp:310
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:105
void addArea(const Area *a)
Add an area modifier to the terrain.
Definition: Terrain.cpp:446
void addShader(const Shader *t, int id)
Add a new Shader to the list for this terrain.
Definition: Terrain.cpp:66
const Pointstore & getPoints() const
Accessor for 2D sparse array of BasePoint objects.
Definition: Terrain.h:152
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:36
const Shaderstore & getShaders() const
Accessor for list of Shader pointers.
Definition: Terrain.h:157
Base class for Shader objects which create surface data for use when rendering terrain.
Definition: Shader.h:24
float getSpacing() const
Accessor for base point spacing.
Definition: Terrain.h:142
bool getBasePoint(int x, int z, BasePoint &y) const
Get the BasePoint at a given base point coordinate.
Definition: Terrain.cpp:226
bool hasMod(long id) const
Checks if a mod with the supplied id has been registered with the terrain.
Definition: Terrain.cpp:427
Point on the fundamental grid that is used as the basis for terrain.
Definition: BasePoint.h:19
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
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:323
static const unsigned int SHADED
set if shaders are going to be used on this terrain.
Definition: Terrain.h:59
int getResolution() const
Accessor for base point resolution.
Definition: Terrain.h:137
void removeArea(const Area *a)
Remove an area modifier from the terrain.
Definition: Terrain.cpp:563
Region of terrain surface which is modified.
Definition: Area.h:28
bool hasArea(const Area *a) const
Checks if the supplied area has been registered with the terrain.
Definition: Terrain.cpp:584
Segment * getSegmentAtPos(float x, float z) const
Get a pointer to the segment which contains the coord x,y.
Definition: Terrain.h:216
Base class for modifiers to the procedurally generated terrain.
Definition: TerrainMod.h:20
int posToIndex(float pos) const
Converts the supplied position into a segment index.
Definition: Terrain.h:212