Mercator
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/Mercator.h>
9 #include <Mercator/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 
19 namespace Mercator {
20 
21 class Segment;
22 class Shader;
23 class TerrainMod;
24 class Area;
25 class Effector;
26 
35 class Terrain {
36  public:
38  typedef WFMath::AxisBox<2> Rect;
39 
41  typedef std::map<int, BasePoint> Pointcolumn;
43  typedef std::map<int, Segment *> Segmentcolumn;
44 
46  typedef std::map<int, Pointcolumn > Pointstore;
48  typedef std::map<int, Segmentcolumn > Segmentstore;
49 
51  typedef std::map<int, const Shader *> Shaderstore;
52 
54  typedef std::map<const Effector *, Rect> Effectorstore;
55 
57  static const unsigned int DEFAULT;
59  static const unsigned int SHADED;
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 
78  Effectorstore m_effectors;
79 
80  void addSurfaces(Segment &);
81  void shadeSurfaces(Segment &);
82 
83  void addEffector(const Effector * effector);
84 
91  Rect updateEffector(const Effector * effector);
92  void removeEffector(const Effector * effector);
93 
97  bool isShaded() const {
98  return ((m_options & SHADED) == SHADED);
99  }
100  public:
102  static const float defaultLevel;
103 
104  explicit Terrain(unsigned int options = DEFAULT,
105  unsigned int resolution = defaultResolution);
106  ~Terrain();
107 
108  float get(float x, float y) const;
109  bool getHeightAndNormal(float x, float y, float&, WFMath::Vector<3>&) const;
110 
111  bool getBasePoint(int x, int y, BasePoint& z) const;
112  void setBasePoint(int x, int y, const BasePoint& z);
113 
115  void setBasePoint(int x, int y, float z) {
116  BasePoint bp(z);
117  setBasePoint(x, y, bp);
118  }
119 
124  Segment * getSegment(float x, float y) const {
125  int ix = (int)floor(x / m_spacing);
126  int iy = (int)floor(y / m_spacing);
127  return getSegment(ix, iy);
128  }
129 
130  Segment * getSegment(int x, int y) const;
131 
133  int getResolution() const {
134  return m_res;
135  }
136 
138  float getSpacing() const {
139  return m_spacing;
140  }
141 
143  const Segmentstore & getTerrain() const {
144  return m_segments;
145  }
146 
148  const Pointstore & getPoints() const {
149  return m_basePoints;
150  }
151 
153  const Shaderstore & getShaders() const {
154  return m_shaders;
155  }
156 
158  void addShader(const Shader * t, int id);
159  void removeShader(const Shader * t, int id);
160 
161  void addMod(const TerrainMod * mod);
162 
169  Rect updateMod(const TerrainMod * mod);
170  void removeMod(const TerrainMod * mod);
171 
172  void addArea(const Area* a);
173 
180  Rect updateArea(const Area* a);
181  void removeArea(const Area* a);
182 };
183 
184 } // namespace Mercator
185 
186 #endif // MERCATOR_TERRAIN_H
void addSurfaces(Segment &)
Add the required Surface objects to a Segment.
Definition: Terrain.cpp:128
Device which effects a change in the terrain.
Definition: Effector.h:25
void removeShader(const Shader *t, int id)
remove a Shader from the list for this terrain.
Definition: Terrain.cpp:93
std::map< int, Pointcolumn > Pointstore
STL map to store sparse array of BasePoint columns.
Definition: Terrain.h:46
std::map< const Effector *, Rect > Effectorstore
STL map to store terrain effectors.
Definition: Terrain.h:54
Terrain(unsigned int options=DEFAULT, unsigned int resolution=defaultResolution)
Construct a new Terrain object with optional options and resolution.
Definition: Terrain.cpp:36
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:143
void shadeSurfaces(Segment &)
Populate the Surface objects associated with a Segment.
Definition: Terrain.cpp:154
void addMod(const TerrainMod *mod)
Add a modifier to the terrain.
Definition: Terrain.cpp:452
Rect updateArea(const Area *a)
Updates the terrain affected by an area.
Definition: Terrain.cpp:484
~Terrain()
Desctruct Terrain object, deleting contained objects.
Definition: Terrain.cpp:47
void setBasePoint(int x, int y, float z)
Set the height of the basepoint at x,y to z.
Definition: Terrain.h:115
Rect updateMod(const TerrainMod *mod)
Updates the terrain affected by a mod.
Definition: Terrain.cpp:457
std::map< int, Segment * > Segmentcolumn
STL map to store sparse array of Segment pointers.
Definition: Terrain.h:43
Definition: Area.cpp:20
Segmentstore m_segments
2D spatial container with pointers to all Segments.
Definition: Terrain.h:73
WFMath::AxisBox< 2 > Rect
Bounding box.
Definition: Terrain.h:38
void addEffector(const Effector *effector)
Add an effector to the terrain.
Definition: Terrain.cpp:325
void addArea(const Area *a)
Add an area modifier to the terrain.
Definition: Terrain.cpp:471
void addShader(const Shader *t, int id)
Add a new Shader to the list for this terrain.
Definition: Terrain.cpp:65
const Pointstore & getPoints() const
Accessor for 2D sparse array of BasePoint objects.
Definition: Terrain.h:148
std::map< int, const Shader * > Shaderstore
STL map to store sparse array of Shader pointers.
Definition: Terrain.h:51
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:36
Rect updateEffector(const Effector *effector)
Updates the terrain affected by an Effector.
Definition: Terrain.cpp:349
const Shaderstore & getShaders() const
Accessor for list of Shader pointers.
Definition: Terrain.h:153
const int m_res
BasePoint resolution, or distance between adjacent points.
Definition: Terrain.h:66
Base class for Shader objects which create surface data for use when rendering terrain.
Definition: Shader.h:29
float getSpacing() const
Accessor for base point spacing.
Definition: Terrain.h:138
void setBasePoint(int x, int y, const BasePoint &z)
Set the BasePoint value at a given base point coordinate.
Definition: Terrain.cpp:249
bool getBasePoint(int x, int y, BasePoint &z) const
Get the BasePoint at a given base point coordinate.
Definition: Terrain.cpp:223
Pointstore m_basePoints
2D spatial container with all BasePoints.
Definition: Terrain.h:71
Point on the fundamental grid that is used as the basis for terrain.
Definition: BasePoint.h:19
Shaderstore m_shaders
List of shaders to be applied to terrain.
Definition: Terrain.h:75
std::map< int, Segmentcolumn > Segmentstore
STL map to store sparse array of Segment pointer columns.
Definition: Terrain.h:48
bool isShaded() const
Determine whether this terrain object has shading enabled.
Definition: Terrain.h:97
Class storing centrally all data about an instance of some terrain.
Definition: Terrain.h:35
std::map< int, BasePoint > Pointcolumn
STL map to store sparse array of BasePoints.
Definition: Terrain.h:41
Segment * getSegment(float x, float y) const
Get a pointer to the segment which contains the coord x,y.
Definition: Terrain.h:124
static const unsigned int SHADED
set if shaders are going to be used on this terrain.
Definition: Terrain.h:59
const unsigned int m_options
Bitset of option flags controlling various aspects of terrain.
Definition: Terrain.h:64
Effectorstore m_effectors
List of effectors be applied to the terrain.
Definition: Terrain.h:78
int getResolution() const
Accessor for base point resolution.
Definition: Terrain.h:133
bool getHeightAndNormal(float x, float y, float &, WFMath::Vector< 3 > &) const
Get an accurate height and normal vector at a given coordinate x,y.
Definition: Terrain.cpp:199
void removeArea(const Area *a)
Remove an area modifier from the terrain.
Definition: Terrain.cpp:493
Region of terrain surface which is modified.
Definition: Area.h:28
const float m_spacing
BasePoints spacing, same as m_res in float form for efficiency.
Definition: Terrain.h:68
static const float defaultLevel
Height value used when no data is available.
Definition: Terrain.h:102
Base class for modifiers to the procedurally generated terrain.
Definition: TerrainMod.h:20
void removeEffector(const Effector *effector)
Remove an effector from the terrain.
Definition: Terrain.cpp:424