2004-05-29 22:56:24 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Name: Terrain.h
|
|
|
|
// Author: Rich Cross
|
|
|
|
// Contact: rich@wildfiregames.com
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _TERRAIN_H
|
|
|
|
#define _TERRAIN_H
|
|
|
|
|
2004-06-08 14:10:51 +02:00
|
|
|
#include "Patch.h"
|
|
|
|
#include "Vector3D.h"
|
2004-07-23 12:56:52 +02:00
|
|
|
#include "Vector2D.h"
|
2006-05-04 06:14:48 +02:00
|
|
|
#include "Entity.h"
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CTerrain: main terrain class; contains the heightmap describing elevation
|
|
|
|
// data, and the smaller subpatches that form the terrain
|
|
|
|
class CTerrain
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CTerrain();
|
|
|
|
~CTerrain();
|
|
|
|
|
2006-04-17 22:02:51 +02:00
|
|
|
// Coordinate naming convention: world-space coordinates are float x,z;
|
|
|
|
// tile-space coordinates are int i,j.
|
|
|
|
|
|
|
|
bool Initialize(u32 size, const u16* ptr);
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
// return number of vertices along edge of the terrain
|
2005-10-19 19:16:34 +02:00
|
|
|
u32 GetVerticesPerSide() const { return m_MapSize; }
|
2006-04-17 22:02:51 +02:00
|
|
|
// return number of tiles along edge of the terrain
|
|
|
|
u32 GetTilesPerSide() const { return GetVerticesPerSide()-1; }
|
2004-05-29 22:56:24 +02:00
|
|
|
// return number of patches along edge of the terrain
|
2005-10-19 19:16:34 +02:00
|
|
|
u32 GetPatchesPerSide() const { return m_MapSizePatches; }
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2006-04-17 22:02:51 +02:00
|
|
|
inline bool isOnMap(float x, float z) const
|
2004-07-23 12:56:52 +02:00
|
|
|
{
|
2006-04-17 22:02:51 +02:00
|
|
|
return ((x >= 0.0f) && (x < (float)((m_MapSize-1) * CELL_SIZE)) && (z >= 0.0f) && (z < (float)((m_MapSize-1) * CELL_SIZE)));
|
2004-07-23 12:56:52 +02:00
|
|
|
}
|
2006-04-17 22:02:51 +02:00
|
|
|
inline bool isOnMap(const CVector2D& v) const { return isOnMap(v.x, v.y); }
|
|
|
|
float getVertexGroundLevel(int i, int j) const;
|
|
|
|
float getExactGroundLevel(float x, float z) const;
|
|
|
|
inline float getExactGroundLevel(const CVector2D& v) const { return getExactGroundLevel(v.x, v.y); }
|
2004-07-23 12:56:52 +02:00
|
|
|
|
2006-04-17 22:02:51 +02:00
|
|
|
float getSlope(float x, float z) const ;
|
2006-05-04 06:14:48 +02:00
|
|
|
//Find the slope of in X and Z axes depending on the way the entity is facing
|
|
|
|
CVector2D getSlopeAngleFace(float x, float y, CEntity*& entity) const;
|
2004-05-29 22:56:24 +02:00
|
|
|
// resize this terrain such that each side has given number of patches
|
|
|
|
void Resize(u32 size);
|
|
|
|
|
|
|
|
// set up a new heightmap from 16 bit data; assumes heightmap matches current terrain size
|
|
|
|
void SetHeightMap(u16* heightmap);
|
|
|
|
// return a pointer to the heightmap
|
|
|
|
u16* GetHeightMap() const { return m_Heightmap; }
|
|
|
|
|
|
|
|
// get patch at given coordinates, expressed in patch-space; return 0 if
|
|
|
|
// coordinates represent patch off the edge of the map
|
2006-04-17 22:02:51 +02:00
|
|
|
CPatch* GetPatch(i32 i, i32 j) const;
|
2004-05-29 22:56:24 +02:00
|
|
|
// get tile at given coordinates, expressed in tile-space; return 0 if
|
|
|
|
// coordinates represent tile off the edge of the map
|
2006-04-17 22:02:51 +02:00
|
|
|
CMiniPatch* GetTile(i32 i, i32 j) const;
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
// calculate the position of a given vertex
|
2005-10-19 19:16:34 +02:00
|
|
|
void CalcPosition(i32 i, i32 j, CVector3D& pos) const;
|
2005-09-12 22:04:26 +02:00
|
|
|
// calculate the vertex under a given position (rounding down coordinates)
|
2005-11-06 02:33:16 +01:00
|
|
|
static void CalcFromPosition(const CVector3D& pos, i32& i, i32& j)
|
|
|
|
{
|
2005-11-07 03:04:28 +01:00
|
|
|
i = i32_from_float(pos.X/CELL_SIZE);
|
|
|
|
j = i32_from_float(pos.Z/CELL_SIZE);
|
2005-11-06 02:33:16 +01:00
|
|
|
}
|
|
|
|
// calculate the vertex under a given position (rounding down coordinates)
|
2006-04-17 22:02:51 +02:00
|
|
|
static void CalcFromPosition(float x, float z, i32& i, i32& j)
|
2005-11-06 02:33:16 +01:00
|
|
|
{
|
2005-11-07 03:04:28 +01:00
|
|
|
i = i32_from_float(x/CELL_SIZE);
|
2006-04-17 22:02:51 +02:00
|
|
|
j = i32_from_float(z/CELL_SIZE);
|
2005-11-06 02:33:16 +01:00
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
// calculate the normal at a given vertex
|
2005-10-19 19:16:34 +02:00
|
|
|
void CalcNormal(u32 i, u32 j, CVector3D& normal) const;
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2004-10-06 20:45:59 +02:00
|
|
|
// flatten out an area of terrain (specified in world space coords); return
|
|
|
|
// the average height of the flattened area
|
2006-04-17 22:02:51 +02:00
|
|
|
float FlattenArea(float x0, float x1, float z0, float z1);
|
2004-10-06 20:45:59 +02:00
|
|
|
|
2005-07-03 18:25:48 +02:00
|
|
|
// mark a specific square of tiles as dirty - use this after modifying the heightmap
|
2006-04-17 22:02:51 +02:00
|
|
|
void MakeDirty(int i0, int j0, int i1, int j1, int dirtyFlags);
|
2005-11-05 05:59:54 +01:00
|
|
|
// mark the entire map as dirty
|
2005-11-06 06:05:07 +01:00
|
|
|
void MakeDirty(int dirtyFlags);
|
2005-07-03 18:25:48 +02:00
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
private:
|
|
|
|
// delete any data allocated by this terrain
|
|
|
|
void ReleaseData();
|
|
|
|
// setup patch pointers etc
|
|
|
|
void InitialisePatches();
|
|
|
|
|
|
|
|
// size of this map in each direction, in vertices; ie. total tiles = sqr(m_MapSize-1)
|
|
|
|
u32 m_MapSize;
|
|
|
|
// size of this map in each direction, in patches; total patches = sqr(m_MapSizePatches)
|
|
|
|
u32 m_MapSizePatches;
|
|
|
|
// the patches comprising this terrain
|
|
|
|
CPatch* m_Patches;
|
|
|
|
// 16-bit heightmap data
|
|
|
|
u16* m_Heightmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|