Skip to content

Commit

Permalink
expose texture filtering options for surface mesh quantities (#310)
Browse files Browse the repository at this point in the history
* initial implementation of texture filtering option

* factor out texture map options

* templated texture options for scalars too
  • Loading branch information
nmwsharp authored Dec 26, 2024
1 parent 9f97b70 commit db8c9ee
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 17 deletions.
3 changes: 2 additions & 1 deletion include/polyscope/color_quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class ColorQuantity {
public:
ColorQuantity(QuantityT& parent, const std::vector<glm::vec3>& colors);

// Build the ImGUI UIs for scalars
// Build the ImGUI UIs for colors
void buildColorUI();
virtual void buildColorOptionsUI(); // called inside of an options menu

// Add rules to rendering programs for scalars
std::vector<std::string> addColorRules(std::vector<std::string> rules);
Expand Down
3 changes: 3 additions & 0 deletions include/polyscope/color_quantity.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ ColorQuantity<QuantityT>::ColorQuantity(QuantityT& quantity_, const std::vector<
template <typename QuantityT>
void ColorQuantity<QuantityT>::buildColorUI() {}

template <typename QuantityT>
void ColorQuantity<QuantityT>::buildColorOptionsUI() {}

template <typename QuantityT>
void ColorQuantity<QuantityT>::setColorUniforms(render::ShaderProgram& p) {}

Expand Down
2 changes: 2 additions & 0 deletions include/polyscope/persistent_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ extern PersistentCache<std::vector<std::string>> persistentCache_vectorstring;
extern PersistentCache<ParamVizStyle> persistentCache_paramVizStyle;
extern PersistentCache<BackFacePolicy> persistentCache_BackFacePolicy;
extern PersistentCache<MeshShadeStyle> persistentCache_MeshNormalType;
extern PersistentCache<FilterMode> persistentCache_FilterMode;

template<> inline PersistentCache<double>& getPersistentCacheRef<double>() { return persistentCache_double; }
template<> inline PersistentCache<float>& getPersistentCacheRef<float>() { return persistentCache_float; }
Expand All @@ -155,6 +156,7 @@ template<> inline PersistentCache<std::vector<std::string>>& getPersistentCacheR
template<> inline PersistentCache<ParamVizStyle>& getPersistentCacheRef<ParamVizStyle>() { return persistentCache_paramVizStyle; }
template<> inline PersistentCache<BackFacePolicy>& getPersistentCacheRef<BackFacePolicy>() { return persistentCache_BackFacePolicy; }
template<> inline PersistentCache<MeshShadeStyle>& getPersistentCacheRef<MeshShadeStyle>() { return persistentCache_MeshNormalType; }
template<> inline PersistentCache<FilterMode>& getPersistentCacheRef<FilterMode>() { return persistentCache_FilterMode; }
}
// clang-format on

Expand Down
7 changes: 3 additions & 4 deletions include/polyscope/render/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ enum class DrawMode {
TriangleStripInstanced,
};

enum class FilterMode { Nearest = 0, Linear };
enum class TextureFormat { RGB8 = 0, RGBA8, RG16F, RGB16F, RGBA16F, RGBA32F, RGB32F, R32F, R16F, DEPTH24 };
enum class RenderBufferType { Color, ColorAlpha, Depth, Float4 };
enum class DepthMode { Less, LEqual, LEqualReadOnly, Greater, Disable, PassReadOnly };
Expand Down Expand Up @@ -207,7 +206,7 @@ class RenderBuffer {
public:
// abstract class: use the factory methods from the Engine class
RenderBuffer(RenderBufferType type_, unsigned int sizeX_, unsigned int sizeY_);
virtual ~RenderBuffer(){};
virtual ~RenderBuffer() {};

virtual void resize(unsigned int newX, unsigned int newY);

Expand All @@ -228,7 +227,7 @@ class FrameBuffer {
public:
// abstract class: use the factory methods from the Engine class
FrameBuffer();
virtual ~FrameBuffer(){};
virtual ~FrameBuffer() {};

virtual void bind() = 0;
// Bind to this framebuffer so subsequent draw calls will go to it
Expand Down Expand Up @@ -347,7 +346,7 @@ class ShaderProgram {

public:
ShaderProgram(DrawMode dm);
virtual ~ShaderProgram(){};
virtual ~ShaderProgram() {};


// === Store data
Expand Down
10 changes: 7 additions & 3 deletions include/polyscope/surface_color_quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "polyscope/color_quantity.h"
#include "polyscope/render/engine.h"
#include "polyscope/surface_mesh.h"
#include "polyscope/texture_map_quantity.h"

namespace polyscope {

Expand All @@ -19,6 +20,7 @@ class SurfaceColorQuantity : public SurfaceMeshQuantity, public ColorQuantity<Su
const std::vector<glm::vec3>& colorValues);

virtual void draw() override;
virtual void buildCustomUI() override;
virtual std::string niceName() override;
virtual void refresh() override;

Expand All @@ -40,6 +42,7 @@ class SurfaceVertexColorQuantity : public SurfaceColorQuantity {
SurfaceVertexColorQuantity(std::string name, SurfaceMesh& mesh_, std::vector<glm::vec3> values_);

virtual void createProgram() override;
virtual void buildColorOptionsUI() override;

void buildVertexInfoGUI(size_t vInd) override;
};
Expand All @@ -53,6 +56,7 @@ class SurfaceFaceColorQuantity : public SurfaceColorQuantity {
SurfaceFaceColorQuantity(std::string name, SurfaceMesh& mesh_, std::vector<glm::vec3> values_);

virtual void createProgram() override;
virtual void buildColorOptionsUI() override;

void buildFaceInfoGUI(size_t fInd) override;
};
Expand All @@ -62,17 +66,17 @@ class SurfaceFaceColorQuantity : public SurfaceColorQuantity {
// ========== Texture Color ==========
// ========================================================

class SurfaceTextureColorQuantity : public SurfaceColorQuantity {
class SurfaceTextureColorQuantity : public SurfaceColorQuantity,
public TextureMapQuantity<SurfaceTextureColorQuantity> {
public:
SurfaceTextureColorQuantity(std::string name, SurfaceMesh& mesh_, SurfaceParameterizationQuantity& param_,
size_t dimX, size_t dimY, std::vector<glm::vec3> values_, ImageOrigin origin_);

virtual void createProgram() override;
virtual void buildColorOptionsUI() override;

protected:
SurfaceParameterizationQuantity& param;
size_t dimX, dimY;
ImageOrigin imageOrigin;
};

} // namespace polyscope
8 changes: 5 additions & 3 deletions include/polyscope/surface_scalar_quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "polyscope/render/engine.h"
#include "polyscope/scalar_quantity.h"
#include "polyscope/surface_mesh.h"
#include "polyscope/texture_map_quantity.h"

namespace polyscope {

Expand All @@ -25,6 +26,7 @@ class SurfaceScalarQuantity : public SurfaceMeshQuantity, public ScalarQuantity<

virtual void draw() override;
virtual void buildCustomUI() override;
virtual void buildSurfaceScalarOptionsUI() {};
virtual std::string niceName() override;
virtual void refresh() override;

Expand Down Expand Up @@ -117,20 +119,20 @@ class SurfaceCornerScalarQuantity : public SurfaceScalarQuantity {
// ========== Texture Scalar ==========
// ========================================================

class SurfaceTextureScalarQuantity : public SurfaceScalarQuantity {
class SurfaceTextureScalarQuantity : public SurfaceScalarQuantity,
public TextureMapQuantity<SurfaceTextureScalarQuantity> {
public:
SurfaceTextureScalarQuantity(std::string name, SurfaceMesh& mesh_, SurfaceParameterizationQuantity& param_,
size_t dimX, size_t dimY, const std::vector<float>& values_, ImageOrigin origin_,
DataType dataType_ = DataType::STANDARD);

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


protected:
SurfaceParameterizationQuantity& param;
size_t dimX, dimY;
ImageOrigin imageOrigin;
};

} // namespace polyscope
43 changes: 43 additions & 0 deletions include/polyscope/texture_map_quantity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run

#pragma once

#include "polyscope/persistent_value.h"
#include "polyscope/polyscope.h"
#include "polyscope/render/engine.h"
#include "polyscope/render/managed_buffer.h"
#include "polyscope/standardize_data_array.h"

namespace polyscope {

// Encapsulates logic which is common to all texture map quantities

template <typename QuantityT>
class TextureMapQuantity {
public:
TextureMapQuantity(QuantityT& parent, size_t dimX, size_t dimY, ImageOrigin origin_);

// Build the ImGUI UIs for texture maps
virtual void buildTextureMapOptionsUI(); // called inside of an options menu

// === Members
QuantityT& quantity;

// NOTE: the main quantity types (scalar quantity, color quantity, etc) provide the buffer members, so this class just
// has secondary options and such

// what kind of texture filtering is used
QuantityT* setFilterMode(FilterMode newFilterMode);
FilterMode getFilterMode();

protected:
size_t dimX, dimY;
ImageOrigin imageOrigin;

// === Visualization parameters
PersistentValue<FilterMode> filterMode; // default is FilterMode::Linear
};

} // namespace polyscope

#include "polyscope/texture_map_quantity.ipp"
32 changes: 32 additions & 0 deletions include/polyscope/texture_map_quantity.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run

namespace polyscope {

template <typename QuantityT>
TextureMapQuantity<QuantityT>::TextureMapQuantity(QuantityT& quantity_, size_t dimX_, size_t dimY_, ImageOrigin origin_)
: quantity(quantity_), dimX(dimX_), dimY(dimY_), imageOrigin(origin_),
filterMode(quantity.uniquePrefix() + "filterMode", FilterMode::Linear) {}

template <typename QuantityT>
void TextureMapQuantity<QuantityT>::buildTextureMapOptionsUI() {

if (ImGui::BeginMenu("Filter Mode")) {
if (ImGui::MenuItem("linear", NULL, filterMode.get() == FilterMode::Linear)) setFilterMode(FilterMode::Linear);
if (ImGui::MenuItem("nearest", NULL, filterMode.get() == FilterMode::Nearest)) setFilterMode(FilterMode::Nearest);
ImGui::EndMenu();
}
}

template <typename QuantityT>
QuantityT* TextureMapQuantity<QuantityT>::setFilterMode(FilterMode newFilterMode) {
filterMode = newFilterMode;
quantity.refresh();
return &quantity;
}

template <typename QuantityT>
FilterMode TextureMapQuantity<QuantityT>::getFilterMode() {
return filterMode.get();
}

} // namespace polyscope
1 change: 1 addition & 0 deletions include/polyscope/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum class VolumeCellType { TET = 0, HEX };

enum class ImplicitRenderMode { SphereMarch, FixedStep };
enum class ImageOrigin { LowerLeft, UpperLeft };
enum class FilterMode { Nearest = 0, Linear };

enum class ParamCoordsType { UNIT = 0, WORLD }; // UNIT -> [0,1], WORLD -> length-valued
enum class ParamVizStyle {
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ SET(HEADERS
${INCLUDE_ROOT}/surface_parameterization_quantity.h
${INCLUDE_ROOT}/surface_scalar_quantity.h
${INCLUDE_ROOT}/surface_vector_quantity.h
${INCLUDE_ROOT}/texture_map_quantity.h
${INCLUDE_ROOT}/texture_map_quantity.ipp
${INCLUDE_ROOT}/types.h
${INCLUDE_ROOT}/utilities.h
${INCLUDE_ROOT}/view.h
Expand Down
1 change: 1 addition & 0 deletions src/persistent_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ PersistentCache<std::vector<std::string>> persistentCache_vectorstring;
PersistentCache<ParamVizStyle> persistentCache_paramVizStyle;
PersistentCache<BackFacePolicy> persistentCache_BackFacePolicy;
PersistentCache<MeshShadeStyle> persistentCache_MeshNormalType;
PersistentCache<FilterMode> persistentCache_FilterMode;
// clang-format on
} // namespace detail
} // namespace polyscope
39 changes: 36 additions & 3 deletions src/surface_color_quantity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ void SurfaceColorQuantity::draw() {
program->draw();
}

void SurfaceColorQuantity::buildCustomUI() {
ImGui::SameLine();

// == Options popup
if (ImGui::Button("Options")) {
ImGui::OpenPopup("OptionsPopup");
}
if (ImGui::BeginPopup("OptionsPopup")) {

buildColorOptionsUI();

ImGui::EndPopup();
}

buildColorUI();
}


// ========================================================
// ========== Vertex Color ==========
// ========================================================
Expand Down Expand Up @@ -56,6 +74,11 @@ void SurfaceVertexColorQuantity::createProgram() {
render::engine->setMaterial(*program, parent.getMaterial());
}

void SurfaceVertexColorQuantity::buildColorOptionsUI() {
ColorQuantity::buildColorOptionsUI();
ImGui::TextUnformatted("(no options available)"); // remove once there is something in this menu
}

void SurfaceVertexColorQuantity::buildVertexInfoGUI(size_t vInd) {
ImGui::TextUnformatted(name.c_str());
ImGui::NextColumn();
Expand Down Expand Up @@ -104,6 +127,11 @@ void SurfaceFaceColorQuantity::createProgram() {
render::engine->setMaterial(*program, parent.getMaterial());
}

void SurfaceFaceColorQuantity::buildColorOptionsUI() {
ColorQuantity::buildColorOptionsUI();
ImGui::TextUnformatted("(no options available)"); // remove once there is something in this menu
}

void SurfaceFaceColorQuantity::buildFaceInfoGUI(size_t fInd) {
ImGui::TextUnformatted(name.c_str());
ImGui::NextColumn();
Expand All @@ -125,8 +153,8 @@ SurfaceTextureColorQuantity::SurfaceTextureColorQuantity(std::string name, Surfa
SurfaceParameterizationQuantity& param_, size_t dimX_,
size_t dimY_, std::vector<glm::vec3> colorValues_,
ImageOrigin origin_)
: SurfaceColorQuantity(name, mesh_, "texture", colorValues_), param(param_), dimX(dimX_), dimY(dimY_),
imageOrigin(origin_) {
: SurfaceColorQuantity(name, mesh_, "texture", colorValues_), TextureMapQuantity(*this, dimX_, dimY_, origin_),
param(param_) {
colors.setTextureSize(dimX, dimY);
}

Expand Down Expand Up @@ -162,7 +190,12 @@ void SurfaceTextureColorQuantity::createProgram() {
program->setTextureFromBuffer("t_color", colors.getRenderTextureBuffer().get());
render::engine->setMaterial(*program, parent.getMaterial());

colors.getRenderTextureBuffer()->setFilterMode(FilterMode::Linear);
colors.getRenderTextureBuffer()->setFilterMode(filterMode.get());
}

void SurfaceTextureColorQuantity::buildColorOptionsUI() {
ColorQuantity::buildColorOptionsUI();
buildTextureMapOptionsUI();
}


Expand Down
9 changes: 6 additions & 3 deletions src/surface_scalar_quantity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void SurfaceScalarQuantity::buildCustomUI() {
if (ImGui::BeginPopup("OptionsPopup")) {

buildScalarOptionsUI();
buildSurfaceScalarOptionsUI();

ImGui::EndPopup();
}
Expand Down Expand Up @@ -293,8 +294,8 @@ SurfaceTextureScalarQuantity::SurfaceTextureScalarQuantity(std::string name, Sur
SurfaceParameterizationQuantity& param_, size_t dimX_,
size_t dimY_, const std::vector<float>& values_,
ImageOrigin origin_, DataType dataType_)
: SurfaceScalarQuantity(name, mesh_, "vertex", values_, dataType_), param(param_), dimX(dimX_), dimY(dimY_),
imageOrigin(origin_) {
: SurfaceScalarQuantity(name, mesh_, "vertex", values_, dataType_),
TextureMapQuantity(*this, dimX_, dimY_, origin_), param(param_) {
values.setTextureSize(dimX, dimY);
values.ensureHostBufferPopulated();
hist.buildHistogram(values.data);
Expand Down Expand Up @@ -334,9 +335,11 @@ void SurfaceTextureScalarQuantity::createProgram() {
render::engine->setMaterial(*program, parent.getMaterial());
program->setTextureFromColormap("t_colormap", cMap.get());

values.getRenderTextureBuffer()->setFilterMode(FilterMode::Linear);
values.getRenderTextureBuffer()->setFilterMode(filterMode.get());
}

void SurfaceTextureScalarQuantity::buildSurfaceScalarOptionsUI() { buildTextureMapOptionsUI(); }

std::shared_ptr<render::AttributeBuffer> SurfaceTextureScalarQuantity::getAttributeBuffer() {
exception("unsupported operation -- cannot get attribute buffer for texture scalar quantity [" + this->name + "]");
return std::shared_ptr<render::AttributeBuffer>(nullptr);
Expand Down

0 comments on commit db8c9ee

Please sign in to comment.