Biome.h
1 /************************************************************
2 * (C) Voxel Farm Inc. 2015
3 */
4 
5 #pragma once
6 
7 #include <vector>
8 #include <string>
9 #include "VoxelLayer.h"
10 #include "TileSet.h"
11 #include "MaterialLibrary.h"
12 #include "Vector.h"
13 
14 namespace VoxelFarm
15 {
17  const double HEIGHT_INFINITE = 1e+20;
19  const double CAVE_MAX_HEIGHT = 500.0;
21  const double CAVE_TOP_MIN_HEIGHT = 150.0;
22 
25  {
26  public:
30  ITileSet* tileSet,
32  int tilePeriod,
34  double tileAmplitude,
36  int noiseType,
38  double noiseAmplitude,
40  double noiseFreq,
42  double noiseStep,
44  double noiseLacunarity,
46  int noiseOctaves);
47  public:
49  double getHeight(double x, double z);
51  double getHeight(double x, double z, double& noiseHeight, double& tileHeight, double& tileHeightMinFiltered, double& tileHeightMaxFiltered);
53  unsigned int getHeight(int x, int z);
54  public:
55  ITileSet* tileSet;
56  int tilePeriod;
57  double tileAmplitude;
58  int noiseType;
59  double noiseAmplitude;
60  double noiseFreq;
61  double noiseStep;
62  double noiseLacunarity;
63  int noiseOctaves;
64  };
65 
67  typedef enum InstanceMirror_T
68  {
69  m_N,
70  m_X,
71  m_Y,
72  m_Z,
74 
75  typedef enum InstanceRotation_T
76  {
77  n_R, // no rotation
80  RX_180,
81  RX_270,
84  RY_180,
85  RY_270,
88  RZ_180,
89  RZ_270,
90  } InstanceRotation;
91 
93  {
94  public:
96  double angleMin;
98  double angleMax;
100  TVector<String> classes;
102  TVector<int*> materialMaps;
104  double minHeight;
106  double maxHeight;
108  int maskType;
110  double maskFreq;
116  double maskStep;
118  double maskScaleX;
120  double maskScaleY;
122  double maskScaleZ;
124  double maskPhaseX;
126  double maskPhaseY;
128  double maskPhaseZ;
130  double maskClampMin;
132  double maskClampMax;
134  double decimate;
135 
136  double shiftMin;
137  double shiftMax;
138 
139  bool mirrorX;
140  bool mirrorY;
141  bool mirrorZ;
142  InstanceMirror getRandomMirror(int random);
143  bool rotateX;
144  bool rotateY;
145  bool rotateZ;
146  InstanceRotation getRandomRotation(int random);
147  };
148 
150  class CBiome : public IMacroColorSource
151  {
152  public:
154  CBiome(
160  int maskType,
162  double maskFreq,
164  double maskStep,
166  double maskLacunarity,
168  int maskOctaves,
170  double maskShift,
172  double maskClampMin,
174  double maskClampMax,
176  double snowHeight,
178  int caveFloorMaterial,
180  int caveWallMaterial);
181  virtual ~CBiome() {}
182  public:
183  void setMacroColor(unsigned char* colors);
184  virtual void getColor(double x, double y, double z, unsigned char& r, unsigned char& g, unsigned char& b);
185  public:
191  int maskType;
193  double maskFreq;
195  double maskStep;
201  double maskShift;
203  double maskClampMin;
205  double maskClampMax;
207  double snowHeight;
213  unsigned char* macroColorMap;
214  public:
216  TVector<CPlantingRule> plantingRules;
218  TVector<int> materials;
219  public:
221  void addPlantingRule(CPlantingRule rule);
224  void addMaterial(CMaterialLibrary* materialLibrary, int id);
226  double getMaskValue(double x, double z);
228  bool getCaveProfile(
230  double x,
232  double z,
234  double noiseHeight,
236  double tileHeight,
238  double tileHeightMin,
240  double tileHeightMax,
242  double displacement,
244  double caveBottomDisplacement,
246  double& height,
248  double& caveBottom,
250  double& caveTop);
251  };
252 
255  {
256  public:
257  CHeightmap();
258  public:
260  void addBiome(CBiome* biome);
262  double getValue(double x, double z);
264  double getValue(double x, double z, CBiome* &biome);
266  double getValue(double x, double z, CBiome* &biome, double& noiseHeight, double& tileHeight, double& tileHeightMinFiltered, double& tileHeightMaxFiltered);
268  unsigned int getValue(int x, int z);
270  unsigned int getValue(int x, int z, CBiome* &topBiome);
272  CBiome* getBiome(int x, int z);
273  public:
274  TVector<CBiome*> biomes;
275  };
276 
279  {
280  public:
283  CMaterialLibrary* materialLibrary,
285  CHeightmap* heightmap,
286  IMask* instanceMask,
287  IMask* waterMask,
288  int downsampling);
289  public:
290  CMaterialLibrary* materialLibrary;
291  CHeightmap* heightmap;
292  IMask* instanceMask;
293  IMask* waterMask;
294  int downsampling;
295  CBiome* underwaterBiome;
296  public:
298  virtual void getContourData(
300  CellId cell,
302  ContourVoxelData* data,
304  bool& empty,
305  void* threadContext) override;
306  virtual void* createThreadContext() override;
307  virtual void disposeThreadContext(void* threadContext) override;
308  int getStatsContourId() override
309  {
310  return STAT_TERRAIN;
311  }
312  protected:
316  CellId cell,
318  ContourVoxelData* data,
320  bool& empty,
321  void* threadContext);
325  CellId cell,
327  ContourVoxelData* data,
329  bool& empty,
330  void* threadContext);
331  public:
333  {
334  public:
336  {
337  int minY, maxY;
338  };
339  public:
340  float* field;
341  float* fieldLowRes;
342  MaterialId* materialLowRes;
343  HeightCachePoint* heightCache;
344  };
345  public:
346  const static int CornerMap[8][3];
347  const static int EdgeMap[12][2];
348  public:
349  static inline double biLinearInterpolate(double p[2][2], double x, double y);
350  static inline double getDisplacementMapValue(unsigned char* displacementData, int displacementDataSize, double x, double y);
351  static inline double getDisplacement(unsigned char* displacementData, int displacementDataSize, double pos[3], Algebra::Vector& normal, double freq);
352  };
353 
355  {
356  public:
357  class CBiome
358  {
359  public:
360  class CLayer
361  {
362  public:
363  CLayer(
364  unsigned char* aMask,
365  float aMaskCutMin,
366  float aMaskCutMax,
367  unsigned char* aDisplacement,
368  int aDisplacementMapSize,
369  double aDisplacementFrequency,
370  int aDisplacementHeight,
371  int aDisplacementShift,
372  MaterialId aMaterial) :
373  mask(aMask),
374  maskCutMin(aMaskCutMin),
375  maskCutMax(aMaskCutMax),
376  displacement(aDisplacement),
377  displacementMapSize(aDisplacementMapSize),
378  displacementFrequency(aDisplacementFrequency),
379  displacementHeight(aDisplacementHeight),
380  displacementShift(aDisplacementShift),
381  material(aMaterial) {};
382  unsigned char* mask;
383  float maskCutMin;
384  float maskCutMax;
385  unsigned char* displacement;
386  int displacementMapSize;
387  double displacementFrequency;
388  int displacementHeight;
389  int displacementShift;
390  MaterialId material;
391 
392  public:
394  TVector<CPlantingRule> plantingRules;
397  {
398  plantingRules.push_back(rule);
399  }
400  };
401  void setLayers();
402  CLayer** layers;
403  int layerCount;
404  int mapSize;
405  unsigned short* heightmap;
406  int heightMapSize;
407  double heightShift;
408  double heightMax;
409  double heightmapFreq;
410  double terraceSize;
411  public:
412  CBiome(
413  unsigned short* heightmap,
414  int heightMapSize,
415  double heightShift,
416  double heightMax,
417  double heightmapFreq,
418  CLayer** layers,
419  int count,
420  int mapSize,
421  double terraceSize);
422  ~CBiome();
423  };
424 
425  double getHeight(double x, double z, CBiome* &topBiome, CBiome::CLayer* &topLayer);
426  void getHeightPage(double xo, double zo, double pageLength, int pageSize, double* heights);
427  double* odds;
428  CBiome** biomes;
429  unsigned char* probabilityMap;
430  int probabilityMapSize;
431  int biomeCount;
432  double tileFrequency;
433  int biomeFrequency;
434  int biomeDistribution[256];
435  public:
437  const double* odds,
438  CBiome** biomes,
439  int count,
440  double tileFrequency,
441  int biomeFrequency,
442  unsigned char* probabilityMap,
443  int probabilityMapSize);
444  ~CSimplexWorld();
445  };
446 
448  {
449  public:
450  CSimplexWorldVoxelLayer(CSimplexWorld* aworld) : world(aworld) {};
451  CSimplexWorld* world;
452  public:
454  virtual void getContourData(
456  CellId cell,
458  ContourVoxelData* data,
460  bool& empty,
461  void* threadContext) override;
462  virtual void* createThreadContext() override
463  {
464  return VF_NEW ThreadContext();
465  };
466  virtual void disposeThreadContext(void* threadContext) override
467  {
468  VF_DELETE(ThreadContext*)threadContext;
469  };
470  int getStatsContourId() override
471  {
472  return STAT_TERRAIN;
473  }
474  public:
476  {
477  public:
478  static const int BUFFER_SIZE = BLOCK_SIZE + 1;
479  float biomeStrength[BUFFER_SIZE][BUFFER_SIZE][4][4];
480  unsigned char biome[BUFFER_SIZE][BUFFER_SIZE][4][4];
481  unsigned char biomeProb[BUFFER_SIZE][BUFFER_SIZE];
482  double heights[BUFFER_SIZE][BUFFER_SIZE];
483  float mask[BUFFER_SIZE][BUFFER_SIZE];
484  float displacement[BUFFER_SIZE][BUFFER_SIZE];
485  float workBuffer[BUFFER_SIZE][BUFFER_SIZE];
486  double finalHeights[BUFFER_SIZE][BUFFER_SIZE];
487  MaterialId material[BUFFER_SIZE][BUFFER_SIZE];
488  MaterialId finalMaterial[BUFFER_SIZE][BUFFER_SIZE];
489  bool biomeInUse[256];
490  };
491  };
492 
493  template <typename T> void expandInterpolationKernel(T* dest, T* source, int size)
494  {
495  for (int z = 0; z < size; z++)
496  {
497  for (int x = 0; x < size; x++)
498  {
499  for (int dz = 0; dz < 2; dz++)
500  {
501  for (int dx = 0; dx < 2; dx++)
502  {
503  int nx = x + dx;
504  if (nx >= size)
505  {
506  nx = 0;
507  }
508  if (nx < 0)
509  {
510  nx = size - 1;
511  }
512 
513  int nz = z + dz;
514  if (nz >= size)
515  {
516  nz = 0;
517  }
518  if (nz < 0)
519  {
520  nz = size - 1;
521  }
522 
523  int idx0 = 4 * (z*size + x) + 2 * dz + dx;
524  int idx1 = nz*size + nx;
525  dest[idx0] = source[idx1];
526  dest[idx0] = source[idx1];
527  }
528  }
529  }
530  }
531  };
532 
533 }
double maskShift
Adds a global shift to the mask.
Definition: Biome.h:201
virtual void getContourData(CellId cell, ContourVoxelData *data, bool &empty, void *threadContext) override
Returns voxel data for the specified cell (IVoxelLayer)
double getValue(double x, double z)
Returns the height for the provided point in the XZ plane as a double precision. This method will per...
double minHeight
Minimum world height required for the scattering to happen.
Definition: Biome.h:104
int maskOctaves
Number of octaves in the mask signal.
Definition: Biome.h:199
TVector< CPlantingRule > plantingRules
List of instance planting rules for the biome.
Definition: Biome.h:216
virtual void disposeThreadContext(void *threadContext) override
Destroys the specified thread context.
double maskStep
Mask noise step (amplitude mutiplier on each octave)
Definition: Biome.h:116
void addBiome(CBiome *biome)
Adds a biome to the heightmap.
enum VoxelFarm::InstanceMirror_T InstanceMirror
Defines a rule for scattering instances.
virtual void * createThreadContext() override
The voxel layer can use this method to return a structure that will be unique for each calling thread...
Definition: Biome.h:462
double maskClampMin
Mask noise value clamp minimum.
Definition: Biome.h:130
Contains all classes and functions for the VoxelFarm engine.
double maskPhaseX
Mask noise phase along X axis.
Definition: Biome.h:124
Mirror on XZ/ZX plane...
Definition: Biome.h:72
CHeightLayer(ITileSet *tileSet, int tilePeriod, double tileAmplitude, int noiseType, double noiseAmplitude, double noiseFreq, double noiseStep, double noiseLacunarity, int noiseOctaves)
Constructs a HeightLayer object.
An interface for a 3D mask. A mask can be used to control the application of other features...
Definition: VoxelLayer.h:581
Contains all materials available in the system.
TVector< int * > materialMaps
List of material re-mappings.
Definition: Biome.h:102
const int BLOCK_SIZE
Actual number of voxels in one Cell Dim once the margin is considered.
Definition: VoxelLayer.h:38
double getHeight(double x, double z)
Returns the height for the provided point in the XZ plane as a double precision. This method will per...
double snowHeight
Height at which snow stats to appear for the biome.
Definition: Biome.h:207
int getStatsContourId() override
Returns the stats ID to be used for measuring performance of the getContourData() method...
Definition: Biome.h:470
A 3D Vector.
Definition: Vector.h:34
double maskFreq
Mask noise frequency.
Definition: Biome.h:110
no Mirror
Definition: Biome.h:70
double maskScaleX
Mask noise scale along X axis.
Definition: Biome.h:118
void getContourDataRegular(CellId cell, ContourVoxelData *data, bool &empty, void *threadContext)
Returns voxel data for the specified cell (IVoxelLayer)
int maskType
Type of mask. Only 0 (Perlin) is currently supported.
Definition: Biome.h:191
TVector< int > materials
List of terrain materials that appear in the biome.
Definition: Biome.h:218
int caveWallMaterial
Material that will be applied to cave walls.
Definition: Biome.h:211
Defines a type of biome. Several biomes can coexist in the same world. Each biome has a density mask ...
Definition: Biome.h:150
unsigned __int64 CellId
A 64bit integer that identifies a single world octree Cell.
Definition: mapindex.h:23
A Voxel Layer object that outputs terrain voxels based on a heightmap.
Definition: Biome.h:278
double decimate
Additional probablity that an instance will not be placed.
Definition: Biome.h:134
double angleMin
Minimum slope required for the scattering to happen.
Definition: Biome.h:96
void addPlantingRule(CPlantingRule rule)
Adds a new PlantingRule object.
An interface for a voxel layer. By implementing this interface, very different modules can contribute...
Definition: VoxelLayer.h:535
TVector< String > classes
List of classes that will be scattered.
Definition: Biome.h:100
void addPlantingRule(CPlantingRule rule)
Adds a new PlantingRule object.
Definition: Biome.h:396
double maskScaleY
Mask noise scale along Y axis.
Definition: Biome.h:120
virtual void disposeThreadContext(void *threadContext) override
Destroys the specified thread context.
Definition: Biome.h:466
void addMaterial(CMaterialLibrary *materialLibrary, int id)
TVector< CPlantingRule > plantingRules
List of instance planting rules for the layer.
Definition: Biome.h:394
double maskPhaseY
Mask noise phase along Y axis.
Definition: Biome.h:126
Rotations around Y axis.
Definition: Biome.h:83
Rotations around X axis.
Definition: Biome.h:79
int caveFloorMaterial
Material that will be applied to cave floors.
Definition: Biome.h:209
double maskFreq
Frequency of the mask signal.
Definition: Biome.h:193
const double CAVE_TOP_MIN_HEIGHT
Minimum depth of ground layer on top of a cave.
Definition: Biome.h:21
int maskType
Specified what type of mask noise should be used for scattering.
Definition: Biome.h:108
double maskClampMax
Maximum value for the mask. Values above this point will be considered one, and the remaining mask am...
Definition: Biome.h:205
A tileset with matching corners and two data channels.
Definition: TileSet.h:72
double angleMax
Maximum slope required for the scattering to happen.
Definition: Biome.h:98
CHeightLayer * heightLayer
A HeightLayer object that specifies the height values for the biome.
Definition: Biome.h:187
Defines the world elevation at any XZ point. Contains a collection of biomes.
Definition: Biome.h:254
double maskClampMin
Minimum value for the mask. Values below this point will be considered zero, and the remaining mask a...
Definition: Biome.h:203
int getStatsContourId() override
Returns the stats ID to be used for measuring performance of the getContourData() method...
Definition: Biome.h:308
virtual void * createThreadContext() override
The voxel layer can use this method to return a structure that will be unique for each calling thread...
double maskLacunarity
Increment in frequency from one octave to the next for the mask signal.
Definition: Biome.h:197
double maskScaleZ
Mask noise scale along Z axis.
Definition: Biome.h:122
CBiome(CHeightLayer *heightLayer, CCornerTileSet *caveLayer, int maskType, double maskFreq, double maskStep, double maskLacunarity, int maskOctaves, double maskShift, double maskClampMin, double maskClampMax, double snowHeight, int caveFloorMaterial, int caveWallMaterial)
Constructs a biome object.
virtual void getContourData(CellId cell, ContourVoxelData *data, bool &empty, void *threadContext) override
Returns voxel data for the specified cell (IVoxelLayer)
const double CAVE_MAX_HEIGHT
Maximum ceiling height for a cave.
Definition: Biome.h:19
CCornerTileSet * caveLayer
A tileset to be used for underground features like caves.
Definition: Biome.h:189
InstanceMirror_T
Defines a rule for scattering instances.
Definition: Biome.h:67
void getContourDataAdaptive(CellId cell, ContourVoxelData *data, bool &empty, void *threadContext)
Returns voxel data for the specified cell (IVoxelLayer)
One layer of an elevation map (heightmap). A VoxelFarm terrains starts with an elevation map...
Definition: Biome.h:24
double maskClampMax
Mask noise value clamp maximum.
Definition: Biome.h:132
double maxHeight
Maximum world height required for the scattering to happen.
Definition: Biome.h:106
unsigned char * macroColorMap
Contains the values read from the macro color map.
Definition: Biome.h:213
Mirror on YZ/ZY plane...
Definition: Biome.h:71
Rotations around Z axis.
Definition: Biome.h:87
InstanceRotation_T
Definition: Biome.h:75
int maskOctaves
Mask noise number of octaves.
Definition: Biome.h:112
const double HEIGHT_INFINITE
Maximum possible height.
Definition: Biome.h:17
bool getCaveProfile(double x, double z, double noiseHeight, double tileHeight, double tileHeightMin, double tileHeightMax, double displacement, double caveBottomDisplacement, double &height, double &caveBottom, double &caveTop)
Retrieves cave profile for the biome. Returns true if the spot is exposed to open air...
CHeightmapTerrain(CMaterialLibrary *materialLibrary, CHeightmap *heightmap, IMask *instanceMask, IMask *waterMask, int downsampling)
double maskPhaseZ
Mask noise phase along Z axis.
Definition: Biome.h:128
double maskLacunarity
Mask noise lacunarity (frequency mutiplier on each octave)
Definition: Biome.h:114
double getMaskValue(double x, double z)
Retrieves the mask value for the specified point in the world map.
CBiome * getBiome(int x, int z)
Retruns the top biome for the specified point.
double maskStep
Increment in amplitude from one octave to the next for the mask signal.
Definition: Biome.h:195