Docs and examples for RenderableBoxGrid (#3479)

* Add boxgrid examples

* Add description about the box grid renderable for the docs page

* change phrasing in example assets slightly

* Apply suggestions from code review

Co-authored-by: Ylva Selling <ylva.selling@gmail.com>

* Remove labels support for `RenderableBoxGrid`

---------

Co-authored-by: Ylva Selling <ylva.selling@gmail.com>
This commit is contained in:
Emma Broman
2025-01-17 13:50:40 +01:00
committed by GitHub
parent 7e6128d620
commit 0dbc331ebf
5 changed files with 92 additions and 56 deletions

View File

@@ -0,0 +1,31 @@
-- Basic
-- This example adds a box grid, which is a 3D box rendered using grid lines, to the
-- scene.
--
-- Per default, the box will be given a size of 1x1x1 meters, and here it is scaled up by a
-- factor of 100. It will hence have a size of 100x100x100 meters.
local Node = {
Identifier = "RenderableBoxGrid_Example",
Transform = {
Scale = {
Type = "StaticScale",
Scale = 100
}
},
Renderable = {
Type = "RenderableBoxGrid"
},
GUI = {
Name = "RenderableBoxGrid - Basic",
Path = "/Examples"
}
}
asset.onInitialize(function()
openspace.addSceneGraphNode(Node)
end)
asset.onDeinitialize(function()
openspace.removeSceneGraphNode(Node)
end)

View File

@@ -0,0 +1,32 @@
-- With Non-uniform Size
-- This example creates a box grid with a non-uniform size. The size has been set so the
-- box is two times larger in the Y-direction.
--
-- The `Size` values are given in meters, and here the box is scaled up by a factor of
-- 100. So, the box will have a size of 100 times the `Size` values.
local Node = {
Identifier = "RenderableBoxGrid_Example_Size",
Transform = {
Scale = {
Type = "StaticScale",
Scale = 100
}
},
Renderable = {
Type = "RenderableBoxGrid",
Size = { 1.0, 2.0, 1.0 }
},
GUI = {
Name = "RenderableBoxGrid - Non-uniform Size",
Path = "/Examples"
}
}
asset.onInitialize(function()
openspace.addSceneGraphNode(Node)
end)
asset.onDeinitialize(function()
openspace.removeSceneGraphNode(Node)
end)

View File

@@ -0,0 +1,24 @@
-- Styled
-- This example creates a box grid where the grid lines are styled to have a specific
-- color and line width.
local Node = {
Identifier = "RenderableBoxGrid_Example_Styled",
Renderable = {
Type = "RenderableBoxGrid",
LineWidth = 4.0,
Color = { 1.0, 1.0, 0.0 }
},
GUI = {
Name = "RenderableBoxGrid - Styled",
Path = "/Examples"
}
}
asset.onInitialize(function()
openspace.addSceneGraphNode(Node)
end)
asset.onDeinitialize(function()
openspace.removeSceneGraphNode(Node)
end)

View File

@@ -57,12 +57,10 @@ namespace {
openspace::properties::Property::Visibility::AdvancedUser
};
const openspace::properties::PropertyOwner::PropertyOwnerInfo LabelsInfo = {
"Labels",
"Labels",
"The labels for the grid."
};
// A RenderableBoxGrid creates a 3D box that is rendered using grid lines.
//
// Per default the box is given a uniform size of 1x1x1 meters. It can then be scaled
// to the desired size. Alternatively, the size in each dimension can be specified.
struct [[codegen::Dictionary(RenderableBoxGrid)]] Parameters {
// [[codegen::verbatim(ColorInfo.description)]]
std::optional<glm::vec3> color [[codegen::color()]];
@@ -72,10 +70,6 @@ namespace {
// [[codegen::verbatim(SizeInfo.description)]]
std::optional<glm::vec3> size;
// [[codegen::verbatim(LabelsInfo.description)]]
std::optional<ghoul::Dictionary> labels
[[codegen::reference("labelscomponent")]];
};
#include "renderableboxgrid_codegen.cpp"
} // namespace
@@ -106,24 +100,10 @@ RenderableBoxGrid::RenderableBoxGrid(const ghoul::Dictionary& dictionary)
_size = p.size.value_or(_size);
_size.onChange([this]() { _gridIsDirty = true; });
addProperty(_size);
if (p.labels.has_value()) {
_labels = std::make_unique<LabelsComponent>(*p.labels);
_hasLabels = true;
addPropertySubOwner(_labels.get());
// Fading of the labels should also depend on the fading of the renderable
_labels->setParentFadeable(this);
}
}
bool RenderableBoxGrid::isReady() const {
return _hasLabels ? _gridProgram && _labels->isReady() : _gridProgram != nullptr;
}
void RenderableBoxGrid::initialize() {
if (_hasLabels) {
_labels->initialize();
}
return _gridProgram != nullptr;
}
void RenderableBoxGrid::initializeGL() {
@@ -195,31 +175,6 @@ void RenderableBoxGrid::render(const RenderData& data, RendererTasks&) {
global::renderEngine->openglStateCache().resetBlendState();
global::renderEngine->openglStateCache().resetLineState();
global::renderEngine->openglStateCache().resetDepthState();
// Draw labels
if (_hasLabels && _labels->enabled()) {
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)) {
const glm::vec3 otherVector = glm::vec3(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, modelViewProjectionTransform, orthoRight, orthoUp);
}
}
void RenderableBoxGrid::update(const UpdateData&) {

View File

@@ -29,7 +29,6 @@
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/vector/vec3property.h>
#include <openspace/rendering/labelscomponent.h>
#include <ghoul/opengl/ghoul_gl.h>
namespace ghoul::opengl {
@@ -44,7 +43,6 @@ class RenderableBoxGrid : public Renderable {
public:
RenderableBoxGrid(const ghoul::Dictionary& dictionary);
void initialize() override;
void initializeGL() override;
void deinitializeGL() override;
@@ -73,10 +71,6 @@ protected:
GLenum _mode = GL_LINE_STRIP;
std::vector<Vertex> _varray;
// Labels
bool _hasLabels = false;
std::unique_ptr<LabelsComponent> _labels;
};
}// namespace openspace