LCOV - code coverage report
Current view: top level - source/graphics - Terrain.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 3 16 18.8 %
Date: 2023-01-19 00:18:29 Functions: 3 11 27.3 %

          Line data    Source code
       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             : 
      25             : #include "graphics/HeightMipmap.h"
      26             : #include "graphics/SColor.h"
      27             : #include "maths/Fixed.h"
      28             : #include "maths/Vector3D.h"
      29             : #include "ps/CStr.h"
      30             : 
      31             : class CPatch;
      32             : class CMiniPatch;
      33             : class CFixedVector3D;
      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
      65       43682 :     ssize_t GetVerticesPerSide() const { return m_MapSize; }
      66             :     // return number of tiles along edge of the terrain
      67           0 :     ssize_t GetTilesPerSide() const { return GetVerticesPerSide()-1; }
      68             :     // return number of patches along edge of the terrain
      69        2061 :     ssize_t GetPatchesPerSide() const { return m_MapSizePatches; }
      70             : 
      71           0 :     float GetMinX() const { return 0.0f; }
      72           0 :     float GetMinZ() const { return 0.0f; }
      73           0 :     float GetMaxX() const { return (float)((m_MapSize-1) * TERRAIN_TILE_SIZE); }
      74           0 :     float GetMaxZ() const { return (float)((m_MapSize-1) * TERRAIN_TILE_SIZE); }
      75             : 
      76           0 :     bool IsOnMap(float x, float z) const
      77             :     {
      78           0 :         return ((x >= GetMinX()) && (x < GetMaxX())
      79           0 :              && (z >= GetMinZ()) && (z < GetMaxZ()));
      80             :     }
      81             : 
      82             :     float GetVertexGroundLevel(ssize_t i, ssize_t j) const;
      83             :     fixed GetVertexGroundLevelFixed(ssize_t i, ssize_t j) const;
      84             :     float GetExactGroundLevel(float x, float z) const;
      85             :     fixed GetExactGroundLevelFixed(fixed x, fixed 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)
      90             :     fixed GetSlopeFixed(ssize_t i, ssize_t j) const;
      91             : 
      92             :     // get the precise slope of a point, accounting for triangulation direction
      93             :     fixed GetExactSlopeFixed(fixed x, fixed z) const;
      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       43669 :     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           0 :     static void CalcFromPosition(const CVector3D& pos, ssize_t& i, ssize_t& j)
     120             :     {
     121           0 :         i = (ssize_t)(pos.X/TERRAIN_TILE_SIZE);
     122           0 :         j = (ssize_t)(pos.Z/TERRAIN_TILE_SIZE);
     123           0 :     }
     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             :      */
     151             :     CBoundingBoxAligned GetVertexesBound(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1);
     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)
     155             :     SColor4ub GetBaseColor() const { return m_BaseColor; }
     156             :     // set the base color for the terrain
     157             :     void SetBaseColor(SColor4ub color) { m_BaseColor = color; }
     158             : 
     159           0 :     const CHeightMipmap& GetHeightMipmap() const { return m_HeightMipmap; }
     160             : 
     161             : private:
     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)
     168             :     ssize_t m_MapSize;
     169             :     // size of this map in each direction, in patches; total patches = sqr(m_MapSizePatches)
     170             :     ssize_t m_MapSizePatches;
     171             :     // the patches comprising this terrain
     172             :     CPatch* m_Patches;
     173             :     // 16-bit heightmap data
     174             :     u16* m_Heightmap;
     175             :     // base color (usually white)
     176             :     SColor4ub m_BaseColor;
     177             :     // heightmap mipmap
     178             :     CHeightMipmap m_HeightMipmap;
     179             : };
     180             : 
     181             : #endif // INCLUDED_TERRAIN

Generated by: LCOV version 1.13