Loading...
Searching...
No Matches
Gfx::CTerrain Class Reference

Terrain loader/generator and manager. More...

`#include <src/graphics/engine/terrain.h>`

## Classes

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

## Public Member Functions

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

## Protected Member Functions

bool | AddReliefPoint (Math::Vector pos, float scaleRelief)
| Adds a point of elevation in the buffer of relief.
void | AdjustRelief ()
| Adjust the edges of each mosaic to be compatible with all lower resolutions.
Math::Vector | GetVector (int x, int y)
| Calculates a vector of the terrain.
VertexTex2 | GetVertex (int x, int y, int step)
| Calculates a vertex of the terrain.
bool | CreateMosaic (int ox, int oy, int step, int objRank, const Material &mat)
| Creates all objects of a mosaic.
bool | CreateSquare (int x, int y)
| Creates all objects in a mesh square ground.
TerrainMaterial * | FindMaterial (int id)
| Seeks a material based on its ID.
int | FindMaterialByNeighbors (char *mat)
| Seeks a material based on neighbor values.
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.
float | GetHeight (int x, int y)
| Returns the height of the terrain.
bool | CheckMaterialPoint (int x, int y, float min, float max, float slope)
| Decide whether a point is using the materials.
void | SetMaterialPoint (int x, int y, int id, char *mat)
| Modifies the state of a point and its four neighbors, without testing if possible.
bool | ChangeMaterialPoint (int x, int y, int id)
| Modifies the state of a point.
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.
void | InitMaterialPoints ()
| Initializes material points array.
void | FlushMaterialPoints ()
| Clears the material points.
void | AdjustBuildingLevel (Math::Vector &p)
| Adjusts a position according to a possible rise.

## Protected Attributes

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

## 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:

- by applying texture index table
- by specifying one or more "materials" that cover "material points"

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

## Generate()

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

Generates a new flat terrain.

## InitTextures()

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

Initializes the names of textures to use for the land.

## FlushMaterials()

void Gfx::CTerrain::FlushMaterials | ( |  | ) |

Clears all terrain materials.

## AddMaterial()

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.

## InitMaterials()

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

Initializes all the ground with one material.

## GenerateMaterials()

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.

## FlushRelief()

void Gfx::CTerrain::FlushRelief | ( |  | ) |

Clears the relief, resources and all other associated data.

## LoadRelief()

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

## RandomizeRelief()

bool Gfx::CTerrain::RandomizeRelief | ( |  | ) |

Load ramdomized relief.

## LoadResources()

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

## CreateObjects()

bool Gfx::CTerrain::CreateObjects | ( |  | ) |

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

## Terraform()

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!

## SetWind()

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

Management of the wind.

## GetFineSlope()

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

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

## GetCoarseSlope()

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

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

## GetNormal()

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

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

## GetFloorLevel()

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.

## GetHeightToFloor()

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

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

## AdjustToFloor()

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.

## AdjustToStandardBounds()

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

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

Parameters

  pos | position to adjust

Returns
  `false` if the initial coordinate was outside terrain area; `true` otherwise

## AdjustToBounds()

bool Gfx::CTerrain::AdjustToBounds | ( | Math::Vector & | pos,
|  | float | margin )

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

Parameters

  pos | position to adjust
  margin | margin to the terrain border

Returns
  `false` if the initial coordinate was outside terrain area; `true` otherwise

## GetResource()

TerrainRes Gfx::CTerrain::GetResource | ( | const Math::Vector & | pos | ) |

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

## FlushBuildingLevel()

void Gfx::CTerrain::FlushBuildingLevel | ( |  | ) |

Empty the table of elevations.

## AddBuildingLevel()

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

Adds a new elevation for a building.

## UpdateBuildingLevel()

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

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

## DeleteBuildingLevel()

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

Removes the elevation for a building when it was destroyed.

## GetBuildingFactor()

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

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

## GetHardness()

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

Returns the hardness of the ground in a given place.

## GetMosaicCount()

int Gfx::CTerrain::GetMosaicCount | ( |  | ) |

Returns number of mosaics.

## GetBrickCount()

int Gfx::CTerrain::GetBrickCount | ( |  | ) |

Returns number of bricks in mosaic.

## GetBrickSize()

float Gfx::CTerrain::GetBrickSize | ( |  | ) |

Returns brick size.

## GetReliefScale()

