Colobot
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Gfx::CTerrain Class Reference

Terrain loader/generator and manager. More...

#include <terrain.h>

Classes

struct  BuildingLevel
 Flat level for building. More...
 
struct  FlyingLimit
 Spherical limit of flight. More...
 
struct  TerrainMaterial
 Material for ground surface. More...
 
struct  TerrainMaterialPoint
 Material used for terrain point. More...
 

Public Member Functions

bool Generate (int mosaicCount, int brickCountPow2, float brickSize, float vision, int depth, float hardness)
 Generates a new flat terrain. More...
 
bool InitTextures (const std::string &baseName, int *table, int dx, int dy)
 Initializes the names of textures to use for the land. More...
 
void FlushMaterials ()
 Clears all terrain materials. More...
 
void AddMaterial (int id, const std::string &texName, const Math::Point &uv, int up, int right, int down, int left, float hardness)
 Adds a terrain material the names of textures to use for the land. More...
 
bool InitMaterials (int id)
 Initializes all the ground with one material. More...
 
bool GenerateMaterials (int *id, float min, float max, float slope, float freq, Math::Vector center, float radius)
 Generates a level in the terrain. More...
 
void FlushRelief ()
 Clears the relief, resources and all other associated data. More...
 
bool LoadRelief (const std::string &fileName, float scaleRelief, bool adjustBorder)
 Load relief from image. More...
 
bool RandomizeRelief ()
 Load ramdomized relief. More...
 
bool LoadResources (const std::string &fileName)
 Load resources from image. More...
 
bool CreateObjects ()
 Creates all objects of the terrain within the 3D engine. More...
 
bool Terraform (const Math::Vector &p1, const Math::Vector &p2, float height)
 Modifies the terrain's relief. More...
 
float GetFineSlope (const Math::Vector &pos)
 Gives the exact slope of the terrain at 2D (XZ) position. More...
 
float GetCoarseSlope (const Math::Vector &pos)
 Gives the approximate slope of the terrain at 2D (XZ) position. More...
 
bool GetNormal (Math::Vector &n, const Math::Vector &p)
 Gives the normal vector at 2D (XZ) position. More...
 
float GetFloorLevel (const Math::Vector &pos, bool brut=false, bool water=false)
 Returns the height of the ground level at 2D (XZ) position. More...
 
float GetHeightToFloor (const Math::Vector &pos, bool brut=false, bool water=false)
 Returns the distance to the ground level from 3D position. More...
 
bool AdjustToFloor (Math::Vector &pos, bool brut=false, bool water=false)
 Modifies the Y coordinate of 3D position to rest on the ground floor. More...
 
bool AdjustToStandardBounds (Math::Vector &pos)
 Adjusts 3D position so that it is within standard terrain boundaries. More...
 
bool AdjustToBounds (Math::Vector &pos, float margin)
 Adjusts 3D position so that it is within terrain boundaries and the given margin. More...
 
TerrainRes GetResource (const Math::Vector &pos)
 Returns the resource type available underground at 2D (XZ) position. More...
 
void FlushBuildingLevel ()
 Empty the table of elevations. More...
 
bool AddBuildingLevel (Math::Vector center, float min, float max, float height, float factor)
 Adds a new elevation for a building. More...
 
bool UpdateBuildingLevel (Math::Vector center)
 Updates the elevation for a building when it was moved up (after a terraforming) More...
 
bool DeleteBuildingLevel (Math::Vector center)
 Removes the elevation for a building when it was destroyed. More...
 
float GetBuildingFactor (const Math::Vector &pos)
 Returns the influence factor whether a position is on a possible rise. More...
 
float GetHardness (const Math::Vector &pos)
 Returns the hardness of the ground in a given place. More...
 
int GetMosaicCount ()
 Returns number of mosaics. More...
 
int GetBrickCount ()
 Returns number of bricks in mosaic. More...
 
float GetBrickSize ()
 Returns brick size. More...
 
float GetReliefScale ()
 Returns the vertical scale of relief. More...
 
void ShowFlatGround (Math::Vector pos)
 Shows the flat areas on the ground. More...
 
float GetFlatZoneRadius (Math::Vector center, float max)
 Calculates the radius of the largest flat area available. More...
 
void FlushFlyingLimit ()
 Empty the table of flying limits. More...
 
