A 2D grid containing particles, used to quickly find the neighbors of a particle or the particles covering a given point. More...
#include <ParticleGrid.h>
Public Member Functions | |
ParticleGrid (float radius, int maxParticlesPerCell, float gridFactor=1.0f) | |
Creates a new ParticleGrid. | |
~ParticleGrid () | |
Deletes this ParticleGrid. | |
float | getParticleRadius () const |
Returns the radius of each particle. | |
void | setParticleRadius (float radius) |
Sets the radius of each particle. | |
int | getMaxParticlesPerCell () const |
Returns the maximum number of particles per cell. | |
void | setMaxParticlesPerCell (int maxParticlesPerCell) |
Sets the maximum number of particles per cell. | |
box2i | getViewport () const |
Returns the viewport covered by this grid. | |
void | setViewport (const box2i &viewport) |
Sets the viewport that should be covered by this grid. | |
vec2i | getGridSize () const |
Returns the number of columns and rows of the grid. | |
vec2i | getCell (const vec2f &p) |
Returns the grid cell coordinates corresponding to the given point. | |
int | getCellSize (const vec2i &cell) |
Returns the number of particles in the given cell. | |
ScreenParticleLayer::ScreenParticle ** | getCellContent (const vec2i &cell) |
Returns the particles in the given cell. | |
void | addParticle (ScreenParticleLayer::ScreenParticle *p, float intensity) |
Adds a particle to this grid. | |
void | clear () |
Removes all the particles from the grid. | |
ptr< Texture2D > | copyToTexture (ptr< ScreenParticleLayer > l, ptr< Texture2D > t, int &pixelsPerCell) |
Copies the content of this ParticleGrid to the given texture. | |
Private Member Functions | |
void | createGrid () |
Creates the grid for the current viewport and particle radius. | |
void | deleteGrid () |
Deletes the grid. | |
Private Attributes | |
float | radius |
The radius of each particle, in pixels. | |
int | maxParticlesPerCell |
Maximum number of particles per grid cell. | |
box2i | viewport |
The viewport covered by this particle grid, in pixels. | |
vec2i | gridSize |
The number of columns and rows of the particle grid. | |
vec2i | cellGridSize |
The number of columns and rows of the particle grid sent to the GPU. | |
int * | cellSizes |
The number of particles in each grid cell. | |
ScreenParticleLayer::ScreenParticle ** | cellContents |
The particles in each grid cell. | |
int * | cellIndexes |
The indexes of the particles in each gpu grid cell. | |
float | gridFactor |
Factor for the grid size, if required to be different to the specified one. |
A 2D grid containing particles, used to quickly find the neighbors of a particle or the particles covering a given point.
Each cell of the grid contains the particles that cover this cell (based on the screen particle position and a specified particle radius, all in pixels). The cell size is set approximatively to the particle radius. The grid covers a specified viewport (in pixels), and its cells are recomputed when this viewport changes (so that the size of each cell stays approximatively equal to the particle radius, in pixels). This class can also copy this grid in a GPU texture.
proland::ParticleGrid::ParticleGrid | ( | float | radius, | |
int | maxParticlesPerCell, | |||
float | gridFactor = 1.0f | |||
) |
Creates a new ParticleGrid.
radius | the radius of each particle. A particle is added to each cell covered by its radius. | |
maxParticlesPerCell | maximum number of particles per grid cell. | |
gridFactor | factor for the grid size, if required to be different to the specified one. |
proland::ParticleGrid::~ParticleGrid | ( | ) |
Deletes this ParticleGrid.
void proland::ParticleGrid::addParticle | ( | ScreenParticleLayer::ScreenParticle * | p, | |
float | intensity | |||
) |
Adds a particle to this grid.
The particle is added to each cell covered by the disk of radius getParticleRadius() around the particle.
p | a particle. |
void proland::ParticleGrid::clear | ( | ) |
Removes all the particles from the grid.
ptr<Texture2D> proland::ParticleGrid::copyToTexture | ( | ptr< ScreenParticleLayer > | l, | |
ptr< Texture2D > | t, | |||
int & | pixelsPerCell | |||
) |
Copies the content of this ParticleGrid to the given texture.
Each cell is represented with pixelsPerCell RGBA float values representing particle indexes. The cell particles are stored in the first pixels of the cell, and -1 is used to mark the end of the particle list.
l | the layer managing the particles stored in this grid. This layer is used to compute the index of each particle via ParticleStorage::getParticleIndex(). | |
t | the destination texture, or NULL to create a new one. | |
[out] | pixelsPerCell | the number of pixels used to represent each cell in t. Equals to ceil(getMaxParticlesPerCell() / 4). |
void proland::ParticleGrid::createGrid | ( | ) | [private] |
Creates the grid for the current viewport and particle radius.
void proland::ParticleGrid::deleteGrid | ( | ) | [private] |
Deletes the grid.
vec2i proland::ParticleGrid::getCell | ( | const vec2f & | p | ) |
Returns the grid cell coordinates corresponding to the given point.
p | a point inside the viewport getViewport(). |
ScreenParticleLayer::ScreenParticle** proland::ParticleGrid::getCellContent | ( | const vec2i & | cell | ) |
Returns the particles in the given cell.
cell | a cell given by its x and y coordinates (in number of columns and rows). |
int proland::ParticleGrid::getCellSize | ( | const vec2i & | cell | ) |
Returns the number of particles in the given cell.
cell | a cell given by its x and y coordinates (in number of columns and rows). |
vec2i proland::ParticleGrid::getGridSize | ( | ) | const |
Returns the number of columns and rows of the grid.
int proland::ParticleGrid::getMaxParticlesPerCell | ( | ) | const |
Returns the maximum number of particles per cell.
float proland::ParticleGrid::getParticleRadius | ( | ) | const |
Returns the radius of each particle.
box2i proland::ParticleGrid::getViewport | ( | ) | const |
Returns the viewport covered by this grid.
void proland::ParticleGrid::setMaxParticlesPerCell | ( | int | maxParticlesPerCell | ) |
Sets the maximum number of particles per cell.
void proland::ParticleGrid::setParticleRadius | ( | float | radius | ) |
Sets the radius of each particle.
This clears the grid content.
void proland::ParticleGrid::setViewport | ( | const box2i & | viewport | ) |
Sets the viewport that should be covered by this grid.
This clears the grid content.
The particles in each grid cell.
This array is of size gridSize.x * gridSize.y * maxParticlesPerCell.
vec2i proland::ParticleGrid::cellGridSize [private] |
The number of columns and rows of the particle grid sent to the GPU.
int* proland::ParticleGrid::cellIndexes [private] |
The indexes of the particles in each gpu grid cell.
This array is of size gpuGridSize.x * gpuGridSize.y * maxParticlesPerGpuCell.
int* proland::ParticleGrid::cellSizes [private] |
The number of particles in each grid cell.
This array is of size gridSize.x * gridSize.y.
float proland::ParticleGrid::gridFactor [private] |
Factor for the grid size, if required to be different to the specified one.
vec2i proland::ParticleGrid::gridSize [private] |
The number of columns and rows of the particle grid.
int proland::ParticleGrid::maxParticlesPerCell [private] |
Maximum number of particles per grid cell.
float proland::ParticleGrid::radius [private] |
The radius of each particle, in pixels.
box2i proland::ParticleGrid::viewport [private] |
The viewport covered by this particle grid, in pixels.