Waterflow
Visualize water in terrain
Voxelgrid Class Reference

Handles the representation of the voxelgrid. More...

#include <voxel.h>

List of all members.

Public Member Functions

 Voxelgrid (DataHandler *handle, int64_t hashSize)
 Constructs an empty voxel grid.
 ~Voxelgrid ()
 Completely delete the sparse voxelgrid.
void drawVoxels (glm::mat4 projectionMatrix, glm::mat4 viewMatrix)
 Draw the voxel data.
void FloodFill (int x, int z, int height, bool fillDown=true)
 Floodfills pixels at the specified position and height.
GLint getHeight (int16_t x, int16_t z)
 Gets the height in waterHeight.
std::vector< GLint > * getHeightMap ()
 Returns a pointer to waterHeight.
neighsgetNeighbourhood (int16_t x, int16_t y, int16_t z)
 Get a voxel and the 26 neighbouring pixels.
neighsgetNeighbourhoodHash (int16_t x, int16_t y, int16_t z)
 Get a voxel and the 26 neighbouring pixels.
voxelgetVoxel (int16_t x, int16_t y, int16_t z)
 Get value of voxel at x,y,z.
std::vector< GLuint > * getVoxelPositions ()
 Create a data pointer to all existing voxels.
void hashAdd (int16_t x, int16_t y, int16_t z, bool filled, float a, float b)
 Adds a pixel using the hash functions.
voxelhashGet (int16_t x, int16_t y, int16_t z)
 Gets a pixel using the hash functions.
void hashInit ()
 Initializes the hashmap to the size specified by hashSize.
void initDraw ()
 Initialize Voxelgrid for drawing.
bool isEqualPoint (voxel *vox, short int x, short int y, short int z)
 Returns true if the voxel contains the coordinates specified by x,y,z.
void rehash ()
 rehashes the table.
void setHeight (int16_t x, int16_t y, int16_t z)
 Sets the height in waterHeight.
void setVoxel (int16_t x, int16_t y, int16_t z, bool filled, float a, float b)
 Set value of voxel at x,y,z.
void updateVoxelrender ()
 Updates the buffer on the GPU.

Public Attributes

int64_t hashSize
 size of the hash table
GLuint numCollisions
 How many collisions presently.
GLuint numInTable
 How many elements there are in the hashTable.

Private Member Functions

int64_t hashFunc (int64_t x, int64_t y, int64_t z, int64_t inHashSize)
 hashFunc which returns a position in the hashtable to insert the voxel

Private Attributes

DataHandlerdatahandler
 Handle to the datahandler and thus the model data.
std::vector< voxel * > * hashTable
 Pointer to hashTable. Filled with voxel pointers.
GLuint height
 The height of the map, needed for voxel extraction.
GLuint numVoxels
 Number of voxels in the voxelPositions.
GLfloat rehashTresh
 Rehash treshhold.
GLuint voxelBuffer
 Buffer for rendering.
std::vector< GLuint > * voxelPositions
 Vector for the position where there are voxels, for drawing purposes.
std::vector< std::vector
< std::vector< voxel * > * > * > * 
voxels
 Container for the voxel lookup tables.
GLuint voxelShader
 Shader program.
GLuint voxelVAO
 VAOs for rendering.
std::vector< GLint > * waterHeight
 The map of height data for the topmost water voxel.
GLuint width
 The width of the map, needed for saving data in linear containers.
int xoff [27]
 xoffsets for neighbour extraction.
int yoff [27]
 xoffsets for neighbour extraction.
int zoff [27]
 xoffsets for neighbour extraction.

Detailed Description

Handles the representation of the voxelgrid.

The class is used to construct the voxelgrid used for simulation and visualization. It places a voxel for each point in the base area (x times z) and uses N number of voxels in the precision from the lowest point in the model to the heighest peak. Note voxels outside of highest possible, however not negative voxel coordinates. The class implements a rudimentary sparse voxelgrid.

Definition at line 48 of file voxel.h.


Constructor & Destructor Documentation

Voxelgrid::Voxelgrid ( DataHandler handle,
int64_t  hashSize 
)

Constructs an empty voxel grid.

Constructs an initially empty sparse voxelgrid, which scales so that there is N number of voxels in height representing the lowest point to the heighest. Where N is ceil(DataHandler->getTerrainScale) The grid dynamically grows when voxels are added. The grid supports values between -10 and INT_MAX-10, by offsetting input coordinates with +10.  

Parameters:
handleHandle to the DataHandler will be bound to the class
hashSizeintialsize of the hashTable. Set if you plan on using the hashfunctions, should be a power of 2 for quick calculations.
See also:
DataHandle::getTerrainScale
setVoxel
getVoxel
Todo:
The Voxelgrid should be able to handle one voxel outside the area of the terrain in each dimension.

Definition at line 144 of file voxel.cpp.


Member Function Documentation

void Voxelgrid::drawVoxels ( glm::mat4  projectionMatrix,
glm::mat4  viewMatrix 
)

Draw the voxel data.

Draws the current buffer of voxels to the screen as billboards

See also:
initDraw
updateVoxelrender

Definition at line 440 of file voxel.cpp.

void Voxelgrid::FloodFill ( int  x,
int  z,
int  height,
bool  fillDown = true 
)

Floodfills pixels at the specified position and height.

