Merge pull request #2213 from OpenSpace/feature/du-meshes-selection

Add ability to select constellations
This commit is contained in:
Malin E
2022-09-02 13:23:29 +02:00
committed by GitHub
18 changed files with 1371 additions and 193 deletions

View File

@@ -1,7 +1,7 @@
local data = asset.syncedResource({
Name = "Constellation Bounds Data",
Name = "Constellation Files",
Type = "HttpSynchronization",
Identifier = "digitaluniverse_constellationbounds_data",
Identifier = "digitaluniverse_constellations_data",
Version = 1
})
@@ -16,8 +16,8 @@ local object = {
Type = "RenderableConstellationBounds",
Enabled = false,
File = data .. "bound_20.dat",
ConstellationFile = data .. "constellations.dat"
-- ConstellationSelection = zodiacs
NamesFile = data .. "constellations.dat",
-- Selection = zodiacs
},
Transform = {
Rotation = {
@@ -41,18 +41,18 @@ local object = {
asset.onInitialize(function()
openspace.addSceneGraphNode(object)
end)
asset.onDeinitialize(function()
openspace.removeSceneGraphNode(object)
end)
asset.export(object)
asset.meta = {
Name = "Constellation Bounds",
Version = "1.1",
Version = "1.2",
Description = [[DU asset providing a Spherical mesh dividing the sky into regions that
fit the constellations]],
Author = "Brian Abbott (AMNH)",

View File

@@ -1,24 +1,32 @@
local speck = asset.syncedResource({
Name = "Constellation Speck Files",
Name = "Constellation Files",
Type = "HttpSynchronization",
Identifier = "digitaluniverse_constellations_speck",
Version = 2
Identifier = "digitaluniverse_constellations_data",
Version = 1
})
local zodiacs = {
"Cancer", "Taurus", "Pisces", "Aries", "Libra", "Aquarius", "Capricornus", "Scorpius",
"Virgo", "Sagittarius", "Gemini", "Leo"
}
local constellationsExtragalactic = {
Identifier = "ConstellationsExtragalactic",
Renderable = {
Type = "RenderableDUMeshes",
Type = "RenderableConstellationLines",
Enabled = false,
Opacity = 0.4,
File = speck .. "constellationsEXGAL.speck",
LabelFile = speck .. "constellationsEXGAL.label",
NamesFile = speck .. "constellations.dat",
TextColor = { 0.8, 0.8, 0.8 },
TextOpacity = 0.4,
TextSize = 20.0,
TextMinMaxSize = { 20, 30 },
MeshColor = { { 0.6, 0.4, 0.4 }, { 0.8, 0.0, 0.0 }, { 0.0, 0.3, 0.8 } },
Unit = "Mpc"
Colors = { { 0.6, 0.4, 0.4 }, { 0.8, 0.0, 0.0 }, { 0.0, 0.3, 0.8 } },
LabelUnit = "Mpc",
LineUnit = "Mpc",
-- Selection = zodiacs
},
GUI = {
Name = "Constellations (Extragalactic)",
@@ -29,20 +37,23 @@ local constellationsExtragalactic = {
local constellations = {
Identifier = "Constellations",
Renderable = {
Type = "RenderableDUMeshes",
Type = "RenderableConstellationLines",
Enabled = false,
Opacity = 0.3,
File = speck .. "constellations.speck",
LabelFile = speck .. "constellations.label",
NamesFile = speck .. "constellations.dat",
TextColor = { 0.8, 0.8, 0.8 },
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"
Colors = { { 0.6, 0.4, 0.4 }, { 0.8, 0.0, 0.0 }, { 0.0, 0.3, 0.8 } },
LabelUnit = "pc",
LineUnit = "pc",
-- Selection = zodiacs
},
GUI = {
Name = "Constellations",
Name = "Constellations",
Path = "/Milky Way/Constellations",
Description = [[Census 88 constellations and labels. DU Version 2.3. <br> These
modern constellations are largely based on those of the Babylonians and
@@ -60,12 +71,12 @@ asset.onInitialize(function()
openspace.addSceneGraphNode(constellationsExtragalactic)
openspace.addSceneGraphNode(constellations)
end)
asset.onDeinitialize(function()
openspace.removeSceneGraphNode(constellations)
openspace.removeSceneGraphNode(constellationsExtragalactic)
end)
asset.export(constellationsExtragalactic)
asset.export(constellations)
@@ -73,7 +84,7 @@ asset.export(constellations)
asset.meta = {
Name = "Constellations",
Version = "1.1",
Version = "1.2",
Description = "Digital Universe asset for constellation lines",
Author = "Brian Abbott (AMNH)",
URL = "https://www.amnh.org/research/hayden-planetarium/digital-universe",

View File

@@ -2,12 +2,48 @@ local constellationsCSV = asset.localResource("constellation_data.csv")
local transforms = asset.require("scene/solarsystem/sun/transforms")
local images = asset.syncedResource({
Name = "Constellation Images",
Type = "HttpSynchronization",
Identifier = "constellation_images",
Version = 4
Name = "Constellation Images",
Type = "HttpSynchronization",
Identifier = "constellation_images",
Version = 4
})
local data = asset.syncedResource({
Name = "Constellation Files",
Type = "HttpSynchronization",
Identifier = "digitaluniverse_constellations_data",
Version = 1
})
-- Function that returns the full name of a constellation given its abbreviation
-- The function uses the constellations.dat file to find the name
-- If the file does not exist or if a match could not be found, it returns nil
local findFullName = function(abbreviation)
local namesFile = data .. "constellations.dat"
local file = io.open(namesFile, "r")
if file ~= nil then
io.close(file)
for line in io.lines(namesFile) do
local index, length = string.find(line, abbreviation)
if index ~= nil and index < 4 then
return string.sub(line, length + 1)
end
local fullLine = line
local lowerLine = string.lower(line)
index, length = string.find(lowerLine, string.lower(abbreviation))
if index ~= nil and index < 4 then
return string.sub(fullLine, length + 1)
end
end
end
return nil
end
--function that reads the file
local createConstellations = function (baseIdentifier, guiPath, constellationfile)
local genConstellations = {};
@@ -30,6 +66,13 @@ local createConstellations = function (baseIdentifier, guiPath, constellationfil
local normy = y/magVec
local normz = z/magVec
-- Use the full name in the data constellations.dat if possible
-- Otherwise, use the given name in the constellation_data.csv file
local foundName = findFullName(abbreviation)
if foundName ~= nil then
name = foundName
end
group = (group == "" and globe or group)
local aconstellation = {
@@ -172,7 +215,7 @@ end
asset.meta = {
Name = "Constellation Images",
Version = "1.1",
Version = "1.2",
Description = "Artistic images depicting the constellations",
Author = "James Hedberg",
URL = "http://jameshedberg.com",

View File

@@ -255,6 +255,13 @@ bool RenderableDUMeshes::isReady() const {
(!_renderingMeshesMap.empty() || (!_labelset.entries.empty()));
}
void RenderableDUMeshes::initialize() {
bool success = loadData();
if (!success) {
throw ghoul::RuntimeError("Error loading data");
}
}
void RenderableDUMeshes::initializeGL() {
_program = DigitalUniverseModule::ProgramObjectManager.request(
"RenderableDUMeshes",
@@ -269,11 +276,6 @@ void RenderableDUMeshes::initializeGL() {
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
bool success = loadData();
if (!success) {
throw ghoul::RuntimeError("Error loading data");
}
createMeshes();
if (_hasLabel) {
@@ -536,8 +538,7 @@ bool RenderableDUMeshes::readSpeckFile() {
std::getline(file, line);
std::stringstream dim(line);
dim >> mesh.numU; // numU
dim >> mesh.numV; // numV
dim >> mesh.numU >> mesh.numV;
// We can now read the vertices data:
for (int l = 0; l < mesh.numU * mesh.numV; ++l) {
@@ -602,7 +603,6 @@ bool RenderableDUMeshes::readSpeckFile() {
}
}
}
setBoundingSphere(maxRadius);
return true;
@@ -636,7 +636,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

@@ -55,6 +55,7 @@ public:
explicit RenderableDUMeshes(const ghoul::Dictionary& dictionary);
~RenderableDUMeshes() override = default;
void initialize() override;
void initializeGL() override;
void deinitializeGL() override;
@@ -84,7 +85,7 @@ 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;

View File

@@ -28,7 +28,9 @@ set(HEADER_FILES
horizonsfile.h
kepler.h
speckloader.h
rendering/renderableconstellationsbase.h
rendering/renderableconstellationbounds.h
rendering/renderableconstellationlines.h
rendering/renderablefluxnodes.h
rendering/renderablehabitablezone.h
rendering/renderablerings.h
@@ -48,7 +50,9 @@ set(SOURCE_FILES
kepler.cpp
spacemodule_lua.inl
speckloader.cpp
rendering/renderableconstellationsbase.cpp
rendering/renderableconstellationbounds.cpp
rendering/renderableconstellationlines.cpp
rendering/renderablefluxnodes.cpp
rendering/renderablehabitablezone.cpp
rendering/renderablerings.cpp
@@ -66,6 +70,8 @@ source_group("Source Files" FILES ${SOURCE_FILES})
set(SHADER_FILES
shaders/constellationbounds_fs.glsl
shaders/constellationbounds_vs.glsl
shaders/constellationlines_fs.glsl
shaders/constellationlines_vs.glsl
shaders/debrisViz_fs.glsl
shaders/debrisViz_vs.glsl
shaders/fluxnodes_fs.glsl

View File

@@ -25,13 +25,11 @@
#include <modules/space/rendering/renderableconstellationbounds.h>
#include <openspace/documentation/documentation.h>
#include <openspace/documentation/verifier.h>
#include <openspace/engine/globals.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/misc/misc.h>
#include <ghoul/opengl/programobject.h>
#include <fstream>
#include <optional>
@@ -50,14 +48,6 @@ namespace {
"constellations"
};
constexpr openspace::properties::Property::PropertyInfo ConstellationInfo = {
"ConstellationFile",
"Constellation File Path",
"Specifies the file that contains the mapping between constellation "
"abbreviations and full name of the constellation. If this value is empty, the "
"abbreviations are used as the full names"
};
constexpr openspace::properties::Property::PropertyInfo ColorInfo = {
"Color",
"Color of constellation lines",
@@ -65,33 +55,12 @@ namespace {
"full opacity"
};
constexpr openspace::properties::Property::PropertyInfo LineWidthInfo = {
"LineWidth",
"Line Width",
"The line width of the constellation bounds"
};
constexpr openspace::properties::Property::PropertyInfo SelectionInfo = {
"ConstellationSelection",
"Constellation Selection",
"The constellations that are selected are displayed on the celestial sphere"
};
struct [[codegen::Dictionary(RenderableConstellationBounds)]] Parameters {
// [[codegen::verbatim(VertexInfo.description)]]
std::string file;
// [[codegen::verbatim(ConstellationInfo.description)]]
std::optional<std::string> constellationFile;
std::filesystem::path file;
// [[codegen::verbatim(ColorInfo.description)]]
std::optional<glm::vec3> color [[codegen::color()]];
// [[codegen::verbatim(LineWidthInfo.description)]]
std::optional<float> lineWidth;
// [[codegen::verbatim(SelectionInfo.description)]]
std::optional<std::vector<std::string>> constellationSelection;
};
#include "renderableconstellationbounds_codegen.cpp"
} // namespace
@@ -103,53 +72,46 @@ documentation::Documentation RenderableConstellationBounds::Documentation() {
}
RenderableConstellationBounds::RenderableConstellationBounds(
const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
const ghoul::Dictionary& dictionary)
: RenderableConstellationsBase(dictionary)
, _vertexFilename(VertexInfo)
, _constellationFilename(ConstellationInfo)
, _color(ColorInfo, glm::vec3(1.f, 0.f, 0.f), glm::vec3(0.f), glm::vec3(1.f))
, _lineWidth(LineWidthInfo, 2.f, 1.f, 32.f)
, _constellationSelection(SelectionInfo)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
_vertexFilename.onChange([&](){ loadVertexFile(); });
// Avoid reading files here, instead do it in multithreaded initialize()
_vertexFilename = absPath(p.file.string()).string();
_vertexFilename.onChange([&](){ loadData(); });
addProperty(_vertexFilename);
_vertexFilename = p.file;
_constellationFilename.onChange([&](){ loadConstellationFile(); });
_constellationFilename = p.constellationFile.value_or(_constellationFilename);
addProperty(_constellationFilename);
_color.setViewOption(properties::Property::ViewOptions::Color);
_color = p.color.value_or(_color);
addProperty(_color);
}
_lineWidth = p.lineWidth.value_or(_lineWidth);
addProperty(_lineWidth);
void RenderableConstellationBounds::initialize() {
RenderableConstellationsBase::initialize();
fillSelectionProperty();
_constellationSelection.onChange([this]() { selectionPropertyHasChanged(); });
addProperty(_constellationSelection);
loadData();
if (p.constellationSelection.has_value()) {
const std::vector<std::string> options = _constellationSelection.options();
if (!_assetSelection.empty()) {
const std::vector<std::string> options = _selection.options();
std::set<std::string> selectedConstellations;
std::set<std::string> selectedNames;
for (const std::string& s : *p.constellationSelection) {
for (const std::string& s : _assetSelection) {
const auto it = std::find(options.begin(), options.end(), s);
if (it == options.end()) {
// The user has specified a constellation name that doesn't exist
LWARNINGC(
"RenderableConstellationBounds",
"RenderableConstellationsBase",
fmt::format("Option '{}' not found in list of constellations", s)
);
}
else {
selectedNames.insert(s);
selectedConstellations.insert(s);
}
}
_constellationSelection = selectedNames;
_selection = selectedConstellations;
}
}
@@ -192,10 +154,16 @@ void RenderableConstellationBounds::deinitializeGL() {
}
bool RenderableConstellationBounds::isReady() const {
return (_vao != 0) && (_vbo != 0) && _program;
bool isReady = _program && _vao != 0 && _vbo != 0;
// If we have labels, they also need to be loaded
if (_hasLabel) {
return isReady && !_labelset.entries.empty();
}
return isReady;
}
void RenderableConstellationBounds::render(const RenderData& data, RendererTasks&) {
void RenderableConstellationBounds::render(const RenderData& data, RendererTasks& tasks) {
_program->activate();
_program->setUniform("campos", glm::vec4(data.camera.positionVec3(), 1.f));
@@ -212,6 +180,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("opacity", opacity());
glLineWidth(_lineWidth);
@@ -223,6 +192,16 @@ void RenderableConstellationBounds::render(const RenderData& data, RendererTasks
}
glBindVertexArray(0);
_program->deactivate();
RenderableConstellationsBase::render(data, tasks);
}
bool RenderableConstellationBounds::loadData() {
bool success = loadVertexFile();
if (!success) {
throw ghoul::RuntimeError("Error loading data");
}
return success;
}
bool RenderableConstellationBounds::loadVertexFile() {
@@ -261,8 +240,8 @@ bool RenderableConstellationBounds::loadVertexFile() {
float dec;
s >> dec;
std::string constellationName;
s >> constellationName;
std::string abbreviation;
s >> abbreviation;
if (!s.good()) {
// If this evaluates to true, the stream was not completely filled, which
@@ -277,7 +256,7 @@ bool RenderableConstellationBounds::loadVertexFile() {
}
// Did we arrive at a new constellation?
if (constellationName != currentBound.constellationAbbreviation) {
if (abbreviation != currentBound.constellationAbbreviation) {
// Store how many vertices we read during the active time of the constellation
currentBound.nVertices = static_cast<GLsizei>(
_vertexValues.size() - currentBound.startIndex
@@ -286,8 +265,9 @@ bool RenderableConstellationBounds::loadVertexFile() {
_constellationBounds.push_back(currentBound);
currentBound = ConstellationBound();
currentBound.isEnabled = true;
currentBound.constellationAbbreviation = constellationName;
currentBound.constellationFullName = constellationName;
currentBound.constellationAbbreviation = abbreviation;
std::string name = constellationFullName(abbreviation);
currentBound.constellationFullName = name.empty() ? abbreviation : name;
currentBound.startIndex = static_cast<GLsizei>(_vertexValues.size());
}
@@ -326,64 +306,9 @@ bool RenderableConstellationBounds::loadVertexFile() {
return true;
}
bool RenderableConstellationBounds::loadConstellationFile() {
if (_constellationFilename.value().empty()) {
return true;
}
std::ifstream file;
file.exceptions(std::ifstream::goodbit);
file.open(absPath(_constellationFilename));
std::string line;
int index = 0;
while (file.good()) {
std::getline(file, line);
if (line.empty()) {
continue;
}
std::string abbreviation;
std::stringstream s(line);
s >> abbreviation;
const auto it = std::find_if(
_constellationBounds.begin(),
_constellationBounds.end(),
[abbreviation](const ConstellationBound& bound) {
return bound.constellationAbbreviation == abbreviation;
}
);
if (it == _constellationBounds.end()) {
LERRORC(
"RenderableConstellationBounds",
fmt::format("Could not find constellation '{}' in list", abbreviation)
);
return false;
}
// Update the constellations full name
std::string fullName;
std::getline(s, fullName);
ghoul::trimWhitespace(fullName);
it->constellationFullName = std::move(fullName);
++index;
}
return true;
}
void RenderableConstellationBounds::fillSelectionProperty() {
for (int i = 0 ; i < static_cast<int>(_constellationBounds.size()); ++i) {
const ConstellationBound& bound = _constellationBounds[i];
_constellationSelection.addOption(bound.constellationFullName);
}
}
void RenderableConstellationBounds::selectionPropertyHasChanged() {
// If no values are selected (the default), we want to show all constellations
if (!_constellationSelection.hasSelected()) {
if (!_selection.hasSelected()) {
for (ConstellationBound& b : _constellationBounds) {
b.isEnabled = true;
}
@@ -391,7 +316,7 @@ void RenderableConstellationBounds::selectionPropertyHasChanged() {
else {
// Enable all constellations that are selected
for (ConstellationBound& b : _constellationBounds) {
b.isEnabled = _constellationSelection.isSelected(b.constellationFullName);
b.isEnabled = _selection.isSelected(b.constellationFullName);
}
}
}

View File

@@ -25,13 +25,7 @@
#ifndef __OPENSPACE_MODULE_SPACE___RENDERABLECONSTELLATIONBOUNDS___H__
#define __OPENSPACE_MODULE_SPACE___RENDERABLECONSTELLATIONBOUNDS___H__
#include <openspace/rendering/renderable.h>
#include <openspace/properties/selectionproperty.h>
#include <openspace/properties/stringproperty.h>
#include <openspace/properties/vector/vec3property.h>
#include <ghoul/opengl/ghoul_gl.h>
#include <vector>
#include <modules/space/rendering/renderableconstellationsbase.h>
namespace ghoul::opengl { class ProgramObject; }
@@ -48,10 +42,11 @@ namespace documentation { struct Documentation; }
* <code>_distance</code> property. Currently, all constellation bounds are lines, which
* leads to artifacts if the radius is very small.
*/
class RenderableConstellationBounds : public Renderable {
class RenderableConstellationBounds : public RenderableConstellationsBase {
public:
RenderableConstellationBounds(const ghoul::Dictionary& dictionary);
void initialize() override;
void initializeGL() override;
void deinitializeGL() override;
@@ -79,41 +74,25 @@ private:
* \return \c true if the loading succeeded, \c false otherwise
*/
bool loadVertexFile();
/**
* Loads the file specified in _constellationFilename that contains the mapping
* between abbreviations and full names of constellations.
*
* \return <code>true</code> if the loading succeeded, <code>false</code> otherwise
*/
bool loadConstellationFile();
/// Fills the <code>_constellationSelection</code> property with all constellations
void fillSelectionProperty();
bool loadData();
/**
* Callback method that gets triggered when <code>_constellationSelection</code>
* changes.
*/
void selectionPropertyHasChanged();
void selectionPropertyHasChanged() override;
/// The filename containing the constellation bounds
properties::StringProperty _vertexFilename;
/// The file containing constellation names
properties::StringProperty _constellationFilename;
/// Determines the color of the constellation lines
properties::Vec3Property _color;
// Linewidth for the constellation bounds
properties::FloatProperty _lineWidth;
std::unique_ptr<ghoul::opengl::ProgramObject> _program;
/// The list of all loaded constellation bounds
std::vector<ConstellationBound> _constellationBounds;
std::unique_ptr<ghoul::opengl::ProgramObject> _program;
struct Vertex {
float x;
float y;
@@ -121,9 +100,6 @@ private:
};
std::vector<Vertex> _vertexValues; ///< A list of all vertices of all bounds
/// The property that stores all indices of constellations that should be drawn
properties::SelectionProperty _constellationSelection;
GLuint _vao = 0;
GLuint _vbo = 0;
};

View File

@@ -0,0 +1,448 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2022 *
* *
* 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 <modules/space/rendering/renderableconstellationlines.h>
#include <openspace/documentation/documentation.h>
#include <openspace/engine/globals.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/glm.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/misc/misc.h>
#include <ghoul/opengl/openglstatecache.h>
#include <ghoul/opengl/programobject.h>
#include <scn/scn.h>
#include <array>
#include <filesystem>
#include <fstream>
#include <optional>
namespace {
constexpr std::string_view _loggerCat = "RenderableConstellationLines";
constexpr std::array<const char*, 4> UniformNames = {
"modelViewTransform", "projectionTransform", "opacity", "color"
};
constexpr openspace::properties::Property::PropertyInfo SpeckInfo = {
"File",
"Constellation Data File Path",
"The file that contains the data for the constellation lines"
};
constexpr openspace::properties::Property::PropertyInfo DrawElementsInfo = {
"DrawElements",
"Draw Elements",
"Enables/Disables the drawing of the constellations"
};
constexpr openspace::properties::Property::PropertyInfo LineUnitInfo = {
"LineUnit",
"Line Unit",
"The distance unit used for the constellation lines data"
};
constexpr openspace::properties::Property::PropertyInfo ColorsInfo = {
"Colors",
"Constellation Colors",
"The defined colors for the constellations to be rendered. "
"There can be several groups of constellaitons that can have distinct colors."
};
struct [[codegen::Dictionary(RenderableConstellationLines)]] Parameters {
// The path to the SPECK file that contains constellation lines data
std::filesystem::path file;
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(LineUnitInfo.description)]]
std::optional<Unit> lineUnit;
// [[codegen::verbatim(ColorsInfo.description)]]
std::optional<std::vector<glm::vec3>> colors;
};
#include "renderableconstellationlines_codegen.cpp"
} // namespace
namespace openspace {
documentation::Documentation RenderableConstellationLines::Documentation() {
return codegen::doc<Parameters>("space_renderable_constellationlines");
}
RenderableConstellationLines::RenderableConstellationLines(
const ghoul::Dictionary& dictionary)
: RenderableConstellationsBase(dictionary)
, _speckFile(SpeckInfo)
, _drawElements(DrawElementsInfo, true)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
// Avoid reading files here, instead do it in multithreaded initialize()
_speckFile = absPath(p.file.string()).string();
_speckFile.onChange([&]() { loadData(); });
addProperty(_speckFile);
addProperty(_drawElements);
if (p.lineUnit.has_value()) {
_constellationUnit = codegen::map<DistanceUnit>(*p.lineUnit);
}
else {
_constellationUnit = DistanceUnit::Meter;
}
if (p.colors.has_value()) {
std::vector<glm::vec3> ops = *p.colors;
for (size_t i = 0; i < ops.size(); ++i) {
_constellationColorMap.insert({ static_cast<int>(i) + 1, ops[i] });
}
}
}
void RenderableConstellationLines::selectionPropertyHasChanged() {
using ConstellationKeyValuePair = std::pair<const int, ConstellationLine>;
// If no values are selected (the default), we want to show all constellations
if (!_selection.hasSelected()) {
for (ConstellationKeyValuePair& pair : _renderingConstellationsMap)
{
pair.second.isEnabled = true;
}
}
else {
// Enable all constellations that are selected
for (ConstellationKeyValuePair& pair : _renderingConstellationsMap)
{
pair.second.isEnabled = _selection.isSelected(pair.second.name);
}
}
}
bool RenderableConstellationLines::isReady() const {
bool isReady = _program && !_renderingConstellationsMap.empty();
// If we have labels, they also need to be loaded
if (_hasLabel) {
return isReady && !_labelset.entries.empty();
}
return isReady;
}
void RenderableConstellationLines::initialize() {
RenderableConstellationsBase::initialize();
loadData();
if (!_assetSelection.empty()) {
const std::vector<std::string> options = _selection.options();
std::set<std::string> selectedConstellations;
for (const std::string& s : _assetSelection) {
const auto it = std::find(options.begin(), options.end(), s);
if (it == options.end()) {
// The user has specified a constellation name that doesn't exist
LWARNINGC(
"RenderableConstellationsBase",
fmt::format("Option '{}' not found in list of constellations", s)
);
}
else {
selectedConstellations.insert(s);
}
}
_selection = selectedConstellations;
}
}
void RenderableConstellationLines::initializeGL() {
_program = global::renderEngine->buildRenderProgram(
"RenderableConstellationLines",
absPath("${MODULE_SPACE}/shaders/constellationlines_vs.glsl"),
absPath("${MODULE_SPACE}/shaders/constellationlines_fs.glsl")
);
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
createConstellations();
}
void RenderableConstellationLines::deinitializeGL() {
using ConstellationKeyValuePair = std::pair<const int, ConstellationLine>;
for (const ConstellationKeyValuePair& pair : _renderingConstellationsMap)
{
glDeleteVertexArrays(1, &pair.second.vaoArray);
glDeleteBuffers(1, &pair.second.vboArray);
}
if (_program) {
global::renderEngine->removeRenderProgram(_program.get());
_program = nullptr;
}
}
void RenderableConstellationLines::renderConstellations(const RenderData&,
const glm::dmat4& modelViewMatrix,
const glm::dmat4& projectionMatrix)
{
glEnablei(GL_BLEND, 0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glDepthMask(false);
glEnable(GL_DEPTH_TEST);
_program->activate();
_program->setUniform(_uniformCache.modelViewTransform, modelViewMatrix);
_program->setUniform(_uniformCache.projectionTransform, projectionMatrix);
_program->setUniform(_uniformCache.opacity, opacity());
using ConstellationKeyValuePair = std::pair<const int, ConstellationLine>;
for (const ConstellationKeyValuePair& pair : _renderingConstellationsMap)
{
if (!pair.second.isEnabled) {
continue;
}
_program->setUniform(
_uniformCache.color,
_constellationColorMap[pair.second.colorIndex]
);
glBindVertexArray(pair.second.vaoArray);
glLineWidth(_lineWidth);
glDrawArrays(GL_LINE_STRIP, 0, pair.second.numV);
global::renderEngine->openglStateCache().resetLineState();
}
glBindVertexArray(0);
_program->deactivate();
// Restores GL State
global::renderEngine->openglStateCache().resetDepthState();
global::renderEngine->openglStateCache().resetBlendState();
}
void RenderableConstellationLines::render(const RenderData& data, RendererTasks& tasks) {
const glm::dmat4 modelMatrix =
glm::translate(glm::dmat4(1.0), data.modelTransform.translation) * // Translation
glm::dmat4(data.modelTransform.rotation) * // Spice rotation
glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale));
const glm::dmat4 modelViewMatrix = data.camera.combinedViewMatrix() * modelMatrix;
const glm::dmat4 projectionMatrix = data.camera.projectionMatrix();
if (_drawElements) {
renderConstellations(data, modelViewMatrix, projectionMatrix);
}
RenderableConstellationsBase::render(data, tasks);
}
void RenderableConstellationLines::update(const UpdateData&) {
if (_program->isDirty()) {
_program->rebuildFromFile();
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
}
}
bool RenderableConstellationLines::loadData() {
bool success = readSpeckFile();
if (!success) {
throw ghoul::RuntimeError("Error loading data");
}
return success;
}
bool RenderableConstellationLines::readSpeckFile() {
if (_speckFile.value().empty()) {
return false;
}
std::filesystem::path fileName = absPath(_speckFile);
LINFO(fmt::format("Loading Speck file {}", fileName));
std::ifstream file(fileName);
if (!file.good()) {
LERROR(fmt::format("Failed to open Speck file {}", fileName));
return false;
}
const float scale = static_cast<float>(toMeter(_constellationUnit));
double maxRadius = 0.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
// (signaled by the keywords 'datavar', 'texturevar', and 'texture')
std::string line;
while (true) {
std::getline(file, line);
if (file.eof()) {
break;
}
// Guard against wrong line endings (copying files from Windows to Mac) causes
// lines to have a final \r
if (!line.empty() && line.back() == '\r') {
line = line.substr(0, line.length() - 1);
}
if (line.empty() || line[0] == '#') {
continue;
}
std::size_t found = line.find("mesh");
if (found == std::string::npos) {
continue;
}
else {
// mesh lines are structured as follows:
// mesh -c colorindex {
// colorindex is the index of the color for the mesh
std::stringstream str(line);
ConstellationLine constellationLine;
constellationLine.lineIndex = lineIndex;
std::string dummy;
str >> dummy; // mesh command
dummy.clear();
str >> dummy; // color index command
do {
if (dummy == "-c") {
str >> constellationLine.colorIndex; // color index
}
else {
std::string message = fmt::format("Unknown command '{}' found in "
"constellation file '{}'", dummy, fileName);
LWARNING(message);
}
dummy.clear();
str >> dummy;
} while (dummy != "{");
std::getline(file, line);
// Read the identifier
std::stringstream id(line);
std::string identifier;
id >> dummy; // id command
dummy.clear();
std::getline(id, identifier); // identifier
ghoul::trimWhitespace(identifier);
std::string name = constellationFullName(identifier);
if (!name.empty()) {
constellationLine.name = name;
}
// Read the number of vertices
std::getline(file, line);
std::stringstream dim(line);
dim >> constellationLine.numV;
// We can now read the vertices data:
for (int l = 0; l < constellationLine.numV; ++l) {
std::getline(file, line);
if (line.substr(0, 1) == "}") {
break;
}
// Try to read three values for the position
glm::vec3 pos;
bool success = true;
auto reading = scn::scan(line, "{} {} {}", pos.x, pos.y, pos.z);
if (reading) {
pos *= scale;
constellationLine.vertices.push_back(pos.x);
constellationLine.vertices.push_back(pos.y);
constellationLine.vertices.push_back(pos.z);
}
else {
success = false;
LERROR(fmt::format(
"Failed reading position on line {} of mesh {} in file: '{}'. "
"Stopped reading constellation data", l, lineIndex, fileName
));
}
// Check if new max radius
const double r = glm::length(glm::dvec3(pos));
maxRadius = std::max(maxRadius, r);
}
std::getline(file, line);
if (line.substr(0, 1) == "}") {
_renderingConstellationsMap.insert({ lineIndex++, constellationLine });
}
else {
return false;
}
}
}
setBoundingSphere(maxRadius);
return true;
}
void RenderableConstellationLines::createConstellations() {
LDEBUG("Creating constellations");
for (std::pair<const int, ConstellationLine>& p : _renderingConstellationsMap) {
GLuint vao;
glGenVertexArrays(1, &vao);
p.second.vaoArray = vao;
GLuint vbo;
glGenBuffers(1, &vbo);
p.second.vboArray = vbo;
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(
GL_ARRAY_BUFFER,
p.second.vertices.size() * sizeof(GLfloat),
p.second.vertices.data(),
GL_STATIC_DRAW
);
// in_position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
}
glBindVertexArray(0);
}
} // namespace openspace

View File

@@ -0,0 +1,102 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2022 *
* *
* 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. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_SPACE___RENDERABLECONSTELLATIONLINES___H__
#define __OPENSPACE_MODULE_SPACE___RENDERABLECONSTELLATIONLINES___H__
#include <modules/space/rendering/renderableconstellationsbase.h>
#include <ghoul/opengl/uniformcache.h>
#include <unordered_map>
namespace ghoul::filesystem { class File; }
namespace ghoul::fontrendering { class Font; }
namespace ghoul::opengl {
class ProgramObject;
class Texture;
} // namespace ghoul::opengl
namespace openspace {
namespace documentation { struct Documentation; }
class RenderableConstellationLines : public RenderableConstellationsBase {
public:
explicit RenderableConstellationLines(const ghoul::Dictionary& dictionary);
~RenderableConstellationLines() override = default;
void initialize() override;
void initializeGL() override;
void deinitializeGL() override;
bool isReady() const override;
void render(const RenderData& data, RendererTasks& rendererTask) override;
void update(const UpdateData& data) override;
static documentation::Documentation Documentation();
private:
struct ConstellationLine {
bool isEnabled = true;
std::string name;
int lineIndex;
int colorIndex;
int numV;
GLuint vaoArray;
GLuint vboArray;
std::vector<GLfloat> vertices;
};
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 <code>_constellationSelection</code>
* changes
*/
void selectionPropertyHasChanged() override;
properties::BoolProperty _drawElements;
std::unique_ptr<ghoul::opengl::ProgramObject> _program = nullptr;
UniformCache(modelViewTransform, projectionTransform, opacity,
color) _uniformCache;
properties::StringProperty _speckFile;
DistanceUnit _constellationUnit = DistanceUnit::Parsec;
std::vector<float> _fullData;
std::unordered_map<int, glm::vec3> _constellationColorMap;
std::unordered_map<int, ConstellationLine> _renderingConstellationsMap;
};
} // namespace openspace
#endif // __OPENSPACE_MODULE_SPACE___RENDERABLECONSTELLATIONLINES___H__

View File

@@ -0,0 +1,412 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2022 *
* *
* 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 <modules/space/rendering/renderableconstellationsbase.h>
#include <openspace/documentation/documentation.h>
#include <openspace/engine/globals.h>
#include <openspace/engine/windowdelegate.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/font/fontmanager.h>
#include <ghoul/font/fontrenderer.h>
#include <ghoul/glm.h>
#include <ghoul/misc/misc.h>
#include <ghoul/opengl/programobject.h>
#include <fstream>
#include <optional>
namespace {
constexpr int RenderOptionViewDirection = 0;
constexpr int RenderOptionPositionNormal = 1;
constexpr openspace::properties::Property::PropertyInfo TextColorInfo = {
"TextColor",
"Text Color",
"The text color of the labels for the constellations"
};
constexpr openspace::properties::Property::PropertyInfo TextOpacityInfo = {
"TextOpacity",
"Text Opacity",
"Determines the transparency of the text label, where 1 is completely opaque "
"and 0 fully transparent"
};
constexpr openspace::properties::Property::PropertyInfo TextSizeInfo = {
"TextSize",
"Text Size",
"The text size of the labels for the constellations"
};
constexpr openspace::properties::Property::PropertyInfo LabelFileInfo = {
"LabelFile",
"Label File",
"The path to the label file that contains information about the constellations"
};
constexpr openspace::properties::Property::PropertyInfo LabelMinMaxSizeInfo = {
"TextMinMaxSize",
"Text Min/Max Size",
"The minimum and maximum size (in pixels) for the text of the labels for the "
"constellations"
};
constexpr openspace::properties::Property::PropertyInfo NamesFileInfo = {
"NamesFile",
"Constellation Names File Path",
"Specifies the file that contains the mapping between constellation "
"abbreviations and full names of the constellations. If this value is empty, the "
"abbreviations are used as the full names"
};
constexpr openspace::properties::Property::PropertyInfo LineWidthInfo = {
"LineWidth",
"Line Width",
"The line width of the constellation"
};
constexpr openspace::properties::Property::PropertyInfo DrawLabelInfo = {
"DrawLabels",
"Draw Labels",
"Determines whether labels should be drawn or hidden"
};
constexpr openspace::properties::Property::PropertyInfo RenderOptionInfo = {
"RenderOption",
"Render Option",
"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",
"The constellations that are selected are displayed on the celestial sphere"
};
struct [[codegen::Dictionary(RenderableConstellationsBase)]] Parameters {
// [[codegen::verbatim(DrawLabelInfo.description)]]
std::optional<bool> drawLabels;
// [[codegen::verbatim(NamesFileInfo.description)]]
std::optional<std::filesystem::path> namesFile;
// [[codegen::verbatim(TextColorInfo.description)]]
std::optional<glm::vec3> textColor [[codegen::color()]];
// [[codegen::verbatim(TextOpacityInfo.description)]]
std::optional<float> textOpacity;
// [[codegen::verbatim(TextSizeInfo.description)]]
std::optional<float> textSize;
// [[codegen::verbatim(LabelFileInfo.description)]]
std::optional<std::string> labelFile;
// [[codegen::verbatim(LabelMinMaxSizeInfo.description)]]
std::optional<glm::ivec2> textMinMaxSize;
// [[codegen::verbatim(LineWidthInfo.description)]]
std::optional<float> 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<Unit> labelUnit;
// [[codegen::verbatim(SelectionInfo.description)]]
std::optional<std::vector<std::string>> selection;
};
#include "renderableconstellationsbase_codegen.cpp"
} // namespace
namespace openspace {
documentation::Documentation RenderableConstellationsBase::Documentation() {
return codegen::doc<Parameters>("space_renderable_constellationsbase");
}
RenderableConstellationsBase::RenderableConstellationsBase(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _textColor(TextColorInfo, glm::vec3(1.f), glm::vec3(0.f), glm::vec3(1.f))
, _textOpacity(TextOpacityInfo, 1.f, 0.f, 1.f)
, _textSize(TextSizeInfo, 8.f, 0.5f, 24.f)
, _drawLabels(DrawLabelInfo, false)
, _textMinMaxSize(
LabelMinMaxSizeInfo,
glm::ivec2(8, 500),
glm::ivec2(0),
glm::ivec2(1000)
)
, _lineWidth(LineWidthInfo, 2.f, 1.f, 16.f)
, _renderOption(RenderOptionInfo, properties::OptionProperty::DisplayType::Dropdown)
, _namesFilename(NamesFileInfo)
, _selection(SelectionInfo)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
addProperty(_opacity);
registerUpdateRenderBinFromOpacity();
_renderOption.addOption(RenderOptionViewDirection, "Camera View Direction");
_renderOption.addOption(RenderOptionPositionNormal, "Camera Position Normal");
// @TODO (abock. 2021-01-31) In the other classes, this is done with an enum, and
// doing it based on the fisheye rendering seems a bit brittle?
if (global::windowDelegate->isFisheyeRendering()) {
_renderOption = RenderOptionPositionNormal;
}
else {
_renderOption = RenderOptionViewDirection;
}
addProperty(_renderOption);
// Avoid reading files here, instead do it in multithreaded initialize()
if (p.namesFile.has_value()) {
_namesFilename = absPath(p.namesFile.value().string()).string();
}
_namesFilename.onChange([&]() { loadConstellationFile(); });
addProperty(_namesFilename);
_lineWidth = p.lineWidth.value_or(_lineWidth);
addProperty(_lineWidth);
if (p.labelFile.has_value()) {
_labelFile = absPath(*p.labelFile).string();
_hasLabel = true;
_drawLabels = p.drawLabels.value_or(_drawLabels);
addProperty(_drawLabels);
_textColor = p.textColor.value_or(_textColor);
_hasLabel = p.textColor.has_value();
_textColor.setViewOption(properties::Property::ViewOptions::Color);
addProperty(_textColor);
_textOpacity = p.textOpacity.value_or(_textOpacity);
addProperty(_textOpacity);
_textSize = p.textSize.value_or(_textSize);
addProperty(_textSize);
_textMinMaxSize = p.textMinMaxSize.value_or(_textMinMaxSize);
_textMinMaxSize.setViewOption(properties::Property::ViewOptions::MinMaxRange);
addProperty(_textMinMaxSize);
if (p.labelUnit.has_value()) {
_labelUnit = codegen::map<DistanceUnit>(*p.labelUnit);
}
else {
_labelUnit = DistanceUnit::Meter;
}
}
_selection.onChange([this]() { selectionPropertyHasChanged(); });
addProperty(_selection);
_assetSelection = p.selection.value_or(_assetSelection);
}
std::string RenderableConstellationsBase::constellationFullName(
const std::string& identifier) const
{
if (_namesTranslation.empty() || identifier.empty()) {
std::string message = "List of constellations or the given identifier was empty";
LWARNINGC("RenderableConstellationsBase", message);
return "";
}
if (_namesTranslation.contains(identifier)) {
return _namesTranslation.at(identifier);
}
std::string message = fmt::format(
"Identifier '{}' could not be found in list of constellations", identifier
);
LERRORC("RenderableConstellationsBase", message);
return "";
}
void RenderableConstellationsBase::loadConstellationFile() {
if (_namesFilename.value().empty()) {
return;
}
// Reset
_selection.clearOptions();
_namesTranslation.clear();
std::ifstream file;
file.exceptions(std::ifstream::goodbit);
file.open(absPath(_namesFilename));
std::string line;
while (file.good()) {
std::getline(file, line);
if (line.empty()) {
continue;
}
std::string abbreviation;
std::stringstream s(line);
s >> abbreviation;
std::string fullName;
std::getline(s, fullName);
ghoul::trimWhitespace(fullName);
_namesTranslation[abbreviation] = fullName;
}
fillSelectionProperty();
}
void RenderableConstellationsBase::fillSelectionProperty() {
for (const std::pair<std::string, std::string>& pair : _namesTranslation) {
_selection.addOption(pair.second);
}
}
void RenderableConstellationsBase::initialize() {
loadConstellationFile();
if (!_hasLabel) {
return;
}
if (!_font) {
constexpr int FontSize = 50;
_font = global::fontManager->font(
"Mono",
static_cast<float>(FontSize),
ghoul::fontrendering::FontManager::Outline::Yes,
ghoul::fontrendering::FontManager::LoadGlyphs::No
);
}
std::string labelFile = _labelFile;
if (!labelFile.empty()) {
_labelset = speck::label::loadFileWithCache(_labelFile);
}
for (speck::Labelset::Entry& entry : _labelset.entries) {
if (!entry.identifier.empty()) {
std::string fullName = constellationFullName(entry.identifier);
if (!fullName.empty()) {
entry.text = fullName;
}
}
}
}
void RenderableConstellationsBase::render(const RenderData& data, RendererTasks&) {
if (!_hasLabel || !_drawLabels) {
return;
}
const glm::dmat4 modelMatrix =
glm::translate(glm::dmat4(1.0), data.modelTransform.translation) * // Translation
glm::dmat4(data.modelTransform.rotation) * // Spice rotation
glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale));
const glm::dmat4 modelViewMatrix = data.camera.combinedViewMatrix() * modelMatrix;
const glm::dmat4 projectionMatrix = data.camera.projectionMatrix();
const glm::dmat4 modelViewProjectionMatrix = projectionMatrix * modelViewMatrix;
const glm::vec3 lookup = data.camera.lookUpVectorWorldSpace();
const glm::vec3 viewDirection = data.camera.viewDirectionWorldSpace();
glm::vec3 right = glm::cross(viewDirection, lookup);
const glm::vec3 up = glm::cross(right, viewDirection);
const glm::dmat4 worldToModelTransform = glm::inverse(modelMatrix);
glm::vec3 orthoRight = glm::normalize(
glm::vec3(worldToModelTransform * glm::vec4(right, 0.f))
);
if (orthoRight == glm::vec3(0.f)) {
glm::vec3 otherVector(lookup.y, lookup.x, lookup.z);
right = glm::cross(viewDirection, otherVector);
orthoRight = glm::normalize(
glm::vec3(worldToModelTransform * glm::vec4(right, 0.f))
);
}
const glm::vec3 orthoUp = glm::normalize(
glm::vec3(worldToModelTransform * glm::dvec4(up, 0.f))
);
renderLabels(data, modelViewProjectionMatrix, orthoRight, orthoUp);
}
void RenderableConstellationsBase::renderLabels(const RenderData& data,
const glm::dmat4& modelViewProjectionMatrix,
const glm::vec3& orthoRight,
const glm::vec3& orthoUp)
{
float scale = static_cast<float>(toMeter(_labelUnit));
ghoul::fontrendering::FontRenderer::ProjectedLabelsInformation labelInfo;
labelInfo.orthoRight = orthoRight;
labelInfo.orthoUp = orthoUp;
labelInfo.minSize = _textMinMaxSize.value().x;
labelInfo.maxSize = _textMinMaxSize.value().y;
labelInfo.cameraPos = data.camera.positionVec3();
labelInfo.cameraLookUp = data.camera.lookUpVectorWorldSpace();
labelInfo.renderType = _renderOption;
labelInfo.mvpMatrix = modelViewProjectionMatrix;
labelInfo.scale = pow(10.f, _textSize);
labelInfo.enableDepth = true;
labelInfo.enableFalseDepth = false;
glm::vec4 textColor = glm::vec4(glm::vec3(_textColor), _textOpacity);
for (const speck::Labelset::Entry& e : _labelset.entries) {
if (_selection.hasSelected() && !_selection.isSelected(e.text)) {
continue;
}
glm::vec3 scaledPos(e.position);
scaledPos *= scale;
ghoul::fontrendering::FontRenderer::defaultProjectionRenderer().render(
*_font,
scaledPos,
e.text,
textColor,
labelInfo
);
}
}
} // namespace openspace

View File

@@ -0,0 +1,125 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2022 *
* *
* 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. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_SPACE___RENDERABLECONSTELLATIONSBASE___H__
#define __OPENSPACE_MODULE_SPACE___RENDERABLECONSTELLATIONSBASE___H__
#include <openspace/rendering/renderable.h>
#include <modules/space/speckloader.h>
#include <openspace/properties/optionproperty.h>
#include <openspace/properties/selectionproperty.h>
#include <openspace/properties/vector/vec3property.h>
#include <openspace/properties/vector/ivec2property.h>
#include <openspace/util/distanceconversion.h>
#include <ghoul/opengl/ghoul_gl.h>
#include <map>
#include <vector>
namespace ghoul::fontrendering { class Font; }
namespace ghoul::opengl { class ProgramObject; }
namespace openspace {
namespace documentation { struct Documentation; }
/**
* This is a base class for constellation lines and bounds
*/
class RenderableConstellationsBase : public Renderable {
public:
virtual ~RenderableConstellationsBase() override = default;
virtual void initialize() override;
virtual void initializeGL() override = 0;
virtual void deinitializeGL() override = 0;
virtual bool isReady() const override = 0;
virtual void render(const RenderData& data, RendererTasks& rendererTask) override;
void renderLabels(const RenderData& data, const glm::dmat4& modelViewProjectionMatrix,
const glm::vec3& orthoRight, const glm::vec3& orthoUp);
static documentation::Documentation Documentation();
protected:
explicit RenderableConstellationsBase(const ghoul::Dictionary& dictionary);
/**
* Callback method that gets triggered when <code>_constellationSelection</code>
* changes
*/
virtual void selectionPropertyHasChanged() = 0;
/// Takes the given constellation <code>identifier</code> and returns the coresponding
/// full name
std::string constellationFullName(const std::string& identifier) const;
// Width for the rendered lines
properties::FloatProperty _lineWidth;
// Property that stores all constellations chosen by the user to be drawn
properties::SelectionProperty _selection;
// Temporary storage of which constellations should be rendered as stated in the
// asset file
std::vector<std::string> _assetSelection;
// Label text settings
bool _hasLabel = false;
speck::Labelset _labelset;
properties::BoolProperty _drawLabels;
private:
// Map over the constellations names and their abbreviations
// key = abbreviation, value = full name
std::map<std::string, std::string> _namesTranslation;
/**
* Loads the file specified in <code>_constellationNamesFilename</code> that contains
* the mapping between abbreviations and full names of constellations
*/
void loadConstellationFile();
/// Fills the <code>_constellationSelection</code> property with all constellations
void fillSelectionProperty();
// The file containing constellation names and abbreviations
properties::StringProperty _namesFilename;
// Label text settings
std::string _labelFile;
std::shared_ptr<ghoul::fontrendering::Font> _font = nullptr;
DistanceUnit _labelUnit = DistanceUnit::Parsec;
properties::Vec3Property _textColor;
properties::FloatProperty _textOpacity;
properties::FloatProperty _textSize;
properties::IVec2Property _textMinMaxSize;
properties::OptionProperty _renderOption;
};
} // namespace openspace
#endif // __OPENSPACE_MODULE_SPACE___RENDERABLECONSTELLATIONSBASE___H__

View File

@@ -28,13 +28,18 @@
in vec4 vs_position;
uniform vec3 color;
uniform float opacity;
Fragment getFragment() {
vec4 position = vs_position;
Fragment frag;
frag.color = vec4(color, 1.0);
if (opacity == 0.0) {
discard;
}
vec4 position = vs_position;
frag.color = vec4(color, opacity);
frag.depth = pscDepth(position);
return frag;

View File

@@ -0,0 +1,47 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2022 *
* *
* 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 "fragment.glsl"
in float vs_screenSpaceDepth;
in vec4 vs_positionViewSpace;
uniform vec3 color;
uniform float opacity;
Fragment getFragment() {
Fragment frag;
if (opacity == 0.0) {
discard;
}
frag.color = vec4(color, opacity);
frag.depth = vs_screenSpaceDepth;
frag.gPosition = vs_positionViewSpace;
frag.gNormal = vec4(0.0, 0.0, 0.0, 1.0);
return frag;
}

View File

@@ -0,0 +1,47 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2022 *
* *
* 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. *
****************************************************************************************/
#version __CONTEXT__
#include "PowerScaling/powerScaling_vs.hglsl"
in vec3 in_position;
out float vs_screenSpaceDepth;
out vec4 vs_positionViewSpace;
uniform dmat4 modelViewTransform;
uniform dmat4 projectionTransform;
void main() {
dvec4 positionViewSpace = modelViewTransform * dvec4(in_position, 1.0);
vec4 positionClipSpace = vec4(projectionTransform * positionViewSpace);
vec4 positionScreenSpace = vec4(z_normalization(positionClipSpace));
vs_screenSpaceDepth = positionScreenSpace.w;
vs_positionViewSpace = vec4(positionViewSpace);
gl_Position = positionScreenSpace;
}

View File

@@ -25,6 +25,7 @@
#include <modules/space/spacemodule.h>
#include <modules/space/rendering/renderableconstellationbounds.h>
#include <modules/space/rendering/renderableconstellationlines.h>
#include <modules/space/rendering/renderablefluxnodes.h>
#include <modules/space/rendering/renderablehabitablezone.h>
#include <modules/space/rendering/renderableorbitalkepler.h>
@@ -79,6 +80,9 @@ void SpaceModule::internalInitialize(const ghoul::Dictionary& dictionary) {
fRenderable->registerClass<RenderableConstellationBounds>(
"RenderableConstellationBounds"
);
fRenderable->registerClass<RenderableConstellationLines>(
"RenderableConstellationLines"
);
fRenderable->registerClass<RenderableFluxNodes>("RenderableFluxNodes");
fRenderable->registerClass<RenderableHabitableZone>("RenderableHabitableZone");
fRenderable->registerClass<RenderableRings>("RenderableRings");
@@ -115,6 +119,7 @@ std::vector<documentation::Documentation> SpaceModule::documentations() const {
HorizonsTranslation::Documentation(),
KeplerTranslation::Documentation(),
RenderableConstellationBounds::Documentation(),
RenderableConstellationLines::Documentation(),
RenderableFluxNodes::Documentation(),
RenderableHabitableZone::Documentation(),
RenderableRings::Documentation(),

View File

@@ -37,7 +37,7 @@
namespace {
constexpr int8_t DataCacheFileVersion = 10;
constexpr int8_t LabelCacheFileVersion = 10;
constexpr int8_t LabelCacheFileVersion = 11;
constexpr int8_t ColorCacheFileVersion = 10;
bool startsWith(std::string lhs, std::string_view rhs) noexcept {
@@ -321,7 +321,7 @@ Dataset loadFile(std::filesystem::path path, SkipAllZeroLines skipAllZeroLines)
if (!str.good()) {
// Need to subtract one of the line number here as we increase the current
// line count in the beginning of the while loop we are currently in
// line count in the beginning of the while loop we are currently in
throw ghoul::RuntimeError(fmt::format(
"Error loading position information out of data line {} in file {}. "
"Value was not a number",
@@ -346,7 +346,7 @@ Dataset loadFile(std::filesystem::path path, SkipAllZeroLines skipAllZeroLines)
if (valueStream.fail()) {
// Need to subtract one of the line number here as we increase the
// current line count in the beginning of the while loop we are
// currently in
// currently in
throw ghoul::RuntimeError(fmt::format(
"Error loading data value {} out of data line {} in file {}. "
"Value was not a number",
@@ -674,10 +674,20 @@ Labelset loadFile(std::filesystem::path path, SkipAllZeroLines) {
std::getline(str, rest);
strip(rest);
if (startsWith(rest, "id")) {
// optional arument with identifier
// Remove the 'id' text
rest = rest.substr(std::string_view("id ").size());
size_t index = rest.find("text");
entry.identifier = rest.substr(0, index - 1);
// update the rest, remove the identifier
rest = rest.substr(index);
}
if (!startsWith(rest, "text")) {
throw ghoul::RuntimeError(fmt::format(
"Error loading label file {}: File contains some value between "
"positions and text label, which is unsupported", path
"Error loading label file {}: File contains an unsupported value "
"between positions and text label", path
));
}
@@ -731,6 +741,13 @@ std::optional<Labelset> loadCachedFile(std::filesystem::path path) {
file.read(reinterpret_cast<char*>(&e.position.y), sizeof(float));
file.read(reinterpret_cast<char*>(&e.position.z), sizeof(float));
// Identifier
uint8_t idLen;
file.read(reinterpret_cast<char*>(&idLen), sizeof(uint8_t));
e.identifier.resize(idLen);
file.read(e.identifier.data(), idLen);
// Text
uint16_t len;
file.read(reinterpret_cast<char*>(&len), sizeof(uint16_t));
e.text.resize(len);
@@ -763,6 +780,13 @@ void saveCachedFile(const Labelset& labelset, std::filesystem::path path) {
file.write(reinterpret_cast<const char*>(&e.position.y), sizeof(float));
file.write(reinterpret_cast<const char*>(&e.position.z), sizeof(float));
// Identifier
checkSize<uint8_t>(e.identifier.size(), "Identifier too long");
uint8_t idLen = static_cast<uint8_t>(e.identifier.size());
file.write(reinterpret_cast<const char*>(&idLen), sizeof(uint8_t));
file.write(e.identifier.data(), idLen);
// Text
checkSize<uint16_t>(e.text.size(), "Text too long");
uint16_t len = static_cast<uint16_t>(e.text.size());
file.write(reinterpret_cast<const char*>(&len), sizeof(uint16_t));

View File

@@ -68,6 +68,7 @@ struct Labelset {
struct Entry {
glm::vec3 position = glm::vec3(0.f);
std::string identifier;
std::string text;
};
std::vector<Entry> entries;