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/vector.h>
12 #include <wfmath/axisbox.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 = 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;
67 
74 
77 
78  void addSurfaces(Segment &);
79  void shadeSurfaces(Segment &);
80 
81  void addEffector(const Effector * effector);
82 
89  Rect updateEffector(const Effector * effector);
90  void removeEffector(const Effector * effector);
91 
95  bool isShaded() const {
96  return ((m_options & SHADED) == SHADED);
97  }
98  public:
100  static const float defaultLevel;
101 
102  explicit Terrain(unsigned int options = DEFAULT,
103  unsigned int resolution = defaultResolution);
104  ~Terrain();
105 
106  float get(float x, float y) const;
107  bool getHeightAndNormal(float x, float y, float&, WFMath::Vector<3>&) const;
108 
109  bool getBasePoint(int x, int y, BasePoint& z) const;
110  void setBasePoint(int x, int y, const BasePoint& z);
111 
113  void setBasePoint(int x, int y, float z) {
114  BasePoint bp(z);
115  setBasePoint(x, y, bp);
116  }
117 
122  Segment * getSegment(float x, float y) const {
123  int ix = (int)floor(x / m_res);
124  int iy = (int)floor(y / m_res);
125  return getSegment(ix, iy);
126  }
127 
128  Segment * getSegment(int x, int y) const;
129 
131  const int getResolution() const {
132  return m_res;
133  }
134 
136  const Segmentstore & getTerrain() const {
137  return m_segments;
138  }
139 
141  const Pointstore & getPoints() const {
142  return m_basePoints;
143  }
144 
146  const Shaderstore & getShaders() const {
147  return m_shaders;
148  }
149 
151  void addShader(const Shader * t, int id);
152  void removeShader(const Shader * t, int id);
153 
154  void addMod(const TerrainMod * mod);
155 
162  Rect updateMod(const TerrainMod * mod);
163  void removeMod(const TerrainMod * mod);
164 
165  void addArea(const Area* a);
166 
173  Rect updateArea(const Area* a);
174  void removeArea(const Area* a);
175 };
176 
177 } // namespace Mercator
178 
179 #endif // MERCATOR_TERRAIN_H