/***************************************************************************************** * * * OpenSpace * * * * Copyright (c) 2014-2025 * * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * * software and associated documentation files (the "Software"), to deal in the Software * * without restriction, including without limitation the rights to use, copy, modify, * * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * * permit persons to whom the Software is furnished to do so, subject to the following * * conditions: * * * * The above copyright notice and this permission notice shall be included in all copies * * or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include "SpiceUsr.h" namespace { constexpr std::string_view _loggerCat = "RenderableConstellationBounds"; constexpr float convertHrsToRadians(float rightAscension) { // 360 degrees / 24h = 15 degrees/h return glm::radians(rightAscension * 15); } constexpr openspace::properties::Property::PropertyInfo VertexInfo = { "File", "Vertex File Path", "A file that contains the vertex locations of the constellations bounds, as RA " "Dec coordinates on the celestial sphere.", openspace::properties::Property::Visibility::AdvancedUser }; constexpr openspace::properties::Property::PropertyInfo ColorInfo = { "Color", "Color", "The color of the lines.", openspace::properties::Property::Visibility::NoviceUser }; // This `Renderable` type can be used to draw bounding shapes on the night sky, where // each shape encapsulates a group of night sky objects, such as the stars of a // constellation. // // The shapes are defined in a file where each line specifies a vertex location in RA // Dec coordinates on the celestial sphere. Each coordinate must also be marked with // an abbreviation for the corresponding constellation that the shapes encapsulates. // This gives each line the following order: `RA Dec Abbreviation`. The units for the // coordinate values are hours for RA, and degrees for Dec. An example of a line // corresponding to a vertex location may look like this: // `23.5357132 +35.1897736 AND`, where `AND` is the identifier of the constellation. // In this case it is short for Andromeda. // // The abbreviations act as identifiers of the individual constellations and can // be mapped to full names in the optional `NamesFile`. The names in this file are // then the ones that will show in the user interface, for example. A line in the // file should first include the abbreviation and then the full name. For example, // for the `AND` abbreviation in the example above, the line would look like this: // `AND Andromeda`. // // If labels were added, the full names in the `NamesFile` may also be used for the // text of the labels. Note that labels are added using a different file, where each // line may or may not include an identifier for that specific label, marked by `id` // in the file. If a row in the label file has an `id` that matches the abbreviation // of the constellation, the text of that label is replaced with the full name from // the `NamesFile`. struct [[codegen::Dictionary(RenderableConstellationBounds)]] Parameters { // [[codegen::verbatim(VertexInfo.description)]] std::filesystem::path file; // [[codegen::verbatim(ColorInfo.description)]] std::optional color [[codegen::color()]]; }; #include "renderableconstellationbounds_codegen.cpp" } // namespace namespace openspace { documentation::Documentation RenderableConstellationBounds::Documentation() { return codegen::doc( "space_renderable_constellationbounds", RenderableConstellationsBase::Documentation() ); } RenderableConstellationBounds::RenderableConstellationBounds( const ghoul::Dictionary& dictionary) : RenderableConstellationsBase(dictionary) , _vertexFilename(VertexInfo) , _color(ColorInfo, glm::vec3(1.f, 0.f, 0.f), glm::vec3(0.f), glm::vec3(1.f)) { const Parameters p = codegen::bake(dictionary); // Avoid reading files here, instead do it in multithreaded initialize() _vertexFilename = p.file.string(); _vertexFilename.onChange([this](){ loadData(); }); addProperty(_vertexFilename); _color.setViewOption(properties::Property::ViewOptions::Color); _color = p.color.value_or(_color); addProperty(_color); } void RenderableConstellationBounds::initialize() { RenderableConstellationsBase::initialize(); loadData(); if (!_assetSelection.empty()) { const std::vector options = _selection.options(); std::set selectedConstellations; for (const std::string& s : _assetSelection) { auto it = std::find(options.cbegin(), options.cend(), s); if (it == options.cend()) { // Test if the provided name was an identifier instead of the full name it = std::find( options.cbegin(), options.cend(), constellationFullName(s) ); if (it == options.cend()) { // The user has specified a constellation name that doesn't exist LWARNING(std::format( "Option '{}' not found in list of constellations", s )); } else { selectedConstellations.insert(constellationFullName(s)); } } else { selectedConstellations.insert(s); } } _selection = selectedConstellations; } } void RenderableConstellationBounds::initializeGL() { _program = global::renderEngine->buildRenderProgram( "ConstellationBounds", absPath("${MODULE_SPACE}/shaders/constellationbounds_vs.glsl"), absPath("${MODULE_SPACE}/shaders/constellationbounds_fs.glsl") ); ghoul::opengl::updateUniformLocations(*_program, _uniformCache); glGenVertexArrays(1, &_vao); glBindVertexArray(_vao); glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData( GL_ARRAY_BUFFER, _vertexValues.size() * 3 * sizeof(float), _vertexValues.data(), GL_STATIC_DRAW ); const GLint positionAttrib = _program->attributeLocation("in_position"); glEnableVertexAttribArray(positionAttrib); glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, nullptr); glBindVertexArray(0); } void RenderableConstellationBounds::deinitializeGL() { glDeleteBuffers(1, &_vbo); _vbo = 0; glDeleteVertexArrays(1, &_vao); _vao = 0; if (_program) { global::renderEngine->removeRenderProgram(_program.get()); _program = nullptr; } } bool RenderableConstellationBounds::isReady() const { bool isReady = _program && _vao != 0 && _vbo != 0; // If we have labels, they also need to be loaded if (_hasLabels) { isReady = isReady && RenderableConstellationsBase::isReady(); } return isReady; } void RenderableConstellationBounds::render(const RenderData& data, RendererTasks& tasks) { _program->activate(); _program->setUniform( _uniformCache.campos, glm::vec4(data.camera.positionVec3(), 1.f) ); _program->setUniform( _uniformCache.objpos, glm::vec4(data.modelTransform.translation, 0.f) ); _program->setUniform( _uniformCache.camrot, glm::mat4(data.camera.viewRotationMatrix()) ); _program->setUniform(_uniformCache.scaling, glm::vec2(1.f, 0.f)); const glm::dmat4 modelTransform = calcModelTransform(data); _program->setUniform( _uniformCache.ViewProjection, data.camera.viewProjectionMatrix() ); _program->setUniform(_uniformCache.ModelTransform, glm::mat4(modelTransform)); _program->setUniform(_uniformCache.color, _color); _program->setUniform(_uniformCache.opacity, opacity()); glLineWidth(_lineWidth); glBindVertexArray(_vao); for (const ConstellationBound& bound : _constellationBounds) { if (bound.isEnabled) { glDrawArrays(GL_LINE_LOOP, bound.startIndex, bound.nVertices); } } glBindVertexArray(0); _program->deactivate(); RenderableConstellationsBase::render(data, tasks); } bool RenderableConstellationBounds::loadData() { const bool success = loadVertexFile(); if (!success) { throw ghoul::RuntimeError("Error loading data"); } return success; } bool RenderableConstellationBounds::loadVertexFile() { if (_vertexFilename.value().empty()) { return false; } std::filesystem::path fileName = absPath(_vertexFilename); std::ifstream file = std::ifstream(fileName); if (!file.good()) { return false; } ConstellationBound currentBound; std::string currentLine; int currentLineNumber = 1; // Overview of the reading algorithm: // We keep an active ConstellationBound (currentBound) and update it until we read // a new constellation name, at which point the currentBound is stored away, a new, // empty ConstellationBound is created and set at the currentBound while (file.good()) { ghoul::getline(file, currentLine); if (currentLine.empty()) { continue; } // @CHECK: Is this the best way of doing this? ---abock std::stringstream s(currentLine); float ra = 0.f; s >> ra; float dec = 0.f; s >> dec; std::string abbreviation; s >> abbreviation; if (!s.good()) { // If this evaluates to true, the stream was not completely filled, which // means that the line was incomplete, so there was an error LERROR(std::format( "Error reading file '{}' at line #{}", fileName, currentLineNumber )); break; } // Did we arrive at a new constellation? if (abbreviation != currentBound.constellationAbbreviation) { // Store how many vertices we read during the active time of the constellation currentBound.nVertices = static_cast( _vertexValues.size() - currentBound.startIndex ); // Store the constellation and start a new one _constellationBounds.push_back(currentBound); currentBound = ConstellationBound(); currentBound.isEnabled = true; currentBound.constellationAbbreviation = abbreviation; std::string name = constellationFullName(abbreviation); currentBound.constellationFullName = name.empty() ? abbreviation : std::move(name); currentBound.startIndex = static_cast(_vertexValues.size()); } // The file format stores the right ascension in hours, while SPICE expects them // to be in radians ra = convertHrsToRadians(ra); // Likewise, the declination is stored in degrees and needs to be converted dec = glm::radians(dec); // Convert the (right ascension, declination) to rectangular coordinates. // The 1.0 is the distance of the celestial sphere, we will scale that in the // render function std::array rectangularValues; radrec_c(1.0, ra, dec, rectangularValues.data()); // Add the new vertex to our list of vertices _vertexValues.push_back({ static_cast(rectangularValues[0]), static_cast(rectangularValues[1]), static_cast(rectangularValues[2]) }); ++currentLineNumber; } // Due to the way we read the file, the first (empty) constellation bounds will not // contain any valid values. So we have to remove it _constellationBounds.erase(_constellationBounds.begin()); // And we still have the one value that was left when we exited the loop currentBound.nVertices = static_cast( _vertexValues.size() - currentBound.startIndex ); _constellationBounds.push_back(currentBound); return true; } void RenderableConstellationBounds::selectionPropertyHasChanged() { // If no values are selected (the default), we want to show all constellations if (!_selection.hasSelected()) { for (ConstellationBound& b : _constellationBounds) { b.isEnabled = true; } } else { // Enable all constellations that are selected for (ConstellationBound& b : _constellationBounds) { b.isEnabled = _selection.isSelected(b.constellationFullName); } } } } // namespace openspace