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
19namespace WFMath {
20class MTRand;
21}
22
23namespace Mercator {
24
25class Terrain;
26class Surface;
27class TerrainMod;
28class Area;
29class 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
37class 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::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
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
Region of terrain surface which is modified.
Definition: Area.h:29
Point on the fundamental grid that is used as the basis for terrain.
Definition: BasePoint.h:19
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
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: HeightMap.h:21
float getMin() const
Accessor for the minimum height value in this Segment.
Definition: HeightMap.h:51
float get(int x, int z) const
Get the height at a relative integer position in the Segment.
Definition: HeightMap.h:40
float getMax() const
Accessor for the maximum height value in this Segment.
Definition: HeightMap.h:49
A fixed sized array of objects.
Definition: Matrix.h:14
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:37
bool clipToSegment(const WFMath::AxisBox< 2 > &bbox, int &lx, int &hx, int &lz, int &hz) const
Determine the intersection between an axis aligned box and this segment.
Definition: Segment.cpp:238
int getResolution() const
Accessor for resolution of this segment.
Definition: Segment.h:83
bool isValid() const
Check whether this Segment contains valid point data.
Definition: Segment.h:105
HeightMap & getHeightMap()
Accessor for write access to height map.
Definition: Segment.h:158
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
void populate()
Populate the Segment with heightfield data.
Definition: Segment.cpp:60
std::multimap< int, AreaEntry > Areastore
STL multimap of pointers to Area objects affecting this segment.
Definition: Segment.h:48
Matrix< 2, 2, BasePoint > & getControlPoints()
Accessor for modifying 2D matrix of base points.
Definition: Segment.h:128
WFMath::AxisBox< 2 > getRect() const
The 2d area covered by this segment.
Definition: Segment.cpp:337
void populateNormals()
Populate the Segment with surface normal data.
Definition: Segment.cpp:111
float * getNormals()
Accessor for write access to buffer containing surface normals.
Definition: Segment.h:168
float * getPoints()
Accessor for write access to buffer containing height points.
Definition: Segment.h:148
int getSize() const
Accessor for array size of this segment.
Definition: Segment.h:88
Surfacestore & getSurfaces()
Accessor for modifying list of attached Surface objects.
Definition: Segment.h:138
Segment(int x, int z, int resolution)
Construct an empty segment with the given resolution.
Definition: Segment.cpp:37
const float * getNormals() const
Accessor for buffer containing surface normals.
Definition: Segment.h:163
float getMin() const
Accessor for the minimum height value in this Segment.
Definition: Segment.h:191
const Surfacestore & getSurfaces() const
Accessor for list of attached Surface objects.
Definition: Segment.h:133
int getXRef() const
Accessor for Global x reference of this segment.
Definition: Segment.h:93
std::map< int, std::unique_ptr< Surface > > Surfacestore
STL map of pointers to Surface objects.
Definition: Segment.h:40
void getHeightAndNormal(float x, float z, float &h, WFMath::Vector< 3 > &normal) const
Get an accurate height and normal vector at a given coordinate relative to this segment.
Definition: Segment.cpp:223
float getMax() const
Accessor for the maximum height value in this Segment.
Definition: Segment.h:189
const Matrix< 2, 2, BasePoint > & getControlPoints() const
Accessor for 2D matrix of base points.
Definition: Segment.h:123
int getZRef() const
Accessor for Global y reference of this segment.
Definition: Segment.h:98
const float * getPoints() const
Accessor for buffer containing height points.
Definition: Segment.h:143
void populateSurfaces()
Populate the surfaces associated with this Segment.
Definition: Segment.cpp:197
void invalidate(bool points=true)
Mark the contents of this Segment as stale.
Definition: Segment.cpp:83
const HeightMap & getHeightMap() const
Accessor for height map.
Definition: Segment.h:153
const Areastore & getAreas() const
Accessor for multimap of Area objects.
Definition: Segment.h:201
void clearMods()
Delete all the modifications applied to this Segment.
Definition: Segment.cpp:274
float get(int x, int z) const
Get the height at a relative integer position in the Segment.
Definition: Segment.h:173
~Segment()
Destruct the Segment.
Definition: Segment.cpp:50
Base class for modifiers to the procedurally generated terrain.
Definition: TerrainMod.h:21