Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
43 const ssize_t HEIGHT_UNITS_PER_METRE = 732; // == approx int(256.0f/0.35f)
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 (0 = horizontal, 0.5 = 30 degrees, 1.0 = 45 degrees, etc)
91  fixed GetSlopeFixed(ssize_t i, ssize_t j) const;
92 
93  // Returns true if the triangulation diagonal for tile (i, j)
94  // should be in the direction (1,-1); false if it should be (1,1)
95  bool GetTriangulationDir(ssize_t i, ssize_t j) const;
96 
97  // resize this terrain such that each side has given number of patches
98  void Resize(ssize_t size);
99 
100  // set up a new heightmap from 16 bit data; assumes heightmap matches current terrain size
101  void SetHeightMap(u16* heightmap);
102  // return a pointer to the heightmap
103  u16* GetHeightMap() const { return m_Heightmap; }
104 
105  // get patch at given coordinates, expressed in patch-space; return 0 if
106  // coordinates represent patch off the edge of the map
107  CPatch* GetPatch(ssize_t i, ssize_t j) const;
108  // get tile at given coordinates, expressed in tile-space; return 0 if
109  // coordinates represent tile off the edge of the map
110  CMiniPatch* GetTile(ssize_t i, ssize_t j) const;
111 
112  // calculate the position of a given vertex
113  void CalcPosition(ssize_t i, ssize_t j, CVector3D& pos) const;
114  void CalcPositionFixed(ssize_t i, ssize_t j, CFixedVector3D& pos) const;
115  // calculate the vertex under a given position (rounding down coordinates)
116  static void CalcFromPosition(const CVector3D& pos, ssize_t& i, ssize_t& j)
117  {
118  i = (ssize_t)(pos.X/TERRAIN_TILE_SIZE);
119  j = (ssize_t)(pos.Z/TERRAIN_TILE_SIZE);
120  }
121  // calculate the vertex under a given position (rounding down coordinates)
122  static void CalcFromPosition(float x, float z, ssize_t& i, ssize_t& j)
123  {
124  i = (ssize_t)(x/TERRAIN_TILE_SIZE);
125  j = (ssize_t)(z/TERRAIN_TILE_SIZE);
126  }
127  // calculate the normal at a given vertex
128  void CalcNormal(ssize_t i, ssize_t j, CVector3D& normal) const;
129  void CalcNormalFixed(ssize_t i, ssize_t j, CFixedVector3D& normal) const;
130 
131  CVector3D CalcExactNormal(float x, float z) const;
132 
133  // Mark a specific square of tiles (inclusive lower bound, exclusive upper bound)
134  // as dirty - use this after modifying the heightmap.
135  // If you modify a vertex (i,j), you should dirty tiles
136  // from (i-1, j-1) [inclusive] to (i+1, j+1) [exclusive]
137  // since their geometry depends on that vertex.
138  // If you modify a tile (i,j), you should dirty tiles
139  // from (i-1, j-1) [inclusive] to (i+2, j+2) [exclusive]
140  // since their texture blends depend on that tile.
141  void MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags);
142  // mark the entire map as dirty
143  void MakeDirty(int dirtyFlags);
144 
145  /**
146  * Returns a 3D bounding box encompassing the given vertex range (inclusive)
147  */
149 
150  // get the base colour for the terrain (typically pure white - other colours
151  // will interact badly with LOS - but used by the Actor Viewer tool)
153  // set the base colour for the terrain
154  void SetBaseColour(SColor4ub colour) { m_BaseColour = colour; }
155 
156  const CHeightMipmap& GetHeightMipmap() const { return m_HeightMipmap; }
157 
158 private:
159  // delete any data allocated by this terrain
160  void ReleaseData();
161  // setup patch pointers etc
162  void InitialisePatches();
163 
164  // size of this map in each direction, in vertices; ie. total tiles = sqr(m_MapSize-1)
166  // size of this map in each direction, in patches; total patches = sqr(m_MapSizePatches)
168  // the patches comprising this terrain
170  // 16-bit heightmap data
172  // base colour (usually white)
174  // heightmap mipmap
176 };
177 
178 #endif
void ReleaseData()
Definition: Terrain.cpp:58
CStr8 GetMovementClass(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:105
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:598
CTerrain()
Definition: Terrain.cpp:42
const ssize_t TERRAIN_TILE_SIZE
metres [world space units] per tile in x and z
Definition: Terrain.h:40
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:645
void SetHeightMap(u16 *heightmap)
Definition: Terrain.cpp:575
SColor4ub GetBaseColour() const
Definition: Terrain.h:152
void CalcPosition(ssize_t i, ssize_t j, CVector3D &pos) const
Definition: Terrain.cpp:118
ssize_t GetVerticesPerSide() const
Definition: Terrain.h:65
bool GetTriangulationDir(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:427
float GetMinZ() const
Definition: Terrain.h:72
float GetExactGroundLevel(float x, float z) const
Definition: Terrain.cpp:353
CPatch * m_Patches
Definition: Terrain.h:169
void CalcNormal(ssize_t i, ssize_t j, CVector3D &normal) const
Definition: Terrain.cpp:144
void CalcPositionFixed(ssize_t i, ssize_t j, CFixedVector3D &pos) const
Definition: Terrain.cpp:130
fixed GetSlopeFixed(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:323
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:122
CMiniPatch * GetTile(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:298
ssize_t GetTilesPerSide() const
Definition: Terrain.h:67
CHeightMipmap m_HeightMipmap
Definition: Terrain.h:175
void InitialisePatches()
Definition: Terrain.cpp:560
bool IsOnMap(float x, float z) const
Definition: Terrain.h:76
Definition: Patch.h:48
bool Initialize(ssize_t patchesPerSide, const u16 *ptr)
Definition: Terrain.cpp:70
intptr_t ssize_t
Definition: wposix_types.h:82
fixed GetExactGroundLevelFixed(fixed x, fixed z) const
Definition: Terrain.cpp:398
float GetFilteredGroundLevel(float x, float z, float radius) const
Definition: Terrain.cpp:342
void CalcNormalFixed(ssize_t i, ssize_t j, CFixedVector3D &normal) const
Definition: Terrain.cpp:191
#define u16
Definition: types.h:40
fixed GetVertexGroundLevelFixed(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:315
CPatch * GetPatch(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:283
void SetBaseColour(SColor4ub colour)
Definition: Terrain.h:154
u16 * m_Heightmap
Definition: Terrain.h:171
float GetMinX() const
Definition: Terrain.h:71
float GetVertexGroundLevel(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:308
float GetMaxZ() const
Definition: Terrain.h:74
float Z
Definition: Vector3D.h:31
void Resize(ssize_t size)
Definition: Terrain.cpp:448
u16 * GetHeightMap() const
Definition: Terrain.h:103
static void CalcFromPosition(const CVector3D &pos, ssize_t &i, ssize_t &j)
Definition: Terrain.h:116
ssize_t GetPatchesPerSide() const
Definition: Terrain.h:69
~CTerrain()
Definition: Terrain.cpp:50
ssize_t m_MapSizePatches
Definition: Terrain.h:167
const CHeightMipmap & GetHeightMipmap() const
Definition: Terrain.h:156
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:235
SColor4ub m_BaseColour
Definition: Terrain.h:173
ssize_t m_MapSize
Definition: Terrain.h:165