Add simple plane geometry class

This commit is contained in:
Emma Broman
2021-01-08 14:05:15 +01:00
parent 76d599d284
commit c075dbcdeb
7 changed files with 207 additions and 114 deletions
+55
View File
@@ -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 <ghoul/glm.h>
#include <ghoul/opengl/ghoul_gl.h>
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__
+7 -54
View File
@@ -133,6 +133,8 @@ RenderableDisc::RenderableDisc(const ghoul::Dictionary& dictionary)
_textureFile->setCallback([&](const File&) { _textureIsDirty = true; });
addProperty(_opacity);
_plane = std::make_unique<PlaneGeometry>(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<void*>(offsetof(VertexData, s)) // NOLINT
);
}
} // namespace openspace
+3 -4
View File
@@ -29,7 +29,7 @@
#include <openspace/properties/stringproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/util/planegeometry.h>
#include <ghoul/opengl/uniformcache.h>
#include <ghoul/opengl/ghoul_gl.h>
@@ -59,7 +59,6 @@ public:
private:
void loadTexture();
void createPlane();
properties::StringProperty _texturePath;
properties::FloatProperty _size;
@@ -70,9 +69,9 @@ private:
std::unique_ptr<ghoul::opengl::Texture> _texture;
std::unique_ptr<ghoul::filesystem::File> _textureFile;
std::unique_ptr<PlaneGeometry> _plane;
bool _textureIsDirty = false;
GLuint _quad = 0;
GLuint _vertexPositionBuffer = 0;
bool _planeIsDirty = false;
};
@@ -158,6 +158,8 @@ RenderableOrbitDisc::RenderableOrbitDisc(const ghoul::Dictionary& dictionary)
addProperty(_eccentricity);
addProperty(_opacity);
_plane = std::make_unique<PlaneGeometry>(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<void*>(offsetof(VertexData, s))
);
float RenderableOrbitDisc::planeSize() const {
float maxRadius = _size + _offset.value().y * _size;
maxRadius *= (1.f + _eccentricity);
return 2.f * maxRadius;
}
} // namespace openspace
@@ -29,6 +29,7 @@
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/vector/vec2property.h>
#include <openspace/rendering/renderable.h>
#include <openspace/util/planegeometry.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/opengl/texture.h>
#include <ghoul/opengl/uniformcache.h>
@@ -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<ghoul::opengl::Texture> _texture = nullptr;
std::unique_ptr<ghoul::filesystem::File> _textureFile;
std::unique_ptr<PlaneGeometry> _plane;
bool _textureIsDirty = false;
bool _planeIsDirty = false;
GLuint _quad = 0;
GLuint _vertexPositionBuffer = 0;
};
} // namespace openspace
+2
View File
@@ -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
+123
View File
@@ -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 <openspace/util/planegeometry.h>
#include <ghoul/logging/logmanager.h>
#include <string>
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<void*>(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