Add selection property to RenderableDUMeshes

This commit is contained in:
Malin E
2022-08-02 09:38:30 +02:00
parent 041a08adb6
commit 3ab97231bb
2 changed files with 110 additions and 5 deletions

View File

@@ -121,6 +121,12 @@ namespace {
"Debug option for rendering of billboards and texts"
};
constexpr openspace::properties::Property::PropertyInfo MeshSelectionInfo = {
"MeshSelection",
"Mesh Selection",
"Property to select one or more of the meshes to be rendered."
};
struct [[codegen::Dictionary(RenderableDUMeshes)]] Parameters {
// The path to the SPECK file that contains information about the astronomical
// object being rendered
@@ -160,6 +166,9 @@ namespace {
// [[codegen::verbatim(MeshColorInfo.description)]]
std::optional<std::vector<glm::vec3>> meshColor;
// [[codegen::verbatim(MeshSelectionInfo.description)]]
std::optional<std::vector<std::string>> selectedMeshes;
};
#include "renderabledumeshes_codegen.cpp"
} // namespace
@@ -185,6 +194,7 @@ RenderableDUMeshes::RenderableDUMeshes(const ghoul::Dictionary& dictionary)
)
, _lineWidth(LineWidthInfo, 2.f, 1.f, 16.f)
, _renderOption(RenderOptionInfo, properties::OptionProperty::DisplayType::Dropdown)
, _selectedMeshes(MeshSelectionInfo)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
@@ -248,6 +258,66 @@ RenderableDUMeshes::RenderableDUMeshes(const ghoul::Dictionary& dictionary)
_meshColorMap.insert({ static_cast<int>(i) + 1, ops[i] });
}
}
_selectedMeshes.onChange([this]() { selectionPropertyHasChanged(); });
addProperty(_selectedMeshes);
if (p.selectedMeshes.has_value()) {
const std::vector<std::string> options = _selectedMeshes.options();
std::set<std::string> selectedNames;
for (const std::string& s : *p.selectedMeshes) {
const auto it = std::find(options.begin(), options.end(), s);
if (it == options.end()) {
// The user has specified a mesh name that doesn't exist
LWARNING(fmt::format("Option '{}' not found in list of meshes", s));
}
else {
selectedNames.insert(s);
}
}
_selectedMeshes = selectedNames;
}
}
void RenderableDUMeshes::fillSelectionProperty() {
for (const std::pair<const int, RenderingMesh>& pair : _renderingMeshesMap) {
if (pair.second.name.empty()) {
continue;
}
auto it = std::find(
_selectedMeshes.options().begin(),
_selectedMeshes.options().end(),
pair.second.name
);
if (it != _selectedMeshes.options().end()) {
continue;
}
_selectedMeshes.addOption(pair.second.name);
}
if (_selectedMeshes.options().empty()) {
_selectedMeshes.setVisibility(properties::Property::Visibility::Developer);
}
else {
_selectedMeshes.setVisibility(properties::Property::Visibility::NoviceUser);
}
}
void RenderableDUMeshes::selectionPropertyHasChanged() {
// If no values are selected (the default), we want to show all constellations
if (!_selectedMeshes.hasSelected()) {
for (std::pair<const int, RenderingMesh>& pair : _renderingMeshesMap) {
pair.second.isEnabled = true;
}
}
else {
// Enable all constellations that are selected
for (std::pair<const int, RenderingMesh>& pair : _renderingMeshesMap) {
pair.second.isEnabled = _selectedMeshes.isSelected(pair.second.name);
}
}
}
bool RenderableDUMeshes::isReady() const {
@@ -322,6 +392,10 @@ void RenderableDUMeshes::renderMeshes(const RenderData&,
_program->setUniform(_uniformCache.alphaValue, opacity());
for (const std::pair<const int, RenderingMesh>& pair : _renderingMeshesMap) {
if (!pair.second.isEnabled) {
continue;
}
_program->setUniform(_uniformCache.color, _meshColorMap[pair.second.colorIndex]);
for (size_t i = 0; i < pair.second.vaoArray.size(); ++i) {
glBindVertexArray(pair.second.vaoArray[i]);
@@ -535,9 +609,26 @@ bool RenderableDUMeshes::readSpeckFile() {
} while (dummy != "{");
std::getline(file, line);
std::stringstream dim(line);
dim >> mesh.numU; // numU
dim >> mesh.numV; // numV
std::stringstream dimOrName(line);
std::string dummyU, dummyV;
// Try to read name of mesh if it exist
dimOrName >> dummyU; // numU or "name"
dimOrName >> dummyV; // numV or the name of the mesh
if (dummyU == "name") {
mesh.name = dummyV;
// Dimensions are specified in the next line as usual
std::getline(file, line);
std::stringstream dim(line);
dim >> mesh.numU; // numU
dim >> mesh.numV; // numV
}
else {
mesh.numU = stoi(dummyU);
mesh.numV = stoi(dummyV);
}
// We can now read the vertices data:
for (int l = 0; l < mesh.numU * mesh.numV; ++l) {
@@ -603,6 +694,7 @@ bool RenderableDUMeshes::readSpeckFile() {
}
}
fillSelectionProperty();
setBoundingSphere(maxRadius);
return true;
@@ -636,7 +728,7 @@ void RenderableDUMeshes::createMeshes() {
// in_position
glEnableVertexAttribArray(0);
// (2022-03-23, emmbr) This code was actually never used. We only read three
// values per line and di not handle any texture cooridnates, even if there
// values per line and did not handle any texture cooridnates, even if there
// would have been some in the file
//// U and V may not be given by the user
//if (p.second.vertices.size() / (p.second.numU * p.second.numV) > 3) {

View File

@@ -29,6 +29,7 @@
#include <modules/space/speckloader.h>
#include <openspace/properties/optionproperty.h>
#include <openspace/properties/selectionproperty.h>
#include <openspace/properties/stringproperty.h>
#include <openspace/properties/scalar/boolproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
@@ -84,13 +85,15 @@ private:
// "If you wish to draw a line between points, then numU will be 1 while
// numV will equal the number of points to connect.
// If you want a square, 4000×4000 grid with lines every 200 units,
// then numU numU will both equal 21
// then numU numV will both equal 21
int numU;
int numV;
MeshType style;
std::vector<GLuint> vaoArray;
std::vector<GLuint> vboArray;
std::vector<GLfloat> vertices;
bool isEnabled = true;
std::string name;
};
void createMeshes();
@@ -102,6 +105,15 @@ private:
bool loadData();
bool readSpeckFile();
/// Fills the <code>_selectedMeshes</code> property with all available meshes
void fillSelectionProperty();
/**
* Callback method that gets triggered when <code>_selectedMeshes</code>
* changes.
*/
void selectionPropertyHasChanged();
bool _hasSpeckFile = false;
bool _dataIsDirty = true;
bool _textColorIsDirty = true;
@@ -114,6 +126,7 @@ private:
properties::BoolProperty _drawLabels;
properties::IVec2Property _textMinMaxSize;
properties::FloatProperty _lineWidth;
properties::SelectionProperty _selectedMeshes;
// DEBUG:
properties::OptionProperty _renderOption;