Skip to content

Commit

Permalink
add per-element depth for meshes and point clouds
Browse files Browse the repository at this point in the history
  • Loading branch information
nmwsharp committed Aug 16, 2024
1 parent 7ca5e0e commit b48cc3a
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 10 deletions.
14 changes: 14 additions & 0 deletions include/polyscope/point_cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class PointCloud : public QuantityStructure<PointCloud> {
void setPointRadiusQuantity(std::string name, bool autoScale = true);
void clearPointRadiusQuantity();

// === Set transparency alpha from a scalar quantity
// effect is multiplicative with other transparency values
// values are clamped to [0,1]
void setTransparencyQuantity(PointCloudScalarQuantity* quantity);
void setTransparencyQuantity(std::string name);
void clearTransparencyQuantity();

// The points that make up this point cloud
// Normally, the values are stored here. But if the render buffer
// is being manually updated, they will live only in the render buffer
Expand Down Expand Up @@ -174,9 +181,16 @@ class PointCloud : public QuantityStructure<PointCloud> {

// Manage varying point size
// which (scalar) quantity to set point size from
// TODO make these PersistentValue<>?
std::string pointRadiusQuantityName = ""; // empty string means none
bool pointRadiusQuantityAutoscale = true;
PointCloudScalarQuantity& resolvePointRadiusQuantity(); // helper

// Manage per-element transparency
// which (scalar) quantity to set point size from
// TODO make these PersistentValue<>?
std::string transparencyQuantityName = ""; // empty string means none
PointCloudScalarQuantity& resolveTransparencyQuantity(); // helper
};


Expand Down
1 change: 1 addition & 0 deletions include/polyscope/render/opengl/shaders/sphere_shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern const ShaderStageSpecification FLEX_POINTQUAD_FRAG_SHADER;

// Rules specific to spheres
extern const ShaderReplacementRule SPHERE_PROPAGATE_VALUE;
extern const ShaderReplacementRule SPHERE_PROPAGATE_VALUEALPHA;
extern const ShaderReplacementRule SPHERE_PROPAGATE_VALUE2;
extern const ShaderReplacementRule SPHERE_PROPAGATE_COLOR;
extern const ShaderReplacementRule SPHERE_VARIABLE_SIZE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern const ShaderReplacementRule MESH_BACKFACE_NORMAL_FLIP;
extern const ShaderReplacementRule MESH_BACKFACE_DIFFERENT;
extern const ShaderReplacementRule MESH_BACKFACE_DARKEN;
extern const ShaderReplacementRule MESH_PROPAGATE_VALUE;
extern const ShaderReplacementRule MESH_PROPAGATE_VALUEALPHA;
extern const ShaderReplacementRule MESH_PROPAGATE_FLAT_VALUE;
extern const ShaderReplacementRule MESH_PROPAGATE_INT;
extern const ShaderReplacementRule MESH_PROPAGATE_VALUE2;
Expand Down
15 changes: 15 additions & 0 deletions include/polyscope/surface_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace polyscope {
class SurfaceVertexColorQuantity;
class SurfaceFaceColorQuantity;
class SurfaceTextureColorQuantity;
class SurfaceScalarQuantity;
class SurfaceVertexScalarQuantity;
class SurfaceFaceScalarQuantity;
class SurfaceEdgeScalarQuantity;
Expand Down Expand Up @@ -176,6 +177,13 @@ class SurfaceMesh : public QuantityStructure<SurfaceMesh> {
template <class V>
void updateVertexPositions2D(const V& newPositions2D);

// === Set transparency alpha from a scalar quantity
// effect is multiplicative with other transparency values
// values are clamped to [0,1]
void setTransparencyQuantity(SurfaceScalarQuantity* quantity);
void setTransparencyQuantity(std::string name);
void clearTransparencyQuantity();


// === Indexing conventions

Expand Down Expand Up @@ -376,6 +384,13 @@ class SurfaceMesh : public QuantityStructure<SurfaceMesh> {
void buildHalfedgeInfoGui(size_t heInd);
void buildCornerInfoGui(size_t cInd);

// Manage per-element transparency
// which (scalar) quantity to set point size from
// TODO make these PersistentValue<>?
std::string transparencyQuantityName = ""; // empty string means none
SurfaceScalarQuantity& resolveTransparencyQuantity(); // helper


// ==== Gui implementation details

std::shared_ptr<render::ShaderProgram> program;
Expand Down
17 changes: 12 additions & 5 deletions include/polyscope/surface_scalar_quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ class SurfaceScalarQuantity : public SurfaceMeshQuantity, public ScalarQuantity<
virtual std::string niceName() override;
virtual void refresh() override;

protected:
virtual std::shared_ptr<render::AttributeBuffer> getAttributeBuffer() = 0;

const std::string definedOn;

protected:
std::shared_ptr<render::ShaderProgram> program;

// Helpers
Expand All @@ -47,6 +50,8 @@ class SurfaceVertexScalarQuantity : public SurfaceScalarQuantity {

virtual void createProgram() override;

virtual std::shared_ptr<render::AttributeBuffer> getAttributeBuffer() override;

void buildVertexInfoGUI(size_t vInd) override;
};

Expand All @@ -61,7 +66,7 @@ class SurfaceFaceScalarQuantity : public SurfaceScalarQuantity {
DataType dataType_ = DataType::STANDARD);

virtual void createProgram() override;

virtual std::shared_ptr<render::AttributeBuffer> getAttributeBuffer() override;
void buildFaceInfoGUI(size_t fInd) override;
};

Expand All @@ -76,7 +81,7 @@ class SurfaceEdgeScalarQuantity : public SurfaceScalarQuantity {
DataType dataType_ = DataType::STANDARD);

virtual void createProgram() override;

virtual std::shared_ptr<render::AttributeBuffer> getAttributeBuffer() override;
void buildEdgeInfoGUI(size_t edgeInd) override;
};

Expand All @@ -90,7 +95,7 @@ class SurfaceHalfedgeScalarQuantity : public SurfaceScalarQuantity {
DataType dataType_ = DataType::STANDARD);

virtual void createProgram() override;

virtual std::shared_ptr<render::AttributeBuffer> getAttributeBuffer() override;
void buildHalfedgeInfoGUI(size_t heInd) override;
};

Expand All @@ -104,7 +109,7 @@ class SurfaceCornerScalarQuantity : public SurfaceScalarQuantity {
DataType dataType_ = DataType::STANDARD);

virtual void createProgram() override;

virtual std::shared_ptr<render::AttributeBuffer> getAttributeBuffer() override;
void buildCornerInfoGUI(size_t heInd) override;
};