Floodfills at height, stopping when hitting terrain which is heigher than the specified flood height. Currently only fills voxels at the topmost layer for optimization reasons.

Definition at line 322 of file voxel.cpp.

GLint Voxelgrid::getHeight ( int16_t  x,
int16_t  z 
)

Gets the height in waterHeight.

The function returns the value at x,z in waterHeight

See also:
getHeight

Definition at line 464 of file voxel.cpp.

std::vector< GLint > * Voxelgrid::getHeightMap ( )

Returns a pointer to waterHeight.

Warning:
The heightMap is NOT updated on deletion of a voxel only on addition.
Todo:
The heightMap SHOULD be updated on deletion of a voxel.
See also:
getHeight
setHeight
waterHeight

Definition at line 470 of file voxel.cpp.

neighs * Voxelgrid::getNeighbourhood ( int16_t  x,
int16_t  y,
int16_t  z 
)

Get a voxel and the 26 neighbouring pixels.

See also:
neighs

Definition at line 126 of file voxel.cpp.

neighs * Voxelgrid::getNeighbourhoodHash ( int16_t  x,
int16_t  y,
int16_t  z 
)

Get a voxel and the 26 neighbouring pixels.

Warning:
Use getNeighbourhood instead.
See also:
getNeighbourhood

Definition at line 113 of file voxel.cpp.

voxel * Voxelgrid::getVoxel ( int16_t  x,
int16_t  y,
int16_t  z 
)

Get value of voxel at x,y,z.

Sets the values of the voxel at x,y,z and creates it in the sparse voxelgrid. if setting many voxels at the same time, loop over z first, followed by y, and from go from LOW values towards HIGH. (maximizes cache hits)  

Parameters:
xCoordinate of the voxel, x cannot be smaller than -10  
yCoordinate of the voxel, y cannot be smaller than -10  
zCoordinate of the voxel, z cannot be smaller than -10
See also:
setVoxel
Returns:
Returns a pointer to the voxel (i.e. changes can be made.) If no voxel exists a nullpointer is returned.

Definition at line 283 of file voxel.cpp.

std::vector< GLuint > * Voxelgrid::getVoxelPositions ( )

Create a data pointer to all existing voxels.

Goes through all voxels and creates an array containing the positions, used for uploading data to the GPU.

Returns:
Returs a pointer to a vector with all the positions in the order x1,y1,z1,x2,y2,z2 ...

Definition at line 388 of file voxel.cpp.

void Voxelgrid::hashAdd ( int16_t  x,
int16_t  y,
int16_t  z,
bool  filled,
float  a,
float  b 
)

Adds a pixel using the hash functions.

Warning:
Please use setVoxel instead since it for most cases is much faster.

Definition at line 52 of file voxel.cpp.

int64_t Voxelgrid::hashFunc ( int64_t  x,
int64_t  y,
int64_t  z,
int64_t  inHashSize 
) [private]

hashFunc which returns a position in the hashtable to insert the voxel

The hashfunction multiplies each coordninate with a large prime. then takes xor between the results, takes the absolute value and finally returns the value modulus the hashtable size.

Definition at line 48 of file voxel.cpp.

voxel * Voxelgrid::hashGet ( int16_t  x,
int16_t  y,
int16_t  z 
)

Gets a pixel using the hash functions.

See also:
setVoxel
Warning:
Please use the getVoxel instead, since it for most cases is much faster.

Definition at line 88 of file voxel.cpp.

Initializes the hashmap to the size specified by hashSize.

Warning:
Use voxelgrid instead due to speed.
See also:
setVoxel
getVoxel

Definition at line 108 of file voxel.cpp.

Initialize Voxelgrid for drawing.

Gets the positions of all currently filled voxels, counts them, creates shader program, VAO and VBO.

See also:
drawVoxels

Definition at line 408 of file voxel.cpp.

rehashes the table.

See also:
getVoxel
Warning:
This operation is potentially very slow. Please use a sufficiently large table from the start instead or use the setVoxel and getVoxel instead.

Definition at line 10 of file voxel.cpp.

void Voxelgrid::setHeight ( int16_t  x,
int16_t  y,
int16_t  z 
)

Sets the height in waterHeight.

The function only sets the data granted that height is larger than the value currently residing there.

See also:
getHeight

Definition at line 457 of file voxel.cpp.

void Voxelgrid::setVoxel ( int16_t  x,
int16_t  y,
int16_t  z,
bool  filled,
float  a,
float  b 
)

Set value of voxel at x,y,z.

Sets the values of the voxel at x,y,z and creates it in the sparse voxelgrid. if setting many voxels at the same time, loop over z first, followed by y, and from go from HIGH values towards LOW. (minimizes resizing) negative values supported by offsetting input by 10. supports values in the range -10 to INT_MAX-10  

Parameters:
xCoordinate of the voxel, x cannot be smaller than -10  
yCoordinate of the voxel, y cannot be smaller than -10  
zCoordinate of the voxel, z cannot be smaller than -10  
filledBool determining if the voxel has been filled or not.  
aDummy variable for initial tests of size and performance  
bDummy variable for initial tests of size and performance
See also:
getVoxel

Definition at line 205 of file voxel.cpp.

Updates the buffer on the GPU.

Which can be rendered by using drawVoxels()

See also:
initDraw
drawVoxels

Definition at line 430 of file voxel.cpp.


The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Enumerations