Merge pull request #2550 from OpenSpace/feature/model-opacity

Feature/model opacity
This commit is contained in:
Malin E
2023-04-13 17:26:22 +02:00
committed by GitHub
45 changed files with 1064 additions and 844 deletions
+381 -133
View File
@@ -28,7 +28,10 @@
#include <openspace/documentation/documentation.h>
#include <openspace/documentation/verifier.h>
#include <openspace/engine/globals.h>
#include <openspace/engine/windowdelegate.h>
#include <openspace/rendering/framebufferrenderer.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/util/distanceconversion.h>
#include <openspace/util/time.h>
#include <openspace/util/timeconversion.h>
#include <openspace/util/updatestructures.h>
@@ -39,8 +42,10 @@
#include <ghoul/logging/logmanager.h>
#include <ghoul/misc/invariants.h>
#include <ghoul/misc/profiling.h>
#include <ghoul/opengl/framebufferobject.h>
#include <ghoul/opengl/openglstatecache.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/textureunit.h>
#include <filesystem>
#include <optional>
@@ -62,11 +67,22 @@ namespace {
{ "Color Adding", ColorAddingBlending }
};
constexpr std::array<const char*, 12> UniformNames = {
"opacity", "nLightSources", "lightDirectionsViewSpace", "lightIntensities",
const GLenum ColorAttachmentArray[3] = {
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
};
constexpr std::array<const char*, 14> UniformNames = {
"nLightSources", "lightDirectionsViewSpace", "lightIntensities",
"modelViewTransform", "normalTransform", "projectionTransform",
"performShading", "ambientIntensity", "diffuseIntensity",
"specularIntensity", "opacityBlending"
"specularIntensity", "performManualDepthTest", "gBufferDepthTexture",
"resolution", "opacity"
};
constexpr std::array<const char*, 5> UniformOpacityNames = {
"opacity", "colorTexture", "depthTexture", "viewport", "resolution"
};
constexpr openspace::properties::Property::PropertyInfo EnableAnimationInfo = {
@@ -100,10 +116,10 @@ namespace {
"of the Sun"
};
constexpr openspace::properties::Property::PropertyInfo DisableFaceCullingInfo = {
"DisableFaceCulling",
"Disable Face Culling",
"Disable OpenGL automatic face culling optimization"
constexpr openspace::properties::Property::PropertyInfo EnableFaceCullingInfo = {
"EnableFaceCulling",
"Enable Face Culling",
"Enable OpenGL automatic face culling optimization"
};
constexpr openspace::properties::Property::PropertyInfo ModelTransformInfo = {
@@ -125,10 +141,10 @@ namespace {
"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 EnableDepthTestInfo = {
"EnableDepthTest",
"Enable Depth Test",
"Enable Depth Testing for the Model"
};
constexpr openspace::properties::Property::PropertyInfo BlendingOptionInfo = {
@@ -138,12 +154,6 @@ namespace {
"respect to the opacity"
};
constexpr openspace::properties::Property::PropertyInfo EnableOpacityBlendingInfo = {
"EnableOpacityBlending",
"Enable Opacity Blending",
"Enable Opacity Blending"
};
struct [[codegen::Dictionary(RenderableModel)]] Parameters {
// The file or files that should be loaded in this RenderableModel. The file can
// contain filesystem tokens. This specifies the model that is rendered by
@@ -158,8 +168,6 @@ namespace {
Decimeter,
Meter,
Kilometer,
// Weird units
Thou,
Inch,
Foot,
@@ -228,8 +236,8 @@ namespace {
// [[codegen::verbatim(ShadingInfo.description)]]
std::optional<bool> performShading;
// [[codegen::verbatim(DisableFaceCullingInfo.description)]]
std::optional<bool> disableFaceCulling;
// [[codegen::verbatim(EnableFaceCullingInfo.description)]]
std::optional<bool> enableFaceCulling;
// [[codegen::verbatim(ModelTransformInfo.description)]]
std::optional<glm::dmat3x3> modelTransform;
@@ -241,15 +249,12 @@ namespace {
std::optional<std::vector<ghoul::Dictionary>> lightSources
[[codegen::reference("core_light_source")]];
// [[codegen::verbatim(DisableDepthTestInfo.description)]]
std::optional<bool> disableDepthTest;
// [[codegen::verbatim(EnableDepthTestInfo.description)]]
std::optional<bool> enableDepthTest;
// [[codegen::verbatim(BlendingOptionInfo.description)]]
std::optional<std::string> blendingOption;
// [[codegen::verbatim(EnableOpacityBlendingInfo.description)]]
std::optional<bool> enableOpacityBlending;
// The path to the vertex shader program that is used instead of the default
// shader.
std::optional<std::filesystem::path> vertexShader;
@@ -268,13 +273,13 @@ documentation::Documentation RenderableModel::Documentation() {
}
RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
: Renderable(dictionary, { .automaticallyUpdateRenderBin = false })
, _enableAnimation(EnableAnimationInfo, false)
, _ambientIntensity(AmbientIntensityInfo, 0.2f, 0.f, 1.f)
, _diffuseIntensity(DiffuseIntensityInfo, 1.f, 0.f, 1.f)
, _specularIntensity(SpecularIntensityInfo, 1.f, 0.f, 1.f)
, _performShading(ShadingInfo, true)
, _disableFaceCulling(DisableFaceCullingInfo, false)
, _enableFaceCulling(EnableFaceCullingInfo, true)
, _modelTransform(
ModelTransformInfo,
glm::dmat3(1.0),
@@ -282,8 +287,7 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
glm::dmat3(1.0)
)
, _rotationVec(RotationVecInfo, glm::dvec3(0.0), glm::dvec3(0.0), glm::dvec3(360.0))
, _disableDepthTest(DisableDepthTestInfo, false)
, _enableOpacityBlending(EnableOpacityBlendingInfo, false)
, _enableDepthTest(EnableDepthTestInfo, true)
, _blendingFuncOption(
BlendingOptionInfo,
properties::OptionProperty::DisplayType::Dropdown
@@ -410,8 +414,8 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
_diffuseIntensity = p.diffuseIntensity.value_or(_diffuseIntensity);
_specularIntensity = p.specularIntensity.value_or(_specularIntensity);
_performShading = p.performShading.value_or(_performShading);
_disableDepthTest = p.disableDepthTest.value_or(_disableDepthTest);
_disableFaceCulling = p.disableFaceCulling.value_or(_disableFaceCulling);
_enableDepthTest = p.enableDepthTest.value_or(_enableDepthTest);
_enableFaceCulling = p.enableFaceCulling.value_or(_enableFaceCulling);
if (p.vertexShader.has_value()) {
_vertexShaderPath = p.vertexShader->string();
@@ -440,8 +444,8 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
addProperty(_diffuseIntensity);
addProperty(_specularIntensity);
addProperty(_performShading);
addProperty(_disableFaceCulling);
addProperty(_disableDepthTest);
addProperty(_enableFaceCulling);
addProperty(_enableDepthTest);
addProperty(_modelTransform);
addProperty(_rotationVec);
@@ -468,7 +472,6 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
_blendingFuncOption.addOption(DefaultBlending, "Default");
_blendingFuncOption.addOption(AdditiveBlending, "Additive");
_blendingFuncOption.addOption(PointsAndLinesBlending, "Points and Lines");
_blendingFuncOption.addOption(PolygonBlending, "Polygon");
_blendingFuncOption.addOption(ColorAddingBlending, "Color Adding");
@@ -479,13 +482,11 @@ RenderableModel::RenderableModel(const ghoul::Dictionary& dictionary)
_blendingFuncOption.set(BlendingMapping[blendingOpt]);
}
_enableOpacityBlending = p.enableOpacityBlending.value_or(_enableOpacityBlending);
addProperty(_enableOpacityBlending);
_originalRenderBin = renderBin();
}
bool RenderableModel::isReady() const {
return _program;
return _program && _quadProgram;
}
void RenderableModel::initialize() {
@@ -538,14 +539,112 @@ void RenderableModel::initializeGL() {
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
_quadProgram = BaseModule::ProgramObjectManager.request(
"ModelOpacityProgram",
[&]() -> std::unique_ptr<ghoul::opengl::ProgramObject> {
std::filesystem::path vs =
absPath("${MODULE_BASE}/shaders/modelOpacity_vs.glsl");
std::filesystem::path fs =
absPath("${MODULE_BASE}/shaders/modelOpacity_fs.glsl");
return global::renderEngine->buildRenderProgram("ModelOpacityProgram", vs, fs);
}
);
ghoul::opengl::updateUniformLocations(
*_quadProgram,
_uniformOpacityCache,
UniformOpacityNames
);
// Screen quad VAO
const GLfloat quadVertices[] = {
// x y s t
-1.f, -1.f, 0.f, 0.f,
1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 0.f, 1.f,
-1.f, -1.f, 0.f, 0.f,
1.f, -1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f
};
glGenVertexArrays(1, &_quadVao);
glBindVertexArray(_quadVao);
glGenBuffers(1, &_quadVbo);
glBindBuffer(GL_ARRAY_BUFFER, _quadVbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);
glEnableVertexAttribArray(1);
glVertexAttribPointer(
1,
2,
GL_FLOAT,
GL_FALSE,
4 * sizeof(GLfloat),
reinterpret_cast<void*>(2 * sizeof(GLfloat))
);
// Generate textures and the frame buffer
glGenFramebuffers(1, &_framebuffer);
// Bind textures to the framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
glFramebufferTexture(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
global::renderEngine->renderer().additionalColorTexture1(),
0
);
glFramebufferTexture(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT1,
global::renderEngine->renderer().additionalColorTexture2(),
0
);
glFramebufferTexture(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT2,
global::renderEngine->renderer().additionalColorTexture3(),
0
);
glFramebufferTexture(
GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
global::renderEngine->renderer().additionalDepthTexture(),
0
);
if (glbinding::Binding::ObjectLabel.isResolved()) {
glObjectLabel(GL_FRAMEBUFFER, _framebuffer, -1, "RenderableModel Framebuffer");
}
// Check status
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
LERROR("Framebuffer is not complete");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Initialize geometry
_geometry->initialize();
_geometry->calculateBoundingRadius();
setBoundingSphere(_geometry->boundingRadius() * _modelScale);
// Set Interaction sphere size to be 10% of the bounding sphere
setInteractionSphere(boundingSphere() * 0.1);
}
void RenderableModel::deinitializeGL() {
_geometry->deinitialize();
_geometry.reset();
glDeleteFramebuffers(1, &_framebuffer);
glDeleteBuffers(1, &_quadVbo);
glDeleteVertexArrays(1, &_quadVao);
std::string program = std::string(ProgramName);
if (!_vertexShaderPath.empty()) {
program += "|vs=" + _vertexShaderPath;
@@ -559,7 +658,17 @@ void RenderableModel::deinitializeGL() {
global::renderEngine->removeRenderProgram(p);
}
);
BaseModule::ProgramObjectManager.release(
"ModelOpacityProgram",
[](ghoul::opengl::ProgramObject* p) {
global::renderEngine->removeRenderProgram(p);
}
);
_program = nullptr;
_quadProgram = nullptr;
ghoul::opengl::FramebufferObject::deactivate();
}
void RenderableModel::render(const RenderData& data, RendererTasks&) {
@@ -573,110 +682,241 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) {
// Formula from RenderableGlobe
constexpr double tfov = 0.5773502691896257;
constexpr int res = 2880;
const double maxDistance = res * boundingSphere() / tfov;
if (distanceToCamera < maxDistance) {
_program->activate();
// @TODO (malej 13-APR-23): This should only use the boundingSphere function once
// that takes the gui scale into account too for all renderables
const double maxDistance =
res * boundingSphere() * glm::compMax(data.modelTransform.scale) / tfov;
_program->setUniform(_uniformCache.opacity, opacity());
// Don't render if model is too far away
if (distanceToCamera >= maxDistance) {
return;
}
// Model transform and view transform needs to be in double precision
const 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::scale(_modelTransform.value(), glm::dvec3(_modelScale));
const glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() *
modelTransform;
_program->activate();
int nLightSources = 0;
_lightIntensitiesBuffer.resize(_lightSources.size());
_lightDirectionsViewSpaceBuffer.resize(_lightSources.size());
for (const std::unique_ptr<LightSource>& lightSource : _lightSources) {
if (!lightSource->isEnabled()) {
continue;
}
_lightIntensitiesBuffer[nLightSources] = lightSource->intensity();
_lightDirectionsViewSpaceBuffer[nLightSources] =
lightSource->directionViewSpace(data);
// Model transform and view transform needs to be in double precision
const 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::scale(_modelTransform.value(), glm::dvec3(_modelScale));
const glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() *
modelTransform;
++nLightSources;
int nLightSources = 0;
_lightIntensitiesBuffer.resize(_lightSources.size());
_lightDirectionsViewSpaceBuffer.resize(_lightSources.size());
for (const std::unique_ptr<LightSource>& lightSource : _lightSources) {
if (!lightSource->isEnabled()) {
continue;
}
_lightIntensitiesBuffer[nLightSources] = lightSource->intensity();
_lightDirectionsViewSpaceBuffer[nLightSources] =
lightSource->directionViewSpace(data);
++nLightSources;
}
_program->setUniform(
_uniformCache.nLightSources,
nLightSources
);
_program->setUniform(
_uniformCache.lightIntensities,
_lightIntensitiesBuffer
);
_program->setUniform(
_uniformCache.lightDirectionsViewSpace,
_lightDirectionsViewSpaceBuffer
);
_program->setUniform(
_uniformCache.modelViewTransform,
glm::mat4(modelViewTransform)
);
glm::dmat4 normalTransform = glm::transpose(glm::inverse(modelViewTransform));
_program->setUniform(
_uniformCache.normalTransform,
glm::mat4(normalTransform)
);
_program->setUniform(
_uniformCache.projectionTransform,
data.camera.projectionMatrix()
);
_program->setUniform(_uniformCache.ambientIntensity, _ambientIntensity);
_program->setUniform(_uniformCache.diffuseIntensity, _diffuseIntensity);
_program->setUniform(_uniformCache.specularIntensity, _specularIntensity);
_program->setUniform(_uniformCache.performShading, _performShading);
if (!_enableFaceCulling) {
glDisable(GL_CULL_FACE);
}
// Configure blending
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 PolygonBlending:
glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
break;
case ColorAddingBlending:
glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
break;
};
if (!_enableDepthTest) {
glDisable(GL_DEPTH_TEST);
}
if (!_shouldRenderTwice) {
// Reset manual depth test
_program->setUniform(
_uniformCache.nLightSources,
nLightSources
);
_program->setUniform(
_uniformCache.lightIntensities,
_lightIntensitiesBuffer
);
_program->setUniform(
_uniformCache.lightDirectionsViewSpace,
_lightDirectionsViewSpaceBuffer
);
_program->setUniform(
_uniformCache.modelViewTransform,
glm::mat4(modelViewTransform)
_uniformCache.performManualDepthTest,
false
);
glm::dmat4 normalTransform = glm::transpose(glm::inverse(modelViewTransform));
_program->setUniform(
_uniformCache.normalTransform,
glm::mat4(normalTransform)
);
_program->setUniform(
_uniformCache.projectionTransform,
data.camera.projectionMatrix()
);
_program->setUniform(_uniformCache.ambientIntensity, _ambientIntensity);
_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);
if (hasOverrideRenderBin()) {
// If override render bin is set then use the opacity values as normal
_program->setUniform(_uniformCache.opacity, opacity());
}
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);
else {
// Otherwise reset to 1
_program->setUniform(_uniformCache.opacity, 1.f);
}
_geometry->render(*_program);
if (_disableFaceCulling) {
glEnable(GL_CULL_FACE);
}
global::renderEngine->openglStateCache().resetBlendState();
if (_disableDepthTest) {
glEnable(GL_DEPTH_TEST);
}
_program->deactivate();
}
else {
// Prepare framebuffer
GLint defaultFBO = ghoul::opengl::FramebufferObject::getActiveObject();
glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
// Re-bind first texture to use the currently not used Ping-Pong texture in the
// FramebufferRenderer
glFramebufferTexture(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
global::renderEngine->renderer().additionalColorTexture1(),
0
);
// Check status
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
LERROR("Framebuffer is not complete");
}
glDrawBuffers(3, ColorAttachmentArray);
glClearBufferfv(GL_COLOR, 1, glm::value_ptr(glm::vec4(0.f, 0.f, 0.f, 0.f)));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use a manuel depth test to make the models aware of the rest of the scene
_program->setUniform(
_uniformCache.performManualDepthTest,
_enableDepthTest
);
// Bind the G-buffer depth texture for a manual depth test towards the rest
// of the scene
ghoul::opengl::TextureUnit gBufferDepthTextureUnit;
gBufferDepthTextureUnit.activate();
glBindTexture(
GL_TEXTURE_2D,
global::renderEngine->renderer().gBufferDepthTexture()
);
_program->setUniform(
_uniformCache.gBufferDepthTexture,
gBufferDepthTextureUnit
);
// Will also need the resolution to get a texture coordinate for the G-buffer
// depth texture
_program->setUniform(
_uniformCache.resolution,
glm::vec2(global::windowDelegate->currentDrawBufferResolution())
);
// Make sure opacity in first pass is always 1
_program->setUniform(_uniformCache.opacity, 1.f);
// Render Pass 1
// Render all parts of the model into the new framebuffer without opacity
_geometry->render(*_program);
_program->deactivate();
// Render pass 2
// Render the whole model into the G-buffer with the correct opacity
glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
// Screen-space quad should not be discarded due to depth test,
// but we still want to be able to write to the depth buffer -> GL_ALWAYS
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
_quadProgram->activate();
_quadProgram->setUniform(_uniformOpacityCache.opacity, opacity());
// Bind textures
ghoul::opengl::TextureUnit colorTextureUnit;
colorTextureUnit.activate();
glBindTexture(
GL_TEXTURE_2D,
global::renderEngine->renderer().additionalColorTexture1()
);
_quadProgram->setUniform(_uniformOpacityCache.colorTexture, colorTextureUnit);
ghoul::opengl::TextureUnit depthTextureUnit;
depthTextureUnit.activate();
glBindTexture(
GL_TEXTURE_2D,
global::renderEngine->renderer().additionalDepthTexture()
);
_quadProgram->setUniform(_uniformOpacityCache.depthTexture, depthTextureUnit);
// Will also need the resolution and viewport to get a texture coordinate
_quadProgram->setUniform(
_uniformOpacityCache.resolution,
glm::vec2(global::windowDelegate->currentDrawBufferResolution())
);
GLint vp[4] = { 0 };
global::renderEngine->openglStateCache().viewport(vp);
glm::ivec4 viewport = glm::ivec4(vp[0], vp[1], vp[2], vp[3]);
_quadProgram->setUniform(
_uniformOpacityCache.viewport,
viewport[0],
viewport[1],
viewport[2],
viewport[3]
);
// Draw
glBindVertexArray(_quadVao);
glDrawArrays(GL_TRIANGLES, 0, 6);
_quadProgram->deactivate();
}
// Reset
if (!_enableFaceCulling) {
glEnable(GL_CULL_FACE);
}
if (!_enableDepthTest) {
glEnable(GL_DEPTH_TEST);
}
global::renderEngine->openglStateCache().resetBlendState();
global::renderEngine->openglStateCache().resetDepthState();
glActiveTexture(GL_TEXTURE0);
}
void RenderableModel::update(const UpdateData& data) {
@@ -685,11 +925,19 @@ void RenderableModel::update(const UpdateData& data) {
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
}
setBoundingSphere(_geometry->boundingRadius() * _modelScale *
glm::compMax(data.modelTransform.scale)
);
// Set Interaction sphere size to be 10% of the bounding sphere
setInteractionSphere(boundingSphere() * 0.1);
if (!hasOverrideRenderBin()) {
// Only render two pass if the model is in any way transparent
const float o = opacity();
if ((o >= 0.f && o < 1.f) || _geometry->isTransparent()) {
setRenderBin(Renderable::RenderBin::PostDeferredTransparent);
_shouldRenderTwice = true;
}
else {
setRenderBin(_originalRenderBin);
_shouldRenderTwice = false;
}
}
if (_geometry->hasAnimation() && !_animationStart.empty()) {
double relativeTime;
+19 -6
View File
@@ -33,7 +33,6 @@
#include <openspace/properties/scalar/boolproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/vector/vec3property.h>
#include <openspace/util/distanceconversion.h>
#include <ghoul/misc/managedmemoryuniqueptr.h>
#include <ghoul/io/model/modelreader.h>
#include <ghoul/opengl/uniformcache.h>
@@ -93,21 +92,21 @@ private:
properties::FloatProperty _specularIntensity;
properties::BoolProperty _performShading;
properties::BoolProperty _disableFaceCulling;
properties::BoolProperty _enableFaceCulling;
properties::DMat4Property _modelTransform;
properties::Vec3Property _rotationVec;
properties::BoolProperty _disableDepthTest;
properties::BoolProperty _enableOpacityBlending;
properties::BoolProperty _enableDepthTest;
properties::OptionProperty _blendingFuncOption;
std::string _vertexShaderPath;
std::string _fragmentShaderPath;
ghoul::opengl::ProgramObject* _program = nullptr;
UniformCache(opacity, nLightSources, lightDirectionsViewSpace, lightIntensities,
UniformCache(nLightSources, lightDirectionsViewSpace, lightIntensities,
modelViewTransform, normalTransform, projectionTransform,
performShading, ambientIntensity, diffuseIntensity,
specularIntensity, opacityBlending) _uniformCache;
specularIntensity, performManualDepthTest, gBufferDepthTexture,
resolution, opacity) _uniformCache;
std::vector<std::unique_ptr<LightSource>> _lightSources;
@@ -116,6 +115,20 @@ private:
std::vector<glm::vec3> _lightDirectionsViewSpaceBuffer;
properties::PropertyOwner _lightSourcePropertyOwner;
// Framebuffer and screen space quad
GLuint _framebuffer = 0;
GLuint _quadVao = 0;
GLuint _quadVbo = 0;
bool _shouldRenderTwice = false;
// Opacity program
ghoul::opengl::ProgramObject* _quadProgram = nullptr;
UniformCache(opacity, colorTexture, depthTexture, viewport,
resolution) _uniformOpacityCache;
// Store the original RenderBin
Renderable::RenderBin _originalRenderBin;
};
} // namespace openspace
+6 -2
View File
@@ -434,6 +434,9 @@ void RenderableTrail::render(const RenderData& data, RendererTasks&) {
const double distance = glm::distance(trailPosWorld, data.camera.eyePositionVec3());
if (distance > boundingSphere() * DISTANCE_CULLING_RADII) {
// Reset
global::renderEngine->openglStateCache().resetBlendState();
global::renderEngine->openglStateCache().resetDepthState();
return;
}
@@ -468,8 +471,9 @@ void RenderableTrail::render(const RenderData& data, RendererTasks&) {
glBindVertexArray(0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(true);
// Reset
global::renderEngine->openglStateCache().resetBlendState();
global::renderEngine->openglStateCache().resetDepthState();
_programObject->deactivate();
}
@@ -108,7 +108,7 @@ void ScreenSpaceFramebuffer::render() {
const glm::vec4& size = _size.value();
const float xratio = resolution.x / (size.z - size.x);
const float yratio = resolution.y / (size.w - size.y);;
const float yratio = resolution.y / (size.w - size.y);
if (!_renderFunctions.empty()) {
GLint viewport[4];
+64
View File
@@ -0,0 +1,64 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2022 *
* *
* 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"
#include "floatoperations.glsl"
in vec2 vs_st;
uniform float opacity = 1.0;
uniform sampler2D colorTexture;
uniform sampler2D depthTexture;
uniform ivec4 viewport;
uniform vec2 resolution;
Fragment getFragment() {
Fragment frag;
// Modify the texCoord based on the Viewport and Resolution. This modification is
// necessary in case of side-by-side stereo as we only want to access the part of the
// feeding texture that we are currently responsible for. Otherwise we would map the
// entire feeding texture into our half of the result texture, leading to a doubling of
// the "missing" half. If you don't believe me, load a configuration file with the
// side_by_side stereo mode enabled, disable FXAA, and remove this modification.
// The same calculation is done in the HDR resolving shader
vec2 st = vs_st;
st.x = st.x / (resolution.x / viewport[2]) + (viewport[0] / resolution.x);
st.y = st.y / (resolution.y / viewport[3]) + (viewport[1] / resolution.y);
vec4 textureColor = texture(colorTexture, st);
if (textureColor.a == 0.0 || opacity == 0.0) {
discard;
}
frag.color.rgb = textureColor.rgb;
frag.color.a = opacity * textureColor.a;
frag.depth = denormalizeFloat(texture(depthTexture, vs_st).x);
frag.disableLDR2HDR = true;
return frag;
}
+35
View File
@@ -0,0 +1,35 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2022 *
* *
* 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__
layout(location = 0) in vec2 in_position;
layout(location = 1) in vec2 in_st;
out vec2 vs_st;
void main() {
vs_st = in_st;
gl_Position = vec4(in_position.x, in_position.y, 0.0, 1.0);
}
+68 -64
View File
@@ -28,135 +28,139 @@ in vec2 vs_st;
in vec3 vs_normalViewSpace;
in vec4 vs_positionCameraSpace;
in float vs_screenSpaceDepth;
in mat3 TBN;
in mat3 vs_TBN;
uniform float ambientIntensity = 0.2;
uniform float diffuseIntensity = 1.0;
uniform float specularIntensity = 1.0;
uniform bool performShading = true;
uniform bool use_forced_color = false;
uniform bool has_texture_diffuse;
uniform bool has_texture_normal;
uniform bool has_texture_specular;
uniform bool has_color_specular;
uniform bool opacityBlending = false;
uniform sampler2D texture_diffuse;
uniform sampler2D texture_normal;
uniform sampler2D texture_specular;
uniform vec3 color_diffuse;
uniform vec3 color_specular;
uniform vec4 color_diffuse;
uniform vec4 color_specular;
uniform float opacity = 1.0;
uniform int nLightSources;
uniform vec3 lightDirectionsViewSpace[8];
uniform float lightIntensities[8];
uniform float opacity = 1.0;
uniform bool performManualDepthTest = false;
uniform sampler2D gBufferDepthTexture;
uniform vec2 resolution;
Fragment getFragment() {
Fragment frag;
frag.depth = vs_screenSpaceDepth;
frag.gPosition = vs_positionCameraSpace;
frag.gNormal = vec4(vs_normalViewSpace, 0.0);
frag.disableLDR2HDR = true;
frag.color.a = opacity;
if (performManualDepthTest) {
// gl_FragCoord.x goes from 0 to resolution.x and gl_FragCoord.y goes from 0 to
// resolution.y, need to normalize it
vec2 texCoord = gl_FragCoord.xy;
texCoord.x = texCoord.x / resolution.x;
texCoord.y = texCoord.y / resolution.y;
// Manual depth test
float gBufferDepth = denormalizeFloat(texture(gBufferDepthTexture, texCoord).x);
if (vs_screenSpaceDepth > gBufferDepth) {
frag.color = vec4(0.0);
frag.depth = gBufferDepth;
return frag;
}
}
// Render invisible mesh with flashy procedural material
if (use_forced_color) {
Fragment frag;
vec3 adjustedPos = floor(vs_positionCameraSpace.xyz * 3.0);
vec3 adjustedPos = floor(vs_positionCameraSpace.xyz / 500.0);
float chessboard = adjustedPos.x + adjustedPos.y + adjustedPos.z;
chessboard = fract(chessboard * 0.5);
chessboard *= 2;
// Pink and complementary green in a chessboard pattern
frag.color.rgb = mix(vec3(1.0, 0.0, 0.8), vec3(0.0, 1.0, 0.2), chessboard);
frag.color.a = opacity;
frag.depth = vs_screenSpaceDepth;
frag.gPosition = vs_positionCameraSpace;
frag.gNormal = vec4(vs_normalViewSpace, 0.0);
frag.disableLDR2HDR = true;
return frag;
}
vec3 diffuseAlbedo;
// Base color
vec4 diffuseAlbedo;
if (has_texture_diffuse) {
diffuseAlbedo = texture(texture_diffuse, vs_st).rgb;
diffuseAlbedo = texture(texture_diffuse, vs_st);
}
else {
diffuseAlbedo = color_diffuse;
}
if (opacity == 0.0) {
discard;
}
Fragment frag;
if (performShading) {
// Specular color
vec3 specularAlbedo;
if (has_texture_specular) {
specularAlbedo = texture(texture_specular, vs_st).rgb;
}
else {
if (has_color_specular) {
specularAlbedo = color_specular;
specularAlbedo = color_specular.rgb;
}
else {
specularAlbedo = vec3(1.0);
specularAlbedo = diffuseAlbedo.rgb;
}
}
// Some of these values could be passed in as uniforms
const vec3 lightColorAmbient = vec3(1.0);
const vec3 lightColor = vec3(1.0);
vec3 n;
// Bumb mapping
vec3 normal;
if (has_texture_normal) {
vec3 normalAlbedo = texture(texture_normal, vs_st).rgb;
normalAlbedo = normalize(normalAlbedo * 2.0 - 1.0);
n = normalize(TBN * normalAlbedo);
normal = normalize(vs_TBN * normalAlbedo);
}
else {
n = normalize(vs_normalViewSpace);
normal = normalize(vs_normalViewSpace);
}
frag.gNormal = vec4(normal, 0.0);
vec3 c = normalize(vs_positionCameraSpace.xyz);
// Could be seperated into ambinet, diffuse and specular and passed in as uniforms
const vec3 lightColor = vec3(1.0);
const float specularPower = 100.0;
vec3 color = ambientIntensity * lightColorAmbient * diffuseAlbedo;
// Ambient light
vec3 totalLightColor = ambientIntensity * lightColor * diffuseAlbedo.rgb;
vec3 viewDirection = normalize(vs_positionCameraSpace.xyz);
for (int i = 0; i < nLightSources; ++i) {
vec3 l = lightDirectionsViewSpace[i];
vec3 r = reflect(l, n);
float diffuseCosineFactor = dot(n,l);
float specularCosineFactor = dot(c,r);
const float specularPower = 100.0;
// Diffuse light
vec3 lightDirection = lightDirectionsViewSpace[i];
float diffuseFactor = max(dot(normal, lightDirection), 0.0);
vec3 diffuseColor =
diffuseIntensity * lightColor * diffuseAlbedo * max(diffuseCosineFactor, 0);
diffuseIntensity * lightColor * diffuseFactor * diffuseAlbedo.rgb;
// Specular light
vec3 reflectDirection = reflect(lightDirection, normal);
float specularFactor =
pow(max(dot(viewDirection, reflectDirection), 0.0), specularPower);
vec3 specularColor =
specularIntensity * lightColor * specularAlbedo *
pow(max(specularCosineFactor, 0), specularPower);
specularIntensity * lightColor * specularFactor * specularAlbedo;
color += lightIntensities[i] * (diffuseColor + specularColor);
totalLightColor += lightIntensities[i] * (diffuseColor + specularColor);
}
frag.color.rgb = color;
frag.color.rgb = totalLightColor;
}
else {
frag.color.rgb = diffuseAlbedo;
frag.color.rgb = diffuseAlbedo.rgb;
}
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;
frag.color.a = diffuseAlbedo.a * opacity;
return frag;
}
+11 -10
View File
@@ -35,7 +35,7 @@ out vec2 vs_st;
out vec3 vs_normalViewSpace;
out float vs_screenSpaceDepth;
out vec4 vs_positionCameraSpace;
out mat3 TBN;
out mat3 vs_TBN;
uniform mat4 modelViewTransform;
uniform mat4 projectionTransform;
@@ -53,17 +53,18 @@ void main() {
vs_st = in_st;
vs_screenSpaceDepth = positionScreenSpace.w;
vs_normalViewSpace = normalize(mat3(normalTransform) * (mat3(meshNormalTransform) * in_normal));
vs_normalViewSpace =
normalize(mat3(normalTransform) * (mat3(meshNormalTransform) * in_normal));
// TBN matrix for normal mapping
vec3 T = normalize(mat3(normalTransform) * (mat3(meshNormalTransform) * in_tangent));
vec3 N = normalize(mat3(normalTransform) * (mat3(meshNormalTransform) * in_normal));
// TBN matrix for normal mapping
vec3 T = normalize(mat3(normalTransform) * (mat3(meshNormalTransform) * in_tangent));
vec3 N = normalize(mat3(normalTransform) * (mat3(meshNormalTransform) * in_normal));
// Re-orthogonalize T with respect to N
T = normalize(T - dot(T, N) * N);
// Re-orthogonalize T with respect to N
T = normalize(T - dot(T, N) * N);
// Retrieve perpendicular vector B with cross product of T and N
vec3 B = normalize(cross(N, T));
// Retrieve perpendicular vector B with cross product of T and N
vec3 B = normalize(cross(N, T));
TBN = mat3(T, B, N);
vs_TBN = mat3(T, B, N);
}
@@ -30,6 +30,7 @@
#include <openspace/engine/globals.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/scene/scene.h>
#include <openspace/util/distanceconversion.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/io/model/modelgeometry.h>
@@ -67,18 +68,38 @@ namespace {
// This specifies the model that is rendered by the Renderable.
std::filesystem::path geometryFile;
enum class [[codegen::map(openspace::DistanceUnit)]] ScaleUnit {
Nanometer,
Micrometer,
Millimeter,
Centimeter,
Decimeter,
Meter,
Kilometer,
Thou,
Inch,
Foot,
Yard,
Chain,
Furlong,
Mile
};
// The scale of the model. For example if the model is in centimeters
// then ModelScale = Centimeter or ModelScale = 0.01
std::optional<std::variant<ScaleUnit, double>> modelScale;
// By default the given ModelScale is used to scale the model down,
// by setting this setting to true the model is instead scaled up with the
// given ModelScale
std::optional<bool> invertModelScale;
// Contains information about projecting onto this planet.
ghoul::Dictionary projection
[[codegen::reference("spacecraftinstruments_projectioncomponent")]];
// [[codegen::verbatim(PerformShadingInfo.description)]]
std::optional<bool> performShading;
// The radius of the bounding sphere of this object. This has to be a
// radius that is larger than anything that is rendered by it. It has to
// be at least as big as the convex hull of the object. The default value
// is 10e9 meters.
std::optional<double> boundingSphereRadius;
};
#include "renderablemodelprojection_codegen.cpp"
} // namespace
@@ -102,12 +123,30 @@ RenderableModelProjection::RenderableModelProjection(const ghoul::Dictionary& di
ghoul::io::ModelReader::NotifyInvisibleDropped::Yes
);
_invertModelScale = p.invertModelScale.value_or(_invertModelScale);
if (p.modelScale.has_value()) {
if (std::holds_alternative<Parameters::ScaleUnit>(*p.modelScale)) {
Parameters::ScaleUnit scaleUnit =
std::get<Parameters::ScaleUnit>(*p.modelScale);
DistanceUnit distanceUnit = codegen::map<DistanceUnit>(scaleUnit);
_modelScale = toMeter(distanceUnit);
}
else if (std::holds_alternative<double>(*p.modelScale)) {
_modelScale = std::get<double>(*p.modelScale);
}
else {
throw ghoul::MissingCaseException();
}
if (_invertModelScale) {
_modelScale = 1.0 / _modelScale;
}
}
addPropertySubOwner(_projectionComponent);
_projectionComponent.initialize(identifier(), p.projection);
double boundingSphereRadius = p.boundingSphereRadius.value_or(1.0e9);
setBoundingSphere(boundingSphereRadius);
_performShading = p.performShading.value_or(_performShading);
addProperty(_performShading);
}
@@ -161,9 +200,11 @@ void RenderableModelProjection::initializeGL() {
_projectionComponent.initializeGL();
double bs = boundingSphere();
_geometry->initialize();
setBoundingSphere(bs); // ignore bounding sphere set by geometry.
setBoundingSphere(_geometry->boundingRadius() * _modelScale);
// Set Interaction sphere size to be 10% of the bounding sphere
setInteractionSphere(boundingSphere() * 0.1);
}
void RenderableModelProjection::deinitializeGL() {
@@ -222,8 +263,11 @@ void RenderableModelProjection::render(const RenderData& data, RendererTasks&) {
const glm::dmat4 transform =
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::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale)) *
glm::scale(glm::dmat4(1.0), glm::dvec3(_modelScale));
const glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() * transform;
// malej 2023-FEB-23: The light sources should probably not be hard coded
const glm::vec3 directionToSun = glm::normalize(_sunPosition - bodyPos);
const glm::vec3 directionToSunViewSpace = glm::normalize(
glm::mat3(data.camera.combinedViewMatrix()) * directionToSun
@@ -84,6 +84,8 @@ private:
UniformCache(ProjectorMatrix, ModelTransform) _depthFboUniformCache;
std::unique_ptr<ghoul::modelgeometry::ModelGeometry> _geometry;
double _modelScale = 1.0;
bool _invertModelScale = false;
glm::dmat3 _instrumentMatrix = glm::dmat3(1.0);
@@ -32,7 +32,7 @@ in vec4 vs_positionCameraSpace;
uniform bool has_texture_diffuse;
uniform sampler2D baseTexture;
uniform vec3 baseColor;
uniform vec4 baseColor;
uniform sampler2D projectionTexture;
uniform bool performShading;
uniform float projectionFading;
@@ -47,13 +47,23 @@ const float specularPower = 100.0;
Fragment getFragment() {
Fragment frag;
frag.depth = vs_depth;
frag.gPosition = vs_positionCameraSpace;
frag.gNormal = vec4(vs_normalViewSpace, 0.0);
frag.disableLDR2HDR = true;
frag.color.a = 1.0;
// Base color
vec4 textureColor;
if (has_texture_diffuse) {
textureColor = texture(baseTexture, vs_st);
textureColor = texture(baseTexture, vs_st);
}
else {
textureColor = vec4(baseColor, 1.0);
textureColor = vec4(baseColor.rgb, 1.0);
}
// Mix base color with the projection images
vec4 projectionColor = texture(projectionTexture, vs_st);
if (projectionColor.a > 0.0) {
textureColor.rgb = mix(
@@ -65,34 +75,35 @@ Fragment getFragment() {
vec3 diffuseAlbedo = textureColor.rgb;
Fragment frag;
if (performShading) {
// Some of these values could be passed in as uniforms
const vec3 lightColorAmbient = vec3(1.0);
// Could be seperated into ambinet, diffuse and specular and passed in as uniforms
const vec3 lightColor = vec3(1.0);
const float specularPower = 100.0;
vec3 n = normalize(vs_normalViewSpace);
vec3 l = directionToSunViewSpace;
vec3 c = normalize(vs_positionCameraSpace.xyz);
vec3 r = reflect(l, n);
// Ambient light
vec3 ambientColor = ambientIntensity * lightColor * diffuseAlbedo;
float diffuseCosineFactor = dot(n,l);
float specularCosineFactor = dot(c,r);
vec3 ambientColor = ambientIntensity * lightColorAmbient * diffuseAlbedo;
// Diffuse light
vec3 normal = normalize(vs_normalViewSpace);
vec3 lightDirection = directionToSunViewSpace;
float diffuseFactor = max(dot(normal, lightDirection), 0.0);
vec3 diffuseColor =
diffuseIntensity * lightColor * diffuseAlbedo * max(diffuseCosineFactor, 0.0);
vec3 specularColor = specularIntensity * lightColor * specularAlbedo *
pow(max(specularCosineFactor, 0.0), specularPower);
diffuseIntensity * lightColor * diffuseFactor * diffuseAlbedo;
// Specular light
vec3 viewDirection = normalize(vs_positionCameraSpace.xyz);
vec3 reflectDirection = reflect(lightDirection, normal);
float specularFactor =
pow(max(dot(viewDirection, reflectDirection), 0.0), specularPower);
vec3 specularColor =
specularIntensity * lightColor * specularFactor * specularAlbedo;
// Total light
frag.color.rgb = ambientColor + diffuseColor + specularColor;
}
else {
frag.color.rgb = diffuseAlbedo;
}
frag.color.a = 1.0;
frag.depth = vs_depth;
// frag.depth = 0.0;
return frag;
}
@@ -37,7 +37,6 @@ out vec4 vs_positionCameraSpace;
uniform mat4 modelViewTransform;
uniform mat4 projectionTransform;
uniform vec3 cameraDirectionWorldSpace;
uniform mat4 meshTransform;
uniform mat4 meshNormalTransform;