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 = 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 
76 
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 = 8.f;
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