Skip to content

Commit

Permalink
stabilize particle size (#3613)
Browse files Browse the repository at this point in the history
* stabilize particle size

* inc

* fix inconsistent grid iteration types
  • Loading branch information
StasJ committed Jun 18, 2024
1 parent ea0692c commit cd0cd91
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions apps/vaporgui/ParticleEventRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ParticleEventRouter::ParticleEventRouter(QWidget *parent, ControlExec *ce) : Ren
// (new PShowIf(ParticleParams::RenderRadiusVariableTag))->Not()->Equals("")->Then({
// (new PDoubleSliderEdit(ParticleParams::RenderRadiusVariableStrengthTag, "Radius Variable Strength"))->SetRange(0.001, 1)->AllowUserRange(true)->EnableDynamicUpdate()
// }),
(new PButton("Recalculate Base Radius", [](ParamsBase *p){ p->SetValueLong(ParticleParams::RecalculateRadiusBaseRequestTag, "", true); })),
new PCheckbox(ParticleParams::ShowDirectionTag, "Show direction"),
(new PDoubleSliderEdit(ParticleParams::DirectionScaleTag, "Length scale"))->SetRange(0.0001, 10)->AllowUserRange(true)->EnableDynamicUpdate()->EnableBasedOnParam(ParticleParams::ShowDirectionTag),
(new PXFieldVariableSelector)->ShowParticleVars()->EnableBasedOnParam(ParticleParams::ShowDirectionTag),
Expand Down
1 change: 1 addition & 0 deletions include/vapor/ParticleParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class PARAMS_API ParticleParams : public RenderParams {
static const std::string RenderRadiusVariableStrengthTag;

static const std::string RenderRadiusBaseTag;
static const std::string RecalculateRadiusBaseRequestTag;
static const std::string RenderLegacyTag;

static const std::string LightingEnabledTag;
Expand Down
2 changes: 2 additions & 0 deletions include/vapor/ParticleRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ class RENDER_API ParticleRenderer : public Renderer {

void _clearCache() {}

bool _particleBaseSizeIsDirty() const;
bool _particleCacheIsDirty() const;
bool _colormapCacheIsDirty() const;
void _resetParticleCache();
void _resetColormapCache();
int _generateParticlesLegacy(Grid*& grid, std::vector<Grid*>& vecGrids);
int _getGrids(Grid*& grid, std::vector<Grid*>& vecGrids) const;
template<typename T> void UploadDataBuffer(vector<T> buffer);
void _generateTextureData(const Grid* grid, const std::vector<Grid*>& vecGrids);
void _computeBaseRadius();
void _renderParticlesLegacy(const Grid* grid, const std::vector<Grid*>& vecGrids) const;
Expand Down
1 change: 1 addition & 0 deletions lib/params/ParticleParams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const std::string ParticleParams::StrideTag = "StrideTag";
const std::string ParticleParams::RenderRadiusScalarTag = "RenderRadiusScalarTag";
PARAMS_IMPL_TAG(ParticleParams, RenderRadiusVariableTag);
PARAMS_IMPL_TAG(ParticleParams, RenderRadiusVariableStrengthTag);
PARAMS_IMPL_TAG(ParticleParams, RecalculateRadiusBaseRequestTag);
const std::string ParticleParams::LightingEnabledTag = "LightingEnabledTag";
const std::string ParticleParams::RenderRadiusBaseTag = "RenderRadiusBaseTag";
const std::string ParticleParams::RenderLegacyTag = "RenderLegacyTag";
Expand Down
43 changes: 30 additions & 13 deletions lib/render/ParticleRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ int ParticleRenderer::_paintGL(bool)
glEnable(GL_DEPTH_TEST);

bool regenerateParticles = false;
bool recomputeBaseRadius = false;

if (_particleBaseSizeIsDirty())
recomputeBaseRadius = true;

if (_particleCacheIsDirty()) {
_resetParticleCache();
regenerateParticles = true;
Expand All @@ -116,10 +121,10 @@ int ParticleRenderer::_paintGL(bool)
_renderParticlesLegacy(grid, vecGrids);
}
else {
if (regenerateParticles) {
if (regenerateParticles)
_generateTextureData(grid, vecGrids);
if (recomputeBaseRadius)
_computeBaseRadius();
}
_renderParticlesHelper();
}

Expand Down Expand Up @@ -197,6 +202,18 @@ static void SetupParticleDirectionGL(const int VAO, const int VBO, const bool dy
glBindVertexArray(0);
}

bool ParticleRenderer::_particleBaseSizeIsDirty() const {
auto p = GetActiveParams();
if (p->GetValueLong(ParticleParams::RecalculateRadiusBaseRequestTag, false)) {
p->SetValueLong(ParticleParams::RecalculateRadiusBaseRequestTag, "", false);
return true;
}
if (_cacheParams.varName != p->GetVariableName()) return true;
if (_cacheParams.radiusVarName != p->GetValueString( ParticleParams::RenderRadiusVariableTag, "")) return true;
if (_cacheParams.stride != p->GetValueLong( ParticleParams::StrideTag, 1)) return true;
return false;
}

bool ParticleRenderer::_particleCacheIsDirty() const {
auto p = GetActiveParams();

Expand Down Expand Up @@ -378,8 +395,9 @@ int ParticleRenderer::_getGrids(Grid*& grid, std::vector<Grid*>& vecGrids) const
return 0;
}

template<typename T> static void UploadDataBuffer(vector<T> buffer) {
template<typename T> void ParticleRenderer::UploadDataBuffer(vector<T> buffer) {
glBufferData(GL_ARRAY_BUFFER, sizeof(T) * buffer.size(), buffer.data(), GL_STATIC_DRAW);
_particlesCount = buffer.size();
}

void ParticleRenderer::_generateTextureData(const Grid* grid, const std::vector<Grid*>& vecGrids) {
Expand All @@ -389,18 +407,12 @@ void ParticleRenderer::_generateTextureData(const Grid* grid, const std::vector<
auto node = grid->ConstNodeBegin(_cacheParams.boxMin, _cacheParams.boxMax);
auto nodeEnd = grid->ConstNodeEnd();
CoordType coordsBuf;
vector<Grid::ConstIterator> dirs;

if (showDir) for (int i = 0; i < 3; i++) dirs.push_back(vecGrids[i]->cbegin(_cacheParams.boxMin, _cacheParams.boxMax));
if (dynamicSize) dirs.push_back(vecGrids[vecGrids.size()-1]);

struct PointDataT {vec3 pos; float val;}; vector<PointDataT> pointData;
struct DirectionDataT {vec3 pos; vec3 norm; float val;}; vector<DirectionDataT> directionData;
struct PointDynSizeDataT {vec3 pos; float val; float radius;}; vector<PointDynSizeDataT> pointDynSizeData;
struct DirectionDynSizeDataT {vec3 pos; vec3 norm; float val; float radius;}; vector<DirectionDynSizeDataT> directionDynSizeData;

_particlesCount = grid->GetCoordDimensions(1)[0] / stride;

if (showDir) {
SetupParticleDirectionGL(_VAO, _VBO, dynamicSize);
directionData.reserve(_particlesCount);
Expand All @@ -419,21 +431,26 @@ void ParticleRenderer::_generateTextureData(const Grid* grid, const std::vector<
const vec3 p = CoordTypeToVec3(coordsBuf);

if (showDir){
const glm::vec3 norm(*(dirs[0]), *(dirs[1]), *(dirs[2]));
const glm::vec3 norm(
vecGrids[0]->GetValueAtIndex(*node),
vecGrids[1]->GetValueAtIndex(*node),
vecGrids[2]->GetValueAtIndex(*node)
);
if (dynamicSize)
directionDynSizeData.push_back({p, norm, value, *(dirs[dirs.size()-1])});
directionDynSizeData.push_back({p, norm, value, vecGrids[vecGrids.size()-1]->GetValueAtIndex(*node)});
else
directionData.push_back({p, norm, value});
} else {
if (dynamicSize)
pointDynSizeData.push_back({p, value, *(dirs[dirs.size()-1])});
// pointDynSizeData.push_back({p, value, *(dirs[dirs.size()-1])});
pointDynSizeData.push_back({p, value, vecGrids[vecGrids.size()-1]->GetValueAtIndex(*node)});

else
pointData.push_back({p, value});
}
}
step:
++node, ++i;
for (auto &it : dirs) ++it;
}

glBindVertexArray(_VAO);
Expand Down

0 comments on commit cd0cd91

Please sign in to comment.