Pyrogenesis  trunk
Terrain.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Describes ground via heightmap and array of CPatch.
20  */
21 
22 #ifndef INCLUDED_TERRAIN
23 #define INCLUDED_TERRAIN
24 
25 #include "maths/Vector3D.h"
26 #include "maths/Fixed.h"
27 #include "graphics/SColor.h"
28 #include "graphics/HeightMipmap.h"
29 
30 class CPatch;
31 class CMiniPatch;
32 class CFixedVector3D;
33 class CStr8;
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // Terrain Constants:
38 
39 /// metres [world space units] per tile in x and z
41 
42 /// number of u16 height units per metre
44 
45 /// metres per u16 height unit
47 
48 ///////////////////////////////////////////////////////////////////////////////
49 // CTerrain: main terrain class; contains the heightmap describing elevation
50 // data, and the smaller subpatches that form the terrain
51 class CTerrain
52 {
53 public:
54  CTerrain();
55  ~CTerrain();
56 
57  // Coordinate naming convention: world-space coordinates are float x,z;
58  // tile-space coordinates are ssize_t i,j. rationale: signed types can
59  // more efficiently be converted to/from floating point. use ssize_t
60  // instead of int/long because these are sizes.
61 
62  bool Initialize(ssize_t patchesPerSide, const u16* ptr);
63 
64  // return number of vertices along edge of the terrain
65  ssize_t GetVerticesPerSide() const { return m_MapSize; }
66  // return number of tiles along edge of the terrain
67  ssize_t GetTilesPerSide() const { return GetVerticesPerSide()-1; }
68  // return number of patches along edge of the terrain
70 
71  float GetMinX() const { return 0.0f; }
72  float GetMinZ() const { return 0.0f; }
73  float GetMaxX() const { return (float)((m_MapSize-1) * TERRAIN_TILE_SIZE); }
74  float GetMaxZ() const { return (float)((m_MapSize-1) * TERRAIN_TILE_SIZE); }
75 
76  bool IsOnMap(float x, float z) const
77  {
78  return ((x >= GetMinX()) && (x < GetMaxX())
79  && (z >= GetMinZ()) && (z < GetMaxZ()));
80  }
81 
82  CStr8 GetMovementClass(ssize_t i, ssize_t j) const;
83 
84  float GetVertexGroundLevel(ssize_t i, ssize_t j) const;
86  float GetExactGroundLevel(float x, float z) const;
88  float GetFilteredGroundLevel(float x, float z, float radius) const;
89 
90  // get the approximate slope of a tile
91  // (0 = horizontal, 0.5 = 30 degrees, 1.0 = 45 degrees, etc)
92  fixed GetSlopeFixed(ssize_t i, ssize_t j) const;
93 
94  // get the precise slope of a point, accounting for triangulation direction
95  fixed GetExactSlopeFixed(fixed x, fixed z) const;
96 
97  // Returns true if the triangulation diagonal for tile (i, j)
98  // should be in the direction (1,-1); false if it should be (1,1)
99  bool GetTriangulationDir(ssize_t i, ssize_t j) const;
100 
101  // resize this terrain such that each side has given number of patches
102  void Resize(ssize_t size);
103 
104  // set up a new heightmap from 16 bit data; assumes heightmap matches current terrain size
105  void SetHeightMap(u16* heightmap);
106  // return a pointer to the heightmap
107  u16* GetHeightMap() const { return m_Heightmap; }
108 
109  // get patch at given coordinates, expressed in patch-space; return 0 if
110  // coordinates represent patch off the edge of the map
111  CPatch* GetPatch(ssize_t i, ssize_t j) const;
112  // get tile at given coordinates, expressed in tile-space; return 0 if
113  // coordinates represent tile off the edge of the map
114  CMiniPatch* GetTile(ssize_t i, ssize_t j) const;
115 
116  // calculate the position of a given vertex
117  void CalcPosition(ssize_t i, ssize_t j, CVector3D& pos) const;
118  void CalcPositionFixed(ssize_t i, ssize_t j, CFixedVector3D& pos) const;
119  // calculate the vertex under a given position (rounding down coordinates)
120  static void CalcFromPosition(const CVector3D& pos, ssize_t& i, ssize_t& j)
121  {
122  i = (ssize_t)(pos.X/TERRAIN_TILE_SIZE);
123  j = (ssize_t)(pos.Z/TERRAIN_TILE_SIZE);
124  }
125  // calculate the vertex under a given position (rounding down coordinates)
126  static void CalcFromPosition(float x, float z, ssize_t& i, ssize_t& j)
127  {
128  i = (ssize_t)(x/TERRAIN_TILE_SIZE);
129  j = (ssize_t)(z/TERRAIN_TILE_SIZE);
130  }
131  // calculate the normal at a given vertex
132  void CalcNormal(ssize_t i, ssize_t j, CVector3D& normal) const;
133  void CalcNormalFixed(ssize_t i, ssize_t j, CFixedVector3D& normal) const;
134 
135  CVector3D CalcExactNormal(float x, float z) const;
136 
137  // Mark a specific square of tiles (inclusive lower bound, exclusive upper bound)
138  // as dirty - use this after modifying the heightmap.
139  // If you modify a vertex (i,j), you should dirty tiles
140  // from (i-1, j-1) [inclusive] to (i+1, j+1) [exclusive]
141  // since their geometry depends on that vertex.
142  // If you modify a tile (i,j), you should dirty tiles
143  // from (i-1, j-1) [inclusive] to (i+2, j+2) [exclusive]
144  // since their texture blends depend on that tile.
145  void MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags);
146  // mark the entire map as dirty
147  void MakeDirty(int dirtyFlags);
148 
149  /**
150  * Returns a 3D bounding box encompassing the given vertex range (inclusive)
151  */
153 
154  // get the base color for the terrain (typically pure white - other colors
155  // will interact badly with LOS - but used by the Actor Viewer tool)
156  SColor4ub GetBaseColor() const { return m_BaseColor; }
157  // set the base color for the terrain
158  void SetBaseColor(SColor4ub color) { m_BaseColor = color; }
159 
160  const CHeightMipmap& GetHeightMipmap() const { return m_HeightMipmap; }
161 
162 private:
163  // delete any data allocated by this terrain
164  void ReleaseData();
165  // setup patch pointers etc
166  void InitialisePatches();
167 
168  // size of this map in each direction, in vertices; ie. total tiles = sqr(m_MapSize-1)
170  // size of this map in each direction, in patches; total patches = sqr(m_MapSizePatches)
172  // the patches comprising this terrain
174  // 16-bit heightmap data
176  // base color (usually white)
178  // heightmap mipmap
180 };
181 
182 #endif
void ReleaseData()
Definition: Terrain.cpp:59
CStr8 GetMovementClass(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:106
A simple fixed-point number class.
Definition: Fixed.h:115
void MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags)
Definition: Terrain.cpp:651
CTerrain()
Definition: Terrain.cpp:43
const ssize_t TERRAIN_TILE_SIZE
metres [world space units] per tile in x and z
Definition: Terrain.h:40
SColor4ub m_BaseColor
Definition: Terrain.h:177
uint16_t u16
Definition: types.h:38
float GetMaxX() const
Definition: Terrain.h:73
CBoundingBoxAligned GetVertexesBound(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1)
Returns a 3D bounding box encompassing the given vertex range (inclusive)
Definition: Terrain.cpp:698
void SetHeightMap(u16 *heightmap)
Definition: Terrain.cpp:628
void CalcPosition(ssize_t i, ssize_t j, CVector3D &pos) const
Definition: Terrain.cpp:119
Definition: Vector3D.h:28
ssize_t GetVerticesPerSide() const
Definition: Terrain.h:65
bool GetTriangulationDir(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:480
Definition: Terrain.h:51
float GetMinZ() const
Definition: Terrain.h:72
float GetExactGroundLevel(float x, float z) const
Definition: Terrain.cpp:406
CPatch * m_Patches
Definition: Terrain.h:173
SColor4ub GetBaseColor() const
Definition: Terrain.h:156
fixed GetExactSlopeFixed(fixed x, fixed z) const
Definition: Terrain.cpp:343
void CalcNormal(ssize_t i, ssize_t j, CVector3D &normal) const
Definition: Terrain.cpp:145
void SetBaseColor(SColor4ub color)
Definition: Terrain.h:158
Definition: HeightMipmap.h:38
void CalcPositionFixed(ssize_t i, ssize_t j, CFixedVector3D &pos) const
Definition: Terrain.cpp:131
fixed GetSlopeFixed(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:324
float X
Definition: Vector3D.h:31
const float HEIGHT_SCALE
metres per u16 height unit
Definition: Terrain.h:46
static void CalcFromPosition(float x, float z, ssize_t &i, ssize_t &j)
Definition: Terrain.h:126
CMiniPatch * GetTile(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:299
ssize_t GetTilesPerSide() const
Definition: Terrain.h:67
CHeightMipmap m_HeightMipmap
Definition: Terrain.h:179
Definition: FixedVector3D.h:24
void InitialisePatches()
Definition: Terrain.cpp:613
bool IsOnMap(float x, float z) const
Definition: Terrain.h:76
Definition: SColor.h:30
Definition: Patch.h:48
bool Initialize(ssize_t patchesPerSide, const u16 *ptr)
Definition: Terrain.cpp:71
intptr_t ssize_t
Definition: wposix_types.h:82
fixed GetExactGroundLevelFixed(fixed x, fixed z) const
Definition: Terrain.cpp:451
float GetFilteredGroundLevel(float x, float z, float radius) const
Definition: Terrain.cpp:395
void CalcNormalFixed(ssize_t i, ssize_t j, CFixedVector3D &normal) const
Definition: Terrain.cpp:192
Definition: MiniPatch.h:31
fixed GetVertexGroundLevelFixed(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:316
CPatch * GetPatch(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:284
Definition: BoundingBoxAligned.h:35
u16 * m_Heightmap
Definition: Terrain.h:175
float GetMinX() const
Definition: Terrain.h:71
float GetVertexGroundLevel(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:309
float GetMaxZ() const
Definition: Terrain.h:74
float Z
Definition: Vector3D.h:31
void Resize(ssize_t size)
Definition: Terrain.cpp:501
u16 * GetHeightMap() const
Definition: Terrain.h:107
static void CalcFromPosition(const CVector3D &pos, ssize_t &i, ssize_t &j)
Definition: Terrain.h:120
ssize_t GetPatchesPerSide() const
Definition: Terrain.h:69
~CTerrain()
Definition: Terrain.cpp:51
ssize_t m_MapSizePatches
Definition: Terrain.h:171
const CHeightMipmap & GetHeightMipmap() const
Definition: Terrain.h:160
const ssize_t HEIGHT_UNITS_PER_METRE
number of u16 height units per metre
Definition: Terrain.h:43
CVector3D CalcExactNormal(float x, float z) const
Definition: Terrain.cpp:236
ssize_t m_MapSize
Definition: Terrain.h:169