mercator  0.4.0
A terrain generation library for the Worldforge system.
Segment.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_SEGMENT_H
6 #define MERCATOR_SEGMENT_H
7 
8 #include "Mercator.h"
9 #include "Matrix.h"
10 #include "BasePoint.h"
11 #include "HeightMap.h"
12 
13 #include <wfmath/vector.h>
14 #include <wfmath/axisbox.h>
15 
16 #include <set>
17 #include <map>
18 
19 namespace WFMath {
20 class MTRand;
21 }
22 
23 namespace Mercator {
24 
25 class Terrain;
26 class Surface;
27 class TerrainMod;
28 class Area;
29 class Shader;
30 
31 // This class will need to be reference counted if we want the code to
32 // be able to hold onto it, as currently they get deleted internally
33 // whenever height points are asserted.
34 
37 class Segment {
38  public:
40  typedef std::map<int, std::unique_ptr<Surface>> Surfacestore;
41 
42  struct AreaEntry {
43  long id;
44  const Area* area;
45  };
46 
48  typedef std::multimap<int, AreaEntry> Areastore;
49  private:
51  const int m_res;
53  const int m_size;
55  const int m_xRef;
57  const int m_zRef;
59  Matrix<2, 2, BasePoint> m_controlPoints;
61  HeightMap m_heightMap;
63  std::unique_ptr<std::vector<float>> m_normals;
64 
66  Surfacestore m_surfaces;
67 
69  Areastore m_areas;
70 
74  std::map<long, std::multimap<int, AreaEntry>::iterator> m_areaLookup;
75 
77  std::map<long, const TerrainMod*> m_terrainMods;
78  public:
79  explicit Segment(int x, int z, int resolution);
80  ~Segment();
81 
83  int getResolution() const {
84  return m_res;
85  }
86 
88  int getSize() const {
89  return m_size;
90  }
91 
93  int getXRef() const {
94  return m_xRef;
95  }
96 
98  int getZRef() const {
99  return m_zRef;
100  }
101 
105  bool isValid() const {
106  return m_heightMap.isValid();
107  }
108 
109  void invalidate(bool points = true);
110 
117  void setCornerPoint(unsigned int x, unsigned int z, const BasePoint & bp) {
118  m_controlPoints(x, z) = bp;
119  invalidate();
120  }
121 
124  return m_controlPoints;
125  }
126 
129  return m_controlPoints;
130  }
131 
133  const Surfacestore & getSurfaces() const {
134  return m_surfaces;
135  }
136 
138  Surfacestore & getSurfaces() {
139  return m_surfaces;
140  }
141 
143  const float * getPoints() const {
144  return m_heightMap.getData();
145  }
146 
148  float * getPoints() {
149  return m_heightMap.getData();
150  }
151 
153  const HeightMap& getHeightMap() const {
154  return m_heightMap;
155  }
156 
159  return m_heightMap;
160  }
161 
163  const float * getNormals() const {
164  return m_normals->data();
165  }
166 
168  float * getNormals() {
169  return m_normals->data();
170  }
171 
173  float get(int x, int z) const {
174  return m_heightMap.get(x, z);
175  }
176 
177  void getHeight(float x, float y, float &h) const;
178  void getHeightAndNormal(float x, float z, float &h,
179  WFMath::Vector<3> &normal) const;
180  bool clipToSegment(const WFMath::AxisBox<2> &bbox, int &lx, int &hx, int &lz, int &hz) const;
181 
182 
183  void populate();
184  void populateNormals();
185  void populateSurfaces();
186  void populateHeightMap(HeightMap& heightMap);
187 
189  float getMax() const { return m_heightMap.getMax(); }
191  float getMin() const { return m_heightMap.getMin(); }
192 
194  WFMath::AxisBox<2> getRect() const;
195 
196  void updateMod(long id, const TerrainMod *t);
197 
198  void clearMods();
199 
201  const Areastore& getAreas() const
202  { return m_areas; }
203 
204  const std::map<long, const TerrainMod*>& getMods() const
205  { return m_terrainMods; }
206 
207  void updateArea(long id, const Area* area, const Shader* shader);
208 
209  private:
210 
211  void applyMod(const TerrainMod *t);
212 
213  void invalidateSurfaces();
214 
215 };
216 
217 } // namespace Mercator
218 
219 #endif // MERCATOR_SEGMENT_H
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: HeightMap.h:21
DataType * getData()
Accessor for a pointer to buffer containing data values.
Definition: Buffer.h:63
bool isValid() const
Determine if this buffer has valid allocated storage.
Definition: Buffer.h:83
float getMax() const
Accessor for the maximum height value in this Segment.
Definition: HeightMap.h:49
bool isValid() const
Check whether this Segment contains valid point data.
Definition: Segment.h:105
int getResolution() const
Accessor for resolution of this segment.
Definition: Segment.h:83
Matrix< 2, 2, BasePoint > & getControlPoints()
Accessor for modifying 2D matrix of base points.
Definition: Segment.h:128
int getZRef() const
Accessor for Global y reference of this segment.
Definition: Segment.h:98
float getMin() const
Accessor for the minimum height value in this Segment.
Definition: HeightMap.h:51
const Surfacestore & getSurfaces() const
Accessor for list of attached Surface objects.
Definition: Segment.h:133
std::map< int, std::unique_ptr< Surface > > Surfacestore
STL map of pointers to Surface objects.
Definition: Segment.h:40
float getMin() const
Accessor for the minimum height value in this Segment.
Definition: Segment.h:191
int getSize() const
Accessor for array size of this segment.
Definition: Segment.h:88
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:37
const Areastore & getAreas() const
Accessor for multimap of Area objects.
Definition: Segment.h:201
const HeightMap & getHeightMap() const
Accessor for height map.
Definition: Segment.h:153
int getXRef() const
Accessor for Global x reference of this segment.
Definition: Segment.h:93
const float * getNormals() const
Accessor for buffer containing surface normals.
Definition: Segment.h:163
Base class for Shader objects which create surface data for use when rendering terrain.
Definition: Shader.h:25
void setCornerPoint(unsigned int x, unsigned int z, const BasePoint &bp)
Set the BasePoint data for one of the four that define this Segment.
Definition: Segment.h:117
float getMax() const
Accessor for the maximum height value in this Segment.
Definition: Segment.h:189
Point on the fundamental grid that is used as the basis for terrain.
Definition: BasePoint.h:19
A fixed sized array of objects.
Definition: Matrix.h:14
const Matrix< 2, 2, BasePoint > & getControlPoints() const
Accessor for 2D matrix of base points.
Definition: Segment.h:123
std::multimap< int, AreaEntry > Areastore
STL multimap of pointers to Area objects affecting this segment.
Definition: Segment.h:48
float * getNormals()
Accessor for write access to buffer containing surface normals.
Definition: Segment.h:168
const float * getPoints() const
Accessor for buffer containing height points.
Definition: Segment.h:143
float * getPoints()
Accessor for write access to buffer containing height points.
Definition: Segment.h:148
float get(int x, int z) const
Get the height at a relative integer position in the Segment.
Definition: HeightMap.h:40
Surfacestore & getSurfaces()
Accessor for modifying list of attached Surface objects.
Definition: Segment.h:138
Region of terrain surface which is modified.
Definition: Area.h:28
HeightMap & getHeightMap()
Accessor for write access to height map.
Definition: Segment.h:158
Base class for modifiers to the procedurally generated terrain.
Definition: TerrainMod.h:20