Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Terrain.h
Go to the documentation of this file.
1/* Copyright (C) 2022 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
26#include "graphics/SColor.h"
27#include "maths/Fixed.h"
28#include "maths/Vector3D.h"
29#include "ps/CStr.h"
30
31class CPatch;
32class CMiniPatch;
33class CFixedVector3D;
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
52{
53public:
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
66 // return number of tiles along edge of the terrain
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 float GetVertexGroundLevel(ssize_t i, ssize_t j) const;
84 float GetExactGroundLevel(float x, float z) const;
86 float GetFilteredGroundLevel(float x, float z, float radius) const;
87
88 // get the approximate slope of a tile
89 // (0 = horizontal, 0.5 = 30 degrees, 1.0 = 45 degrees, etc)
91
92 // get the precise slope of a point, accounting for triangulation direction
94
95 // Returns true if the triangulation diagonal for tile (i, j)
96 // should be in the direction (1,-1); false if it should be (1,1)
97 bool GetTriangulationDir(ssize_t i, ssize_t j) const;
98
99 // Resize this terrain such that each side has given number of patches,
100 // with the center offset in patches from the center of the source.
101 void ResizeAndOffset(ssize_t size, ssize_t horizontalOffset = 0, ssize_t verticalOffset = 0);
102
103 // set up a new heightmap from 16 bit data; assumes heightmap matches current terrain size
104 void SetHeightMap(u16* heightmap);
105 // return a pointer to the heightmap
106 u16* GetHeightMap() const { return m_Heightmap; }
107
108 // get patch at given coordinates, expressed in patch-space; return 0 if
109 // coordinates represent patch off the edge of the map
110 CPatch* GetPatch(ssize_t i, ssize_t j) const;
111 // get tile at given coordinates, expressed in tile-space; return 0 if
112 // coordinates represent tile off the edge of the map
113 CMiniPatch* GetTile(ssize_t i, ssize_t j) const;
114
115 // calculate the position of a given vertex
116 void CalcPosition(ssize_t i, ssize_t j, CVector3D& pos) const;
117 void CalcPositionFixed(ssize_t i, ssize_t j, CFixedVector3D& pos) const;
118 // calculate the vertex under a given position (rounding down coordinates)
119 static void CalcFromPosition(const CVector3D& pos, ssize_t& i, ssize_t& j)
120 {
121 i = (ssize_t)(pos.X/TERRAIN_TILE_SIZE);
122 j = (ssize_t)(pos.Z/TERRAIN_TILE_SIZE);
123 }
124 // calculate the vertex under a given position (rounding down coordinates)
125 static void CalcFromPosition(float x, float z, ssize_t& i, ssize_t& j)
126 {
127 i = (ssize_t)(x/TERRAIN_TILE_SIZE);
128 j = (ssize_t)(z/TERRAIN_TILE_SIZE);
129 }
130 // calculate the normal at a given vertex
131 void CalcNormal(ssize_t i, ssize_t j, CVector3D& normal) const;
132 void CalcNormalFixed(ssize_t i, ssize_t j, CFixedVector3D& normal) const;
133
134 CVector3D CalcExactNormal(float x, float z) const;
135
136 // Mark a specific square of tiles (inclusive lower bound, exclusive upper bound)
137 // as dirty - use this after modifying the heightmap.
138 // If you modify a vertex (i,j), you should dirty tiles
139 // from (i-1, j-1) [inclusive] to (i+1, j+1) [exclusive]
140 // since their geometry depends on that vertex.
141 // If you modify a tile (i,j), you should dirty tiles
142 // from (i-1, j-1) [inclusive] to (i+2, j+2) [exclusive]
143 // since their texture blends depend on that tile.
144 void MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags);
145 // mark the entire map as dirty
146 void MakeDirty(int dirtyFlags);
147
148 /**
149 * Returns a 3D bounding box encompassing the given vertex range (inclusive)
150 */
152
153 // get the base color for the terrain (typically pure white - other colors
154 // will interact badly with LOS - but used by the Actor Viewer tool)
156 // set the base color for the terrain
157 void SetBaseColor(SColor4ub color) { m_BaseColor = color; }
158
160
161private:
162 // delete any data allocated by this terrain
163 void ReleaseData();
164 // setup patch pointers etc
165 void InitialisePatches();
166
167 // size of this map in each direction, in vertices; ie. total tiles = sqr(m_MapSize-1)
169 // size of this map in each direction, in patches; total patches = sqr(m_MapSizePatches)
171 // the patches comprising this terrain
173 // 16-bit heightmap data
175 // base color (usually white)
177 // heightmap mipmap
179};
180
181#endif // INCLUDED_TERRAIN
const float HEIGHT_SCALE
metres per u16 height unit
Definition: Terrain.h:46
const ssize_t HEIGHT_UNITS_PER_METRE
number of u16 height units per metre
Definition: Terrain.h:43
const ssize_t TERRAIN_TILE_SIZE
metres [world space units] per tile in x and z
Definition: Terrain.h:40
Definition: BoundingBoxAligned.h:34
Definition: FixedVector3D.h:25
A simple fixed-point number class.
Definition: Fixed.h:120
Definition: HeightMipmap.h:42
Definition: MiniPatch.h:29
Definition: Patch.h:49
Definition: Terrain.h:52
ssize_t GetPatchesPerSide() const
Definition: Terrain.h:69
fixed GetExactSlopeFixed(fixed x, fixed z) const
Definition: Terrain.cpp:326
float GetMinX() const
Definition: Terrain.h:71
static void CalcFromPosition(const CVector3D &pos, ssize_t &i, ssize_t &j)
Definition: Terrain.h:119
void SetHeightMap(u16 *heightmap)
Definition: Terrain.cpp:734
void CalcNormal(ssize_t i, ssize_t j, CVector3D &normal) const
Definition: Terrain.cpp:128
SColor4ub m_BaseColor
Definition: Terrain.h:176
CPatch * m_Patches
Definition: Terrain.h:172
float GetMaxZ() const
Definition: Terrain.h:74
void SetBaseColor(SColor4ub color)
Definition: Terrain.h:157
ssize_t GetTilesPerSide() const
Definition: Terrain.h:67
void CalcPosition(ssize_t i, ssize_t j, CVector3D &pos) const
Definition: Terrain.cpp:102
float GetMinZ() const
Definition: Terrain.h:72
CMiniPatch * GetTile(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:282
static void CalcFromPosition(float x, float z, ssize_t &i, ssize_t &j)
Definition: Terrain.h:125
ssize_t GetVerticesPerSide() const
Definition: Terrain.h:65
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:804
ssize_t m_MapSizePatches
Definition: Terrain.h:170
bool GetTriangulationDir(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:463
CHeightMipmap m_HeightMipmap
Definition: Terrain.h:178
~CTerrain()
Definition: Terrain.cpp:45
fixed GetSlopeFixed(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:307
u16 * GetHeightMap() const
Definition: Terrain.h:106
u16 * m_Heightmap
Definition: Terrain.h:174
ssize_t m_MapSize
Definition: Terrain.h:168
void InitialisePatches()
Definition: Terrain.cpp:719
void CalcNormalFixed(ssize_t i, ssize_t j, CFixedVector3D &normal) const
Definition: Terrain.cpp:175
void ResizeAndOffset(ssize_t size, ssize_t horizontalOffset=0, ssize_t verticalOffset=0)
Definition: Terrain.cpp:482
CTerrain()
Definition: Terrain.cpp:37
fixed GetVertexGroundLevelFixed(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:299
SColor4ub GetBaseColor() const
Definition: Terrain.h:155
bool IsOnMap(float x, float z) const
Definition: Terrain.h:76
void MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags)
Definition: Terrain.cpp:757
void CalcPositionFixed(ssize_t i, ssize_t j, CFixedVector3D &pos) const
Definition: Terrain.cpp:114
float GetVertexGroundLevel(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:292
float GetMaxX() const
Definition: Terrain.h:73
CVector3D CalcExactNormal(float x, float z) const
Definition: Terrain.cpp:219
CPatch * GetPatch(ssize_t i, ssize_t j) const
Definition: Terrain.cpp:267
bool Initialize(ssize_t patchesPerSide, const u16 *ptr)
Definition: Terrain.cpp:65
void ReleaseData()
Definition: Terrain.cpp:53
const CHeightMipmap & GetHeightMipmap() const
Definition: Terrain.h:159
float GetExactGroundLevel(float x, float z) const
Definition: Terrain.cpp:389
fixed GetExactGroundLevelFixed(fixed x, fixed z) const
Definition: Terrain.cpp:434
float GetFilteredGroundLevel(float x, float z, float radius) const
Definition: Terrain.cpp:378
Definition: Vector3D.h:31
float Z
Definition: Vector3D.h:33
float X
Definition: Vector3D.h:33
Definition: SColor.h:31
uint16_t u16
Definition: types.h:38
intptr_t ssize_t
Definition: wposix_types.h:82