Correctly make RenderableDebugPlane derive from RenderablePlane (closes #3175)

This commit is contained in:
Alexander Bock
2024-04-24 15:11:36 +02:00
parent 1d91e48a6c
commit 0bc1af8951
2 changed files with 11 additions and 225 deletions
@@ -22,32 +22,11 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
// @TODO: Clean up this class and make it not copy much code from RenderablePlane ---abock
#include <modules/debugging/rendering/renderabledebugplane.h>
#include <modules/spacecraftinstruments/rendering/renderableplanetprojection.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/io/texture/texturereader.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/texture.h>
#include <ghoul/opengl/textureunit.h>
#include <optional>
namespace {
enum Origin {
LowerLeft = 0,
LowerRight,
UpperLeft,
UpperRight,
Center
};
constexpr openspace::properties::Property::PropertyInfo TextureInfo = {
"Texture",
"Texture",
@@ -55,48 +34,9 @@ namespace {
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo BillboardInfo = {
"Billboard",
"Billboard mode",
"This value specifies whether the plane is a billboard, which means that it is "
"always facing the camera. If this is false, it can be oriented using other "
"transformations.",
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo SizeInfo = {
"Size",
"Size (in meters)",
"This value specifies the size of the plane in meters.",
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo OriginInfo = {
"Origin",
"Texture Coordinate Origin",
"The origin of the texture coorinate system.",
openspace::properties::Property::Visibility::AdvancedUser
};
struct [[codegen::Dictionary(RenderableDebugPlane)]] Parameters {
// [[codegen::verbatim(TextureInfo.description)]]
std::optional<int> texture;
// [[codegen::verbatim(BillboardInfo.description)]]
std::optional<bool> billboard;
// [[codegen::verbatim(SizeInfo.description)]]
std::optional<float> size;
enum class [[codegen::map(Origin)]] Origin {
LowerLeft,
LowerRight,
UpperLeft,
UpperRight,
Center
};
// [[codegen::verbatim(OriginInfo.description)]]
std::optional<Origin> origin;
};
#include "renderabledebugplane_codegen.cpp"
} // namespace
@@ -104,149 +44,28 @@ namespace {
namespace openspace {
documentation::Documentation RenderableDebugPlane::Documentation() {
return codegen::doc<Parameters>("debugging_renderable_debugplane");
return codegen::doc<Parameters>(
"debugging_renderable_debugplane",
RenderablePlane::Documentation()
);
}
RenderableDebugPlane::RenderableDebugPlane(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
: RenderablePlane(dictionary)
, _texture(TextureInfo, -1, -1, 512)
, _billboard(BillboardInfo, false)
, _size(SizeInfo, 10.f, 0.f, 1e25f)
, _origin(OriginInfo, properties::OptionProperty::DisplayType::Dropdown)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
_texture = p.texture.value_or(_texture);
addProperty(_texture);
_size.setExponent(15.f);
_size.onChange([this](){ _planeIsDirty = true; });
_size = p.size.value_or(_size);
setBoundingSphere(_size);
addProperty(_size);
_billboard = p.billboard.value_or(_billboard);
addProperty(_billboard);
_origin.addOptions({
{ LowerLeft, "LowerLeft" },
{ LowerRight, "LowerRight" },
{ UpperLeft, "UpperLeft" },
{ UpperRight, "UpperRight" },
{ Center, "Center" }
});
_origin.setValue(Center);
if (p.origin.has_value()) {
_origin = codegen::map<Origin>(*p.origin);
}
else {
_origin = Center;
}
}
bool RenderableDebugPlane::isReady() const {
bool ready = true;
if (!_shader) {
ready &= false;
}
return ready;
return _texture >= 0 && RenderablePlane::isReady();
}
void RenderableDebugPlane::initializeGL() {
glGenVertexArrays(1, &_quad); // generate array
glGenBuffers(1, &_vertexPositionBuffer); // generate buffer
createPlane();
if (!_shader) {
_shader = global::renderEngine->buildRenderProgram("PlaneProgram",
absPath("${MODULE_BASE}/shaders/plane_vs.glsl"),
absPath("${MODULE_BASE}/shaders/plane_fs.glsl")
);
}
}
void RenderableDebugPlane::deinitializeGL() {
glDeleteVertexArrays(1, &_quad);
_quad = 0;
glDeleteBuffers(1, &_vertexPositionBuffer);
_vertexPositionBuffer = 0;
if (_shader) {
global::renderEngine->removeRenderProgram(_shader.get());
_shader = nullptr;
}
}
void RenderableDebugPlane::render(const RenderData& data, RendererTasks&) {
glm::mat4 transform = glm::mat4(1.f);
if (_billboard) {
transform = glm::inverse(glm::mat4(data.camera.viewRotationMatrix()));
}
// Activate shader
_shader->activate();
_shader->setUniform("ViewProjection", data.camera.viewProjectionMatrix());
_shader->setUniform("ModelTransform", transform);
_shader->setUniform("campos", glm::vec4(data.camera.positionVec3(), 1.f));
_shader->setUniform("objpos", glm::vec4(data.modelTransform.translation, 0.f));
_shader->setUniform("camrot", glm::mat4(data.camera.viewRotationMatrix()));
_shader->setUniform("scaling", glm::vec2(1.f, 0.f));
ghoul::opengl::TextureUnit unit;
unit.activate();
void RenderableDebugPlane::bindTexture() {
glBindTexture(GL_TEXTURE_2D, _texture);
_shader->setUniform("texture1", unit);
glBindVertexArray(_quad);
glDrawArrays(GL_TRIANGLES, 0, 6);
_shader->deactivate();
}
void RenderableDebugPlane::update(const UpdateData&) {
if (_shader->isDirty()) {
_shader->rebuildFromFile();
}
if (_planeIsDirty) {
createPlane();
}
}
void RenderableDebugPlane::createPlane() {
// ============================
// GEOMETRY (quad)
// ============================
const GLfloat size = _size;
const std::array<GLfloat, 36> vertexData = {
// x y z w s t
-size, -size, 0.f, 0.f, 0.f, 0.f,
size, size, 0.f, 0.f, 1.f, 1.f,
-size, size, 0.f, 0.f, 0.f, 1.f,
-size, -size, 0.f, 0.f, 0.f, 0.f,
size, -size, 0.f, 0.f, 1.f, 0.f,
size, size, 0.f, 0.f, 1.f, 1.f,
};
glBindVertexArray(_quad); // bind array
glBindBuffer(GL_ARRAY_BUFFER, _vertexPositionBuffer); // bind buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, nullptr);
glEnableVertexAttribArray(1);
glVertexAttribPointer(
1,
2,
GL_FLOAT,
GL_FALSE,
sizeof(GLfloat) * 6,
reinterpret_cast<void*>(sizeof(GLfloat) * 4)
);
}
} // namespace openspace
@@ -25,55 +25,22 @@
#ifndef __OPENSPACE_MODULE_DEBUGGING___RENDERABLEDEBUGPLANE___H__
#define __OPENSPACE_MODULE_DEBUGGING___RENDERABLEDEBUGPLANE___H__
#include <openspace/rendering/renderable.h>
#include <openspace/properties/optionproperty.h>
#include <openspace/properties/stringproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <ghoul/opengl/ghoul_gl.h>
namespace ghoul::filesystem { class File; }
namespace ghoul::opengl {
class ProgramObject;
class Texture;
} // namespace ghoul::opengl
namespace documentation { struct Documentation; }
#include <modules/base/rendering/renderableplane.h>
namespace openspace {
struct LinePoint;
struct UpdateStructure;
class RenderableDebugPlane : public Renderable {
class RenderableDebugPlane : public RenderablePlane {
public:
RenderableDebugPlane(const ghoul::Dictionary& dictionary);
~RenderableDebugPlane() override = default;
void initializeGL() override;
void deinitializeGL() override;
bool isReady() const override;
void render(const RenderData& data, RendererTasks& rendererTask) override;
void update(const UpdateData& data) override;
bool isReady() const;
static documentation::Documentation Documentation();
private:
void createPlane();
virtual void bindTexture() override;
properties::IntProperty _texture;
properties::BoolProperty _billboard;
properties::FloatProperty _size;
properties::OptionProperty _origin;
bool _planeIsDirty = true;
std::unique_ptr<ghoul::opengl::ProgramObject> _shader;
GLuint _quad = 0;
GLuint _vertexPositionBuffer = 0;
};
} // namespace openspace