From b457dfd8b838ad63613c4c19edf51f43af4943a5 Mon Sep 17 00:00:00 2001 From: "glassmancody.info" Date: Fri, 4 Jun 2021 12:13:52 -0700 Subject: [PATCH] fix water RTTs and minor math error in non-infinite projection matrix --- components/sceneutil/util.cpp | 31 +++++++++++++++++++++++++++++-- components/sceneutil/util.hpp | 17 +++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/components/sceneutil/util.cpp b/components/sceneutil/util.cpp index 2e37260eb7..79fe5d13c5 100644 --- a/components/sceneutil/util.cpp +++ b/components/sceneutil/util.cpp @@ -150,6 +150,33 @@ void GlowUpdater::setDuration(float duration) mDuration = duration; } +AttachMultisampledDepthColorCallback::AttachMultisampledDepthColorCallback(const osg::ref_ptr& colorTex, const osg::ref_ptr& depthTex, int samples, int colorSamples) +{ + int width = colorTex->getTextureWidth(); + int height = colorTex->getTextureHeight(); + + osg::ref_ptr rbColor = new osg::RenderBuffer(width, height, colorTex->getInternalFormat(), samples, colorSamples); + osg::ref_ptr rbDepth = new osg::RenderBuffer(width, height, depthTex->getInternalFormat(), samples, colorSamples); + + mMsaaFbo = new osg::FrameBufferObject; + mMsaaFbo->setAttachment(osg::Camera::COLOR_BUFFER0, osg::FrameBufferAttachment(rbColor)); + mMsaaFbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment(rbDepth)); + + mFbo = new osg::FrameBufferObject; + mFbo->setAttachment(osg::Camera::COLOR_BUFFER0, osg::FrameBufferAttachment(colorTex)); + mFbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment(depthTex)); +} + +void AttachMultisampledDepthColorCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) +{ + osgUtil::RenderStage* renderStage = nv->asCullVisitor()->getCurrentRenderStage(); + + renderStage->setMultisampleResolveFramebufferObject(mFbo); + renderStage->setFrameBufferObject(mMsaaFbo); + + traverse(node, nv); +} + void transformBoundingSphere (const osg::Matrixf& matrix, osg::BoundingSphere& bsphere) { osg::BoundingSphere::vec_type xdash = bsphere._center; @@ -308,8 +335,8 @@ osg::Matrix getReversedZProjectionMatrixAsPerspective(double fov, double aspect, return osg::Matrix( A/aspect, 0, 0, 0, 0, A, 0, 0, - 0, 0, far/(far-near)-1.0, -1, - 0, 0, -(far*near)/(far - near), 0 + 0, 0, near/(far-near), -1, + 0, 0, (far*near)/(far - near), 0 ); } diff --git a/components/sceneutil/util.hpp b/components/sceneutil/util.hpp index 0164e0c99e..69e66bb8b3 100644 --- a/components/sceneutil/util.hpp +++ b/components/sceneutil/util.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -48,6 +49,22 @@ namespace SceneUtil bool mDone; }; + // Allows camera to render to a color and floating point depth texture with a multisampled framebuffer. + // Must be set on a cameras cull callback. + // When the depth texture isn't needed as a sampler, use osg::Camera::attach(osg::Camera::DEPTH_COMPONENT, GL_DEPTH_COMPONENT32F) instead. + // If multisampling is not being used on the color buffer attachment, use the osg::Camera::attach() method. + class AttachMultisampledDepthColorCallback : public osg::NodeCallback + { + public: + AttachMultisampledDepthColorCallback(const osg::ref_ptr& colorTex, const osg::ref_ptr& depthTex, int samples, int colorSamples); + + void operator()(osg::Node* node, osg::NodeVisitor* nv) override; + + private: + osg::ref_ptr mFbo; + osg::ref_ptr mMsaaFbo; + }; + // Transform a bounding sphere by a matrix // based off private code in osg::Transform // TODO: patch osg to make public