float Gfx::CTerrain::GetReliefScale | ( |  | ) |

Returns the vertical scale of relief.

## ShowFlatGround()

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

Shows the flat areas on the ground.

## GetFlatZoneRadius()

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

Calculates the radius of the largest flat area available.

## SetFlyingMaxHeight()

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

Management of the global max flying height.

## FlushFlyingLimit()

void Gfx::CTerrain::FlushFlyingLimit | ( |  | ) |

Empty the table of flying limits.

## AddFlyingLimit()

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

Adds a new flying limit.

## GetFlyingLimit()

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

Returns the maximum height of flight.

## AddReliefPoint()

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

protected

Adds a point of elevation in the buffer of relief.

## AdjustRelief()

void Gfx::CTerrain::AdjustRelief | ( |  | ) |

protected

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

## GetVector()

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

protected

Calculates a vector of the terrain.

## GetVertex()

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

## CreateMosaic()

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

## CreateSquare()

bool Gfx::CTerrain::CreateSquare | ( | int | x,
|  | int | y )

protected

Creates all objects in a mesh square ground.

## FindMaterial()

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

protected

Seeks a material based on its ID.

## FindMaterialByNeighbors()

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

protected

Seeks a material based on neighbor values.

## GetTexture()

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.

## GetHeight()

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

protected

Returns the height of the terrain.

## CheckMaterialPoint()

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

protected

Decide whether a point is using the materials.

## SetMaterialPoint()

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.

## ChangeMaterialPoint()

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

protected

Modifies the state of a point.

## CondChangeMaterialPoint()

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.

## InitMaterialPoints()

void Gfx::CTerrain::InitMaterialPoints | ( |  | ) |

protected

Initializes material points array.

## FlushMaterialPoints()

void Gfx::CTerrain::FlushMaterialPoints | ( |  | ) |

protected

Clears the material points.

## AdjustBuildingLevel()

void Gfx::CTerrain::AdjustBuildingLevel | ( | Math::Vector & | p | ) |

protected

Adjusts a position according to a possible rise.

## Member Data Documentation

## m_relief

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

protected

Relief data points.

## m_resources

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

protected

Resources data.

## m_textures

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

protected

Texture indices.

## m_objRanks

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

protected

Object ranks for mosaic objects.

## m_mosaicCount

int Gfx::CTerrain::m_mosaicCount

protected

Number of mosaics (along one dimension).

## m_brickCount

int Gfx::CTerrain::m_brickCount

protected

Number of bricks per mosaic (along one dimension).

## m_materialPointCount

int Gfx::CTerrain::m_materialPointCount

protected

Number of terrain material dots (along one dimension).

## m_brickSize

float Gfx::CTerrain::m_brickSize

protected

Size of single brick (along X and Z axis).

## m_scaleRelief

float Gfx::CTerrain::m_scaleRelief

protected

Vertical (relief) scale.

## m_textureSubdivCount

int Gfx::CTerrain::m_textureSubdivCount

protected

Subdivision of material points in mosaic.

## m_depth

int Gfx::CTerrain::m_depth

protected

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

## m_textureScale

float Gfx::CTerrain::m_textureScale

protected

Scale of texture mapping.

## m_vision

float Gfx::CTerrain::m_vision

protected

Vision before a change of resolution.

## m_texBaseName

std::string Gfx::CTerrain::m_texBaseName

protected

Base name for single texture.

## m_texBaseExt

std::string Gfx::CTerrain::m_texBaseExt

protected

Extension for single texture.

## m_defaultHardness

float Gfx::CTerrain::m_defaultHardness

protected

Default hardness for level material.

## m_materials

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

protected

Terrain materials.

## m_materialPoints

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

protected

Material for terrain points.

## m_useMaterials

bool Gfx::CTerrain::m_useMaterials

protected

True if using terrain material mapping.

## m_maxMaterialID

int Gfx::CTerrain::m_maxMaterialID

protected

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

## m_materialAutoID

int Gfx::CTerrain::m_materialAutoID

protected

Internal counter for auto generation of material IDs.

## m_wind

Math::Vector Gfx::CTerrain::m_wind

protected

Wind speed.

## m_flyingMaxHeight

float Gfx::CTerrain::m_flyingMaxHeight

protected

Global flying height limit.

## m_flyingLimits

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

protected

List of local flight limits.

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

- src/graphics/engine/terrain.h
- src/graphics/engine/terrain.cpp