void AddFlyingLimit (Math::Vector center, float extRadius, float intRadius, float maxHeight)
 Adds a new flying limit. More...
 
float GetFlyingLimit (Math::Vector pos, bool noLimit)
 Returns the maximum height of flight. More...
 
void SetWind (Math::Vector speed)
 Management of the wind. More...
 
Math::Vector GetWind ()
 
void SetFlyingMaxHeight (float height)
 Management of the global max flying height. More...
 
float GetFlyingMaxHeight ()
 

Protected Member Functions

bool AddReliefPoint (Math::Vector pos, float scaleRelief)
 Adds a point of elevation in the buffer of relief. More...
 
void AdjustRelief ()
 Adjust the edges of each mosaic to be compatible with all lower resolutions. More...
 
Math::Vector GetVector (int x, int y)
 Calculates a vector of the terrain. More...
 
VertexTex2 GetVertex (int x, int y, int step)
 Calculates a vertex of the terrain. More...
 
bool CreateMosaic (int ox, int oy, int step, int objRank, const Material &mat)
 Creates all objects of a mosaic. More...
 
bool CreateSquare (int x, int y)
 Creates all objects in a mesh square ground. More...
 
TerrainMaterialFindMaterial (int id)
 Seeks a material based on its ID. More...
 
int FindMaterialByNeighbors (char *mat)
 Seeks a material based on neighbor values. More...
 
void GetTexture (int x, int y, std::string &name, Math::Point &uv)
 Returns the texture name and UV coords to use for a given square. More...
 
float GetHeight (int x, int y)
 Returns the height of the terrain. More...
 
bool CheckMaterialPoint (int x, int y, float min, float max, float slope)
 Decide whether a point is using the materials. More...
 
void SetMaterialPoint (int x, int y, int id, char *mat)
 Modifies the state of a point and its four neighbors, without testing if possible. More...
 
bool ChangeMaterialPoint (int x, int y, int id)
 Modifies the state of a point. More...
 
bool CondChangeMaterialPoint (int x, int y, int id, char *mat)
 Tests if a material can give a place, according to its four neighbors. If yes, puts the point. More...
 
void InitMaterialPoints ()
 Initializes material points array. More...
 
void FlushMaterialPoints ()
 Clears the material points. More...
 
void AdjustBuildingLevel (Math::Vector &p)
 Adjusts a position according to a possible rise. More...
 

Protected Attributes

CEnginem_engine
 
CWaterm_water
 
std::vector< float > m_relief
 Relief data points. More...
 
std::vector< unsigned char > m_resources
 Resources data. More...
 
std::vector< int > m_textures
 Texture indices. More...
 
std::vector< int > m_objRanks
 Object ranks for mosaic objects. More...
 
int m_mosaicCount
 Number of mosaics (along one dimension) More...
 
int m_brickCount
 Number of bricks per mosaic (along one dimension) More...
 
int m_materialPointCount
 Number of terrain material dots (along one dimension) More...
 
float m_brickSize
 Size of single brick (along X and Z axis) More...
 
float m_scaleRelief
 Vertical (relief) scale. More...
 
int m_textureSubdivCount
 Subdivision of material points in mosaic. More...
 
int m_depth
 Number of different resolutions (1,2,3,4) More...
 
float m_textureScale
 Scale of texture mapping. More...
 
float m_vision
 Vision before a change of resolution. More...
 
std::string m_texBaseName
 Base name for single texture. More...
 
std::string m_texBaseExt
 Extension for single texture. More...
 
float m_defaultHardness
 Default hardness for level material. More...
 
std::vector< TerrainMaterialm_materials
 Terrain materials. More...
 
std::vector< TerrainMaterialPointm_materialPoints
 Material for terrain points. More...
 
bool m_useMaterials
 True if using terrain material mapping. More...
 
int m_maxMaterialID
 Maximum level ID (no ID is >= to this) More...
 
int m_materialAutoID
 Internal counter for auto generation of material IDs. More...
 
std::vector< BuildingLevelm_buildingLevels
 
Math::Vector m_wind
 Wind speed. More...
 
float m_flyingMaxHeight
 Global flying height limit. More...
 
std::vector< FlyingLimitm_flyingLimits
 List of local flight limits. More...
 

Detailed Description

Terrain loader/generator and manager.

Terrain mapping

Terrain is created from relief textures specifying a XY plane with height values which are then scaled and translated into XZ surface forming the terrain of game level.

