From 3fbefa5324962c3bf7166d1d364da87c8fd2591d Mon Sep 17 00:00:00 2001 From: Jonathas Costa Date: Wed, 4 Nov 2020 07:11:25 -0500 Subject: [PATCH] Feature/orion changes (#1359) * Changed RenderableModel to allow user-control depth and blending. * Updated RenderableModel to correctly handle the Orion Nebula model. Co-authored-by: Alexander Bock --- data/assets/base.asset | 1 + .../objects/orionnebula/cluster.asset | 2 +- .../milkyway/objects/orionnebula/nebula.asset | 13 +- modules/base/rendering/renderablemodel.cpp | 124 +++++++++++++++++- modules/base/rendering/renderablemodel.h | 9 +- modules/base/shaders/model_fs.glsl | 19 ++- 6 files changed, 152 insertions(+), 16 deletions(-) diff --git a/data/assets/base.asset b/data/assets/base.asset index ee94a61231..d857183682 100644 --- a/data/assets/base.asset +++ b/data/assets/base.asset @@ -18,6 +18,7 @@ asset.require('scene/solarsystem/dwarf_planets/pluto/charon/default_layers') asset.require('scene/milkyway/milkyway/volume') asset.require('scene/milkyway/constellations/constellation_art') asset.require('scene/milkyway/constellations/constellation_keybinds') +asset.require('scene/milkyway/objects/orionnebula/orionnebula') asset.require('util/launcher_images') local assetHelper = asset.require('util/asset_helper') diff --git a/data/assets/scene/milkyway/objects/orionnebula/cluster.asset b/data/assets/scene/milkyway/objects/orionnebula/cluster.asset index 910aca0673..8a49c4bc33 100644 --- a/data/assets/scene/milkyway/objects/orionnebula/cluster.asset +++ b/data/assets/scene/milkyway/objects/orionnebula/cluster.asset @@ -16,7 +16,7 @@ local OrionClusterStars = { Type = "RenderableStars", File = sync .. "/oricluster.speck", Texture = sync .. "/halo.png", - Texture = sync .. "/colorbv.cmap", + ColorMap = sync .. "/colorbv.cmap", MagnitudeExponent = 5.02, SizeComposition = "Distance Modulus", RenderMethod = "Texture Based" diff --git a/data/assets/scene/milkyway/objects/orionnebula/nebula.asset b/data/assets/scene/milkyway/objects/orionnebula/nebula.asset index dc99a09a4f..46a2303a5f 100644 --- a/data/assets/scene/milkyway/objects/orionnebula/nebula.asset +++ b/data/assets/scene/milkyway/objects/orionnebula/nebula.asset @@ -54,8 +54,9 @@ local OrionNebulaModel = { Opacity = 1.0, DisableFaceCulling = false, SpecularIntensity = 0.0, - AmbientIntensity = 0.45, - DiffuseIntensity = 0.0, + AmbientIntensity = 0.0, + DiffuseIntensity = 1.0, + --PerformShading = false, RotationVector = { 0.000000, 22.300000, 0.000000 }, LightSources = LIGHTS; }, @@ -85,8 +86,9 @@ local OrionNebulaShocksModel = { Opacity = 1.0, DisableFaceCulling = false, SpecularIntensity = 0.0, - AmbientIntensity = 0.19, - DiffuseIntensity = 0.4, + AmbientIntensity = 0.0, + DiffuseIntensity = 1.0, + --PerformShading = false, RotationVector = { 0.000000, 22.300000, 0.000000 }, LightSources = LIGHTS; }, @@ -116,8 +118,9 @@ local OrionNebulaProplydsModel = { Opacity = 1.0, DisableFaceCulling = false, SpecularIntensity = 0.0, - AmbientIntensity = 1.0, + AmbientIntensity = 0.0, DiffuseIntensity = 1.0, + --PerformShading = false, RotationVector = { 0.000000, 22.300000, 0.000000 }, LightSources = LIGHTS; }, diff --git a/modules/base/rendering/renderablemodel.cpp b/modules/base/rendering/renderablemodel.cpp index ab58a5a148..39c920ded5 100644 --- a/modules/base/rendering/renderablemodel.cpp +++ b/modules/base/rendering/renderablemodel.cpp @@ -34,11 +34,11 @@ #include #include #include - #include #include #include #include +#include #include #include @@ -46,11 +46,25 @@ namespace { constexpr const char* ProgramName = "ModelProgram"; constexpr const char* KeyGeometry = "Geometry"; - constexpr const std::array UniformNames = { + constexpr const int DefaultBlending = 0; + constexpr const int AdditiveBlending = 1; + constexpr const int PointsAndLinesBlending = 2; + constexpr const int PolygonBlending = 3; + constexpr const int ColorAddingBlending = 4; + + std::map BlendingMapping = { + { "Default", DefaultBlending }, + { "Additive", AdditiveBlending }, + { "Points and Lines", PointsAndLinesBlending }, + { "Polygon", PolygonBlending }, + { "Color Adding", ColorAddingBlending } + }; + + constexpr const std::array UniformNames = { "opacity", "nLightSources", "lightDirectionsViewSpace", "lightIntensities", "modelViewTransform", "normalTransform", "projectionTransform", "performShading", "texture1", "ambientIntensity", "diffuseIntensity", - "specularIntensity" + "specularIntensity", "opacityBlending" }; constexpr openspace::properties::Property::PropertyInfo AmbientIntensityInfo = { @@ -102,6 +116,24 @@ namespace { "Light Sources", "A list of light sources that this model should accept light from." }; + + constexpr openspace::properties::Property::PropertyInfo DisableDepthTestInfo = { + "DisableDepthTest", + "Disable Depth Test", + "Disable Depth Testing for the Model." + }; + + constexpr openspace::properties::Property::PropertyInfo BlendingOptionInfo = { + "BledingOption", + "Blending Options", + "Debug option for blending colors." + }; + + constexpr openspace::properties::Property::PropertyInfo EnableOpacityBlendingInfo = { + "EnableOpacityBlending", + "Enable Opacity Blending", + "Enable Opacity Blending." + }; } // namespace namespace openspace { @@ -177,7 +209,25 @@ documentation::Documentation RenderableModel::Documentation() { }), Optional::Yes, LightSourcesInfo.description - } + }, + { + DisableDepthTestInfo.identifier, + new BoolVerifier, + Optional::Yes, + DisableDepthTestInfo.description + }, + { + BlendingOptionInfo.identifier, + new StringVerifier, + Optional::Yes, + BlendingOptionInfo.description + }, + { + EnableOpacityBlendingInfo.identifier, + new BoolVerifier, + Optional::Yes, + EnableOpacityBlendingInfo.description + }, } }; } @@ -196,6 +246,12 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary) glm::dmat3(1.0) ) , _rotationVec(RotationVecInfo, glm::dvec3(0.0), glm::dvec3(0.0), glm::dvec3(360.0)) + , _enableOpacityBlending(EnableOpacityBlendingInfo, false) + , _disableDepthTest(DisableDepthTestInfo, false) + , _blendingFuncOption( + BlendingOptionInfo, + properties::OptionProperty::DisplayType::Dropdown + ) , _lightSourcePropertyOwner({ "LightSources", "Light Sources" }) { documentation::testSpecificationAndThrow( @@ -235,6 +291,10 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary) _performShading = dictionary.value(ShadingInfo.identifier); } + if (dictionary.hasKey(DisableDepthTestInfo.identifier)) { + _disableDepthTest = dictionary.value(DisableDepthTestInfo.identifier); + } + if (dictionary.hasKey(DisableFaceCullingInfo.identifier)) { _disableFaceCulling = dictionary.value(DisableFaceCullingInfo.identifier); } @@ -252,14 +312,13 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary) } } - addPropertySubOwner(_lightSourcePropertyOwner); - addProperty(_ambientIntensity); addProperty(_diffuseIntensity); addProperty(_specularIntensity); addProperty(_performShading); addProperty(_disableFaceCulling); + addProperty(_disableDepthTest); addProperty(_modelTransform); addProperty(_rotationVec); @@ -271,6 +330,29 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary) if (dictionary.hasKey(RotationVecInfo.identifier)) { _rotationVec = dictionary.value(RotationVecInfo.identifier); } + + _blendingFuncOption.addOption(DefaultBlending, "Default"); + _blendingFuncOption.addOption(AdditiveBlending, "Additive"); + _blendingFuncOption.addOption(PointsAndLinesBlending, "Points and Lines"); + _blendingFuncOption.addOption(PolygonBlending, "Polygon"); + _blendingFuncOption.addOption(ColorAddingBlending, "Color Adding"); + + addProperty(_blendingFuncOption); + + if (dictionary.hasKey(BlendingOptionInfo.identifier)) { + const std::string blendingOpt = dictionary.value( + BlendingOptionInfo.identifier + ); + _blendingFuncOption.set(BlendingMapping[blendingOpt]); + } + + if (dictionary.hasKey(DisableDepthTestInfo.identifier)) { + _enableOpacityBlending = dictionary.value( + EnableOpacityBlendingInfo.identifier + ); + } + + addProperty(_enableOpacityBlending); } bool RenderableModel::isReady() const { @@ -384,11 +466,35 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) { _program->setUniform(_uniformCache.diffuseIntensity, _diffuseIntensity); _program->setUniform(_uniformCache.specularIntensity, _specularIntensity); _program->setUniform(_uniformCache.performShading, _performShading); + _program->setUniform(_uniformCache.opacityBlending, _enableOpacityBlending); if (_disableFaceCulling) { glDisable(GL_CULL_FACE); } + glEnablei(GL_BLEND, 0); + switch (_blendingFuncOption) { + case DefaultBlending: + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + break; + case AdditiveBlending: + glBlendFunc(GL_ONE, GL_ONE); + break; + case PointsAndLinesBlending: + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + break; + case PolygonBlending: + glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE); + break; + case ColorAddingBlending: + glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR); + break; + }; + + if (_disableDepthTest) { + glDisable(GL_DEPTH_TEST); + } + ghoul::opengl::TextureUnit unit; unit.activate(); _program->setUniform(_uniformCache.texture, unit); @@ -401,6 +507,12 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) { glEnable(GL_CULL_FACE); } + global::renderEngine->openglStateCache().resetBlendState(); + + if (_disableDepthTest) { + glEnable(GL_DEPTH_TEST); + } + _program->deactivate(); } diff --git a/modules/base/rendering/renderablemodel.h b/modules/base/rendering/renderablemodel.h index ff382e5ecc..23beb352cc 100644 --- a/modules/base/rendering/renderablemodel.h +++ b/modules/base/rendering/renderablemodel.h @@ -26,7 +26,8 @@ #define __OPENSPACE_MODULE_BASE___RENDERABLEMODEL___H__ #include - +#include +#include #include #include #include @@ -79,11 +80,15 @@ private: properties::DMat4Property _modelTransform; properties::Vec3Property _rotationVec; + properties::BoolProperty _disableDepthTest; + properties::BoolProperty _enableOpacityBlending; + properties::OptionProperty _blendingFuncOption; + ghoul::opengl::ProgramObject* _program = nullptr; UniformCache(opacity, nLightSources, lightDirectionsViewSpace, lightIntensities, modelViewTransform, normalTransform, projectionTransform, performShading, texture, ambientIntensity, diffuseIntensity, - specularIntensity) _uniformCache; + specularIntensity, opacityBlending) _uniformCache; std::vector> _lightSources; diff --git a/modules/base/shaders/model_fs.glsl b/modules/base/shaders/model_fs.glsl index 8ea1340660..a26843ca2c 100644 --- a/modules/base/shaders/model_fs.glsl +++ b/modules/base/shaders/model_fs.glsl @@ -34,6 +34,7 @@ uniform float diffuseIntensity = 1.0; uniform float specularIntensity = 1.0; uniform bool performShading = true; +uniform bool opacityBlending = false; uniform sampler2D texture1; @@ -46,6 +47,10 @@ uniform float opacity = 1.0; const vec3 SpecularAlbedo = vec3(1.0); Fragment getFragment() { + if (opacity == 0.0) { + discard; + } + vec3 diffuseAlbedo = texture(texture1, vs_st).rgb; Fragment frag; @@ -84,12 +89,22 @@ Fragment getFragment() { frag.color.rgb = diffuseAlbedo; } - frag.color.a = opacity; + if (opacityBlending) { + // frag.color.a = opacity * (frag.color.r + frag.color.g + frag.color.b)/3.0; + frag.color.a = opacity * max(max(frag.color.r, frag.color.g), frag.color.b); + } + else { + frag.color.a = opacity; + } + + if (frag.color.a < 0.1) { + discard; + } + frag.depth = vs_screenSpaceDepth; frag.gPosition = vs_positionCameraSpace; frag.gNormal = vec4(vs_normalViewSpace, 0.0); frag.disableLDR2HDR = true; - return frag; } \ No newline at end of file