mercator 0.4.0
A terrain generation library for the Worldforge system.
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.h"
9#include "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#include <tuple>
19#include <functional>
20
21namespace Mercator {
22
23class Segment;
24class Shader;
25class TerrainMod;
26class Area;
27class Effector;
28
37class Terrain {
38 public:
40 typedef WFMath::AxisBox<2> Rect;
41
43 typedef std::map<int, BasePoint> Pointcolumn;
45 typedef std::map<int, std::unique_ptr<Segment>> Segmentcolumn;
46
48 typedef std::map<int, Pointcolumn > Pointstore;
50 typedef std::map<int, Segmentcolumn > Segmentstore;
51
53 typedef std::map<int, const Shader *> Shaderstore;
54
56 static const unsigned int DEFAULT = 0x0000;
58 static const unsigned int SHADED = 0x0001;
59 // More options go here as bit flags, and below should be a private
60 // test function
61 private:
63 const unsigned int m_options;
65 const int m_res;
67 const float m_spacing;
68
70 Pointstore m_basePoints;
72 Segmentstore m_segments;
74 Shaderstore m_shaders;
75
76 struct TerrainModEntry {
80 std::unique_ptr<TerrainMod> terrainMod;
81
85 Rect rect;
86 };
87
95 std::map<long, TerrainModEntry> m_terrainMods;
96
97 struct TerrainAreaEntry {
101 std::unique_ptr<Area> terrainArea;
102
106 Rect rect;
107 };
108
112 std::map<long, TerrainAreaEntry> m_terrainAreas;
113
123 void addSurfaces(Segment &);
124
130 void shadeSurfaces(Segment &);
131
135 bool isShaded() const {
136 return ((m_options & SHADED) == SHADED);
137 }
138 public:
140 static constexpr float defaultLevel = 8.f;
141
148 explicit Terrain(unsigned int options = DEFAULT,
149 int resolution = defaultResolution);
150
156
167 float get(float x, float z) const;
168
184 bool getHeight(float x, float z, float& h) const;
185
204 bool getHeightAndNormal(float x, float z, float& h, WFMath::Vector<3>& n) const;
205
216 bool getBasePoint(int x, int z, BasePoint& y) const;
217
230 void setBasePoint(int x, int z, const BasePoint& y);
231
233 void setBasePoint(int x, int y, float z) {
234 BasePoint bp(z);
235 setBasePoint(x, y, bp);
236 }
237
242 Segment * getSegmentAtPos(float x, float z) const;
243
253 Segment * getSegmentAtIndex(int x, int z) const;
254
256 int getResolution() const {
257 return m_res;
258 }
259
261 float getSpacing() const {
262 return m_spacing;
263 }
264
266 const Segmentstore & getTerrain() const {
267 return m_segments;
268 }
269
271 const Pointstore & getPoints() const {
272 return m_basePoints;
273 }
274
276 const Shaderstore & getShaders() const {
277 return m_shaders;
278 }
279
284 void addShader(const Shader * t, int id);
285
289 void removeShader(const Shader * t, int id);
290
296 Rect updateMod(long id, std::unique_ptr<TerrainMod> mod);
297
302 bool hasMod(long id) const;
303
304 const TerrainMod* getMod(long id) const;
305
310 Rect updateArea(long id, std::unique_ptr<Area> a);
311
312
313 const Area* getArea(long id) const;
314
320 int posToIndex(float pos) const;
321
327 void processSegments(const WFMath::AxisBox<2>& area, const std::function<void(Segment&, int, int)>& func) const;
328};
329
330inline int Terrain::posToIndex(float pos) const {
331 return (int)std::lround(std::floor(pos / m_spacing));
332}
333
334inline Segment * Terrain::getSegmentAtPos(float x, float z) const
335{
337}
338
339
340} // namespace Mercator
341
342#endif // MERCATOR_TERRAIN_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
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:37
Base class for Shader objects which create surface data for use when rendering terrain.
Definition: Shader.h:25
Base class for modifiers to the procedurally generated terrain.
Definition: TerrainMod.h:21
Class storing centrally all data about an instance of some terrain.
Definition: Terrain.h:37
bool getHeight(float x, float z, float &h) const
Get an accurate height at a given coordinate x,z.
Definition: Terrain.cpp:123
float getSpacing() const
Accessor for base point spacing.
Definition: Terrain.h:261
bool getHeightAndNormal(float x, float z, float &h, WFMath::Vector< 3 > &n) const
Get an accurate height and normal vector at a given coordinate x,z.
Definition: Terrain.cpp:133
bool getBasePoint(int x, int z, BasePoint &y) const
Get the BasePoint at a given base point coordinate.
Definition: Terrain.cpp:144
void processSegments(const WFMath::AxisBox< 2 > &area, const std::function< void(Segment &, int, int)> &func) const
Definition: Terrain.cpp:221
void addShader(const Shader *t, int id)
Add a new Shader to the list for this terrain.
Definition: Terrain.cpp:40
bool hasMod(long id) const
Checks if a mod with the supplied id has been registered with the terrain.
Definition: Terrain.cpp:326
int getResolution() const
Accessor for base point resolution.
Definition: Terrain.h:256
std::map< int, Segmentcolumn > Segmentstore
STL map to store sparse array of Segment pointer columns.
Definition: Terrain.h:50
Rect updateMod(long id, std::unique_ptr< TerrainMod > mod)
Updates the terrain with a mod.
Definition: Terrain.cpp:241
Segment * getSegmentAtPos(float x, float z) const
Get a pointer to the segment which contains the coord x,y.
Definition: Terrain.h:334
void setBasePoint(int x, int y, float z)
Set the height of the basepoint at x,y to z.
Definition: Terrain.h:233
std::map< int, Pointcolumn > Pointstore
STL map to store sparse array of BasePoint columns.
Definition: Terrain.h:48
WFMath::AxisBox< 2 > Rect
Bounding box.
Definition: Terrain.h:40
static constexpr float defaultLevel
Height value used when no data is available.
Definition: Terrain.h:140
Segment * getSegmentAtIndex(int x, int z) const
Get the Segment at a given index.
Definition: Terrain.cpp:208
void removeShader(const Shader *t, int id)
remove a Shader from the list for this terrain.
Definition: Terrain.cpp:62
std::map< int, BasePoint > Pointcolumn
STL map to store sparse array of BasePoints.
Definition: Terrain.h:43
int posToIndex(float pos) const
Converts the supplied position into a segment index.
Definition: Terrain.h:330
float get(float x, float z) const
Get the height value at a given coordinate x,z.
Definition: Terrain.cpp:114
std::map< int, std::unique_ptr< Segment > > Segmentcolumn
STL map to store sparse array of Segments.
Definition: Terrain.h:45
Rect updateArea(long id, std::unique_ptr< Area > a)
Updates the terrain affected by an area.
Definition: Terrain.cpp:350
~Terrain()
Destroy Terrain object, deleting contained objects.
std::map< int, const Shader * > Shaderstore
STL map to store sparse array of Shader pointers.
Definition: Terrain.h:53
static const unsigned int DEFAULT
value provided for no flags set.
Definition: Terrain.h:56
const Segmentstore & getTerrain() const
Accessor for 2D sparse array of Segment pointers.
Definition: Terrain.h:266
Terrain(unsigned int options=DEFAULT, int resolution=defaultResolution)
Construct a new Terrain object with optional options and resolution.
Definition: Terrain.cpp:32
const Shaderstore & getShaders() const
Accessor for list of Shader pointers.
Definition: Terrain.h:276
void setBasePoint(int x, int z, const BasePoint &y)
Set the BasePoint value at a given base point coordinate.
Definition: Terrain.cpp:158
static const unsigned int SHADED
set if shaders are going to be used on this terrain.
Definition: Terrain.h:58
const Pointstore & getPoints() const
Accessor for 2D sparse array of BasePoint objects.
Definition: Terrain.h:271