Expand All @@ -119,6 +124,8 @@ class SurfaceTextureScalarQuantity : public SurfaceScalarQuantity {
DataType dataType_ = DataType::STANDARD);

virtual void createProgram() override;
virtual std::shared_ptr<render::AttributeBuffer> getAttributeBuffer() override;


protected:
SurfaceParameterizationQuantity& param;
Expand Down
66 changes: 63 additions & 3 deletions src/point_cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ void PointCloud::setPointProgramGeometryAttributes(render::ShaderProgram& p) {
PointCloudScalarQuantity& radQ = resolvePointRadiusQuantity();
p.setAttribute("a_pointRadius", radQ.values.getRenderAttributeBuffer());
}
if (transparencyQuantityName != "") {
PointCloudScalarQuantity& transparencyQ = resolveTransparencyQuantity();
p.setAttribute("a_valueAlpha", transparencyQ.values.getRenderAttributeBuffer());
}
}

std::string PointCloud::getShaderNameForRenderMode() {
Expand Down Expand Up @@ -212,6 +216,9 @@ std::vector<std::string> PointCloud::addPointCloudRules(std::vector<std::string>
else if (getPointRenderMode() == PointRenderMode::Quad)
initRules.push_back("SPHERE_CULLPOS_FROM_CENTER_QUAD");
}
if (transparencyQuantityName != "") {
initRules.push_back("SPHERE_PROPAGATE_VALUEALPHA");
}
}
return initRules;
}
Expand Down Expand Up @@ -268,6 +275,11 @@ void PointCloud::buildCustomUI() {
}

