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;
34 class CBoundingBoxAligned;
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // Terrain Constants:
38 
39 /// metres [world space units] per tile in x and z
40 const ssize_t TERRAIN_TILE_SIZE = 4;
41 
42 /// number of u16 height units per metre
43 const ssize_t HEIGHT_UNITS_PER_METRE = 92;
44 
45 /// metres per u16 height unit
46 const float HEIGHT_SCALE = 1.f / HEIGHT_UNITS_PER_METRE;
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
GetVerticesPerSide()65 	ssize_t GetVerticesPerSide() const { return m_MapSize; }
66 	// return number of tiles along edge of the terrain
GetTilesPerSide()67 	ssize_t GetTilesPerSide() const { return GetVerticesPerSide()-1; }
68 	// return number of patches along edge of the terrain
GetPatchesPerSide()69 	ssize_t GetPatchesPerSide() const { return m_MapSizePatches; }
70 
GetMinX()71 	float GetMinX() const { return 0.0f; }
GetMinZ()72 	float GetMinZ() const { return 0.0f; }
GetMaxX()73 	float GetMaxX() const { return (float)((m_MapSize-1) * TERRAIN_TILE_SIZE); }
GetMaxZ()74 	float GetMaxZ() const { return (float)((m_MapSize-1) * TERRAIN_TILE_SIZE); }
75 
IsOnMap(float x,float z)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;
85 	fixed GetVertexGroundLevelFixed(ssize_t i, ssize_t j) const;
86 	float GetExactGroundLevel(float x, float z) const;
87 	fixed GetExactGroundLevelFixed(fixed x, fixed 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
GetHeightMap()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)
CalcFromPosition(const CVector3D & pos,ssize_t & i,ssize_t & j)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)
CalcFromPosition(float x,float z,ssize_t & i,ssize_t & j)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 	 */
152 	CBoundingBoxAligned GetVertexesBound(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1);
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)
GetBaseColor()156 	SColor4ub GetBaseColor() const { return m_BaseColor; }
157 	// set the base color for the terrain
SetBaseColor(SColor4ub color)158 	void SetBaseColor(SColor4ub color) { m_BaseColor = color; }
159 
GetHeightMipmap()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)
169 	ssize_t m_MapSize;
170 	// size of this map in each direction, in patches; total patches = sqr(m_MapSizePatches)
171 	ssize_t m_MapSizePatches;
172 	// the patches comprising this terrain
173 	CPatch*	m_Patches;
174 	// 16-bit heightmap data
175 	u16* m_Heightmap;
176 	// base color (usually white)
177 	SColor4ub m_BaseColor;
178 	// heightmap mipmap
179 	CHeightMipmap m_HeightMipmap;
180 };
181 
182 #endif
183