Add example assets and cleanup code

This commit is contained in:
Emma Broman
2020-07-08 08:38:52 +02:00
parent f7b8061601
commit bdc7f7b637
5 changed files with 190 additions and 150 deletions
+62 -66
View File
@@ -62,7 +62,6 @@ namespace {
"Grid Size",
"This value species the size of each dimensions of the grid"
};
} // namespace
namespace openspace {
@@ -81,7 +80,7 @@ documentation::Documentation RenderableGrid::Documentation() {
},
{
SegmentsInfo.identifier,
new DoubleVector2Verifier, // TODO: Should be Int, but specification test fails...
new DoubleVector2Verifier, // @TODO (emmbr 2020-07-07): should be Int, but specification test fails...
Optional::Yes,
SegmentsInfo.description
},
@@ -103,13 +102,8 @@ documentation::Documentation RenderableGrid::Documentation() {
RenderableGrid::RenderableGrid(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _gridColor(
GridColorInfo,
glm::vec3(0.5f, 0.5, 0.5f),
glm::vec3(0.f),
glm::vec3(1.f)
)
, _segments(SegmentsInfo, glm::uvec2(10), glm::uvec2(1), glm::uvec2(200)) // TODO: review range
, _gridColor(GridColorInfo, glm::vec3(0.5f), glm::vec3(0.f), glm::vec3(1.f))
, _segments(SegmentsInfo, glm::uvec2(10), glm::uvec2(1), glm::uvec2(200))
, _lineWidth(LineWidthInfo, 0.5f, 0.f, 20.f)
, _size(SizeInfo, glm::vec2(1e20f), glm::vec2(1.f), glm::vec2(1e35f))
{
@@ -257,64 +251,66 @@ void RenderableGrid::render(const RenderData& data, RendererTasks&){
}
void RenderableGrid::update(const UpdateData&) {
if (_gridIsDirty) {
const glm::vec2 halfSize = _size.value() / 2.f;
const glm::uvec2 nSegments = _segments.value();
const glm::vec2 step = _size.value() / static_cast<glm::vec2>(nSegments);
const int nLines = (2 * nSegments.x * nSegments.y) + nSegments.x + nSegments.y;
const int nVertices = 2 * nLines;
_varray.resize(nVertices);
// OBS! Could be optimized further by removing duplicate vertices
int nr = 0;
for (unsigned int i = 0; i < nSegments.x; ++i) {
for (unsigned int j = 0; j < nSegments.y; ++j) {
float y0 = -halfSize.y + j * step.y;
float y1 = y0 + step.y;
float x0 = -halfSize.x + i * step.x;
float x1 = x0 + step.x;
_varray[nr++] = { x0, y0, 0.f };
_varray[nr++] = { x0, y1, 0.f };
_varray[nr++] = { x0, y0, 0.f };
_varray[nr++] = { x1, y0, 0.f };
}
}
// last x row
for (unsigned int i = 0; i < nSegments.x; ++i) {
float x0 = -halfSize.x + i * step.x;
float x1 = x0 + step.x;
_varray[nr++] = { x0, halfSize.y, 0.f };
_varray[nr++] = { x1, halfSize.y, 0.f };
}
// last y col
for (unsigned int i = 0; i < nSegments.y; ++i) {
float y0 = -halfSize.y + i * step.y;
float y1 = y0 + step.y;
_varray[nr++] = { halfSize.x, y0, 0.f };
_varray[nr++] = { halfSize.x, y1, 0.f };
}
glBindVertexArray(_vaoID);
glBindBuffer(GL_ARRAY_BUFFER, _vBufferID);
glBufferData(
GL_ARRAY_BUFFER,
_varray.size() * sizeof(Vertex),
_varray.data(),
GL_STATIC_DRAW
);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
glBindVertexArray(0);
_gridIsDirty = false;
if (!_gridIsDirty) {
return;
}
const glm::vec2 halfSize = _size.value() / 2.f;
const glm::uvec2 nSegments = _segments.value();
const glm::vec2 step = _size.value() / static_cast<glm::vec2>(nSegments);
const int nLines = (2 * nSegments.x * nSegments.y) + nSegments.x + nSegments.y;
const int nVertices = 2 * nLines;
_varray.resize(nVertices);
// OBS! Could be optimized further by removing duplicate vertices
int nr = 0;
for (unsigned int i = 0; i < nSegments.x; ++i) {
for (unsigned int j = 0; j < nSegments.y; ++j) {
const float y0 = -halfSize.y + j * step.y;
const float y1 = y0 + step.y;
const float x0 = -halfSize.x + i * step.x;
const float x1 = x0 + step.x;
_varray[nr++] = { x0, y0, 0.f };
_varray[nr++] = { x0, y1, 0.f };
_varray[nr++] = { x0, y0, 0.f };
_varray[nr++] = { x1, y0, 0.f };
}
}
// last x row
for (unsigned int i = 0; i < nSegments.x; ++i) {
const float x0 = -halfSize.x + i * step.x;
const float x1 = x0 + step.x;
_varray[nr++] = { x0, halfSize.y, 0.f };
_varray[nr++] = { x1, halfSize.y, 0.f };
}
// last y col
for (unsigned int i = 0; i < nSegments.y; ++i) {
const float y0 = -halfSize.y + i * step.y;
const float y1 = y0 + step.y;
_varray[nr++] = { halfSize.x, y0, 0.f };
_varray[nr++] = { halfSize.x, y1, 0.f };
}
glBindVertexArray(_vaoID);
glBindBuffer(GL_ARRAY_BUFFER, _vBufferID);
glBufferData(
GL_ARRAY_BUFFER,
_varray.size() * sizeof(Vertex),
_varray.data(),
GL_STATIC_DRAW
);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
glBindVertexArray(0);
_gridIsDirty = false;
}
} // namespace openspace
@@ -35,10 +35,7 @@
#include <openspace/properties/vector/vec3property.h>
#include <ghoul/opengl/ghoul_gl.h>
namespace ghoul::opengl {
class ProgramObject;
} // namespace ghoul::opengl
namespace ghoul::opengl { class ProgramObject; }
namespace openspace::documentation { struct Documentation; }
namespace openspace {
@@ -76,7 +76,6 @@ namespace {
"The inner radius of the circular grid, that is the radius of the inmost ring. "
"Must be smaller than the outer radius."
};
} // namespace
namespace openspace {
@@ -127,16 +126,9 @@ documentation::Documentation RenderableRadialGrid::Documentation() {
};
}
RenderableRadialGrid::RenderableRadialGrid(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _gridProgram(nullptr)
, _gridColor(
GridColorInfo,
glm::vec3(0.5f, 0.5, 0.5f),
glm::vec3(0.f),
glm::vec3(1.f)
)
, _gridColor(GridColorInfo, glm::vec3(0.5f), glm::vec3(0.f), glm::vec3(1.f))
, _gridSegments(
GridSegmentsInfo,
glm::ivec2(1, 1),
@@ -247,7 +239,7 @@ void RenderableRadialGrid::deinitializeGL() {
_gridProgram = nullptr;
}
void RenderableRadialGrid::render(const RenderData& data, RendererTasks&){
void RenderableRadialGrid::render(const RenderData& data, RendererTasks&) {
_gridProgram->activate();
_gridProgram->setUniform("opacity", _opacity);
@@ -299,7 +291,7 @@ void RenderableRadialGrid::render(const RenderData& data, RendererTasks&){
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
for (GeometryData &c : _circles) {
for (GeometryData& c : _circles) {
c.render();
}
@@ -322,69 +314,69 @@ void RenderableRadialGrid::render(const RenderData& data, RendererTasks&){
}
void RenderableRadialGrid::update(const UpdateData&) {
if (_gridIsDirty) {
// Circles
const int nRadialSegments = _gridSegments.value()[0];
const float fnCircles = static_cast<float>(nRadialSegments);
const float deltaRadius = (_maxRadius - _minRadius) / fnCircles;
const bool hasInnerRadius = _minRadius > 0;
const int nCircles = hasInnerRadius ? nRadialSegments : nRadialSegments + 1;
_circles.clear();
_circles.reserve(nCircles);
auto addRing = [&](int nSegments, float radius) {
std::vector<rendering::helper::Vertex> vertices =
rendering::helper::createRing(nSegments, radius);
_circles.push_back(GeometryData(GL_LINE_STRIP));
_circles.back().varray = rendering::helper::convert(vertices);
_circles.back().update();
};
// add an extra inmost circle
if (hasInnerRadius) {
addRing(_circleSegments, _minRadius);
}
for (int i = 0; i < nRadialSegments; ++i) {
float ri = static_cast<float>(i + 1) * deltaRadius;
ri += _minRadius;
addRing(_circleSegments, ri);
}
// Lines
const int nLines = _gridSegments.value()[1];
const int nVertices = 2 * nLines;
const float fsegments = static_cast<float>(nLines);
_lines.varray.clear();
_lines.varray.reserve(nVertices);
if (nLines > 1) {
for (int i = 0; i < nLines; ++i) {
const float fi = static_cast<float>(i);
const float theta = fi * glm::pi<float>() * 2.0f / fsegments; // 0 -> 2*PI
float x = _maxRadius * cos(theta);
float y = _maxRadius * sin(theta);
float z = 0.0f;
_lines.varray.push_back({ x, y, z });
x = _minRadius * cos(theta);
y = _minRadius * sin(theta);
_lines.varray.push_back({ x, y, z });
}
}
_lines.update();
_gridIsDirty = false;
if (!_gridIsDirty) {
return;
}
// Circles
const int nRadialSegments = _gridSegments.value()[0];
const float fnCircles = static_cast<float>(nRadialSegments);
const float deltaRadius = (_maxRadius - _minRadius) / fnCircles;
const bool hasInnerRadius = _minRadius > 0;
const int nCircles = hasInnerRadius ? nRadialSegments : nRadialSegments + 1;
_circles.clear();
_circles.reserve(nCircles);
auto addRing = [this](int nSegments, float radius) {
std::vector<rendering::helper::Vertex> vertices =
rendering::helper::createRing(nSegments, radius);
_circles.push_back(GeometryData(GL_LINE_STRIP));
_circles.back().varray = rendering::helper::convert(vertices);
_circles.back().update();
};
// add an extra inmost circle
if (hasInnerRadius) {
addRing(_circleSegments, _minRadius);
}
for (int i = 0; i < nRadialSegments; ++i) {
const float ri = static_cast<float>(i + 1) * deltaRadius + _minRadius;
addRing(_circleSegments, ri);
}
// Lines
const int nLines = _gridSegments.value()[1];
const int nVertices = 2 * nLines;
const float fsegments = static_cast<float>(nLines);
_lines.varray.clear();
_lines.varray.reserve(nVertices);
if (nLines > 1) {
std::vector<rendering::helper::Vertex> outerVertices =
rendering::helper::createRing(nLines, _maxRadius);
std::vector<rendering::helper::Vertex> innerVertices =
rendering::helper::createRing(nLines, _minRadius);
for (int i = 0; i < nLines; ++i) {
const rendering::helper::VertexXYZ vOut =
rendering::helper::convertToXYZ(outerVertices[i]);
const rendering::helper::VertexXYZ vIn =
rendering::helper::convertToXYZ(innerVertices[i]);
_lines.varray.push_back(vOut);
_lines.varray.push_back(vIn);
}
}
_lines.update();
_gridIsDirty = false;
}
RenderableRadialGrid::GeometryData::GeometryData(GLenum renderMode)
@@ -411,7 +403,8 @@ RenderableRadialGrid::GeometryData::GeometryData(GeometryData&& other) noexcept
}
RenderableRadialGrid::GeometryData&
RenderableRadialGrid::GeometryData::operator=(GeometryData&& other) noexcept {
RenderableRadialGrid::GeometryData::operator=(GeometryData&& other) noexcept
{
if (this != &other) {
vao = other.vao;
vbo = other.vbo;