void PointCloud::buildCustomOptionsUI() {
if (render::buildMaterialOptionsGui(material.get())) {
material.manuallyChanged();
setMaterial(material.get()); // trigger the other updates that happen on set()
}

if (ImGui::BeginMenu("Point Render Mode")) {

for (const PointRenderMode& m : {PointRenderMode::Sphere, PointRenderMode::Quad}) {
Expand Down Expand Up @@ -305,9 +317,21 @@ void PointCloud::buildCustomOptionsUI() {
ImGui::EndMenu();
}

if (render::buildMaterialOptionsGui(material.get())) {
material.manuallyChanged();
setMaterial(material.get()); // trigger the other updates that happen on set()
// transparency quantity
if (ImGui::BeginMenu("Per-Point Transparency")) {

if (ImGui::MenuItem("none", nullptr, transparencyQuantityName == "")) clearTransparencyQuantity();
ImGui::Separator();

for (auto& q : quantities) {
PointCloudScalarQuantity* scalarQ = dynamic_cast<PointCloudScalarQuantity*>(q.second.get());
if (scalarQ != nullptr) {
if (ImGui::MenuItem(scalarQ->name.c_str(), nullptr, transparencyQuantityName == scalarQ->name))
setTransparencyQuantity(scalarQ);
}
}

ImGui::EndMenu();
}
}

Expand Down Expand Up @@ -362,6 +386,42 @@ void PointCloud::clearPointRadiusQuantity() {
refresh();
}

void PointCloud::setTransparencyQuantity(PointCloudScalarQuantity* quantity) {
setTransparencyQuantity(quantity->name);
}

void PointCloud::setTransparencyQuantity(std::string name) {
transparencyQuantityName = name;
resolveTransparencyQuantity(); // do it once, just so we fail fast if it doesn't exist

// if transparency is disabled, enable it
if (options::transparencyMode == TransparencyMode::None) {
options::transparencyMode = TransparencyMode::Pretty;
}

refresh();
}

void PointCloud::clearTransparencyQuantity() {
transparencyQuantityName = "";
refresh();
}

PointCloudScalarQuantity& PointCloud::resolveTransparencyQuantity() {
PointCloudScalarQuantity* transparencyScalarQ = nullptr;
PointCloudQuantity* anyQ = getQuantity(transparencyQuantityName);
if (anyQ != nullptr) {
transparencyScalarQ = dynamic_cast<PointCloudScalarQuantity*>(anyQ);
if (transparencyScalarQ == nullptr) {
exception("Cannot populate per-element transparency from quantity [" + name + "], it is not a scalar quantity");
}
} else {
exception("Cannot populate per-element transparency from quantity [" + name + "], it does not exist");
}

return *transparencyScalarQ;
}

// === Quantities

// Quantity default methods
Expand Down
2 changes: 2 additions & 0 deletions src/render/mock_opengl/mock_gl_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,7 @@ void MockGLEngine::populateDefaultShadersAndRules() {
registerShaderRule("MESH_BACKFACE_DIFFERENT", MESH_BACKFACE_DIFFERENT);
registerShaderRule("MESH_BACKFACE_DARKEN", MESH_BACKFACE_DARKEN);
registerShaderRule("MESH_PROPAGATE_VALUE", MESH_PROPAGATE_VALUE);
registerShaderRule("MESH_PROPAGATE_VALUEALPHA", MESH_PROPAGATE_VALUEALPHA);
registerShaderRule("MESH_PROPAGATE_FLAT_VALUE", MESH_PROPAGATE_FLAT_VALUE);
registerShaderRule("MESH_PROPAGATE_VALUE2", MESH_PROPAGATE_VALUE2);
registerShaderRule("MESH_PROPAGATE_TCOORD", MESH_PROPAGATE_TCOORD);
Expand All @@ -2000,6 +2001,7 @@ void MockGLEngine::populateDefaultShadersAndRules() {

// sphere things
registerShaderRule("SPHERE_PROPAGATE_VALUE", SPHERE_PROPAGATE_VALUE);
registerShaderRule("SPHERE_PROPAGATE_VALUEALPHA", SPHERE_PROPAGATE_VALUEALPHA);
registerShaderRule("SPHERE_PROPAGATE_VALUE2", SPHERE_PROPAGATE_VALUE2);
registerShaderRule("SPHERE_PROPAGATE_COLOR", SPHERE_PROPAGATE_COLOR);
registerShaderRule("SPHERE_CULLPOS_FROM_CENTER", SPHERE_CULLPOS_FROM_CENTER);
Expand Down
2 changes: 2 additions & 0 deletions src/render/opengl/gl_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,7 @@ void GLEngine::populateDefaultShadersAndRules() {
registerShaderRule("MESH_BACKFACE_DIFFERENT", MESH_BACKFACE_DIFFERENT);
registerShaderRule("MESH_BACKFACE_DARKEN", MESH_BACKFACE_DARKEN);
registerShaderRule("MESH_PROPAGATE_VALUE", MESH_PROPAGATE_VALUE);
registerShaderRule("MESH_PROPAGATE_VALUEALPHA", MESH_PROPAGATE_VALUEALPHA);
registerShaderRule("MESH_PROPAGATE_FLAT_VALUE", MESH_PROPAGATE_FLAT_VALUE);
registerShaderRule("MESH_PROPAGATE_VALUE2", MESH_PROPAGATE_VALUE2);
registerShaderRule("MESH_PROPAGATE_TCOORD", MESH_PROPAGATE_TCOORD);
Expand All @@ -2559,6 +2560,7 @@ void GLEngine::populateDefaultShadersAndRules() {

// sphere things
registerShaderRule("SPHERE_PROPAGATE_VALUE", SPHERE_PROPAGATE_VALUE);
registerShaderRule("SPHERE_PROPAGATE_VALUEALPHA", SPHERE_PROPAGATE_VALUEALPHA);
registerShaderRule("SPHERE_PROPAGATE_VALUE2", SPHERE_PROPAGATE_VALUE2);
registerShaderRule("SPHERE_PROPAGATE_COLOR", SPHERE_PROPAGATE_COLOR);
registerShaderRule("SPHERE_CULLPOS_FROM_CENTER", SPHERE_CULLPOS_FROM_CENTER);
Expand Down
4 changes: 2 additions & 2 deletions src/render/opengl/shaders/lighting_shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const ShaderReplacementRule TRANSPARENCY_STRUCTURE (
uniform float u_transparency;
)"},
{"GENERATE_ALPHA", R"(
alphaOut = u_transparency;
alphaOut *= u_transparency;
)"},
},
/* uniforms */ {
Expand All @@ -257,7 +257,7 @@ const ShaderReplacementRule TRANSPARENCY_PEEL_STRUCTURE (
uniform vec2 u_viewportDim;
)"},
{"GENERATE_ALPHA", R"(
alphaOut = u_transparency;
alphaOut *= u_transparency;
)"},
{"GLOBAL_FRAGMENT_FILTER", R"(
// assumption: "float depth" must be already set
Expand Down
31 changes: 31 additions & 0 deletions src/render/opengl/shaders/sphere_shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,37 @@ const ShaderReplacementRule SPHERE_PROPAGATE_VALUE (
/* textures */ {}
);

const ShaderReplacementRule SPHERE_PROPAGATE_VALUEALPHA (
/* rule name */ "SPHERE_PROPAGATE_VALUEALPHA",
{ /* replacement sources */
{"VERT_DECLARATIONS", R"(
in float a_valueAlpha;
out float a_valueAlphaToGeom;
)"},
{"VERT_ASSIGNMENTS", R"(
a_valueAlphaToGeom = a_valueAlpha;
)"},
{"GEOM_DECLARATIONS", R"(
in float a_valueAlphaToGeom[];
out float a_valueAlphaToFrag;
)"},
{"GEOM_PER_EMIT", R"(
a_valueAlphaToFrag = a_valueAlphaToGeom[0];
)"},
{"FRAG_DECLARATIONS", R"(
in float a_valueAlphaToFrag;
)"},
{"GENERATE_ALPHA", R"(
alphaOut *= clamp(a_valueAlphaToFrag, 0.f, 1.f);
)"},
},
/* uniforms */ {},
/* attributes */ {
{"a_valueAlpha", RenderDataType::Float},
},
/* textures */ {}
);

const ShaderReplacementRule SPHERE_PROPAGATE_VALUE2 (
/* rule name */ "SPHERE_PROPAGATE_VALUE2",
{ /* replacement sources */
Expand Down
24 changes: 24 additions & 0 deletions src/render/opengl/shaders/surface_mesh_shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,30 @@ const ShaderReplacementRule MESH_PROPAGATE_VALUE (
/* textures */ {}
);

const ShaderReplacementRule MESH_PROPAGATE_VALUEALPHA (
/* rule name */ "MESH_PROPAGATE_VALUEALPHA",
{ /* replacement sources */
{"VERT_DECLARATIONS", R"(
in float a_valueAlpha;
out float a_valueAlphaToFrag;
)"},
{"VERT_ASSIGNMENTS", R"(
a_valueAlphaToFrag = a_valueAlpha;
)"},
{"FRAG_DECLARATIONS", R"(
in float a_valueAlphaToFrag;
)"},
{"GENERATE_ALPHA", R"(
alphaOut *= clamp(a_valueAlphaToFrag, 0.f, 1.f);
)"},
},
/* uniforms */ {},
/* attributes */ {
{"a_valueAlpha", RenderDataType::Float},
},
/* textures */ {}
);

const ShaderReplacementRule MESH_PROPAGATE_FLAT_VALUE (
/* rule name */ "MESH_PROPAGATE_FLAT_VALUE",
{ /* replacement sources */
Expand Down
Loading

0 comments on commit b48cc3a

Please sign in to comment.