From 96e27913ca4f9107231f51ef5943fc0993b09e63 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Sat, 29 Oct 2016 15:24:39 +0200 Subject: [PATCH] Add documentation to RenderableRings Change Size to be specified in meters instead of powerscaled scalars --- data/scene/saturn/saturn/saturn.mod | 3 +- modules/base/basemodule.cpp | 1 + modules/base/rendering/renderablerings.cpp | 153 ++++++++++-------- modules/base/rendering/renderablerings.h | 11 +- modules/base/shaders/nighttexture_fs.glsl | 2 - modules/base/shaders/renderableplanet_fs.glsl | 2 - modules/base/shaders/renderableplanet_vs.glsl | 2 +- modules/base/shaders/rings_fs.glsl | 5 +- modules/base/shaders/rings_vs.glsl | 24 ++- 9 files changed, 112 insertions(+), 91 deletions(-) diff --git a/data/scene/saturn/saturn/saturn.mod b/data/scene/saturn/saturn/saturn.mod index 7b63c66cee..07884ab016 100644 --- a/data/scene/saturn/saturn/saturn.mod +++ b/data/scene/saturn/saturn/saturn.mod @@ -53,9 +53,8 @@ return { Parent = "Saturn", Renderable = { Type = "RenderableRings", - Frame = "IAU_SATURN", Texture = "textures/saturn_rings.png", - Size = { 0.140220, 9.0 }, + Size = 140220000, Offset = { 74500 / 140445.100671159, 1.0 } -- min / max extend }, diff --git a/modules/base/basemodule.cpp b/modules/base/basemodule.cpp index bc9ddc22e9..771253709e 100644 --- a/modules/base/basemodule.cpp +++ b/modules/base/basemodule.cpp @@ -140,6 +140,7 @@ std::vector BaseModule::documentations() const { StaticScale::Documentation(), StaticTranslation::Documentation(), SpiceTranslation::Documentation(), + RenderableRings::Documentation(), modelgeometry::ModelGeometry::Documentation(), planetgeometry::PlanetGeometry::Documentation() }; diff --git a/modules/base/rendering/renderablerings.cpp b/modules/base/rendering/renderablerings.cpp index 8f2927af6f..0ee5676514 100644 --- a/modules/base/rendering/renderablerings.cpp +++ b/modules/base/rendering/renderablerings.cpp @@ -24,6 +24,8 @@ #include +#include + #include #include #include @@ -39,10 +41,8 @@ namespace { const std::string _loggerCat = "RenderableRings"; - const std::string KeySize = "Size"; const std::string KeyTexture = "Texture"; - const std::string KeyFrame = "Frame"; - const std::string KeyOrientation = "Orientation"; + const std::string KeySize = "Size"; const std::string KeyOffset = "Offset"; const std::string KeyNightFactor = "NightFactor"; const std::string KeyTransparency = "Transparency"; @@ -50,10 +50,48 @@ namespace { namespace openspace { +Documentation RenderableRings::Documentation() { + using namespace documentation; + return { + "Renderable Rings", + "base_renderable_rings", + { + { + "Type", + new StringEqualVerifier("RenderableRings"), + "", + Optional::No + }, + { + KeyTexture, + new StringVerifier, + "The one dimensional texture that is used for both sides of the ring.", + Optional::No + }, + { + KeySize, + new DoubleVerifier, + "The radius of the rings in meters.", + Optional::No + }, + { + KeyOffset, + new DoubleVector2Verifier, + "The offset that is used to limit the width of the rings. Each of the " + "two values is a value between 0 and 1, where 0 is the center of the " + "ring and 1 is the maximum extent at the radius. If this value is, for " + "example {0.5, 1.0}, the ring is only shown between radius/2 and radius. " + "It defaults to {0.0, 1.0}.", + Optional::Yes + } + } + }; +} + RenderableRings::RenderableRings(const ghoul::Dictionary& dictionary) : Renderable(dictionary) , _texturePath("texture", "Texture") - , _size("size", "Size", glm::vec2(1.f, 1.f), glm::vec2(0.f), glm::vec2(1.f, 25.f)) + , _size("size", "Size", 1.f, 0.f, std::pow(1.f, 25.f)) , _offset("offset", "Texture Offset", glm::vec2(0.f, 1.f), glm::vec2(0.f), glm::vec2(1.f)) , _nightFactor("nightFactor", "Night Factor", 0.33f, 0.f, 1.f) , _transparency("transparency", "Transparency", 0.15f, 0.f, 1.f) @@ -65,38 +103,26 @@ RenderableRings::RenderableRings(const ghoul::Dictionary& dictionary) , _vertexPositionBuffer(0) , _planeIsDirty(false) { - glm::vec2 size; - dictionary.getValue(KeySize, size); - _size = size; - - if (dictionary.hasKeyAndValue(KeyFrame)) { - _frame = dictionary.value(KeyFrame); - } - - if (dictionary.hasKeyAndValue(KeyOrientation)) { - _orientation = dictionary.value(KeyOrientation); - } + using ghoul::filesystem::File; + + documentation::testSpecificationAndThrow( + Documentation(), + dictionary, + "RenderableRings" + ); + + double size = dictionary.value(KeySize); + _size = size; + setBoundingSphere(PowerScaledScalar::CreatePSS(size)); + + _texturePath = absPath(dictionary.value(KeyTexture)); + _textureFile = std::make_unique(_texturePath); - if (dictionary.hasKeyAndValue(KeyTexture)) { - _texturePath = absPath(dictionary.value(KeyTexture)); - _textureFile = std::make_unique(_texturePath); - } - if (dictionary.hasKeyAndValue(KeyOffset)) { glm::vec2 off = dictionary.value(KeyOffset); _offset = off; } - if (dictionary.hasKeyAndValue(KeyNightFactor)) { - float v = dictionary.value(KeyNightFactor); - _nightFactor = v; - } - - if (dictionary.hasKeyAndValue(KeyTransparency)) { - float v = dictionary.value(KeyTransparency); - _transparency = v; - } - addProperty(_offset); addProperty(_size); @@ -105,15 +131,10 @@ RenderableRings::RenderableRings(const ghoul::Dictionary& dictionary) addProperty(_texturePath); _texturePath.onChange([&](){ loadTexture(); }); - _textureFile->setCallback( - [&](const ghoul::filesystem::File&) { _textureIsDirty = true; } - ); + _textureFile->setCallback([&](const File&) { _textureIsDirty = true; }); addProperty(_nightFactor); - addProperty(_transparency); - - setBoundingSphere(_size.value()); } bool RenderableRings::isReady() const { @@ -155,10 +176,8 @@ bool RenderableRings::deinitialize() { _texture = nullptr; RenderEngine& renderEngine = OsEng.renderEngine(); - if (_shader) { - renderEngine.removeRenderProgram(_shader); - _shader = nullptr; - } + renderEngine.removeRenderProgram(_shader); + _shader = nullptr; return true; } @@ -166,15 +185,27 @@ bool RenderableRings::deinitialize() { void RenderableRings::render(const RenderData& data) { _shader->activate(); - _shader->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); - _shader->setUniform("ModelTransform", glm::mat4(_orientation * _state)); + glm::dmat4 modelTransform = + glm::translate(glm::dmat4(1.0), data.modelTransform.translation) * + glm::dmat4(data.modelTransform.rotation) * + glm::dmat4(glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale))); + + glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() * modelTransform; + + _shader->setUniform( + "modelViewProjectionTransform", + data.camera.projectionMatrix() * glm::mat4(modelViewTransform) + ); _shader->setUniform("textureOffset", _offset); _shader->setUniform("transparency", _transparency); _shader->setUniform("_nightFactor", _nightFactor); - _shader->setUniform("sunPosition", _sunPosition); + _shader->setUniform( + "sunPosition", + _sunPosition + ); - setPscUniforms(*_shader.get(), data.camera, data.position); + setPscUniforms(*_shader, data.camera, data.position); ghoul::opengl::TextureUnit unit; unit.activate(); @@ -204,29 +235,23 @@ void RenderableRings::update(const UpdateData& data) { loadTexture(); _textureIsDirty = false; } - - if (!_frame.empty()) { - _state = SpiceManager::ref().positionTransformMatrix(_frame, "GALACTIC", data.time); - } - - _sunPosition = - OsEng.renderEngine().scene()->sceneGraphNode("Sun")->worldPosition() - + + _sunPosition = OsEng.renderEngine().scene()->sceneGraphNode("Sun")->worldPosition() - data.modelTransform.translation; } void RenderableRings::loadTexture() { if (_texturePath.value() != "") { - std::unique_ptr texture = ghoul::io::TextureReader::ref().loadTexture(absPath(_texturePath)); + std::unique_ptr texture = + ghoul::io::TextureReader::ref().loadTexture(absPath(_texturePath)); + if (texture) { LDEBUG("Loaded texture from '" << absPath(_texturePath) << "'"); _texture = std::move(texture); - // Textures of planets looks much smoother with AnisotropicMipMap rather than linear -// _texture->setFilter(ghoul::opengl::Texture::FilterMode::Linear); _texture->uploadTexture(); _texture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap); - _textureFile = std::make_unique(_texturePath); _textureFile->setCallback( [&](const ghoul::filesystem::File&) { _textureIsDirty = true; } @@ -236,19 +261,15 @@ void RenderableRings::loadTexture() { } void RenderableRings::createPlane() { - // ============================ - // GEOMETRY (quad) - // ============================ - const GLfloat size = _size.value()[0]; - const GLfloat w = _size.value()[1]; + const GLfloat size = _size.value(); const GLfloat vertex_data[] = { // x y z w s t - -size, -size, 0.f, w, 0.f, 0.f, - size, size, 0.f, w, 1.f, 1.f, - -size, size, 0.f, w, 0.f, 1.f, - -size, -size, 0.f, w, 0.f, 0.f, - size, -size, 0.f, w, 1.f, 0.f, - size, size, 0.f, w, 1.f, 1.f, + -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 diff --git a/modules/base/rendering/renderablerings.h b/modules/base/rendering/renderablerings.h index ec063b1f22..75aa28a8c8 100644 --- a/modules/base/rendering/renderablerings.h +++ b/modules/base/rendering/renderablerings.h @@ -27,6 +27,7 @@ #include +#include #include #include #include @@ -54,12 +55,14 @@ public: void render(const RenderData& data) override; void update(const UpdateData& data) override; + static openspace::Documentation Documentation(); + private: void loadTexture(); void createPlane(); properties::StringProperty _texturePath; - properties::Vec2Property _size; + properties::FloatProperty _size; properties::Vec2Property _offset; properties::FloatProperty _nightFactor; properties::FloatProperty _transparency; @@ -72,11 +75,7 @@ private: GLuint _quad; GLuint _vertexPositionBuffer; bool _planeIsDirty; - - std::string _frame; - glm::mat3 _orientation; - glm::mat3 _state; - + glm::vec3 _sunPosition; }; diff --git a/modules/base/shaders/nighttexture_fs.glsl b/modules/base/shaders/nighttexture_fs.glsl index 2096028f8f..94175a9f7b 100644 --- a/modules/base/shaders/nighttexture_fs.glsl +++ b/modules/base/shaders/nighttexture_fs.glsl @@ -45,8 +45,6 @@ in vec4 test; #include "fragment.glsl" Fragment getFragment() { - vec4 position = vs_position; - float depth = pscDepth(position); vec4 diffuse = texture(texture1, vs_st); vec4 diffuse2 = texture(nightTex, vs_st); diff --git a/modules/base/shaders/renderableplanet_fs.glsl b/modules/base/shaders/renderableplanet_fs.glsl index 4304bb6b89..f275091d76 100644 --- a/modules/base/shaders/renderableplanet_fs.glsl +++ b/modules/base/shaders/renderableplanet_fs.glsl @@ -44,8 +44,6 @@ in vec4 vs_position; //#include "PowerScaling/powerScaling_vs.hglsl" Fragment getFragment() { - vec4 position = vs_position; - float depth = pscDepth(position); vec4 diffuse = texture(texture1, vs_st); Fragment frag; diff --git a/modules/base/shaders/renderableplanet_vs.glsl b/modules/base/shaders/renderableplanet_vs.glsl index 70873666b7..e726d0149a 100644 --- a/modules/base/shaders/renderableplanet_vs.glsl +++ b/modules/base/shaders/renderableplanet_vs.glsl @@ -48,7 +48,7 @@ void main() { vs_normal = normalize(ModelTransform * vec4(in_normal,0)); // vs_normal = vec4(in_normal, 0.0); - vec4 position = vec4(tmp.xyz * pow(10, tmp. w), 1.0); + vec4 position = vec4(tmp.xyz * pow(10, tmp.w), 1.0); position = modelViewProjectionTransform * position; vs_position = z_normalization(position); diff --git a/modules/base/shaders/rings_fs.glsl b/modules/base/shaders/rings_fs.glsl index 71ae638cf4..6de4b8503f 100644 --- a/modules/base/shaders/rings_fs.glsl +++ b/modules/base/shaders/rings_fs.glsl @@ -37,9 +37,6 @@ uniform vec3 sunPosition; uniform float _nightFactor; Fragment getFragment() { - vec4 position = vs_position; - float depth = pscDepth(position); - // Moving the origin to the center vec2 st = (vs_st - vec2(0.5)) * 2.0; @@ -85,7 +82,7 @@ Fragment getFragment() { Fragment frag; frag.color = diffuse; - frag.depth = depth; + frag.depth = vs_position.w; return frag; } diff --git a/modules/base/shaders/rings_vs.glsl b/modules/base/shaders/rings_vs.glsl index b1299f30ce..5eaddcd6ee 100644 --- a/modules/base/shaders/rings_vs.glsl +++ b/modules/base/shaders/rings_vs.glsl @@ -32,16 +32,24 @@ layout(location = 1) in vec2 in_st; out vec2 vs_st; out vec4 vs_position; -uniform mat4 ViewProjection; -uniform mat4 ModelTransform; +uniform mat4 modelViewProjectionTransform; void main() { - vec4 tmp = in_position; - vec4 position = pscTransform(tmp, ModelTransform); - - vs_position = tmp; vs_st = in_st; + + vs_position = z_normalization( + modelViewProjectionTransform * vec4(in_position.xyz * pow(10, in_position.w), 1.0) + ); + gl_Position = vs_position; + + // vec4 tmp = in_position; + + + // vec4 tmp = in_position; + // vec4 position = pscTransform(tmp, ModelTransform); + + // vs_position = tmp; - position = ViewProjection * position; - gl_Position = z_normalization(position); + // position = ViewProjection * position; + // gl_Position = z_normalization(position); }