Add labels to the other grid classes too

This commit is contained in:
Malin E
2022-09-19 12:07:59 +02:00
parent 69790882b9
commit e215e9f2a7
6 changed files with 241 additions and 21 deletions
@@ -55,6 +55,19 @@ namespace {
"This value specifies the line width of the spherical grid"
};
constexpr openspace::properties::Property::PropertyInfo DrawLabelInfo = {
"DrawLabels",
"Draw Labels",
"Determines whether labels should be drawn or hidden"
};
static const openspace::properties::PropertyOwner::PropertyOwnerInfo LabelsInfo =
{
"Labels",
"Labels",
"The labels for the grid"
};
struct [[codegen::Dictionary(RenderableSphericalGrid)]] Parameters {
// [[codegen::verbatim(ColorInfo.description)]]
std::optional<glm::vec3> color [[codegen::color()]];
@@ -64,6 +77,13 @@ namespace {
// [[codegen::verbatim(LineWidthInfo.description)]]
std::optional<float> lineWidth;
// [[codegen::verbatim(DrawLabelInfo.description)]]
std::optional<bool> drawLabels;
// [[codegen::verbatim(LabelsInfo.description)]]
std::optional<ghoul::Dictionary> labels
[[codegen::reference("space_labelscomponent")]];
};
#include "renderablesphericalgrid_codegen.cpp"
} // namespace
@@ -80,6 +100,7 @@ RenderableSphericalGrid::RenderableSphericalGrid(const ghoul::Dictionary& dictio
, _color(ColorInfo, glm::vec3(0.5f), glm::vec3(0.f), glm::vec3(1.f))
, _segments(SegmentsInfo, 36, 4, 200)
, _lineWidth(LineWidthInfo, 0.5f, 1.f, 20.f)
, _drawLabels(DrawLabelInfo, false)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
@@ -104,12 +125,31 @@ RenderableSphericalGrid::RenderableSphericalGrid(const ghoul::Dictionary& dictio
// Radius is always 1
setBoundingSphere(1.0);
if (p.labels.has_value()) {
_drawLabels = p.drawLabels.value_or(_drawLabels);
addProperty(_drawLabels);
_labels = std::make_unique<LabelsComponent>(*p.labels);
_hasLabels = true;
addPropertySubOwner(_labels.get());
}
}
bool RenderableSphericalGrid::isReady() const {
bool ready = true;
ready &= (_gridProgram != nullptr);
return ready;
bool isReady = _gridProgram != nullptr;
if (_hasLabels) {
isReady = isReady && _labels->isReady();
}
return isReady;
}
void RenderableSphericalGrid::initialize() {
if (_hasLabels) {
_labels->initialize();
_labels->loadLabels();
}
}
void RenderableSphericalGrid::initializeGL() {
@@ -162,14 +202,14 @@ void RenderableSphericalGrid::render(const RenderData& data, RendererTasks&){
glm::dmat4(data.modelTransform.rotation) * // Spice rotation
glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale));
const glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() *
modelTransform;
const glm::dmat4 modelViewTransform =
data.camera.combinedViewMatrix() * modelTransform;
const glm::dmat4 projectionMatrix = data.camera.projectionMatrix();
const glm::dmat4 modelViewProjectionMatrix = projectionMatrix * modelViewTransform;
_gridProgram->setUniform("modelViewTransform", modelViewTransform);
_gridProgram->setUniform(
"MVPTransform",
glm::dmat4(data.camera.projectionMatrix()) * modelViewTransform
);
_gridProgram->setUniform("MVPTransform", modelViewProjectionMatrix);
_gridProgram->setUniform("opacity", opacity());
_gridProgram->setUniform("gridColor", _color);
@@ -195,6 +235,31 @@ void RenderableSphericalGrid::render(const RenderData& data, RendererTasks&){
global::renderEngine->openglStateCache().resetBlendState();
global::renderEngine->openglStateCache().resetLineState();
global::renderEngine->openglStateCache().resetDepthState();
// Draw labels
if (_drawLabels && _hasLabels) {
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(modelTransform);
glm::vec3 orthoRight = glm::normalize(
glm::vec3(worldToModelTransform * glm::vec4(right, 0.0))
);
if (orthoRight == glm::vec3(0.0)) {
glm::vec3 otherVector(lookup.y, lookup.x, lookup.z);
right = glm::cross(viewDirection, otherVector);
orthoRight = glm::normalize(
glm::vec3(worldToModelTransform * glm::vec4(right, 0.0))
);
}
const glm::vec3 orthoUp = glm::normalize(
glm::vec3(worldToModelTransform * glm::dvec4(up, 0.0))
);
_labels->render(data, modelViewProjectionMatrix, orthoRight, orthoUp);
}
}
void RenderableSphericalGrid::update(const UpdateData&) {