From c075dbcdeb961b639904da4823ef2e9b59150738 Mon Sep 17 00:00:00 2001 From: Emma Broman Date: Fri, 8 Jan 2021 14:05:15 +0100 Subject: [PATCH] Add simple plane geometry class --- include/openspace/util/planegeometry.h | 55 ++++++++ modules/base/rendering/renderabledisc.cpp | 61 +-------- modules/base/rendering/renderabledisc.h | 7 +- .../rendering/renderableorbitdisc.cpp | 64 ++------- .../rendering/renderableorbitdisc.h | 9 +- src/CMakeLists.txt | 2 + src/util/planegeometry.cpp | 123 ++++++++++++++++++ 7 files changed, 207 insertions(+), 114 deletions(-) create mode 100644 include/openspace/util/planegeometry.h create mode 100644 src/util/planegeometry.cpp diff --git a/include/openspace/util/planegeometry.h b/include/openspace/util/planegeometry.h new file mode 100644 index 0000000000..93d4b656c1 --- /dev/null +++ b/include/openspace/util/planegeometry.h @@ -0,0 +1,55 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2021 * + * * + * 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_CORE___PLANEGEOMETRY___H__ +#define __OPENSPACE_CORE___PLANEGEOMETRY___H__ + +#include +#include + +namespace openspace { + +class PlaneGeometry { +public: + PlaneGeometry(glm::vec2 size); + PlaneGeometry(float size); + + ~PlaneGeometry(); + + bool initialize(); + void deinitialize(); + void render(); + + void updateSize(const glm::vec2 size); + void updateSize(const float size); + +private: + GLuint _vaoId = 0; + GLuint _vBufferId = 0; + glm::vec2 _size = glm::vec2(0.f); +}; + +} // namespace openspace + +#endif // __OPENSPACE_CORE___PLANEGEOMETRY___H__ diff --git a/modules/base/rendering/renderabledisc.cpp b/modules/base/rendering/renderabledisc.cpp index f5052e33fe..8d4e71e104 100644 --- a/modules/base/rendering/renderabledisc.cpp +++ b/modules/base/rendering/renderabledisc.cpp @@ -133,6 +133,8 @@ RenderableDisc::RenderableDisc(const ghoul::Dictionary& dictionary) _textureFile->setCallback([&](const File&) { _textureIsDirty = true; }); addProperty(_opacity); + + _plane = std::make_unique(2*_size); } bool RenderableDisc::isReady() const { @@ -148,19 +150,13 @@ void RenderableDisc::initializeGL() { ghoul::opengl::updateUniformLocations(*_shader, _uniformCache, UniformNames); - glGenVertexArrays(1, &_quad); - glGenBuffers(1, &_vertexPositionBuffer); - - createPlane(); + _plane->initialize(); loadTexture(); } void RenderableDisc::deinitializeGL() { - glDeleteVertexArrays(1, &_quad); - _quad = 0; - - glDeleteBuffers(1, &_vertexPositionBuffer); - _vertexPositionBuffer = 0; + _plane->deinitialize(); + _plane = nullptr; _textureFile = nullptr; _texture = nullptr; @@ -197,8 +193,7 @@ void RenderableDisc::render(const RenderData& data, RendererTasks&) { glDepthMask(false); glDisable(GL_CULL_FACE); - glBindVertexArray(_quad); - glDrawArrays(GL_TRIANGLES, 0, 6); + _plane->render(); _shader->deactivate(); @@ -215,7 +210,7 @@ void RenderableDisc::update(const UpdateData&) { } if (_planeIsDirty) { - createPlane(); + _plane->updateSize(2*_size); _planeIsDirty = false; } @@ -251,46 +246,4 @@ void RenderableDisc::loadTexture() { } } -void RenderableDisc::createPlane() { - const GLfloat size = _size; - - struct VertexData { - GLfloat x; - GLfloat y; - GLfloat s; - GLfloat t; - }; - - VertexData data[] = { - { -size, -size, 0.f, 0.f }, - { size, size, 1.f, 1.f }, - { -size, size, 0.f, 1.f }, - { -size, -size, 0.f, 0.f }, - { size, -size, 1.f, 0.f }, - { size, size, 1.f, 1.f }, - }; - - glBindVertexArray(_quad); - glBindBuffer(GL_ARRAY_BUFFER, _vertexPositionBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer( - 0, - 2, - GL_FLOAT, - GL_FALSE, - sizeof(VertexData), - nullptr - ); - glEnableVertexAttribArray(1); - glVertexAttribPointer( - 1, - 2, - GL_FLOAT, - GL_FALSE, - sizeof(VertexData), - reinterpret_cast(offsetof(VertexData, s)) // NOLINT - ); -} - } // namespace openspace diff --git a/modules/base/rendering/renderabledisc.h b/modules/base/rendering/renderabledisc.h index e767b2239c..6cbe6baa75 100644 --- a/modules/base/rendering/renderabledisc.h +++ b/modules/base/rendering/renderabledisc.h @@ -29,7 +29,7 @@ #include #include - +#include #include #include @@ -59,7 +59,6 @@ public: private: void loadTexture(); - void createPlane(); properties::StringProperty _texturePath; properties::FloatProperty _size; @@ -70,9 +69,9 @@ private: std::unique_ptr _texture; std::unique_ptr _textureFile; + std::unique_ptr _plane; + bool _textureIsDirty = false; - GLuint _quad = 0; - GLuint _vertexPositionBuffer = 0; bool _planeIsDirty = false; }; diff --git a/modules/exoplanets/rendering/renderableorbitdisc.cpp b/modules/exoplanets/rendering/renderableorbitdisc.cpp index 2887a6bae9..fdef0e820a 100644 --- a/modules/exoplanets/rendering/renderableorbitdisc.cpp +++ b/modules/exoplanets/rendering/renderableorbitdisc.cpp @@ -158,6 +158,8 @@ RenderableOrbitDisc::RenderableOrbitDisc(const ghoul::Dictionary& dictionary) addProperty(_eccentricity); addProperty(_opacity); + + _plane = std::make_unique(planeSize()); } bool RenderableOrbitDisc::isReady() const { @@ -173,19 +175,13 @@ void RenderableOrbitDisc::initializeGL() { ghoul::opengl::updateUniformLocations(*_shader, _uniformCache, UniformNames); - glGenVertexArrays(1, &_quad); - glGenBuffers(1, &_vertexPositionBuffer); - - createPlane(); + _plane->initialize(); loadTexture(); } void RenderableOrbitDisc::deinitializeGL() { - glDeleteVertexArrays(1, &_quad); - _quad = 0; - - glDeleteBuffers(1, &_vertexPositionBuffer); - _vertexPositionBuffer = 0; + _plane->deinitialize(); + _plane = nullptr; _textureFile = nullptr; _texture = nullptr; @@ -223,8 +219,7 @@ void RenderableOrbitDisc::render(const RenderData& data, RendererTasks&) { glDepthMask(false); glDisable(GL_CULL_FACE); - glBindVertexArray(_quad); - glDrawArrays(GL_TRIANGLES, 0, 6); + _plane->render(); _shader->deactivate(); @@ -241,7 +236,7 @@ void RenderableOrbitDisc::update(const UpdateData&) { } if (_planeIsDirty) { - createPlane(); + _plane->updateSize(planeSize()); _planeIsDirty = false; } @@ -274,47 +269,10 @@ void RenderableOrbitDisc::loadTexture() { } } -void RenderableOrbitDisc::createPlane() { - const GLfloat outerDistance = (_size + _offset.value().y * _size); - const GLfloat size = outerDistance * (1.f + _eccentricity); - - struct VertexData { - GLfloat x; - GLfloat y; - GLfloat s; - GLfloat t; - }; - - VertexData data[] = { - { -size, -size, 0.f, 0.f }, - { size, size, 1.f, 1.f }, - { -size, size, 0.f, 1.f }, - { -size, -size, 0.f, 0.f }, - { size, -size, 1.f, 0.f }, - { size, size, 1.f, 1.f }, - }; - - glBindVertexArray(_quad); - glBindBuffer(GL_ARRAY_BUFFER, _vertexPositionBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer( - 0, - 2, - GL_FLOAT, - GL_FALSE, - sizeof(VertexData), - nullptr - ); - glEnableVertexAttribArray(1); - glVertexAttribPointer( - 1, - 2, - GL_FLOAT, - GL_FALSE, - sizeof(VertexData), - reinterpret_cast(offsetof(VertexData, s)) - ); +float RenderableOrbitDisc::planeSize() const { + float maxRadius = _size + _offset.value().y * _size; + maxRadius *= (1.f + _eccentricity); + return 2.f * maxRadius; } } // namespace openspace diff --git a/modules/exoplanets/rendering/renderableorbitdisc.h b/modules/exoplanets/rendering/renderableorbitdisc.h index 5a94a2bfc1..1aea4ae413 100644 --- a/modules/exoplanets/rendering/renderableorbitdisc.h +++ b/modules/exoplanets/rendering/renderableorbitdisc.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -58,7 +59,9 @@ public: private: void loadTexture(); - void createPlane(); + + // Computes the size of the plane quad using the relevant properties + float planeSize() const; properties::StringProperty _texturePath; properties::FloatProperty _size; @@ -71,10 +74,10 @@ private: std::unique_ptr _texture = nullptr; std::unique_ptr _textureFile; + std::unique_ptr _plane; + bool _textureIsDirty = false; bool _planeIsDirty = false; - GLuint _quad = 0; - GLuint _vertexPositionBuffer = 0; }; } // namespace openspace diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bdd8c0b8b2..8ad4ff0933 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -178,6 +178,7 @@ set(OPENSPACE_SOURCE ${OPENSPACE_BASE_DIR}/src/util/httprequest.cpp ${OPENSPACE_BASE_DIR}/src/util/keys.cpp ${OPENSPACE_BASE_DIR}/src/util/openspacemodule.cpp + ${OPENSPACE_BASE_DIR}/src/util/planegeometry.cpp ${OPENSPACE_BASE_DIR}/src/util/progressbar.cpp ${OPENSPACE_BASE_DIR}/src/util/resourcesynchronization.cpp ${OPENSPACE_BASE_DIR}/src/util/screenlog.cpp @@ -373,6 +374,7 @@ set(OPENSPACE_HEADER ${OPENSPACE_BASE_DIR}/include/openspace/util/memorymanager.h ${OPENSPACE_BASE_DIR}/include/openspace/util/mouse.h ${OPENSPACE_BASE_DIR}/include/openspace/util/openspacemodule.h + ${OPENSPACE_BASE_DIR}/include/openspace/util/planegeometry.h ${OPENSPACE_BASE_DIR}/include/openspace/util/progressbar.h ${OPENSPACE_BASE_DIR}/include/openspace/util/resourcesynchronization.h ${OPENSPACE_BASE_DIR}/include/openspace/util/screenlog.h diff --git a/src/util/planegeometry.cpp b/src/util/planegeometry.cpp new file mode 100644 index 0000000000..33ee25fbc3 --- /dev/null +++ b/src/util/planegeometry.cpp @@ -0,0 +1,123 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2021 * + * * + * 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 + +#include +#include + +namespace { + constexpr const char* _loggerCat = "PlaneGeometry"; +} // namespace + +namespace openspace { + +PlaneGeometry::PlaneGeometry(glm::vec2 size) : _size(std::move(size)) {} + +PlaneGeometry::PlaneGeometry(float size) : _size(size, size) {} + +PlaneGeometry::~PlaneGeometry() { + glDeleteBuffers(1, &_vBufferId); + glDeleteVertexArrays(1, &_vaoId); +} + +bool PlaneGeometry::initialize() { + // Initialize and upload to GPU + const glm::vec2 size = _size * 0.5f; + + struct VertexData { + GLfloat x; + GLfloat y; + GLfloat s; + GLfloat t; + }; + + VertexData vertices[] = { + { -size.x, -size.y, 0.f, 0.f }, + { size.x, size.y, 1.f, 1.f }, + { -size.x, size.y, 0.f, 1.f }, + { -size.x, -size.y, 0.f, 0.f }, + { size.x, -size.y, 1.f, 0.f }, + { size.x, size.y, 1.f, 1.f }, + }; + + if (_vaoId == 0) { + glGenVertexArrays(1, &_vaoId); + } + + if (_vBufferId == 0) { + glGenBuffers(1, &_vBufferId); + + if (_vBufferId == 0) { + LERROR("Could not create vertex buffer"); + return false; + } + } + + // First VAO setup + glBindVertexArray(_vaoId); + + glBindBuffer(GL_ARRAY_BUFFER, _vBufferId); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), nullptr); + glEnableVertexAttribArray(1); + glVertexAttribPointer( + 1, + 2, + GL_FLOAT, + GL_FALSE, + sizeof(VertexData), + reinterpret_cast(offsetof(VertexData, s)) // NOLINT + ); + + glBindVertexArray(0); + return true; +} + +void PlaneGeometry::deinitialize() { + glDeleteVertexArrays(1, &_vaoId); + _vaoId = 0; + + glDeleteBuffers(1, &_vBufferId); + _vBufferId = 0; +} + +void PlaneGeometry::render() { + glBindVertexArray(_vaoId); // select first VAO + glDrawArrays(GL_TRIANGLES, 0, 6); + glBindVertexArray(0); +} + +void PlaneGeometry::updateSize(const glm::vec2 size) { + _size = size; + initialize(); +} + +void PlaneGeometry::updateSize(const float size) { + _size = glm::vec2(size, size); + initialize(); +} + +} // namespace openspace