The basic unit of terrain is called "brick", which is two triangles forming a quad. Bricks have constant size (brick size) in X and Z direction. Points forming the bricks correspond one-to-one to relief data points (pixels in relief image).

Bricks are grouped into "mosaics". Mosaic is a square containing brickCount x brickCount bricks where brickCount is an even power of 2. Each mosaic corresponds to one created engine object.

The whole terrain is also a square formed by mosaicCount * mosaicCount of mosaics.

Image coordinates are converted in the following way to world coordinates of brick points (Wx, Wy, Wz - world coordinates, Ix, Iy - image coordinates, Pxy - pixel value at Ix,Iy):

Wx = (Ix - brickCount*mosaicCount / 2.0f) * brickSize
Wz = (Iy - brickCount*mosaicCount / 2.0f) * brickSize
Wy = (Pxy / 255.0f) * reliefScale

To create and initialize a terrain, you must call Generate() as the first function, setting the number of bricks, mosaics etc.

Materials and textures

The terrain can be textured in two ways:

Textures are applied to subdivisions of mosaics (groups of bricks of size brickCount / textureSubdivCount).

Texture indexes

Texture indexes specify the texture for each textured point by concatenating base name of texture, the index number and texture extension.

Texture indexes are specified directly in InitTextures().

Terrain materials

Terrain materials are more sophisticated system. Each material is a texture, applied to one area, but specifying also the textures to use on surrounding areas: left, right, bottom and top.

You specify one or more terrain materials in AddMaterial() function. The function will add a material for given circle on the ground, with some randomized matching of supplied materials and sophisticated logic for ensuring that borders between neighboring materials follow the specified rules.

Other features

Terrain can have specified building levels - flat space expanses, where relief data is specifically adjusted to level space to allow construction of buildings.

Underground resources can be supplied by loading them from image like relief data.

Terrain also specifies flying limits for player: one global level and possible additional spherical restrictions.

Member Function Documentation

bool Gfx::CTerrain::Generate ( int  mosaicCount,
int  brickCountPow2,
float  brickSize,
float  vision,
int  depth,
float  hardness 
)

Generates a new flat terrain.

bool Gfx::CTerrain::InitTextures ( const std::string &  baseName,
int *  table,
int  dx,
int  dy 
)

Initializes the names of textures to use for the land.

void Gfx::CTerrain::FlushMaterials ( )

Clears all terrain materials.

void Gfx::CTerrain::AddMaterial ( int  id,
const std::string &  texName,
const Math::Point uv,
int  up,
int  right,
int  down,
int  left,
float  hardness 
)

Adds a terrain material the names of textures to use for the land.

bool Gfx::CTerrain::InitMaterials ( int  id)

Initializes all the ground with one material.

bool Gfx::CTerrain::GenerateMaterials ( int *  id,
float  min,
float  max,
float  slope,
float  freq,
Math::Vector  center,
float  radius 
)

Generates a level in the terrain.

void Gfx::CTerrain::FlushRelief ( )

Clears the relief, resources and all other associated data.

bool Gfx::CTerrain::LoadRelief ( const std::string &  fileName,
float  scaleRelief,
bool  adjustBorder 
)

Load relief from image.

The image must be 24 bits/pixel and dx x dy in size with dx = dy = (mosaic*brick)+1

bool Gfx::CTerrain::RandomizeRelief ( )

Load ramdomized relief.

bool Gfx::CTerrain::LoadResources ( const std::string &  fileName)

Load resources from image.

The image must be 24 bits/pixel and grayscale and dx x dy in size with dx = dy = (mosaic*brick)+1

bool Gfx::CTerrain::CreateObjects ( )

Creates all objects of the terrain within the 3D engine.

bool Gfx::CTerrain::Terraform ( const Math::Vector p1,
const Math::Vector p2,
float  height 
)

Modifies the terrain's relief.

ATTENTION: ok only with m_depth = 2!

void Gfx::CTerrain::SetWind ( Math::Vector  speed)

Management of the wind.

float Gfx::CTerrain::GetFineSlope ( const Math::Vector pos)

Gives the exact slope of the terrain at 2D (XZ) position.

float Gfx::CTerrain::GetCoarseSlope ( const Math::Vector pos)

Gives the approximate slope of the terrain at 2D (XZ) position.

