From 19c845b422c7ea1bf6aff690690d88b7a442684c Mon Sep 17 00:00:00 2001 From: Malin Ejdbo Date: Fri, 7 May 2021 16:53:13 +0200 Subject: [PATCH] Add initial version of RenderablePrism --- .../solarsystem/missions/jwst/jwst.asset | 29 +- modules/base/CMakeLists.txt | 4 + modules/base/basemodule.cpp | 2 + modules/base/rendering/renderableprism.cpp | 291 ++++++++++++++++++ modules/base/rendering/renderableprism.h | 86 ++++++ modules/base/shaders/prism_fs.glsl | 36 +++ modules/base/shaders/prism_vs.glsl | 40 +++ 7 files changed, 486 insertions(+), 2 deletions(-) create mode 100644 modules/base/rendering/renderableprism.cpp create mode 100644 modules/base/rendering/renderableprism.h create mode 100644 modules/base/shaders/prism_fs.glsl create mode 100644 modules/base/shaders/prism_vs.glsl diff --git a/data/assets/scene/solarsystem/missions/jwst/jwst.asset b/data/assets/scene/solarsystem/missions/jwst/jwst.asset index adf33249e5..d46258ca64 100644 --- a/data/assets/scene/solarsystem/missions/jwst/jwst.asset +++ b/data/assets/scene/solarsystem/missions/jwst/jwst.asset @@ -84,8 +84,33 @@ local JWSTTrail = { } } +local JWSTFov = { + Identifier = "JWSTFov", + Parent = JWSTModel.Identifier, + Renderable = { + Type = "RenderablePrism", + StartNode = "JWSTModel", + Segments = 6, + Radius = 3.5, + Lines = 7, + LineWidth = 1.5, + Color = { + 1.0, 1.0, 1.0 + }, + LineLength = 1000000, + Direction = { + 1.0, 0.0, 0.0 + } + }, + GUI = { + Name = "James Webb Space Telecope Field of View", + Path = "/Solar System/Missions/JWST", + } +} + assetHelper.registerSceneGraphNodesAndExport(asset, { JWSTTrail, JWSTPosition, - JWSTModel -}) \ No newline at end of file + JWSTModel, + JWSTFov +}) diff --git a/modules/base/CMakeLists.txt b/modules/base/CMakeLists.txt index 75e8508b89..4a69424b32 100644 --- a/modules/base/CMakeLists.txt +++ b/modules/base/CMakeLists.txt @@ -50,6 +50,7 @@ set(HEADER_FILES rendering/renderableplane.h rendering/renderableplaneimagelocal.h rendering/renderableplaneimageonline.h + rendering/renderableprism.h rendering/renderablesphere.h rendering/renderabletrail.h rendering/renderabletrailorbit.h @@ -101,6 +102,7 @@ set(SOURCE_FILES rendering/renderableplane.cpp rendering/renderableplaneimagelocal.cpp rendering/renderableplaneimageonline.cpp + rendering/renderableprism.cpp rendering/renderablesphere.cpp rendering/renderabletrail.cpp rendering/renderabletrailorbit.cpp @@ -141,6 +143,8 @@ set(SHADER_FILES shaders/model_vs.glsl shaders/plane_fs.glsl shaders/plane_vs.glsl + shaders/prism_fs.glsl + shaders/prism_vs.glsl shaders/renderabletrail_fs.glsl shaders/renderabletrail_vs.glsl shaders/screenspace_fs.glsl diff --git a/modules/base/basemodule.cpp b/modules/base/basemodule.cpp index 883e73cc21..67f2332629 100644 --- a/modules/base/basemodule.cpp +++ b/modules/base/basemodule.cpp @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -131,6 +132,7 @@ void BaseModule::internalInitialize(const ghoul::Dictionary&) { fRenderable->registerClass("RenderableNodeLine"); fRenderable->registerClass("RenderablePlaneImageLocal"); fRenderable->registerClass("RenderablePlaneImageOnline"); + fRenderable->registerClass("RenderablePrism"); fRenderable->registerClass("RenderableRadialGrid"); fRenderable->registerClass("RenderableSphere"); fRenderable->registerClass("RenderableSphericalGrid"); diff --git a/modules/base/rendering/renderableprism.cpp b/modules/base/rendering/renderableprism.cpp new file mode 100644 index 0000000000..88f1cd7808 --- /dev/null +++ b/modules/base/rendering/renderableprism.cpp @@ -0,0 +1,291 @@ +/***************************************************************************************** + * * + * 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. * + ****************************************************************************************/ + +#define _USE_MATH_DEFINES + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + constexpr const char _loggerCat[] = "RenderablePrism"; + + constexpr const std::array UniformNames = { + "modelViewProjectionTransform", "vs_color" + }; + + constexpr openspace::properties::Property::PropertyInfo SegmentsInfo = { + "Segments", + "Segments", + "The number of segments the shape of the prism should have." + }; + + static const openspace::properties::Property::PropertyInfo RadiusInfo = { + "Radius", + "Radius", + "The radius of the prism's shape in meters." + }; + + constexpr openspace::properties::Property::PropertyInfo LineWidthInfo = { + "LineWidth", + "Line Width", + "This value specifies the line width." + }; + + constexpr openspace::properties::Property::PropertyInfo LineColorInfo = { + "Color", + "Color", + "This value determines the RGB color for the line." + }; + + constexpr openspace::properties::Property::PropertyInfo LengthInfo = { + "Length", + "Length", + "The length of the prism in meters." + }; + + // Generate a vertices around the unit circle on the XY-plane + void getUnitCircleVertices(std::vector& vertices, int sectorCount) { + float sectorStep = 2 * M_PI / sectorCount; + float sectorAngle; // in radians + + for (int i = 0; i <= sectorCount; ++i) { + sectorAngle = i * sectorStep; + vertices.push_back(cos(sectorAngle)); // x + vertices.push_back(sin(sectorAngle)); // y + vertices.push_back(0); // z + } + } + + struct [[codegen::Dictionary(RenderablePrism)]] Parameters { + // [[codegen::verbatim(SegmentsInfo.description)]] + int segments; + + // [[codegen::verbatim(RadiusInfo.description)]] + std::optional radius; + + // [[codegen::verbatim(LineWidthInfo.description)]] + std::optional lineWidth; + + // [[codegen::verbatim(LineColorInfo.description)]] + std::optional color [[codegen::color()]]; + + // [[codegen::verbatim(LengthInfo.description)]] + std::optional length; + }; +#include "renderableprism_codegen.cpp" +} // namespace + +namespace openspace { + +documentation::Documentation RenderablePrism::Documentation() { + documentation::Documentation doc = codegen::doc(); + doc.id = "base_renderable_prism"; + return doc; +} + +RenderablePrism::RenderablePrism(const ghoul::Dictionary& dictionary) + : Renderable(dictionary) + , _nShapeSegments(SegmentsInfo, 6, 3, 32) + , _radius(RadiusInfo, 10.f, 0.f, 3.0e12f) + , _lineWidth(LineWidthInfo, 1.f, 1.f, 20.f) + , _lineColor(LineColorInfo, glm::vec3(1.f), glm::vec3(0.f), glm::vec3(1.f)) + , _length(LengthInfo, 20.f, 1.f, 3.0e12f) + +{ + const Parameters p = codegen::bake(dictionary); + + _radius.setViewOption(properties::Property::ViewOptions::Logarithmic); + _lineColor.setViewOption(properties::Property::ViewOptions::Color); + _length.setViewOption(properties::Property::ViewOptions::Logarithmic); + + _nShapeSegments.onChange([&]() { _prismIsDirty = true; }); + _radius.onChange([&]() { _prismIsDirty = true; }); + _length.onChange([&]() { _prismIsDirty = true; }); + + _nShapeSegments = p.segments; + _radius = p.radius.value_or(_radius); + _lineWidth = p.lineWidth.value_or(_lineWidth); + _lineColor = p.color.value_or(_lineColor); + _length = p.length.value_or(_length); + + addProperty(_nShapeSegments); + addProperty(_radius); + addProperty(_lineWidth); + addProperty(_lineColor); + addProperty(_length); + addProperty(_opacity); +} + +bool RenderablePrism::isReady() const { + return _shader != nullptr; +} + +void RenderablePrism::bindGL() { + glBindVertexArray(_vaoId); + glBindBuffer(GL_ARRAY_BUFFER, _vboId); +} + +void RenderablePrism::unbindGL() { + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +void RenderablePrism::initialize() { + updateVertexData(); +} + +void RenderablePrism::initializeGL() { + initializeShader(); + ghoul::opengl::updateUniformLocations(*_shader, _uniformCache, UniformNames); + + glGenVertexArrays(1, &_vaoId); + glGenBuffers(1, &_vboId); + + updateBufferData(); +} + +void RenderablePrism::deinitializeGL() { + global::renderEngine->removeRenderProgram(_shader.get()); + _shader = nullptr; + + glDeleteVertexArrays(1, &_vaoId); + _vaoId = 0; + + glDeleteBuffers(1, &_vboId); + _vboId = 0; +} + +void RenderablePrism::updateVertexData() { + _vertexArray.clear(); + + // Get unit circle vertices on the XY-plane + std::vector unitVertices; + unitVertices.reserve(3 * _nShapeSegments); + getUnitCircleVertices(unitVertices, _nShapeSegments); + + // Put base and top shape vertices into array + for (int i = 0; i < 2; ++i) { + float h = i * _length; // z value, 0 to _length + + // Center point + _vertexArray.push_back(0.f); // x + _vertexArray.push_back(0.f); // y + _vertexArray.push_back(h); // z + + for (int j = 0, k = 0; j < _nShapeSegments; ++j, k += 3) { + float ux = unitVertices[k]; + float uy = unitVertices[k + 1]; + // Skip the z point that is 0.f + + _vertexArray.push_back(ux * _radius); // x + _vertexArray.push_back(uy * _radius); // y + _vertexArray.push_back(h); // z + } + } + + // vertices for the lines connecting the shapes +} + +void RenderablePrism::updateBufferData() { + bindGL(); + + glBufferData( + GL_ARRAY_BUFFER, + _vertexArray.size() * sizeof(float), + _vertexArray.data(), + GL_STREAM_DRAW + ); + + glEnableVertexAttribArray(_locVertex); + glVertexAttribPointer(_locVertex, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr); + + unbindGL(); +} + +void RenderablePrism::render(const RenderData& data, RendererTasks&) { + _shader->activate(); + + // Model transform and view transform needs to be in double precision + glm::dmat4 modelTransform = + glm::translate(glm::dmat4(1.0), data.modelTransform.translation) * + glm::dmat4(data.modelTransform.rotation) * + glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale)); + + glm::mat4 modelViewProjectionTransform = + data.camera.projectionMatrix() * + glm::mat4(data.camera.combinedViewMatrix() * modelTransform); + + // Uniforms + _shader->setUniform(_uniformCache.modelViewProjection, modelViewProjectionTransform); + _shader->setUniform(_uniformCache.color, glm::vec4(_lineColor.value(), _opacity)); + + // Render + glLineWidth(_lineWidth); + bindGL(); + + glDrawArrays(GL_LINE_LOOP, 0, static_cast(_vertexArray.size())); + + unbindGL(); + global::renderEngine->openglStateCache().resetLineState(); + + _shader->deactivate(); +} + +void RenderablePrism::update(const UpdateData&) { + if (_shader->isDirty()) { + _shader->rebuildFromFile(); + updateUniformLocations(); + } + if (_prismIsDirty) { + updateVertexData(); + updateBufferData(); + _prismIsDirty = false; + } +} + +void RenderablePrism::initializeShader() { + _shader = global::renderEngine->buildRenderProgram( + "PrismProgram", + absPath("${MODULE_BASE}/shaders/prism_vs.glsl"), + absPath("${MODULE_BASE}/shaders/prism_fs.glsl") + ); + updateUniformLocations(); +} + +void RenderablePrism::updateUniformLocations() { + ghoul::opengl::updateUniformLocations(*_shader, _uniformCache, UniformNames); +} + +} // namespace openspace diff --git a/modules/base/rendering/renderableprism.h b/modules/base/rendering/renderableprism.h new file mode 100644 index 0000000000..ae22ad1da0 --- /dev/null +++ b/modules/base/rendering/renderableprism.h @@ -0,0 +1,86 @@ +/***************************************************************************************** + * * + * 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_MODULE_BASE___RENDERABLEPRISM___H__ +#define __OPENSPACE_MODULE_BASE___RENDERABLEPRISM___H__ + +#include +#include +#include +#include +#include +#include +#include + +namespace ghoul::opengl { class ProgramObject; } + +namespace openspace { + +namespace documentation { struct Documentation; } + +class RenderablePrism : public Renderable { +public: + RenderablePrism(const ghoul::Dictionary& dictionary); + + void initialize() override; + void initializeGL() override; + void deinitializeGL() override; + + bool isReady() const override; + + void render(const RenderData& data, RendererTasks& rendererTask) override; + void update(const UpdateData& data) override; + + static documentation::Documentation Documentation(); + +private: + void initializeShader(); + void updateVertexData(); + void updateBufferData(); + void updateUniformLocations(); + + void bindGL(); + void unbindGL(); + + // Properties + properties::IntProperty _nShapeSegments; + properties::FloatProperty _radius; + properties::FloatProperty _lineWidth; + properties::Vec3Property _lineColor; + properties::FloatProperty _length; + UniformCache(modelViewProjection, color) _uniformCache; + + std::unique_ptr _shader; + const GLuint _locVertex = 0; + GLuint _vaoId = 0; + GLuint _vboId = 0; + std::vector _vertexArray; + glm::dvec3 _startPos = glm::dvec3(0.0); + + bool _prismIsDirty = false; +}; + +} // namespace openspace + +#endif // __OPENSPACE_MODULE_BASE___RENDERABLEPRISM___H__ diff --git a/modules/base/shaders/prism_fs.glsl b/modules/base/shaders/prism_fs.glsl new file mode 100644 index 0000000000..689862a5aa --- /dev/null +++ b/modules/base/shaders/prism_fs.glsl @@ -0,0 +1,36 @@ +/***************************************************************************************** + * * + * 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 "fragment.glsl" + +in vec4 vs_positionScreenSpace; + +uniform vec4 vs_color; + +Fragment getFragment() { + Fragment frag; + frag.color = vs_color; + frag.depth = vs_positionScreenSpace.w; + return frag; +} diff --git a/modules/base/shaders/prism_vs.glsl b/modules/base/shaders/prism_vs.glsl new file mode 100644 index 0000000000..99dce430e1 --- /dev/null +++ b/modules/base/shaders/prism_vs.glsl @@ -0,0 +1,40 @@ +/***************************************************************************************** + * * + * 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. * + ****************************************************************************************/ + +#version __CONTEXT__ + +#include "PowerScaling/powerScaling_vs.hglsl" + +layout (location = 0) in vec3 in_position; + +out vec4 vs_positionScreenSpace; + +uniform mat4 modelViewProjectionTransform; + +void main() { + vec4 position = vec4(in_position, 1); + vec4 positionClipSpace = modelViewProjectionTransform * position; + vs_positionScreenSpace = z_normalization(positionClipSpace); + gl_Position = vs_positionScreenSpace; +}