CellData.h
1 /************************************************************
2 * (C) Voxel Farm Inc. 2015
3 */
4 
5 #pragma once
6 
7 #include "FastQuadrics.h"
8 #include "mapindex.h"
9 #include "Vector.h"
10 #include "VoxelLayer.h"
11 #include "MaterialLibrary.h"
12 #include "ExternalMutex.h"
13 #include "CellSeams.h"
14 #include <map>
15 #include <vector>
16 #include <list>
17 
18 namespace VoxelFarm
19 {
23  const int BILLBOARD_MAX_LEVEL_TYPE1 = 10;
25  const int BILLBOARD_MAX = 50000;
27  const int MAX_VERTICES = 100000;
29  const int BILLBOARD_CACHE_LEVELS = LOD_0 + 2;
34 
37  {
51  float rotation;
53  unsigned int billboardLight;
57  bool enabled;
58  };
59 
61  bool BillboardSort(const BillboardPackData* a, const BillboardPackData* b);
62 
64  typedef TVector<BillboardPackData> BillboardCacheList;
65 
68  {
69  unsigned int timeStamp;
70  BillboardCacheList* list;
71  bool deleted;
72  };
73 
76  {
77  public:
79  ~CBillboardCache();
80  public:
84  void setBillboards(CellId cell, BillboardCellCache* list);
86  void deleteCache(CellId cell);
88  void moveToGarbage(CellId cell);
90  void updateTimeStamp();
92  void collectGarbage();
93  public:
97  TMap<CellId, BillboardCellCache*> cache;
99  TSet<CellId> garbage;
101  TList<BillboardCellCache*> deadCells;
104  };
105 
107  class CCellData
108  {
109  public:
111  const static int MEDIUM_MAX = 4;
113  const static int MEDIUM_SOLID = 0;
115  const static int MEDIUM_FOLIAGE = 1;
117  const static int MEDIUM_PREVIEW = 2;
119  const static int MEDIUM_WATER = 3;
121  struct UV
122  {
123  float u, v;
124  };
126  struct CVert
127  {
128  float x, y, z;
129  };
132  {
133  public:
134  CInstanceMesh() :
135  vertices(NULL),
136  normals(NULL),
137  uv(NULL),
138  faces(NULL),
139  faceCount(0),
140  vertexCount(0),
141  textureSlice(0),
142  reference(),
143  sliceName("")
144  {};
145  public:
147  CVert* vertices;
151  UV* uv;
153  int* faces;
163  String sliceName;
164  };
166  static const int INSTANCE_LOD = 4;
169  {
170  public:
172  {
173  for (int i = 0; i < INSTANCE_LOD; i++)
174  {
175  lodMap[i] = 0;
176  }
177  }
182  };
183  public:
184  CCellData(CellId cellId);
185  virtual ~CCellData(void);
186  bool deleted;
187  public:
189  void loadFromBuffer();
190  public:
196  unsigned int* normals[MEDIUM_MAX];
198  unsigned int* materials[MEDIUM_MAX];
200  unsigned char* materialChannels[MEDIUM_MAX][3];
205  public:
207  void releaseData();
208  public:
214  int mapwidth;
220  bool* uvSet;
234  unsigned int* billboardLightColor;
246  unsigned int* lightColor[MEDIUM_MAX];
250  double snowHeight;
264  unsigned short* instanceFaces;
265 
266  bool inuse;
267 
268  //CSeamCellData* seamData;
269  bool seamCreated[3][2];
270  bool seamBaked[3][2];
271  bool seamDisplay[3][2];
272  CFastQuadrics seamMeshes[3][2][MEDIUM_MAX];
273 
276 
277  CVert* seamVerts[MEDIUM_MAX];
278  CVert* seamVertNormals[MEDIUM_MAX];
279  int* seamMaterials[MEDIUM_MAX];
280  unsigned int* seamLightColor[MEDIUM_MAX];
281 
282  int computedSeamFaceCount[MEDIUM_MAX][2][3];
283  CVert* computedSeamVerts[MEDIUM_MAX][2][3];
284  CVert* computedSeamVertNormals[MEDIUM_MAX][2][3];
285  CVert* computedSeamFaceNormals[MEDIUM_MAX][2][3];
286  unsigned int* computedSeamMaterials[MEDIUM_MAX][2][3];
287  unsigned int* computedSeamLightColor[MEDIUM_MAX][2][3];
288  unsigned char* computedSeamMaterialChannels[MEDIUM_MAX][2][3][3];
289  public:
291  {
292  public:
293  ThreadContext();
294  virtual ~ThreadContext();
295  BillboardPackData* billboardData;
296  bool* billboardsCached;
297  BillboardCellCache** billboardCacheList;
298  CellId* billboardCacheCells;
299  CCellData::CVert* billboardCellMin;
300  CCellData::CVert* billboardCellMax;
301  bool** billboardOccupancy0;
302  bool** billboardOccupancy1;
303  Algebra::Vector* billboardTriangles;
304  Algebra::Vector* billboardNorm;
305  float* ambient;
306  float* ambientBleed;
307  };
308  public:
310  virtual void processMesh(CMaterialLibrary* materialLibrary, CBillboardCache* billboardCache, ThreadContext* tc, LODStats stats);
312  static int getMaterialMedium(CMaterial& materialDefinition, int level)
313  {
314  int medium = materialDefinition.medium;
315  /*
316  if (medium == CCellData::MEDIUM_FOLIAGE && level >= BILLBOARD_MAX_LEVEL_TYPE1 - 2)
317  medium = CCellData::MEDIUM_SOLID;
318  */
319  return medium;
320  }
321  static int cellCount;
322  };
323 
325  extern TMap<int, CCellData::CInstanceMeshLOD*> instanceMeshes;
326 
328  extern TMap<String, int> instanceSliceMap;
329 
331  CCellData::CInstanceMeshLOD* loadInstanceMesh(char* filename);
332 }
CVert * faceNormals[MEDIUM_MAX]
List of face normals.
Definition: CellData.h:226
int instanceFaceCount
Number of faces for material instances in the cell.
Definition: CellData.h:256
Algebra::Vector billboardPos
Billboard's position within the cell (ranges from 0 to 1)
Definition: CellData.h:47
CVert reference
Origin point within the mesh. This point will be used to anchor the mesh.
Definition: CellData.h:155
static const int MEDIUM_FOLIAGE
This medium is used for foliage.
Definition: CellData.h:115
CFastQuadrics seamMesh[MEDIUM_MAX][2][3]
A mesh containing the polygons in the Cell seam before simplification.
Definition: CellData.h:275
int vertexCount[MEDIUM_MAX]
Number of vertices in cell.
Definition: CellData.h:204
int faceCount
Number of faces in the mesh.
Definition: CellData.h:157
static const int MEDIUM_PREVIEW
This medium is use for a placement preview.
Definition: CellData.h:117
int faceCount[MEDIUM_MAX]
Number of triangles in cell.
Definition: CellData.h:202
int timeStamp
Current time vector for the cache system.
Definition: CellData.h:103
Stores all LOD meshes for one instance.
Definition: CellData.h:168
UV * billboardSizes
List of billboard dimensions.
Definition: CellData.h:236
const int BILLBOARD_MAX
Maximum billboard and material instances in one cell.
Definition: CellData.h:25
Contains all classes and functions for the VoxelFarm engine.
void setBillboards(CellId cell, BillboardCellCache *list)
Sets the list of billboards for the specified cell.
const int BILLBOARD_OCCUPANCY_GRID_1
Billboard cache density for type 1 billboards (foliage)
Definition: CellData.h:33
CVert * billboardVertices
List of billboard vertices.
Definition: CellData.h:228
Contains all materials available in the system.
const int BILLBOARD_MAX_LEVEL_TYPE0
Maximum LOD to include type 0 billboards (grass)
Definition: CellData.h:21
static const int MEDIUM_WATER
This medium is use for glass and transparent solids.
Definition: CellData.h:119
Pair of texture coordinates.
Definition: CellData.h:121
A 3D Vector.
Definition: Vector.h:34
CVert * vertices
Array of vertices in the mesh.
Definition: CellData.h:144
int lodMap[INSTANCE_LOD]
Specifies which mesh should be each for each LOD.
Definition: CellData.h:179
int mapwidth
Width in pixels of the diffuse map for the Cell.
Definition: CellData.h:214
ExternalMutex::Mutex lock
A critical section to protect the cache.
Definition: CellData.h:95
int * faces
Array of faces as indices to vertices.
Definition: CellData.h:153
CFastQuadrics mesh[MEDIUM_MAX]
A mesh containing the polygons that appear in the Cell.
Definition: CellData.h:194
TMap< CellId, BillboardCellCache * > cache
This map links each cell to its cache.
Definition: CellData.h:97
static const int MEDIUM_MAX
Number of different mediums in the world. Each medium may be subject to a different rendering logic...
Definition: CellData.h:111
An entry in the billboard cache.
Definition: CellData.h:67
String sliceName
Identifier for the texture map used by the mesh.
Definition: CellData.h:163
int medium
Indentifies to which medium the material belongs to.
int instanceVertCount
Number of vertices for material instances in the cell.
Definition: CellData.h:254
unsigned int * billboardLightColor
List of billboard light colors.
Definition: CellData.h:234
double distanceToViewer
Distance to the viewer when the billboard or material instance was created.
Definition: CellData.h:55
Structure used to cache billboards and material instances.
Definition: CellData.h:36
const int BILLBOARD_OCCUPANCY_GRID_0
Billboard cache density for type 0 billboards (grass and ground instances)
Definition: CellData.h:31
unsigned __int64 CellId
A 64bit integer that identifies a single world octree Cell.
Definition: mapindex.h:23
CVert * vertNormals[MEDIUM_MAX]
List of smoothed vertex normals.
Definition: CellData.h:224
int billboardCount
Number of billboard objects found in the Cell.
Definition: CellData.h:218
int normalMapWidth
Width in pixels of the normal map for the Cell.
Definition: CellData.h:210
TMap< String, int > instanceSliceMap
A map that links the string form ID for texture map in material instance meshes to the numeric ID tha...
Stores the information for a Cell that has already been converted to a polygonal mesh.
Definition: CellData.h:107
static int getMaterialMedium(CMaterial &materialDefinition, int level)
Returns medium identifier based on a material definition.
Definition: CellData.h:312
CVert * instanceUV
Array containing material instance UV coordinates.
Definition: CellData.h:262
double snowHeight
Height of the snow layer for this cell.
Definition: CellData.h:250
void deleteCache(CellId cell)
Deletes the cached billboards for the specified cell.
CVert * billboardLightDir
List of billboard light directions.
Definition: CellData.h:232
virtual void processMesh(CMaterialLibrary *materialLibrary, CBillboardCache *billboardCache, ThreadContext *tc, LODStats stats)
Computes normals, materials and billboards for the Cell.
void loadFromBuffer()
Loads the cell contents from a buffer. This is used by the offline mode of VoxelFarm.
Defines a mesh that can be instanced relative to a face in the mesh.
Definition: CellData.h:131
unsigned int * materials[MEDIUM_MAX]
List of materials to be sent to the GPU.
Definition: CellData.h:198
void moveToGarbage(CellId cell)
Declares the cell billboards can be disposed in any time.
int vertexCount
Number of vertices in the mesh.
Definition: CellData.h:159
const int BILLBOARD_MAX_LEVEL_TYPE1
Maximum LOD to include type 1 billboards (foliage)
Definition: CellData.h:23
StatTracker LODStats[LEVELS][STAT_NULL]
Used to track stats across all LOD.
Definition: VoxelLayer.h:530
CVert * vertices[MEDIUM_MAX]
List of vertices in the Cell.
Definition: CellData.h:222
int billboardType
Billboard type (0 - grass, 1 - foliage, 3 - material instance)
Definition: CellData.h:43
CVert * lightDir[MEDIUM_MAX]
Light directions for vertices in the Cell.
Definition: CellData.h:244
bool enabled
Specified whether this occurence should be actually used.
Definition: CellData.h:57
CVert * instanceVertices
Array containing material instance vertices.
Definition: CellData.h:258
CVert * instanceNormals
Array containing material instance normals.
Definition: CellData.h:260
TMap< int, CCellData::CInstanceMeshLOD * > instanceMeshes
A map containing all the material instance meshes.
int generation
Age of the Cell. This is used by the Cell garbage collection system.
Definition: CellData.h:240
const int BILLBOARD_CACHE_LEVELS
Maximum LOD that will have its billboards and material instances cached.
Definition: CellData.h:29
void collectGarbage()
Disposes information for cells that are not any longer needed.
static const int INSTANCE_LOD
Number of LOD stored for each instance type.
Definition: CellData.h:166
float billboardHeight
Specifies billboard size.
Definition: CellData.h:45
TSet< CellId > garbage
Set of cells to be soon disposed.
Definition: CellData.h:99
TVector< BillboardPackData > BillboardCacheList
An array of billboard information.
Definition: CellData.h:64
unsigned int * lightColor[MEDIUM_MAX]
Light color for vertices in the Cell.
Definition: CellData.h:246
const int MAX_VERTICES
Maximum vertices in one cell.
Definition: CellData.h:27
unsigned char * materialChannels[MEDIUM_MAX][3]
List of materials to be sent to the GPU.
Definition: CellData.h:200
CVert * normals
Array of normals in the mesh.
Definition: CellData.h:149
int textureSlice
Index of the texture map used by the mesh.
Definition: CellData.h:161
unsigned int billboardLight
Light collected by the billboard.
Definition: CellData.h:53
CellId cell
Cell identifier.
Definition: CellData.h:192
void releaseData()
Releases core memory used to feed GPU data.
int billboardId
Identifies which billboard or material instance it is.
Definition: CellData.h:39
Algebra::Vector lastEyePos
Records the viewer position when the cell was created.
Definition: CellData.h:248
bool BillboardSort(const BillboardPackData *a, const BillboardPackData *b)
Sort criteria for billboards.
UV * billboardUV
List of billboard texture coordinates.
Definition: CellData.h:238
int instanceCount
Number of material instances in the cell.
Definition: CellData.h:252
int mapheight
Height in pixels of the normal map for the Cell.
Definition: CellData.h:216
CVert * billboardNormals
List of billboard normals.
Definition: CellData.h:230
unsigned short * instanceFaces
Array containing material instance face indices.
Definition: CellData.h:264
An efficient mesh representation that features Quadratic Error function simplification using a Multip...
Definition: FastQuadrics.h:44
TList< BillboardCellCache * > deadCells
List of cells to be disposed.
Definition: CellData.h:101
float rotation
Billboard's rotation around its normal.
Definition: CellData.h:51
bool * uvSet
Specifies whether the Cell vertex has UV mapping.
Definition: CellData.h:220
Algebra::Vector billboardNormal
Billboard's normal vector.
Definition: CellData.h:49
CInstanceMesh meshes[INSTANCE_LOD]
Array of meshes, one per LOD.
Definition: CellData.h:181
static const int MEDIUM_SOLID
This medium is used for solid world geometry.
Definition: CellData.h:113
A material definition for the VoxelFarm.com engine.
unsigned int * normals[MEDIUM_MAX]
List of normals to be sent to the GPU.
Definition: CellData.h:196
int normalMapHeight
Height in pixels of the normal map for the Cell.
Definition: CellData.h:212
int UVFacesCount[MEDIUM_MAX]
Number of faces that received UV coordinates.
Definition: CellData.h:242
BillboardCellCache * getBillboards(CellId cell)
Retrieves the list of billboards for teh specified cell.
Keeps an billboard cache entry for each cell that had its billboards and material instances generated...
Definition: CellData.h:75
CCellData::CInstanceMeshLOD * loadInstanceMesh(char *filename)
Loads all LOD for an instance mesh.
void updateTimeStamp()
Moves forward the time vector used to compue cache TTLs.
int materialId
Identifies on which material the billboard or material instance appears.
Definition: CellData.h:41