bool Gfx::CTerrain::GetNormal ( Math::Vector n,
const Math::Vector p 
)

Gives the normal vector at 2D (XZ) position.

float Gfx::CTerrain::GetFloorLevel ( const Math::Vector pos,
bool  brut = false,
bool  water = false 
)

Returns the height of the ground level at 2D (XZ) position.

float Gfx::CTerrain::GetHeightToFloor ( const Math::Vector pos,
bool  brut = false,
bool  water = false 
)

Returns the distance to the ground level from 3D position.

bool Gfx::CTerrain::AdjustToFloor ( Math::Vector pos,
bool  brut = false,
bool  water = false 
)

Modifies the Y coordinate of 3D position to rest on the ground floor.

bool Gfx::CTerrain::AdjustToStandardBounds ( Math::Vector pos)

Adjusts 3D position so that it is within standard terrain boundaries.

Parameters
posposition to adjust
Returns
false if the initial coordinate was outside terrain area; true otherwise
bool Gfx::CTerrain::AdjustToBounds ( Math::Vector pos,
float  margin 
)

Adjusts 3D position so that it is within terrain boundaries and the given margin.

Parameters
posposition to adjust
marginmargin to the terrain border
Returns
false if the initial coordinate was outside terrain area; true otherwise
TerrainRes Gfx::CTerrain::GetResource ( const Math::Vector pos)

Returns the resource type available underground at 2D (XZ) position.

void Gfx::CTerrain::FlushBuildingLevel ( )

Empty the table of elevations.

bool Gfx::CTerrain::AddBuildingLevel ( Math::Vector  center,
float  min,
float  max,
float  height,
float  factor 
)

Adds a new elevation for a building.

bool Gfx::CTerrain::UpdateBuildingLevel ( Math::Vector  center)

Updates the elevation for a building when it was moved up (after a terraforming)

bool Gfx::CTerrain::DeleteBuildingLevel ( Math::Vector  center)

Removes the elevation for a building when it was destroyed.

float Gfx::CTerrain::GetBuildingFactor ( const Math::Vector pos)

Returns the influence factor whether a position is on a possible rise.

float Gfx::CTerrain::GetHardness ( const Math::Vector pos)

Returns the hardness of the ground in a given place.

int Gfx::CTerrain::GetMosaicCount ( )

Returns number of mosaics.

int Gfx::CTerrain::GetBrickCount ( )

Returns number of bricks in mosaic.

float Gfx::CTerrain::GetBrickSize ( )

Returns brick size.

float Gfx::CTerrain::GetReliefScale ( )

Returns the vertical scale of relief.

void Gfx::CTerrain::ShowFlatGround ( Math::Vector  pos)

Shows the flat areas on the ground.

float Gfx::CTerrain::GetFlatZoneRadius ( Math::Vector  center,
float  max 
)

Calculates the radius of the largest flat area available.

void Gfx::CTerrain::SetFlyingMaxHeight ( float  height)

Management of the global max flying height.

void Gfx::CTerrain::FlushFlyingLimit ( )

Empty the table of flying limits.

void Gfx::CTerrain::AddFlyingLimit ( Math::Vector  center,
float  extRadius,
float  intRadius,
float  maxHeight 
)

Adds a new flying limit.

float Gfx::CTerrain::GetFlyingLimit ( Math::Vector  pos,
bool  noLimit 
)

Returns the maximum height of flight.

bool Gfx::CTerrain::AddReliefPoint ( Math::Vector  pos,
float  scaleRelief 
)
protected

Adds a point of elevation in the buffer of relief.

void Gfx::CTerrain::AdjustRelief ( )
protected

Adjust the edges of each mosaic to be compatible with all lower resolutions.

Math::Vector Gfx::CTerrain::GetVector ( int  x,
int  y 
)
protected

Calculates a vector of the terrain.

VertexTex2 Gfx::CTerrain::GetVertex ( int  x,
int  y,
int  step 
)
protected

Calculates a vertex of the terrain.

Calculates an averaged normal, taking into account the six adjacent triangles:

  ^ y
  |
  b---c---+
  |\  |\  |
  |  \|  \|
  a---o---d
  |\  |\  |
  |  \|  \|
  +---f---e--> x
bool Gfx::CTerrain::CreateMosaic ( int  ox,
int  oy,
int  step,
int  objRank,
const Material mat 
)
protected

