From 54dcdcf0f1cdf1cc674473681d2360e1439e3138 Mon Sep 17 00:00:00 2001 From: Malin E Date: Tue, 9 Aug 2022 08:54:41 +0200 Subject: [PATCH] Some clean up --- .../digitaluniverse/constellations.asset | 5 +- .../rendering/renderableconstellation.cpp | 33 +++- .../space/rendering/renderableconstellation.h | 37 ++-- .../renderableconstellationbounds.cpp | 2 +- .../rendering/renderableconstellationbounds.h | 9 +- .../renderableconstellationlines.cpp | 164 ++++++++---------- .../rendering/renderableconstellationlines.h | 47 ++--- .../space/shaders/constellationbounds_fs.glsl | 11 +- 8 files changed, 145 insertions(+), 163 deletions(-) diff --git a/data/assets/scene/digitaluniverse/constellations.asset b/data/assets/scene/digitaluniverse/constellations.asset index 3c0bcafa09..62f4d4f1e8 100644 --- a/data/assets/scene/digitaluniverse/constellations.asset +++ b/data/assets/scene/digitaluniverse/constellations.asset @@ -39,8 +39,9 @@ local constellations = { TextOpacity = 0.3, TextSize = 14.5, TextMinMaxSize = { 8, 170 }, - MeshColor = { { 0.6, 0.4, 0.4 }, { 0.8, 0.0, 0.0 }, { 0.0, 0.3, 0.8 } }, - Unit = "pc" + LabelUnit = "pc", + ConstellationColor = { { 0.6, 0.4, 0.4 }, { 0.8, 0.0, 0.0 }, { 0.0, 0.3, 0.8 } }, + ConstellationUnit = "pc" }, GUI = { Name = "Constellations", diff --git a/modules/space/rendering/renderableconstellation.cpp b/modules/space/rendering/renderableconstellation.cpp index c7093c8009..4d7c96b101 100644 --- a/modules/space/rendering/renderableconstellation.cpp +++ b/modules/space/rendering/renderableconstellation.cpp @@ -104,6 +104,12 @@ namespace { "Debug option for rendering of billboards and texts" }; + constexpr openspace::properties::Property::PropertyInfo LabelUnitInfo = { + "LabelUnit", + "Label Unit", + "The unit used for the label data" + }; + constexpr openspace::properties::Property::PropertyInfo SelectionInfo = { "ConstellationSelection", "Constellation Selection", @@ -135,6 +141,18 @@ namespace { // [[codegen::verbatim(LineWidthInfo.description)]] std::optional lineWidth; + enum class [[codegen::map(openspace::DistanceUnit)]] Unit { + Meter [[codegen::key("m")]], + Kilometer [[codegen::key("Km")]], + Parsec [[codegen::key("pc")]], + Kiloparsec [[codegen::key("Kpc")]], + Megaparsec [[codegen::key("Mpc")]], + Gigaparsec [[codegen::key("Gpc")]], + Gigalightyear [[codegen::key("Gly")]] + }; + // [[codegen::verbatim(LabelUnitInfo.description)]] + std::optional labelUnit; + // [[codegen::verbatim(SelectionInfo.description)]] std::optional> constellationSelection; }; @@ -200,7 +218,6 @@ RenderableConstellation::RenderableConstellation(const ghoul::Dictionary& dictio _hasLabel = p.textColor.has_value(); _textColor.setViewOption(properties::Property::ViewOptions::Color); addProperty(_textColor); - _textColor.onChange([&]() { _textColorIsDirty = true; }); _textOpacity = p.textOpacity.value_or(_textOpacity); addProperty(_textOpacity); @@ -211,12 +228,20 @@ RenderableConstellation::RenderableConstellation(const ghoul::Dictionary& dictio _textMinMaxSize = p.textMinMaxSize.value_or(_textMinMaxSize); _textMinMaxSize.setViewOption(properties::Property::ViewOptions::MinMaxRange); addProperty(_textMinMaxSize); + + if (p.labelUnit.has_value()) { + _labelUnit = codegen::map(*p.labelUnit); + } + else { + _labelUnit = DistanceUnit::Meter; + } } _constellationSelection.onChange([this]() { selectionPropertyHasChanged(); }); addProperty(_constellationSelection); - _assetSelectedMeshes = p.constellationSelection.value_or(_assetSelectedMeshes); + _assetSelectedConstellations = + p.constellationSelection.value_or(_assetSelectedConstellations); } std::string RenderableConstellation::constellationFullName( @@ -278,11 +303,11 @@ void RenderableConstellation::fillSelectionProperty() { void RenderableConstellation::initialize() { loadConstellationFile(); - if (!_assetSelectedMeshes.empty()) { + if (!_assetSelectedConstellations.empty()) { const std::vector options = _constellationSelection.options(); std::set selectedConstellations; - for (const std::string& s : _assetSelectedMeshes) { + for (const std::string& s : _assetSelectedConstellations) { 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 diff --git a/modules/space/rendering/renderableconstellation.h b/modules/space/rendering/renderableconstellation.h index b8d4b1a548..514cffe511 100644 --- a/modules/space/rendering/renderableconstellation.h +++ b/modules/space/rendering/renderableconstellation.h @@ -29,10 +29,7 @@ #include #include -#include -#include #include -#include #include #include #include @@ -70,52 +67,54 @@ protected: /** * Callback method that gets triggered when _constellationSelection - * changes. + * changes */ virtual void selectionPropertyHasChanged() = 0; + /// Takes the given constellation identifier and returns the coresponding + /// full name std::string constellationFullName(const std::string& identifier) const; - // Linewidth for the constellation bounds + // Width for the rendered lines properties::FloatProperty _lineWidth; - /// The property that stores all indices of constellations that should be drawn + // Property that stores all constellations chosen by the user to be drawn properties::SelectionProperty _constellationSelection; + // Label text settings bool _hasLabel = false; - properties::BoolProperty _drawLabels; speck::Labelset _labelset; + properties::BoolProperty _drawLabels; private: - // Map over the constellations names and theis abbreviations - // key = abbreviations, value = full name + // Map over the constellations names and their abbreviations + // key = abbreviation, value = full name std::map _constellationNamesTranslation; - std::vector _assetSelectedMeshes; + // Temporary storage of which constellations should be rendered as stated in the + // asset file + std::vector _assetSelectedConstellations; /** - * Loads the file specified in _constellationNamesFilename that contains the mapping - * between abbreviations and full names of constellations. - * - * \return true if the loading succeeded, false otherwise + * Loads the file specified in _constellationNamesFilename that contains + * the mapping between abbreviations and full names of constellations */ void loadConstellationFile(); /// Fills the _constellationSelection property with all constellations void fillSelectionProperty(); - /// The file containing constellation names and abbreviations + // The file containing constellation names and abbreviations properties::StringProperty _constellationNamesFilename; - //Label text settings + // Label text settings std::string _labelFile; + std::shared_ptr _font = nullptr; + DistanceUnit _labelUnit = DistanceUnit::Parsec; properties::Vec3Property _textColor; - bool _textColorIsDirty = true; properties::FloatProperty _textOpacity; properties::FloatProperty _textSize; properties::IVec2Property _textMinMaxSize; - std::shared_ptr _font = nullptr; - DistanceUnit _labelUnit = DistanceUnit::Parsec; properties::OptionProperty _renderOption; }; diff --git a/modules/space/rendering/renderableconstellationbounds.cpp b/modules/space/rendering/renderableconstellationbounds.cpp index f58f038579..a0ac82f046 100644 --- a/modules/space/rendering/renderableconstellationbounds.cpp +++ b/modules/space/rendering/renderableconstellationbounds.cpp @@ -162,6 +162,7 @@ void RenderableConstellationBounds::render(const RenderData& data, RendererTasks _program->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); _program->setUniform("ModelTransform", glm::mat4(modelTransform)); _program->setUniform("color", _color); + _program->setUniform("alphaValue", opacity()); glLineWidth(_lineWidth); @@ -178,7 +179,6 @@ void RenderableConstellationBounds::render(const RenderData& data, RendererTasks } void RenderableConstellationBounds::update(const UpdateData& data) { - } bool RenderableConstellationBounds::loadVertexFile() { diff --git a/modules/space/rendering/renderableconstellationbounds.h b/modules/space/rendering/renderableconstellationbounds.h index 877187a4c4..eacb03aca9 100644 --- a/modules/space/rendering/renderableconstellationbounds.h +++ b/modules/space/rendering/renderableconstellationbounds.h @@ -27,11 +27,6 @@ #include -#include -#include -#include -#include - namespace ghoul::opengl { class ProgramObject; } namespace openspace { @@ -94,11 +89,11 @@ private: /// Determines the color of the constellation lines properties::Vec3Property _color; - std::unique_ptr _program; - /// The list of all loaded constellation bounds std::vector _constellationBounds; + std::unique_ptr _program; + struct Vertex { float x; float y; diff --git a/modules/space/rendering/renderableconstellationlines.cpp b/modules/space/rendering/renderableconstellationlines.cpp index 9a3b8984bf..6f46b952c8 100644 --- a/modules/space/rendering/renderableconstellationlines.cpp +++ b/modules/space/rendering/renderableconstellationlines.cpp @@ -24,7 +24,6 @@ #include -#include #include #include #include @@ -56,24 +55,26 @@ namespace { "modelViewTransform", "projectionTransform", "alphaValue", "color" }; - constexpr int RenderOptionViewDirection = 0; - constexpr int RenderOptionPositionNormal = 1; - constexpr openspace::properties::Property::PropertyInfo DrawElementsInfo = { "DrawElements", "Draw Elements", - "Enables/Disables the drawing of the astronomical objects" + "Enables/Disables the drawing of the constellations" }; - constexpr openspace::properties::Property::PropertyInfo MeshColorInfo = { - "MeshColor", - "Meshes colors", - "The defined colors for the meshes to be rendered" + constexpr openspace::properties::Property::PropertyInfo ConstellationUnitInfo = { + "ConstellationUnit", + "Constellation Unit", + "The unit used for the constellation data" + }; + + constexpr openspace::properties::Property::PropertyInfo ConstellationColorInfo = { + "ConstellationColor", + "Constellation colors", + "The defined colors for the constellations to be rendered" }; struct [[codegen::Dictionary(RenderableConstellationLines)]] Parameters { - // The path to the SPECK file that contains information about the astronomical - // object being rendered + // The path to the SPECK file that contains constellation lines data std::string file; enum class [[codegen::map(openspace::DistanceUnit)]] Unit { @@ -85,10 +86,11 @@ namespace { Gigaparsec [[codegen::key("Gpc")]], Gigalightyear [[codegen::key("Gly")]] }; - std::optional unit; + // [[codegen::verbatim(ConstellationUnitInfo.description)]] + std::optional constellationUnit; - // [[codegen::verbatim(MeshColorInfo.description)]] - std::optional> meshColor; + // [[codegen::verbatim(ConstellationColorInfo.description)]] + std::optional> constellationColor; }; #include "renderableconstellationlines_codegen.cpp" } // namespace @@ -111,17 +113,17 @@ RenderableConstellationLines::RenderableConstellationLines( _drawElements.onChange([&]() { _hasSpeckFile = !_hasSpeckFile; }); addProperty(_drawElements); - if (p.unit.has_value()) { - _unit = codegen::map(*p.unit); + if (p.constellationUnit.has_value()) { + _constellationUnit = codegen::map(*p.constellationUnit); } else { - _unit = DistanceUnit::Meter; + _constellationUnit = DistanceUnit::Meter; } - if (p.meshColor.has_value()) { - std::vector ops = *p.meshColor; + if (p.constellationColor.has_value()) { + std::vector ops = *p.constellationColor; for (size_t i = 0; i < ops.size(); ++i) { - _meshColorMap.insert({ static_cast(i) + 1, ops[i] }); + _constellationColorMap.insert({ static_cast(i) + 1, ops[i] }); } } } @@ -129,13 +131,17 @@ RenderableConstellationLines::RenderableConstellationLines( void RenderableConstellationLines::selectionPropertyHasChanged() { // If no values are selected (the default), we want to show all constellations if (!_constellationSelection.hasSelected()) { - for (std::pair& pair : _renderingMeshesMap) { + for (std::pair& pair : + _renderingConstellationsMap) + { pair.second.isEnabled = true; } } else { // Enable all constellations that are selected - for (std::pair& pair : _renderingMeshesMap) { + for (std::pair& pair : + _renderingConstellationsMap) + { pair.second.isEnabled = _constellationSelection.isSelected(pair.second.identifier); } @@ -143,7 +149,7 @@ void RenderableConstellationLines::selectionPropertyHasChanged() { } bool RenderableConstellationLines::isReady() const { - return (_program != nullptr) && !_renderingMeshesMap.empty() && + return (_program != nullptr) && !_renderingConstellationsMap.empty() && !_labelset.entries.empty(); } @@ -157,44 +163,37 @@ void RenderableConstellationLines::initialize() { } void RenderableConstellationLines::initializeGL() { - _program = DigitalUniverseModule::ProgramObjectManager.request( + _program = global::renderEngine->buildRenderProgram( "RenderableConstellationLines", - []() { - return global::renderEngine->buildRenderProgram( - "RenderableConstellationLines", - absPath("${MODULE_SPACE}/shaders/constellationlines_vs.glsl"), - absPath("${MODULE_SPACE}/shaders/constellationlines_fs.glsl") - ); - } + absPath("${MODULE_SPACE}/shaders/constellationlines_vs.glsl"), + absPath("${MODULE_SPACE}/shaders/constellationlines_fs.glsl") ); ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames); - createMeshes(); + createConstellations(); } void RenderableConstellationLines::deinitialize() { } void RenderableConstellationLines::deinitializeGL() { - for (const std::pair& pair : _renderingMeshesMap) { + for (const std::pair& pair : _renderingConstellationsMap) { for (int i = 0; i < pair.second.numU; ++i) { glDeleteVertexArrays(1, &pair.second.vaoArray[i]); glDeleteBuffers(1, &pair.second.vboArray[i]); } } - DigitalUniverseModule::ProgramObjectManager.release( - "RenderableConstellationLines", - [](ghoul::opengl::ProgramObject* p) { - global::renderEngine->removeRenderProgram(p); - } - ); + if (_program) { + global::renderEngine->removeRenderProgram(_program.get()); + _program = nullptr; + } } -void RenderableConstellationLines::renderMeshes(const RenderData&, - const glm::dmat4& modelViewMatrix, - const glm::dmat4& projectionMatrix) +void RenderableConstellationLines::renderConstellations(const RenderData&, + const glm::dmat4& modelViewMatrix, + const glm::dmat4& projectionMatrix) { glEnablei(GL_BLEND, 0); glBlendFunc(GL_SRC_ALPHA, GL_ONE); @@ -208,28 +207,24 @@ void RenderableConstellationLines::renderMeshes(const RenderData&, _program->setUniform(_uniformCache.projectionTransform, projectionMatrix); _program->setUniform(_uniformCache.alphaValue, opacity()); - for (const std::pair& pair : _renderingMeshesMap) { + for (const std::pair& pair : + _renderingConstellationsMap) + { if (!pair.second.isEnabled) { continue; } - _program->setUniform(_uniformCache.color, _meshColorMap[pair.second.colorIndex]); + _program->setUniform( + _uniformCache.color, + _constellationColorMap[pair.second.colorIndex] + ); for (size_t i = 0; i < pair.second.vaoArray.size(); ++i) { glBindVertexArray(pair.second.vaoArray[i]); - switch (pair.second.style) { - case Solid: - break; - case Wire: - glLineWidth(_lineWidth); - glDrawArrays(GL_LINE_STRIP, 0, pair.second.numV); - global::renderEngine->openglStateCache().resetLineState(); - break; - case Point: - glDrawArrays(GL_POINTS, 0, pair.second.numV); - break; - default: - break; - } + + // Always render as lines + glLineWidth(_lineWidth); + glDrawArrays(GL_LINE_STRIP, 0, pair.second.numV); + global::renderEngine->openglStateCache().resetLineState(); } } @@ -251,7 +246,7 @@ void RenderableConstellationLines::render(const RenderData& data, RendererTasks& const glm::dmat4 projectionMatrix = data.camera.projectionMatrix(); if (_hasSpeckFile) { - renderMeshes(data, modelViewMatrix, projectionMatrix); + renderConstellations(data, modelViewMatrix, projectionMatrix); } RenderableConstellation::render(data, tasks); @@ -286,10 +281,10 @@ bool RenderableConstellationLines::readSpeckFile() { return false; } - const float scale = static_cast(toMeter(_unit)); + const float scale = static_cast(toMeter(_constellationUnit)); double maxRadius = 0.0; - int meshIndex = 0; + int lineIndex = 0; // The beginning of the speck file has a header that either contains comments // (signaled by a preceding '#') or information about the structure of the file @@ -324,8 +319,8 @@ bool RenderableConstellationLines::readSpeckFile() { // and style is solid, wire or point (for now we support only wire) std::stringstream str(line); - RenderingMesh mesh; - mesh.meshIndex = meshIndex; + ConstellationLine constellationLine; + constellationLine.lineIndex = lineIndex; std::string dummy; str >> dummy; // mesh command @@ -334,28 +329,11 @@ bool RenderableConstellationLines::readSpeckFile() { do { if (dummy == "-t") { dummy.clear(); - str >> mesh.textureIndex; // texture index + str >> constellationLine.textureIndex; // texture index } else if (dummy == "-c") { dummy.clear(); - str >> mesh.colorIndex; // color index command - } - else if (dummy == "-s") { - dummy.clear(); - str >> dummy; // style value command - if (dummy == "solid") { - mesh.style = Solid; - } - else if (dummy == "wire") { - mesh.style = Wire; - } - else if (dummy == "point") { - mesh.style = Point; - } - else { - mesh.style = INVALID; - break; - } + str >> constellationLine.colorIndex; // color index command } dummy.clear(); str >> dummy; @@ -371,21 +349,21 @@ bool RenderableConstellationLines::readSpeckFile() { if (dummyU == "id") { ghoul::trimWhitespace(dummyV); - mesh.identifier = constellationFullName(dummyV); + constellationLine.identifier = constellationFullName(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 + dim >> constellationLine.numU; // numU + dim >> constellationLine.numV; // numV } else { - mesh.numU = stoi(dummyU); - mesh.numV = stoi(dummyV); + constellationLine.numU = stoi(dummyU); + constellationLine.numV = stoi(dummyV); } // We can now read the vertices data: - for (int l = 0; l < mesh.numU * mesh.numV; ++l) { + for (int l = 0; l < constellationLine.numU * constellationLine.numV; ++l) { std::getline(file, line); if (line.substr(0, 1) == "}") { break; @@ -407,13 +385,13 @@ bool RenderableConstellationLines::readSpeckFile() { GLfloat scaledValue = value * scale; pos[i] = scaledValue; - mesh.vertices.push_back(scaledValue); + constellationLine.vertices.push_back(scaledValue); } if (!success) { LERROR(fmt::format( "Failed reading position on line {} of mesh {} in file: '{}'. " - "Stopped reading mesh data", l, meshIndex, _speckFile + "Stopped reading mesh data", l, lineIndex, _speckFile )); break; } @@ -440,7 +418,7 @@ bool RenderableConstellationLines::readSpeckFile() { std::getline(file, line); if (line.substr(0, 1) == "}") { - _renderingMeshesMap.insert({ meshIndex++, mesh }); + _renderingConstellationsMap.insert({ lineIndex++, constellationLine }); } else { return false; @@ -452,13 +430,13 @@ bool RenderableConstellationLines::readSpeckFile() { return true; } -void RenderableConstellationLines::createMeshes() { +void RenderableConstellationLines::createConstellations() { if (!(_dataIsDirty && _hasSpeckFile)) { return; } LDEBUG("Creating planes"); - for (std::pair& p : _renderingMeshesMap) { + for (std::pair& p : _renderingConstellationsMap) { for (int i = 0; i < p.second.numU; ++i) { GLuint vao; glGenVertexArrays(1, &vao); diff --git a/modules/space/rendering/renderableconstellationlines.h b/modules/space/rendering/renderableconstellationlines.h index 985ebaaac3..0742ce50c3 100644 --- a/modules/space/rendering/renderableconstellationlines.h +++ b/modules/space/rendering/renderableconstellationlines.h @@ -27,16 +27,6 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include @@ -69,15 +59,10 @@ public: static documentation::Documentation Documentation(); private: - enum MeshType { - Solid = 0, - Wire = 1, - Point = 2, - INVALID = 9 - }; - - struct RenderingMesh { - int meshIndex; + struct ConstellationLine { + bool isEnabled = true; + std::string identifier; + int lineIndex; int colorIndex; int textureIndex; // From: Partiview User's Guide @@ -90,47 +75,41 @@ private: // then numU numV will both equal 21 int numU; int numV; - MeshType style; std::vector vaoArray; std::vector vboArray; std::vector vertices; - bool isEnabled = true; - std::string identifier; }; - void createMeshes(); - void renderMeshes(const RenderData& data, const glm::dmat4& modelViewMatrix, + void createConstellations(); + void renderConstellations(const RenderData& data, const glm::dmat4& modelViewMatrix, const glm::dmat4& projectionMatrix); bool loadData(); bool readSpeckFile(); /** - * Callback method that gets triggered when _selectedMeshes - * changes. + * Callback method that gets triggered when _constellationSelection + * changes */ - void selectionPropertyHasChanged(); + void selectionPropertyHasChanged() override; bool _hasSpeckFile = false; bool _dataIsDirty = true; - bool _textColorIsDirty = true; - std::vector _assetSelectedMeshes; properties::BoolProperty _drawElements; - ghoul::opengl::ProgramObject* _program = nullptr; + std::unique_ptr _program = nullptr; UniformCache(modelViewTransform, projectionTransform, alphaValue, color) _uniformCache; - std::shared_ptr _font = nullptr; std::string _speckFile; - DistanceUnit _unit = DistanceUnit::Parsec; + DistanceUnit _constellationUnit = DistanceUnit::Parsec; std::vector _fullData; - std::unordered_map _meshColorMap; - std::unordered_map _renderingMeshesMap; + std::unordered_map _constellationColorMap; + std::unordered_map _renderingConstellationsMap; }; } // namespace openspace diff --git a/modules/space/shaders/constellationbounds_fs.glsl b/modules/space/shaders/constellationbounds_fs.glsl index 52237023b8..0d138b106d 100644 --- a/modules/space/shaders/constellationbounds_fs.glsl +++ b/modules/space/shaders/constellationbounds_fs.glsl @@ -28,13 +28,18 @@ in vec4 vs_position; uniform vec3 color; +uniform float alphaValue; Fragment getFragment() { - vec4 position = vs_position; - Fragment frag; - frag.color = vec4(color, 1.0); + if (alphaValue == 0.0) { + discard; + } + + vec4 position = vs_position; + + frag.color = vec4(color, alphaValue); frag.depth = pscDepth(position); return frag;