Distance label hack, needs cleanup and replacement

This commit is contained in:
Lovisa Hassler
2019-12-20 10:15:12 +01:00
parent 0d00b6d9a5
commit b2861183ae
4 changed files with 72 additions and 10 deletions

View File

@@ -46,6 +46,11 @@
#include <ghoul/glm.h>
#include <glm/gtx/string_cast.hpp>
#include <openspace/rendering/renderengine.h>
#include <openspace/scene/scene.h>
#include <string>
#include <modules/base/rendering/renderablenodeline.h>
namespace {
constexpr const char* _loggerCat = "base::RenderableLabels";
@@ -175,6 +180,13 @@ namespace {
"Fade-In/-Out ending speed.",
"Fade-In/-Out ending speed."
};
constexpr openspace::properties::Property::PropertyInfo NodeLineInfo = {
"NodeLine",
"Node Line",
"Optional property to track a nodeline. When tracking the label text will be updating the distance "
"from the nodeline start and end. "
};
} // namespace
namespace openspace {
@@ -315,6 +327,7 @@ RenderableLabels::RenderableLabels(const ghoul::Dictionary& dictionary)
, _pixelSizeControl(PixelSizeControlInfo, false)
, _enableFadingEffect(EnableFadingEffectInfo, false)
, _labelText(LabelTextInfo)
, _nodeLine(NodeLineInfo)
, _fadeStartDistance(FadeStartDistInfo, 1.f, 0.f, 100.f)
, _fadeEndDistance(FadeEndDistInfo, 1.f, 0.f, 100.f)
, _fadeStartSpeed(FadeStartSpeedInfo, 1.f, 1.f, 100.f)
@@ -447,6 +460,11 @@ RenderableLabels::RenderableLabels(const ghoul::Dictionary& dictionary)
_fadeStartDistance = dictionary.value<float>(FadeStartDistInfo.identifier);
}
if (dictionary.hasKey(NodeLineInfo.identifier)) {
_nodeLine = dictionary.value<std::string>(NodeLineInfo.identifier);
addProperty(_nodeLine);
}
addProperty(_fadeStartDistance);
_fadeStartUnitOption.addOption(Meter, MeterUnit);
@@ -683,7 +701,30 @@ void RenderableLabels::render(const RenderData& data, RendererTasks&) {
//}
}
void RenderableLabels::update(const UpdateData&) {
void RenderableLabels::update(const UpdateData& data) {
if (global::renderEngine.scene()->sceneGraphNode(_nodeLine)) {
// Calculate distance
SceneGraphNode* nodelineNode = global::renderEngine.scene()->sceneGraphNode(_nodeLine);
RenderableNodeLine* nodeline = dynamic_cast<RenderableNodeLine*>(nodelineNode->renderable());
double myDistance = nodeline->getDistance();
// format string
float scale = getUnit(Kilometer);
std::string distanceText = std::to_string(std::round(myDistance / scale));
int pos = distanceText.find(".");
std::string subStr = distanceText.substr(pos);
distanceText.erase(pos, subStr.size());
std::string finalText = distanceText + " " + KilometerUnit;
setLabelText(finalText);
// Update placement of label with transformation matrix
glm::dvec3 start = global::renderEngine.scene()->sceneGraphNode(nodeline->_start)->worldPosition();
glm::dvec3 end = global::renderEngine.scene()->sceneGraphNode(nodeline->_end)->worldPosition();
glm::dvec3 goalPos = start + (end - start) / 2.0;
_transformationMatrix = glm::translate(glm::dmat4(1.0), goalPos);
}
}
void RenderableLabels::setLabelText(const std::string & newText) {

View File

@@ -107,6 +107,7 @@ private:
properties::BoolProperty _pixelSizeControl;
properties::BoolProperty _enableFadingEffect;
properties::StringProperty _labelText;
properties::StringProperty _nodeLine;
properties::FloatProperty _fadeStartDistance;
properties::FloatProperty _fadeEndDistance;
properties::FloatProperty _fadeStartSpeed;

View File

@@ -43,13 +43,13 @@ namespace {
constexpr openspace::properties::Property::PropertyInfo StartNodeInfo = {
"StartNode",
"Start Node",
"The identifier of the node the line starts from. "
"The identifier of the node the line starts from. Defaults to 'Root' if not specified. "
};
constexpr openspace::properties::Property::PropertyInfo EndNodeInfo = {
"EndNode",
"End Node",
"The identifier of the node the line ends at. "
"The identifier of the node the line ends at. Defaults to 'Root' if not specified. "
};
constexpr openspace::properties::Property::PropertyInfo LineColorInfo = {
@@ -76,13 +76,13 @@ documentation::Documentation RenderableNodeLine::Documentation() {
{
StartNodeInfo.identifier,
new StringVerifier,
Optional::No,
Optional::Yes,
StartNodeInfo.description
},
{
EndNodeInfo.identifier,
new StringVerifier,
Optional::No,
Optional::Yes,
EndNodeInfo.description
},
{
@@ -114,8 +114,13 @@ RenderableNodeLine::RenderableNodeLine(const ghoul::Dictionary& dictionary)
"RenderableNodeLine"
);
_start = dictionary.value<std::string>(StartNodeInfo.identifier);
_end = dictionary.value<std::string>(EndNodeInfo.identifier);
if (dictionary.hasKey(StartNodeInfo.identifier)) {
_start = dictionary.value<std::string>(StartNodeInfo.identifier);
}
if (dictionary.hasKey(EndNodeInfo.identifier)) {
_end = dictionary.value<std::string>(EndNodeInfo.identifier);
}
if (dictionary.hasKey(LineColorInfo.identifier)) {
_lineColor = dictionary.value<glm::vec3>(LineColorInfo.identifier);
@@ -131,6 +136,11 @@ RenderableNodeLine::RenderableNodeLine(const ghoul::Dictionary& dictionary)
addProperty(_opacity);
}
double RenderableNodeLine::getDistance()
{
return glm::distance(_startPos, _endPos);
}
void RenderableNodeLine::initializeGL() {
_program = BaseModule::ProgramObjectManager.request(
ProgramName,

View File

@@ -27,6 +27,7 @@
#include <openspace/rendering/renderable.h>
#include <modules/base/rendering/renderablelabels.h>
#include <openspace/properties/scalar/boolproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/vector/vec3property.h>
@@ -50,7 +51,18 @@ public:
~RenderableNodeLine() = default;
static documentation::Documentation Documentation();
// Identifier of the optional distance label
std::string _distanceLabelId;
//SceneGraphNode* distanceLabelNode = nullptr;
//RenderableLabels* distanceLabel;
// Get the distance between the start and end node
double getDistance();
properties::StringProperty _start;
properties::StringProperty _end;
private:
void initializeGL() override;
void deinitializeGL() override;
@@ -75,8 +87,6 @@ private:
glm::dvec3 _startPos;
glm::dvec3 _endPos;
properties::StringProperty _start;
properties::StringProperty _end;
properties::Vec3Property _lineColor;
properties::FloatProperty _lineWidth;
};