Creates all objects of a mosaic.

The origin of mosaic is its center.

  ^ z
  |
  |  2---4---6--
  |  |\  |\  |\
  |  |  \|  \|
  |  1---3---5--- ...
  |
  +-------------------> x
bool Gfx::CTerrain::CreateSquare ( int  x,
int  y 
)
protected

Creates all objects in a mesh square ground.

CTerrain::TerrainMaterial * Gfx::CTerrain::FindMaterial ( int  id)
protected

Seeks a material based on its ID.

int Gfx::CTerrain::FindMaterialByNeighbors ( char *  mat)
protected

Seeks a material based on neighbor values.

void Gfx::CTerrain::GetTexture ( int  x,
int  y,
std::string &  name,
Math::Point uv 
)
protected

Returns the texture name and UV coords to use for a given square.

float Gfx::CTerrain::GetHeight ( int  x,
int  y 
)
protected

Returns the height of the terrain.

bool Gfx::CTerrain::CheckMaterialPoint ( int  x,
int  y,
float  min,
float  max,
float  slope 
)
protected

Decide whether a point is using the materials.

void Gfx::CTerrain::SetMaterialPoint ( int  x,
int  y,
int  id,
char *  mat 
)
protected

Modifies the state of a point and its four neighbors, without testing if possible.

bool Gfx::CTerrain::ChangeMaterialPoint ( int  x,
int  y,
int  id 
)
protected

Modifies the state of a point.

bool Gfx::CTerrain::CondChangeMaterialPoint ( int  x,
int  y,
int  id,
char *  mat 
)
protected

Tests if a material can give a place, according to its four neighbors. If yes, puts the point.

void Gfx::CTerrain::InitMaterialPoints ( )
protected

Initializes material points array.

void Gfx::CTerrain::FlushMaterialPoints ( )
protected

Clears the material points.

void Gfx::CTerrain::AdjustBuildingLevel ( Math::Vector p)
protected

Adjusts a position according to a possible rise.

Member Data Documentation

std::vector<float> Gfx::CTerrain::m_relief
protected

Relief data points.

std::vector<unsigned char> Gfx::CTerrain::m_resources
protected

Resources data.

std::vector<int> Gfx::CTerrain::m_textures
protected

Texture indices.

std::vector<int> Gfx::CTerrain::m_objRanks
protected

Object ranks for mosaic objects.

int Gfx::CTerrain::m_mosaicCount
protected

Number of mosaics (along one dimension)

int Gfx::CTerrain::m_brickCount
protected

Number of bricks per mosaic (along one dimension)

int Gfx::CTerrain::m_materialPointCount
protected

Number of terrain material dots (along one dimension)

float Gfx::CTerrain::m_brickSize
protected

Size of single brick (along X and Z axis)

float Gfx::CTerrain::m_scaleRelief
protected

Vertical (relief) scale.

int Gfx::CTerrain::m_textureSubdivCount
protected

Subdivision of material points in mosaic.

int Gfx::CTerrain::m_depth
protected

Number of different resolutions (1,2,3,4)

float Gfx::CTerrain::m_textureScale
protected

Scale of texture mapping.

float Gfx::CTerrain::m_vision
protected

Vision before a change of resolution.

std::string Gfx::CTerrain::m_texBaseName
protected

Base name for single texture.

std::string Gfx::CTerrain::m_texBaseExt
protected

Extension for single texture.

float Gfx::CTerrain::m_defaultHardness
protected

Default hardness for level material.

std::vector<TerrainMaterial> Gfx::CTerrain::m_materials
protected

Terrain materials.

std::vector<TerrainMaterialPoint> Gfx::CTerrain::m_materialPoints
protected

Material for terrain points.

bool Gfx::CTerrain::m_useMaterials
protected

True if using terrain material mapping.

int Gfx::CTerrain::m_maxMaterialID
protected

Maximum level ID (no ID is >= to this)

int Gfx::CTerrain::m_materialAutoID
protected

Internal counter for auto generation of material IDs.

Math::Vector Gfx::CTerrain::m_wind
protected

Wind speed.

float Gfx::CTerrain::m_flyingMaxHeight
protected

Global flying height limit.

std::vector<FlyingLimit> Gfx::CTerrain::m_flyingLimits
protected

List of local flight limits.


The documentation for this class was generated from the following files: