refactor and fix wobbly shores

This commit is contained in:
glassmancody.info 2021-08-04 17:49:57 -07:00
parent cad0b151cb
commit 09e03fde2e
45 changed files with 256 additions and 239 deletions

View File

@ -40,6 +40,7 @@
Feature #6017: Separate persistent and temporary cell references when saving Feature #6017: Separate persistent and temporary cell references when saving
Feature #6032: Reverse-z depth buffer Feature #6032: Reverse-z depth buffer
Feature #6162: Refactor GUI to use shaders and to be GLES and GL3+ friendly Feature #6162: Refactor GUI to use shaders and to be GLES and GL3+ friendly
Feature #6199: Support FBO Rendering
0.47.0 0.47.0
------ ------

View File

@ -84,6 +84,7 @@ CSMWorld::Data::Data (ToUTF8::FromType encoding, bool fsStrict, const Files::Pat
defines["preLightEnv"] = "0"; // Apply environment maps after lighting like Morrowind defines["preLightEnv"] = "0"; // Apply environment maps after lighting like Morrowind
defines["radialFog"] = "0"; defines["radialFog"] = "0";
defines["lightingModel"] = "0"; defines["lightingModel"] = "0";
defines["reverseZ"] = "0";
for (const auto& define : shadowDefines) for (const auto& define : shadowDefines)
defines[define.first] = define.second; defines[define.first] = define.second;
mResourceSystem->getSceneManager()->getShaderManager().setGlobalDefines(defines); mResourceSystem->getSceneManager()->getShaderManager().setGlobalDefines(defines);

View File

@ -161,7 +161,7 @@ namespace CSVRender
} }
mWaterGeometry = SceneUtil::createWaterGeometry(size, segments, textureRepeats); mWaterGeometry = SceneUtil::createWaterGeometry(size, segments, textureRepeats);
mWaterGeometry->setStateSet(SceneUtil::createSimpleWaterStateSet(Alpha, RenderBin, false)); mWaterGeometry->setStateSet(SceneUtil::createSimpleWaterStateSet(Alpha, RenderBin));
// Add water texture // Add water texture
std::string textureName = Fallback::Map::getString("Water_SurfaceTexture"); std::string textureName = Fallback::Map::getString("Water_SurfaceTexture");

View File

@ -8,12 +8,11 @@
#include "MyGUI_FactoryManager.h" #include "MyGUI_FactoryManager.h"
#include <components/misc/utf8stream.hpp> #include <components/misc/utf8stream.hpp>
#include <components/sceneutil/util.hpp>
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp" #include "../mwbase/windowmanager.hpp"
#include "../mwrender/util.hpp"
namespace MWGui namespace MWGui
{ {
struct TypesetBookImpl; struct TypesetBookImpl;
@ -1219,7 +1218,7 @@ public:
RenderXform renderXform (mCroppedParent, textFormat.mRenderItem->getRenderTarget()->getInfo()); RenderXform renderXform (mCroppedParent, textFormat.mRenderItem->getRenderTarget()->getInfo());
float z = MWRender::getReverseZ() ? 1.f : -1.f; float z = SceneUtil::getReverseZ() ? 1.f : -1.f;
GlyphStream glyphStream(textFormat.mFont, static_cast<float>(mCoord.left), static_cast<float>(mCoord.top - mViewTop), GlyphStream glyphStream(textFormat.mFont, static_cast<float>(mCoord.left), static_cast<float>(mCoord.top - mViewTop),
z /*mNode->getNodeDepth()*/, vertices, renderXform); z /*mNode->getNodeDepth()*/, vertices, renderXform);

View File

@ -450,7 +450,7 @@ namespace MWGui
osg::ref_ptr<osg::Texture2D> texture (new osg::Texture2D); osg::ref_ptr<osg::Texture2D> texture (new osg::Texture2D);
texture->setImage(result.getImage()); texture->setImage(result.getImage());
texture->setInternalFormat(GL_RGBA); texture->setInternalFormat(GL_RGB);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);

View File

@ -35,6 +35,7 @@
#include <components/resource/resourcesystem.hpp> #include <components/resource/resourcesystem.hpp>
#include <components/resource/imagemanager.hpp> #include <components/resource/imagemanager.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/sceneutil/workqueue.hpp> #include <components/sceneutil/workqueue.hpp>

View File

@ -4,13 +4,13 @@
#include <osg/Geometry> #include <osg/Geometry>
#include <osg/Group> #include <osg/Group>
#include <osg/Material>
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/misc/convert.hpp> #include <components/misc/convert.hpp>
#include <osg/PolygonMode> #include <osg/PolygonMode>
#include <osg/ShapeDrawable> #include <osg/ShapeDrawable>
#include <osg/StateSet> #include <osg/StateSet>
#include <osg/Fog>
#include "bulletdebugdraw.hpp" #include "bulletdebugdraw.hpp"
#include "vismask.hpp" #include "vismask.hpp"
@ -18,8 +18,6 @@
#include <components/resource/resourcesystem.hpp> #include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp> #include <components/resource/scenemanager.hpp>
#include <components/sceneutil/util.hpp>
#include "../mwbase/world.hpp" #include "../mwbase/world.hpp"
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
@ -39,6 +37,7 @@ void DebugDrawer::createGeometry()
{ {
mGeometry = new osg::Geometry; mGeometry = new osg::Geometry;
mGeometry->setNodeMask(Mask_Debug); mGeometry->setNodeMask(Mask_Debug);
mVertices = new osg::Vec3Array; mVertices = new osg::Vec3Array;
mColors = new osg::Vec4Array; mColors = new osg::Vec4Array;
@ -50,8 +49,10 @@ void DebugDrawer::createGeometry()
mGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); mGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
mGeometry->setDataVariance(osg::Object::DYNAMIC); mGeometry->setDataVariance(osg::Object::DYNAMIC);
mGeometry->addPrimitiveSet(mDrawArrays); mGeometry->addPrimitiveSet(mDrawArrays);
// make this friendly to recreateShaders()
mGeometry->setStateSet(new osg::StateSet); osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
mGeometry->getOrCreateStateSet()->setAttribute(material);
mParentNode->addChild(mGeometry); mParentNode->addChild(mGeometry);

View File

@ -325,7 +325,7 @@ namespace MWRender
if (texture) if (texture)
{ {
osg::ref_ptr<osg::Geometry> geom = createTexturedQuad(srcLeft, srcTop, srcRight, srcBottom); osg::ref_ptr<osg::Geometry> geom = createTexturedQuad(srcLeft, srcTop, srcRight, srcBottom);
auto depth = SceneUtil::createDepth(getReverseZ()); auto depth = SceneUtil::createDepth();
depth->setWriteMask(0); depth->setWriteMask(0);
osg::StateSet* stateset = geom->getOrCreateStateSet(); osg::StateSet* stateset = geom->getOrCreateStateSet();
stateset->setAttribute(depth); stateset->setAttribute(depth);

View File

@ -178,7 +178,7 @@ osg::ref_ptr<osg::Camera> LocalMap::createOrthographicCamera(float x, float y, f
{ {
osg::ref_ptr<osg::Camera> camera (new osg::Camera); osg::ref_ptr<osg::Camera> camera (new osg::Camera);
if (getReverseZ()) if (SceneUtil::getReverseZ())
camera->setProjectionMatrix(SceneUtil::getReversedZProjectionMatrixAsOrtho(-width/2, width/2, -height/2, height/2, 5, (zmax-zmin) + 10)); camera->setProjectionMatrix(SceneUtil::getReversedZProjectionMatrixAsOrtho(-width/2, width/2, -height/2, height/2, 5, (zmax-zmin) + 10));
else else
camera->setProjectionMatrixAsOrtho(-width/2, width/2, -height/2, height/2, 5, (zmax-zmin) + 10); camera->setProjectionMatrixAsOrtho(-width/2, width/2, -height/2, height/2, 5, (zmax-zmin) + 10);
@ -201,12 +201,10 @@ osg::ref_ptr<osg::Camera> LocalMap::createOrthographicCamera(float x, float y, f
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet; osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
stateset->setAttribute(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL), osg::StateAttribute::OVERRIDE); stateset->setAttribute(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL), osg::StateAttribute::OVERRIDE);
if (getReverseZ()) if (SceneUtil::getReverseZ())
{ stateset->setAttributeAndModes(SceneUtil::createDepth(), osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
camera->setClearDepth(0.0);
auto depth = SceneUtil::createDepth(true); SceneUtil::setCameraClearDepth(camera);
stateset->setAttributeAndModes(depth, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
}
stateset->addUniform(new osg::Uniform("projectionMatrix", static_cast<osg::Matrixf>(camera->getProjectionMatrix())), osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); stateset->addUniform(new osg::Uniform("projectionMatrix", static_cast<osg::Matrixf>(camera->getProjectionMatrix())), osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);

View File

@ -373,7 +373,7 @@ class DepthClearCallback : public osgUtil::RenderBin::DrawCallback
public: public:
DepthClearCallback() DepthClearCallback()
{ {
mDepth = SceneUtil::createDepth(getReverseZ()); mDepth = SceneUtil::createDepth();
mDepth->setWriteMask(true); mDepth->setWriteMask(true);
} }

View File

@ -661,7 +661,6 @@ namespace MWRender
if (mergeGroup->getNumChildren()) if (mergeGroup->getNumChildren())
{ {
SceneUtil::Optimizer optimizer; SceneUtil::Optimizer optimizer;
optimizer.setReverseZ(mSceneManager->getReverseZ());
if (size > 1/8.f) if (size > 1/8.f)
{ {
optimizer.setViewPoint(relativeViewPoint); optimizer.setViewPoint(relativeViewPoint);

View File

@ -36,10 +36,10 @@ namespace
class CullCallback : public osg::NodeCallback class CullCallback : public osg::NodeCallback
{ {
public: public:
CullCallback(MWRender::PostProcessor* postProcessor) CullCallback()
: mPostProcessor(postProcessor) : mLastFrameNumber(0)
, mLastFrameNumber(0) {
{} }
void operator()(osg::Node* node, osg::NodeVisitor* nv) override void operator()(osg::Node* node, osg::NodeVisitor* nv) override
{ {
@ -49,14 +49,24 @@ namespace
if (frame != mLastFrameNumber) if (frame != mLastFrameNumber)
{ {
mLastFrameNumber = frame; mLastFrameNumber = frame;
if (!mPostProcessor->getMsaaFbo())
MWRender::PostProcessor* postProcessor = dynamic_cast<MWRender::PostProcessor*>(nv->asCullVisitor()->getCurrentCamera()->getUserData());
if (!postProcessor)
{ {
renderStage->setFrameBufferObject(mPostProcessor->getFbo()); Log(Debug::Error) << "Failed retrieving user data for master camera: FBO setup failed";
traverse(node, nv);
return;
}
if (!postProcessor->getMsaaFbo())
{
renderStage->setFrameBufferObject(postProcessor->getFbo());
} }
else else
{ {
renderStage->setMultisampleResolveFramebufferObject(mPostProcessor->getFbo()); renderStage->setMultisampleResolveFramebufferObject(postProcessor->getFbo());
renderStage->setFrameBufferObject(mPostProcessor->getMsaaFbo()); renderStage->setFrameBufferObject(postProcessor->getMsaaFbo());
} }
} }
@ -64,7 +74,6 @@ namespace
} }
private: private:
MWRender::PostProcessor* mPostProcessor;
unsigned int mLastFrameNumber; unsigned int mLastFrameNumber;
}; };
@ -136,12 +145,13 @@ namespace MWRender
// We need to manually set the FBO and resolve FBO during the cull callback. If we were using a separate // We need to manually set the FBO and resolve FBO during the cull callback. If we were using a separate
// RTT camera this would not be needed. // RTT camera this would not be needed.
mViewer->getCamera()->addCullCallback(new CullCallback(this)); mViewer->getCamera()->addCullCallback(new CullCallback);
mViewer->getCamera()->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); mViewer->getCamera()->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
mViewer->getCamera()->attach(osg::Camera::COLOR_BUFFER0, mSceneTex); mViewer->getCamera()->attach(osg::Camera::COLOR_BUFFER0, mSceneTex);
mViewer->getCamera()->attach(osg::Camera::DEPTH_BUFFER, mDepthTex); mViewer->getCamera()->attach(osg::Camera::DEPTH_BUFFER, mDepthTex);
mViewer->getCamera()->getGraphicsContext()->setResizedCallback(new ResizedCallback(this)); mViewer->getCamera()->getGraphicsContext()->setResizedCallback(new ResizedCallback(this));
mViewer->getCamera()->setUserData(this);
} }
void PostProcessor::resize(int width, int height, bool init) void PostProcessor::resize(int width, int height, bool init)
@ -157,8 +167,6 @@ namespace MWRender
mFbo->setAttachment(osg::Camera::COLOR_BUFFER0, osg::FrameBufferAttachment(mSceneTex)); mFbo->setAttachment(osg::Camera::COLOR_BUFFER0, osg::FrameBufferAttachment(mSceneTex));
mFbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment(mDepthTex)); mFbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment(mDepthTex));
mViewer->getCamera()->setUserData(mFbo);
// When MSAA is enabled we must first render to a render buffer, then // When MSAA is enabled we must first render to a render buffer, then
// blit the result to the FBO which is either passed to the main frame // blit the result to the FBO which is either passed to the main frame
// buffer for display or used as the entry point for a post process chain. // buffer for display or used as the entry point for a post process chain.

View File

@ -1,16 +1,12 @@
#ifndef OPENMW_MWRENDER_POSTPROCESSOR_H #ifndef OPENMW_MWRENDER_POSTPROCESSOR_H
#define OPENMW_MWRENDER_POSTPROCESSOR_H #define OPENMW_MWRENDER_POSTPROCESSOR_H
#include <osg/Texture2D>
#include <osg/Group>
#include <osg/FrameBufferObject>
#include <osg/Camera>
#include <osg/ref_ptr> #include <osg/ref_ptr>
namespace osg
{
class Texture2D;
class Group;
class FrameBufferObject;
class Camera;
}
namespace osgViewer namespace osgViewer
{ {
class Viewer; class Viewer;
@ -20,7 +16,7 @@ namespace MWRender
{ {
class RenderingManager; class RenderingManager;
class PostProcessor class PostProcessor : public osg::Referenced
{ {
public: public:
PostProcessor(RenderingManager& rendering, osgViewer::Viewer* viewer, osg::Group* rootNode); PostProcessor(RenderingManager& rendering, osgViewer::Viewer* viewer, osg::Group* rootNode);
@ -33,6 +29,8 @@ namespace MWRender
void resize(int width, int height, bool init=false); void resize(int width, int height, bool init=false);
private: private:
void createTexturesAndCamera(int width, int height);
osgViewer::Viewer* mViewer; osgViewer::Viewer* mViewer;
osg::ref_ptr<osg::Group> mRootNode; osg::ref_ptr<osg::Group> mRootNode;
osg::ref_ptr<osg::Camera> mHUDCamera; osg::ref_ptr<osg::Camera> mHUDCamera;
@ -45,8 +43,6 @@ namespace MWRender
int mDepthFormat; int mDepthFormat;
void createTexturesAndCamera(int width, int height);
RenderingManager& mRendering; RenderingManager& mRendering;
}; };
} }

View File

@ -79,6 +79,8 @@ namespace MWRender
public: public:
SharedUniformStateUpdater() SharedUniformStateUpdater()
: mLinearFac(0.f) : mLinearFac(0.f)
, mNear(0.f)
, mFar(0.f)
{ {
} }
@ -86,6 +88,8 @@ namespace MWRender
{ {
stateset->addUniform(new osg::Uniform("projectionMatrix", osg::Matrixf{})); stateset->addUniform(new osg::Uniform("projectionMatrix", osg::Matrixf{}));
stateset->addUniform(new osg::Uniform("linearFac", 0.f)); stateset->addUniform(new osg::Uniform("linearFac", 0.f));
stateset->addUniform(new osg::Uniform("near", 0.f));
stateset->addUniform(new osg::Uniform("far", 0.f));
} }
void apply(osg::StateSet* stateset, osg::NodeVisitor* nv) override void apply(osg::StateSet* stateset, osg::NodeVisitor* nv) override
@ -97,6 +101,15 @@ namespace MWRender
auto* uLinearFac = stateset->getUniform("linearFac"); auto* uLinearFac = stateset->getUniform("linearFac");
if (uLinearFac) if (uLinearFac)
uLinearFac->set(mLinearFac); uLinearFac->set(mLinearFac);
auto* uNear = stateset->getUniform("near");
if (uNear)
uNear->set(mNear);
auto* uFar = stateset->getUniform("far");
if (uFar)
uFar->set(mFar);
} }
void setProjectionMatrix(const osg::Matrixf& projectionMatrix) void setProjectionMatrix(const osg::Matrixf& projectionMatrix)
@ -109,9 +122,21 @@ namespace MWRender
mLinearFac = linearFac; mLinearFac = linearFac;
} }
void setNear(float near)
{
mNear = near;
}
void setFar(float far)
{
mFar = far;
}
private: private:
osg::Matrixf mProjectionMatrix; osg::Matrixf mProjectionMatrix;
float mLinearFac; float mLinearFac;
float mNear;
float mFar;
}; };
class StateUpdater : public SceneUtil::StateSetUpdater class StateUpdater : public SceneUtil::StateSetUpdater
@ -240,8 +265,7 @@ namespace MWRender
, mFieldOfViewOverridden(false) , mFieldOfViewOverridden(false)
, mFieldOfViewOverride(0.f) , mFieldOfViewOverride(0.f)
{ {
auto ext = osg::GLExtensions::Get(0, false); bool reverseZ = SceneUtil::getReverseZ();
bool reverseZ = Settings::Manager::getBool("reverse z", "Camera") && ext && ext->isClipControlSupported;
if (reverseZ) if (reverseZ)
Log(Debug::Info) << "Using reverse-z depth buffer"; Log(Debug::Info) << "Using reverse-z depth buffer";
@ -267,7 +291,6 @@ namespace MWRender
resourceSystem->getSceneManager()->setSpecularMapPattern(Settings::Manager::getString("specular map pattern", "Shaders")); resourceSystem->getSceneManager()->setSpecularMapPattern(Settings::Manager::getString("specular map pattern", "Shaders"));
resourceSystem->getSceneManager()->setApplyLightingToEnvMaps(Settings::Manager::getBool("apply lighting to environment maps", "Shaders")); resourceSystem->getSceneManager()->setApplyLightingToEnvMaps(Settings::Manager::getBool("apply lighting to environment maps", "Shaders"));
resourceSystem->getSceneManager()->setConvertAlphaTestToAlphaToCoverage(Settings::Manager::getBool("antialias alpha test", "Shaders") && Settings::Manager::getInt("antialiasing", "Video") > 1); resourceSystem->getSceneManager()->setConvertAlphaTestToAlphaToCoverage(Settings::Manager::getBool("antialias alpha test", "Shaders") && Settings::Manager::getInt("antialiasing", "Video") > 1);
resourceSystem->getSceneManager()->setReverseZ(reverseZ);
// Let LightManager choose which backend to use based on our hint. For methods besides legacy lighting, this depends on support for various OpenGL extensions. // Let LightManager choose which backend to use based on our hint. For methods besides legacy lighting, this depends on support for various OpenGL extensions.
osg::ref_ptr<SceneUtil::LightManager> sceneRoot = new SceneUtil::LightManager(lightingMethod == SceneUtil::LightingMethod::FFP); osg::ref_ptr<SceneUtil::LightManager> sceneRoot = new SceneUtil::LightManager(lightingMethod == SceneUtil::LightingMethod::FFP);
@ -294,7 +317,7 @@ namespace MWRender
if (Settings::Manager::getBool("object shadows", "Shadows")) if (Settings::Manager::getBool("object shadows", "Shadows"))
shadowCastingTraversalMask |= (Mask_Object|Mask_Static); shadowCastingTraversalMask |= (Mask_Object|Mask_Static);
mShadowManager.reset(new SceneUtil::ShadowManager(sceneRoot, mRootNode, shadowCastingTraversalMask, indoorShadowCastingTraversalMask, mResourceSystem->getSceneManager()->getShaderManager(), reverseZ)); mShadowManager.reset(new SceneUtil::ShadowManager(sceneRoot, mRootNode, shadowCastingTraversalMask, indoorShadowCastingTraversalMask, mResourceSystem->getSceneManager()->getShaderManager()));
Shader::ShaderManager::DefineMap shadowDefines = mShadowManager->getShadowDefines(); Shader::ShaderManager::DefineMap shadowDefines = mShadowManager->getShadowDefines();
Shader::ShaderManager::DefineMap lightDefines = sceneRoot->getLightDefines(); Shader::ShaderManager::DefineMap lightDefines = sceneRoot->getLightDefines();
@ -408,9 +431,12 @@ namespace MWRender
mGroundcoverWorld->setActiveGrid(osg::Vec4i(0, 0, 0, 0)); mGroundcoverWorld->setActiveGrid(osg::Vec4i(0, 0, 0, 0));
} }
mPostProcessor.reset(new PostProcessor(*this, viewer, mRootNode)); mPostProcessor = new PostProcessor(*this, viewer, mRootNode);
resourceSystem->getSceneManager()->setDepthFormat(mPostProcessor->getDepthFormat()); resourceSystem->getSceneManager()->setDepthFormat(mPostProcessor->getDepthFormat());
if (reverseZ && !SceneUtil::isFloatingPointDepthFormat(mPostProcessor->getDepthFormat()))
Log(Debug::Warning) << "Floating point depth format not in use but reverse-z buffer is enabled, consider disabling it.";
// water goes after terrain for correct waterculling order // water goes after terrain for correct waterculling order
mWater.reset(new Water(sceneRoot->getParent(0), sceneRoot, mResourceSystem, mViewer->getIncrementalCompileOperation(), resourcePath)); mWater.reset(new Water(sceneRoot->getParent(0), sceneRoot, mResourceSystem, mViewer->getIncrementalCompileOperation(), resourcePath));
@ -474,7 +500,6 @@ namespace MWRender
mViewer->getCamera()->setCullMask(~(Mask_UpdateVisitor|Mask_SimpleWater)); mViewer->getCamera()->setCullMask(~(Mask_UpdateVisitor|Mask_SimpleWater));
NifOsg::Loader::setHiddenNodeMask(Mask_UpdateVisitor); NifOsg::Loader::setHiddenNodeMask(Mask_UpdateVisitor);
NifOsg::Loader::setIntersectionDisabledNodeMask(Mask_Effect); NifOsg::Loader::setIntersectionDisabledNodeMask(Mask_Effect);
NifOsg::Loader::setReverseZ(reverseZ);
Nif::NIFFile::setLoadUnsupportedFiles(Settings::Manager::getBool("load unsupported nif files", "Models")); Nif::NIFFile::setLoadUnsupportedFiles(Settings::Manager::getBool("load unsupported nif files", "Models"));
// TODO: Near clip should not need to be bounded like this, but too small values break OSG shadow calculations CPU-side. // TODO: Near clip should not need to be bounded like this, but too small values break OSG shadow calculations CPU-side.
@ -487,8 +512,6 @@ namespace MWRender
mFirstPersonFieldOfView = std::min(std::max(1.f, firstPersonFov), 179.f); mFirstPersonFieldOfView = std::min(std::max(1.f, firstPersonFov), 179.f);
mStateUpdater->setFogEnd(mViewDistance); mStateUpdater->setFogEnd(mViewDistance);
mRootNode->getOrCreateStateSet()->addUniform(new osg::Uniform("near", mNearClip));
mRootNode->getOrCreateStateSet()->addUniform(new osg::Uniform("far", mViewDistance));
mRootNode->getOrCreateStateSet()->addUniform(new osg::Uniform("simpleWater", false)); mRootNode->getOrCreateStateSet()->addUniform(new osg::Uniform("simpleWater", false));
// Hopefully, anything genuinely requiring the default alpha func of GL_ALWAYS explicitly sets it // Hopefully, anything genuinely requiring the default alpha func of GL_ALWAYS explicitly sets it
@ -496,18 +519,15 @@ namespace MWRender
// The transparent renderbin sets alpha testing on because that was faster on old GPUs. It's now slower and breaks things. // The transparent renderbin sets alpha testing on because that was faster on old GPUs. It's now slower and breaks things.
mRootNode->getOrCreateStateSet()->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF); mRootNode->getOrCreateStateSet()->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
mUniformNear = mRootNode->getOrCreateStateSet()->getUniform("near");
mUniformFar = mRootNode->getOrCreateStateSet()->getUniform("far");
if (reverseZ) if (reverseZ)
{ {
auto depth = SceneUtil::createDepth(reverseZ);
osg::ref_ptr<osg::ClipControl> clipcontrol = new osg::ClipControl(osg::ClipControl::LOWER_LEFT, osg::ClipControl::ZERO_TO_ONE); osg::ref_ptr<osg::ClipControl> clipcontrol = new osg::ClipControl(osg::ClipControl::LOWER_LEFT, osg::ClipControl::ZERO_TO_ONE);
mViewer->getCamera()->setClearDepth(0.0); mRootNode->getOrCreateStateSet()->setAttributeAndModes(SceneUtil::createDepth(), osg::StateAttribute::ON);
mRootNode->getOrCreateStateSet()->setAttributeAndModes(depth, osg::StateAttribute::ON);
mRootNode->getOrCreateStateSet()->setAttributeAndModes(clipcontrol, osg::StateAttribute::ON); mRootNode->getOrCreateStateSet()->setAttributeAndModes(clipcontrol, osg::StateAttribute::ON);
} }
SceneUtil::setCameraClearDepth(mViewer->getCamera());
updateProjectionMatrix(); updateProjectionMatrix();
} }
@ -1143,7 +1163,7 @@ namespace MWRender
mViewer->getCamera()->setProjectionMatrixAsPerspective(fov, aspect, mNearClip, mViewDistance); mViewer->getCamera()->setProjectionMatrixAsPerspective(fov, aspect, mNearClip, mViewDistance);
if (mResourceSystem->getSceneManager()->getReverseZ()) if (SceneUtil::getReverseZ())
{ {
mSharedUniformStateUpdater->setLinearFac(-mNearClip / (mViewDistance - mNearClip) - 1.f); mSharedUniformStateUpdater->setLinearFac(-mNearClip / (mViewDistance - mNearClip) - 1.f);
mSharedUniformStateUpdater->setProjectionMatrix(SceneUtil::getReversedZProjectionMatrixAsPerspective(fov, aspect, mNearClip, mViewDistance)); mSharedUniformStateUpdater->setProjectionMatrix(SceneUtil::getReversedZProjectionMatrixAsPerspective(fov, aspect, mNearClip, mViewDistance));
@ -1151,8 +1171,8 @@ namespace MWRender
else else
mSharedUniformStateUpdater->setProjectionMatrix(mViewer->getCamera()->getProjectionMatrix()); mSharedUniformStateUpdater->setProjectionMatrix(mViewer->getCamera()->getProjectionMatrix());
mUniformNear->set(mNearClip); mSharedUniformStateUpdater->setNear(mNearClip);
mUniformFar->set(mViewDistance); mSharedUniformStateUpdater->setFar(mViewDistance);
// Since our fog is not radial yet, we should take FOV in account, otherwise terrain near viewing distance may disappear. // Since our fog is not radial yet, we should take FOV in account, otherwise terrain near viewing distance may disappear.
// Limit FOV here just for sure, otherwise viewing distance can be too high. // Limit FOV here just for sure, otherwise viewing distance can be too high.

View File

@ -110,9 +110,6 @@ namespace MWRender
SceneUtil::UnrefQueue* getUnrefQueue(); SceneUtil::UnrefQueue* getUnrefQueue();
Terrain::World* getTerrain(); Terrain::World* getTerrain();
osg::Uniform* mUniformNear;
osg::Uniform* mUniformFar;
void preloadCommonAssets(); void preloadCommonAssets();
double getReferenceTime() const; double getReferenceTime() const;
@ -243,6 +240,7 @@ namespace MWRender
void getPagedRefnums(const osg::Vec4i &activeGrid, std::set<ESM::RefNum> &out); void getPagedRefnums(const osg::Vec4i &activeGrid, std::set<ESM::RefNum> &out);
void updateProjectionMatrix(); void updateProjectionMatrix();
private: private:
void updateTextureFiltering(); void updateTextureFiltering();
void updateAmbient(); void updateAmbient();
@ -289,7 +287,7 @@ namespace MWRender
std::unique_ptr<ScreenshotManager> mScreenshotManager; std::unique_ptr<ScreenshotManager> mScreenshotManager;
std::unique_ptr<EffectManager> mEffectManager; std::unique_ptr<EffectManager> mEffectManager;
std::unique_ptr<SceneUtil::ShadowManager> mShadowManager; std::unique_ptr<SceneUtil::ShadowManager> mShadowManager;
std::unique_ptr<PostProcessor> mPostProcessor; osg::ref_ptr<PostProcessor> mPostProcessor;
osg::ref_ptr<NpcAnimation> mPlayerAnimation; osg::ref_ptr<NpcAnimation> mPlayerAnimation;
osg::ref_ptr<SceneUtil::PositionAttitudeTransform> mPlayerNode; osg::ref_ptr<SceneUtil::PositionAttitudeTransform> mPlayerNode;
std::unique_ptr<Camera> mCamera; std::unique_ptr<Camera> mCamera;

View File

@ -56,13 +56,13 @@ namespace
stateset->setMode(GL_CULL_FACE, osg::StateAttribute::OFF); stateset->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
stateset->setTextureAttributeAndModes(0, textures[0], osg::StateAttribute::ON); stateset->setTextureAttributeAndModes(0, textures[0], osg::StateAttribute::ON);
auto depth = SceneUtil::createDepth(resourceSystem->getSceneManager()->getReverseZ()); auto depth = SceneUtil::createDepth();
depth->setWriteMask(false); depth->setWriteMask(false);
stateset->setAttributeAndModes(depth, osg::StateAttribute::ON); stateset->setAttributeAndModes(depth, osg::StateAttribute::ON);
osg::ref_ptr<osg::PolygonOffset> polygonOffset (new osg::PolygonOffset); osg::ref_ptr<osg::PolygonOffset> polygonOffset (new osg::PolygonOffset);
polygonOffset->setUnits(-1); polygonOffset->setUnits(SceneUtil::getReverseZ() ? 1 : -1);
polygonOffset->setFactor(-1); polygonOffset->setFactor(SceneUtil::getReverseZ() ? 1 : -1);
stateset->setAttributeAndModes(polygonOffset, osg::StateAttribute::ON); stateset->setAttributeAndModes(polygonOffset, osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

View File

@ -23,6 +23,7 @@
#include "util.hpp" #include "util.hpp"
#include "vismask.hpp" #include "vismask.hpp"
#include "water.hpp" #include "water.hpp"
#include "postprocessor.hpp"
namespace MWRender namespace MWRender
{ {
@ -92,14 +93,13 @@ namespace MWRender
// Ensure we are reading from the resolved framebuffer and not the multisampled render buffer when in use. // Ensure we are reading from the resolved framebuffer and not the multisampled render buffer when in use.
// glReadPixel() cannot read from multisampled targets. // glReadPixel() cannot read from multisampled targets.
PostProcessor* postProcessor = dynamic_cast<PostProcessor*>(renderInfo.getCurrentCamera()->getUserData());
osg::FrameBufferObject* fbo = dynamic_cast<osg::FrameBufferObject*>(renderInfo.getCurrentCamera()->getUserData()); if (postProcessor && postProcessor->getFbo() && postProcessor->getMsaaFbo())
if (fbo)
{ {
osg::GLExtensions* ext = osg::GLExtensions::Get(renderInfo.getContextID(), false); osg::GLExtensions* ext = osg::GLExtensions::Get(renderInfo.getContextID(), false);
if (ext) if (ext)
ext->glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo->getHandle(renderInfo.getContextID())); ext->glBindFramebuffer(GL_FRAMEBUFFER_EXT, postProcessor->getFbo()->getHandle(renderInfo.getContextID()));
} }
mImage->readPixels(leftPadding, topPadding, width, height, GL_RGB, GL_UNSIGNED_BYTE); mImage->readPixels(leftPadding, topPadding, width, height, GL_RGB, GL_UNSIGNED_BYTE);
@ -297,12 +297,10 @@ namespace MWRender
camera->setRenderOrder(osg::Camera::PRE_RENDER); camera->setRenderOrder(osg::Camera::PRE_RENDER);
camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT,osg::Camera::PIXEL_BUFFER_RTT); camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT,osg::Camera::PIXEL_BUFFER_RTT);
if (getReverseZ())
camera->setClearDepth(0.0);
camera->setViewport(0, 0, w, h); camera->setViewport(0, 0, w, h);
SceneUtil::setCameraClearDepth(camera);
osg::ref_ptr<osg::Texture2D> texture (new osg::Texture2D); osg::ref_ptr<osg::Texture2D> texture (new osg::Texture2D);
texture->setInternalFormat(GL_RGB); texture->setInternalFormat(GL_RGB);
texture->setTextureSize(w,h); texture->setTextureSize(w,h);
@ -336,7 +334,7 @@ namespace MWRender
float nearClip = Settings::Manager::getFloat("near clip", "Camera"); float nearClip = Settings::Manager::getFloat("near clip", "Camera");
float viewDistance = Settings::Manager::getFloat("viewing distance", "Camera"); float viewDistance = Settings::Manager::getFloat("viewing distance", "Camera");
// each cubemap side sees 90 degrees // each cubemap side sees 90 degrees
if (getReverseZ()) if (SceneUtil::getReverseZ())
rttCamera->setProjectionMatrix(SceneUtil::getReversedZProjectionMatrixAsPerspectiveInf(90.0, w/float(h), nearClip)); rttCamera->setProjectionMatrix(SceneUtil::getReversedZProjectionMatrixAsPerspectiveInf(90.0, w/float(h), nearClip));
else else
rttCamera->setProjectionMatrixAsPerspective(90.0, w/float(h), nearClip, viewDistance); rttCamera->setProjectionMatrixAsPerspective(90.0, w/float(h), nearClip, viewDistance);

View File

@ -595,11 +595,11 @@ private:
osg::StateSet* queryStateSet = new osg::StateSet; osg::StateSet* queryStateSet = new osg::StateSet;
if (queryVisible) if (queryVisible)
{ {
auto depth = SceneUtil::createDepth(getReverseZ()); auto depth = SceneUtil::createDepth();
// This is a trick to make fragments written by the query always use the maximum depth value, // This is a trick to make fragments written by the query always use the maximum depth value,
// without having to retrieve the current far clipping distance. // without having to retrieve the current far clipping distance.
// We want the sun glare to be "infinitely" far away. // We want the sun glare to be "infinitely" far away.
double far = getReverseZ() ? 0.0 : 1.0; double far = SceneUtil::getReverseZ() ? 0.0 : 1.0;
depth->setZNear(far); depth->setZNear(far);
depth->setZFar(far); depth->setZFar(far);
depth->setWriteMask(false); depth->setWriteMask(false);
@ -1210,7 +1210,7 @@ void SkyManager::create()
mCloudMesh2->addUpdateCallback(mCloudUpdater2); mCloudMesh2->addUpdateCallback(mCloudUpdater2);
mCloudMesh2->setNodeMask(0); mCloudMesh2->setNodeMask(0);
auto depth = SceneUtil::createDepth(mSceneManager->getReverseZ()); auto depth = SceneUtil::createDepth();
depth->setWriteMask(false); depth->setWriteMask(false);
mEarlyRenderBinRoot->getOrCreateStateSet()->setAttributeAndModes(depth, osg::StateAttribute::ON); mEarlyRenderBinRoot->getOrCreateStateSet()->setAttributeAndModes(depth, osg::StateAttribute::ON);
mEarlyRenderBinRoot->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); mEarlyRenderBinRoot->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);

View File

@ -4,13 +4,10 @@
#include <osg/ValueObject> #include <osg/ValueObject>
#include <components/resource/resourcesystem.hpp> #include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/resource/imagemanager.hpp> #include <components/resource/imagemanager.hpp>
#include <components/misc/resourcehelpers.hpp> #include <components/misc/resourcehelpers.hpp>
#include <components/sceneutil/visitor.hpp> #include <components/sceneutil/visitor.hpp>
#include "../mwbase/environment.hpp"
namespace MWRender namespace MWRender
{ {
@ -67,9 +64,4 @@ void overrideTexture(const std::string &texture, Resource::ResourceSystem *resou
node->setStateSet(stateset); node->setStateSet(stateset);
} }
bool getReverseZ()
{
return MWBase::Environment::get().getResourceSystem()->getSceneManager()->getReverseZ();
}
} }

View File

@ -32,8 +32,6 @@ namespace MWRender
// no traverse() // no traverse()
} }
}; };
bool getReverseZ();
} }
#endif #endif

View File

@ -273,7 +273,7 @@ public:
void setDefaults(osg::Camera* camera) override void setDefaults(osg::Camera* camera) override
{ {
camera->setClearDepth(1.0); SceneUtil::setCameraClearDepth(camera);
camera->setReferenceFrame(osg::Camera::RELATIVE_RF); camera->setReferenceFrame(osg::Camera::RELATIVE_RF);
camera->setSmallFeatureCullingPixelSize(Settings::Manager::getInt("small feature culling pixel size", "Water")); camera->setSmallFeatureCullingPixelSize(Settings::Manager::getInt("small feature culling pixel size", "Water"));
camera->setName("RefractionCamera"); camera->setName("RefractionCamera");
@ -339,7 +339,7 @@ public:
void setDefaults(osg::Camera* camera) override void setDefaults(osg::Camera* camera) override
{ {
camera->setClearDepth(1.0); SceneUtil::setCameraClearDepth(camera);
camera->setReferenceFrame(osg::Camera::RELATIVE_RF); camera->setReferenceFrame(osg::Camera::RELATIVE_RF);
camera->setSmallFeatureCullingPixelSize(Settings::Manager::getInt("small feature culling pixel size", "Water")); camera->setSmallFeatureCullingPixelSize(Settings::Manager::getInt("small feature culling pixel size", "Water"));
camera->setName("ReflectionCamera"); camera->setName("ReflectionCamera");
@ -546,7 +546,7 @@ osg::Node* Water::getRefractionNode()
void Water::createSimpleWaterStateSet(osg::Node* node, float alpha) void Water::createSimpleWaterStateSet(osg::Node* node, float alpha)
{ {
osg::ref_ptr<osg::StateSet> stateset = SceneUtil::createSimpleWaterStateSet(alpha, MWRender::RenderBin_Water, mResourceSystem->getSceneManager()->getReverseZ()); osg::ref_ptr<osg::StateSet> stateset = SceneUtil::createSimpleWaterStateSet(alpha, MWRender::RenderBin_Water);
node->setStateSet(stateset); node->setStateSet(stateset);
node->setUpdateCallback(nullptr); node->setUpdateCallback(nullptr);
@ -616,7 +616,7 @@ public:
{ {
stateset->setMode(GL_BLEND, osg::StateAttribute::ON); stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setRenderBinDetails(MWRender::RenderBin_Water, "RenderBin"); stateset->setRenderBinDetails(MWRender::RenderBin_Water, "RenderBin");
osg::ref_ptr<osg::Depth> depth(new osg::Depth); osg::ref_ptr<osg::Depth> depth = SceneUtil::createDepth();
depth->setWriteMask(false); depth->setWriteMask(false);
stateset->setAttributeAndModes(depth, osg::StateAttribute::ON); stateset->setAttributeAndModes(depth, osg::StateAttribute::ON);
} }

View File

@ -226,18 +226,6 @@ namespace NifOsg
return sIntersectionDisabledNodeMask; return sIntersectionDisabledNodeMask;
} }
bool Loader::sReverseZ = false;
void Loader::setReverseZ(bool reverseZ)
{
sReverseZ = reverseZ;
}
bool Loader::getReverseZ()
{
return sReverseZ;
}
class LoaderImpl class LoaderImpl
{ {
public: public:
@ -1835,7 +1823,7 @@ namespace NifOsg
// Depth test flag // Depth test flag
stateset->setMode(GL_DEPTH_TEST, zprop->flags&1 ? osg::StateAttribute::ON stateset->setMode(GL_DEPTH_TEST, zprop->flags&1 ? osg::StateAttribute::ON
: osg::StateAttribute::OFF); : osg::StateAttribute::OFF);
auto depth = SceneUtil::createDepth(Loader::getReverseZ()); auto depth = SceneUtil::createDepth();
// Depth write flag // Depth write flag
depth->setWriteMask((zprop->flags>>1)&1); depth->setWriteMask((zprop->flags>>1)&1);
// Morrowind ignores depth test function // Morrowind ignores depth test function

View File

@ -51,14 +51,10 @@ namespace NifOsg
static void setIntersectionDisabledNodeMask(unsigned int mask); static void setIntersectionDisabledNodeMask(unsigned int mask);
static unsigned int getIntersectionDisabledNodeMask(); static unsigned int getIntersectionDisabledNodeMask();
static void setReverseZ(bool reverseZ);
static bool getReverseZ();
private: private:
static unsigned int sHiddenNodeMask; static unsigned int sHiddenNodeMask;
static unsigned int sIntersectionDisabledNodeMask; static unsigned int sIntersectionDisabledNodeMask;
static bool sShowMarkers; static bool sShowMarkers;
static bool sReverseZ;
}; };
} }

View File

@ -275,16 +275,6 @@ namespace Resource
return mClampLighting; return mClampLighting;
} }
void SceneManager::setReverseZ(bool reverseZ)
{
mReverseZ = reverseZ;
}
bool SceneManager::getReverseZ() const
{
return mReverseZ;
}
void SceneManager::setDepthFormat(GLenum format) void SceneManager::setDepthFormat(GLenum format)
{ {
mDepthFormat = format; mDepthFormat = format;

View File

@ -92,9 +92,6 @@ namespace Resource
void setClampLighting(bool clamp); void setClampLighting(bool clamp);
bool getClampLighting() const; bool getClampLighting() const;
void setReverseZ(bool reverseZ);
bool getReverseZ() const;
void setDepthFormat(GLenum format); void setDepthFormat(GLenum format);
GLenum getDepthFormat() const; GLenum getDepthFormat() const;
@ -208,7 +205,6 @@ namespace Resource
SceneUtil::LightingMethod mLightingMethod; SceneUtil::LightingMethod mLightingMethod;
SceneUtil::LightManager::SupportedMethods mSupportedLightingMethods; SceneUtil::LightManager::SupportedMethods mSupportedLightingMethods;
bool mConvertAlphaTestToAlphaToCoverage; bool mConvertAlphaTestToAlphaToCoverage;
bool mReverseZ;
GLenum mDepthFormat; GLenum mDepthFormat;
osg::ref_ptr<MultiObjectCache> mInstanceCache; osg::ref_ptr<MultiObjectCache> mInstanceCache;

View File

@ -1,6 +1,8 @@
#include "agentpath.hpp" #include "agentpath.hpp"
#include "detourdebugdraw.hpp" #include "detourdebugdraw.hpp"
#include <osg/Material>
#include <components/detournavigator/settings.hpp> #include <components/detournavigator/settings.hpp>
#include <algorithm> #include <algorithm>
@ -65,6 +67,10 @@ namespace SceneUtil
debugDraw.depthMask(true); debugDraw.depthMask(true);
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
group->getOrCreateStateSet()->setAttribute(material);
return group; return group;
} }
} }

View File

@ -26,6 +26,9 @@
#include <osg/ClipControl> #include <osg/ClipControl>
#include <sstream> #include <sstream>
#include <components/sceneutil/util.hpp>
#include "shadowsbin.hpp" #include "shadowsbin.hpp"
namespace { namespace {
@ -891,16 +894,6 @@ void SceneUtil::MWShadowTechnique::disableFrontFaceCulling()
} }
} }
void SceneUtil::MWShadowTechnique::enableReverseZ()
{
_reverseZ = true;
}
void SceneUtil::MWShadowTechnique::disableReverseZ()
{
_reverseZ = false;
}
void SceneUtil::MWShadowTechnique::setupCastingShader(Shader::ShaderManager & shaderManager) void SceneUtil::MWShadowTechnique::setupCastingShader(Shader::ShaderManager & shaderManager)
{ {
// This can't be part of the constructor as OSG mandates that there be a trivial constructor available // This can't be part of the constructor as OSG mandates that there be a trivial constructor available
@ -1647,7 +1640,7 @@ void MWShadowTechnique::createShaders()
_shadowCastingStateSet->addUniform(new osg::Uniform("alphaTestShadows", false)); _shadowCastingStateSet->addUniform(new osg::Uniform("alphaTestShadows", false));
osg::ref_ptr<osg::Depth> depth = new osg::Depth; osg::ref_ptr<osg::Depth> depth = new osg::Depth;
depth->setWriteMask(true); depth->setWriteMask(true);
if (_reverseZ) if (SceneUtil::getReverseZ())
{ {
osg::ref_ptr<osg::ClipControl> clipcontrol = new osg::ClipControl(osg::ClipControl::LOWER_LEFT, osg::ClipControl::NEGATIVE_ONE_TO_ONE); osg::ref_ptr<osg::ClipControl> clipcontrol = new osg::ClipControl(osg::ClipControl::LOWER_LEFT, osg::ClipControl::NEGATIVE_ONE_TO_ONE);
_shadowCastingStateSet->setAttribute(clipcontrol, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); _shadowCastingStateSet->setAttribute(clipcontrol, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);

View File

@ -85,10 +85,6 @@ namespace SceneUtil {
virtual void disableFrontFaceCulling(); virtual void disableFrontFaceCulling();
virtual void enableReverseZ();
virtual void disableReverseZ();
virtual void setupCastingShader(Shader::ShaderManager &shaderManager); virtual void setupCastingShader(Shader::ShaderManager &shaderManager);
class ComputeLightSpaceBounds : public osg::NodeVisitor, public osg::CullStack class ComputeLightSpaceBounds : public osg::NodeVisitor, public osg::CullStack
@ -270,8 +266,6 @@ namespace SceneUtil {
float _shadowFadeStart = 0.0; float _shadowFadeStart = 0.0;
bool _reverseZ = false;
class DebugHUD final : public osg::Referenced class DebugHUD final : public osg::Referenced
{ {
public: public:

View File

@ -6,6 +6,7 @@
#include <DetourDebugDraw.h> #include <DetourDebugDraw.h>
#include <osg/Group> #include <osg/Group>
#include <osg/Material>
namespace SceneUtil namespace SceneUtil
{ {
@ -17,6 +18,11 @@ namespace SceneUtil
navMeshQuery.init(&navMesh, settings.mMaxNavMeshQueryNodes); navMeshQuery.init(&navMesh, settings.mMaxNavMeshQueryNodes);
duDebugDrawNavMeshWithClosedList(&debugDraw, navMesh, navMeshQuery, duDebugDrawNavMeshWithClosedList(&debugDraw, navMesh, navMeshQuery,
DU_DRAWNAVMESH_OFFMESHCONS | DU_DRAWNAVMESH_CLOSEDLIST); DU_DRAWNAVMESH_OFFMESHCONS | DU_DRAWNAVMESH_CLOSEDLIST);
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
group->getOrCreateStateSet()->setAttribute(material);
return group; return group;
} }
} }

View File

@ -109,7 +109,6 @@ void Optimizer::optimize(osg::Node* node, unsigned int options)
MergeGeometryVisitor mgv(this); MergeGeometryVisitor mgv(this);
mgv.setTargetMaximumNumberOfVertices(1000000); mgv.setTargetMaximumNumberOfVertices(1000000);
mgv.setMergeAlphaBlending(_mergeAlphaBlending); mgv.setMergeAlphaBlending(_mergeAlphaBlending);
mgv.setReverseZ(_reverseZ);
mgv.setViewPoint(_viewPoint); mgv.setViewPoint(_viewPoint);
node->accept(mgv); node->accept(mgv);
@ -1563,7 +1562,7 @@ bool Optimizer::MergeGeometryVisitor::mergeGroup(osg::Group& group)
} }
if (_alphaBlendingActive && _mergeAlphaBlending && !geom->getStateSet()) if (_alphaBlendingActive && _mergeAlphaBlending && !geom->getStateSet())
{ {
auto d = createDepth(_reverseZ); auto d = createDepth();
d->setWriteMask(0); d->setWriteMask(0);
geom->getOrCreateStateSet()->setAttribute(d); geom->getOrCreateStateSet()->setAttribute(d);
} }

View File

@ -65,7 +65,7 @@ class Optimizer
public: public:
Optimizer() : _mergeAlphaBlending(false), _reverseZ(false) {} Optimizer() : _mergeAlphaBlending(false) {}
virtual ~Optimizer() {} virtual ~Optimizer() {}
enum OptimizationOptions enum OptimizationOptions
@ -119,7 +119,6 @@ class Optimizer
}; };
void setMergeAlphaBlending(bool merge) { _mergeAlphaBlending = merge; } void setMergeAlphaBlending(bool merge) { _mergeAlphaBlending = merge; }
void setReverseZ(bool reverseZ) { _reverseZ = reverseZ; }
void setViewPoint(const osg::Vec3f& viewPoint) { _viewPoint = viewPoint; } void setViewPoint(const osg::Vec3f& viewPoint) { _viewPoint = viewPoint; }
/** Reset internal data to initial state - the getPermissibleOptionsMap is cleared.*/ /** Reset internal data to initial state - the getPermissibleOptionsMap is cleared.*/
@ -258,7 +257,6 @@ class Optimizer
osg::Vec3f _viewPoint; osg::Vec3f _viewPoint;
bool _mergeAlphaBlending; bool _mergeAlphaBlending;
bool _reverseZ;
public: public:
@ -379,18 +377,12 @@ class Optimizer
/// default to traversing all children. /// default to traversing all children.
MergeGeometryVisitor(Optimizer* optimizer=0) : MergeGeometryVisitor(Optimizer* optimizer=0) :
BaseOptimizerVisitor(optimizer, MERGE_GEOMETRY), BaseOptimizerVisitor(optimizer, MERGE_GEOMETRY),
_targetMaximumNumberOfVertices(10000), _alphaBlendingActive(false), _mergeAlphaBlending(false), _reverseZ(false) {} _targetMaximumNumberOfVertices(10000), _alphaBlendingActive(false), _mergeAlphaBlending(false) {}
void setMergeAlphaBlending(bool merge) void setMergeAlphaBlending(bool merge)
{ {
_mergeAlphaBlending = merge; _mergeAlphaBlending = merge;
} }
void setReverseZ(bool reverseZ)
{
_reverseZ = reverseZ;
}
void setViewPoint(const osg::Vec3f& viewPoint) void setViewPoint(const osg::Vec3f& viewPoint)
{ {
_viewPoint = viewPoint; _viewPoint = viewPoint;
@ -429,7 +421,6 @@ class Optimizer
std::vector<osg::StateSet*> _stateSetStack; std::vector<osg::StateSet*> _stateSetStack;
bool _alphaBlendingActive; bool _alphaBlendingActive;
bool _mergeAlphaBlending; bool _mergeAlphaBlending;
bool _reverseZ;
osg::Vec3f _viewPoint; osg::Vec3f _viewPoint;
}; };

View File

@ -1,6 +1,7 @@
#include "pathgridutil.hpp" #include "pathgridutil.hpp"
#include <osg/Geometry> #include <osg/Geometry>
#include <osg/Material>
#include <components/esm/loadpgrd.hpp> #include <components/esm/loadpgrd.hpp>
@ -174,6 +175,11 @@ namespace SceneUtil
gridGeometry->addPrimitiveSet(lineIndices); gridGeometry->addPrimitiveSet(lineIndices);
gridGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); gridGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
} }
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
gridGeometry->getOrCreateStateSet()->setAttribute(material);
return gridGeometry; return gridGeometry;
} }

View File

@ -8,6 +8,7 @@
#include <RecastDebugDraw.h> #include <RecastDebugDraw.h>
#include <osg/Group> #include <osg/Group>
#include <osg/Material>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
@ -65,6 +66,11 @@ namespace SceneUtil
const auto texScale = 1.0f / (settings.mCellSize * 10.0f); const auto texScale = 1.0f / (settings.mCellSize * 10.0f);
duDebugDrawTriMesh(&debugDraw, vertices.data(), static_cast<int>(vertices.size() / 3), duDebugDrawTriMesh(&debugDraw, vertices.data(), static_cast<int>(vertices.size() / 3),
indices.data(), normals.data(), static_cast<int>(indices.size() / 3), nullptr, texScale); indices.data(), normals.data(), static_cast<int>(indices.size() / 3), nullptr, texScale);
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
group->getOrCreateStateSet()->setAttribute(material);
return group; return group;
} }
} }

View File

@ -94,12 +94,10 @@ namespace SceneUtil
} }
} }
ShadowManager::ShadowManager(osg::ref_ptr<osg::Group> sceneRoot, osg::ref_ptr<osg::Group> rootNode, unsigned int outdoorShadowCastingMask, unsigned int indoorShadowCastingMask, Shader::ShaderManager &shaderManager, bool reverseZ) ShadowManager::ShadowManager(osg::ref_ptr<osg::Group> sceneRoot, osg::ref_ptr<osg::Group> rootNode, unsigned int outdoorShadowCastingMask, unsigned int indoorShadowCastingMask, Shader::ShaderManager &shaderManager) : mShadowedScene(new osgShadow::ShadowedScene),
: mReverseZ(reverseZ) mShadowTechnique(new MWShadowTechnique),
, mShadowedScene(new osgShadow::ShadowedScene) mOutdoorShadowCastingMask(outdoorShadowCastingMask),
, mShadowTechnique(new MWShadowTechnique) mIndoorShadowCastingMask(indoorShadowCastingMask)
, mOutdoorShadowCastingMask(outdoorShadowCastingMask)
, mIndoorShadowCastingMask(indoorShadowCastingMask)
{ {
mShadowedScene->setShadowTechnique(mShadowTechnique); mShadowedScene->setShadowTechnique(mShadowTechnique);
@ -110,9 +108,6 @@ namespace SceneUtil
mShadowSettings = mShadowedScene->getShadowSettings(); mShadowSettings = mShadowedScene->getShadowSettings();
setupShadowSettings(); setupShadowSettings();
if (mReverseZ)
mShadowTechnique->enableReverseZ();
mShadowTechnique->setupCastingShader(shaderManager); mShadowTechnique->setupCastingShader(shaderManager);
enableOutdoorMode(); enableOutdoorMode();
@ -185,9 +180,4 @@ namespace SceneUtil
mShadowTechnique->enableShadows(); mShadowTechnique->enableShadows();
mShadowSettings->setCastsShadowTraversalMask(mOutdoorShadowCastingMask); mShadowSettings->setCastsShadowTraversalMask(mOutdoorShadowCastingMask);
} }
bool ShadowManager::getReverseZ() const
{
return mReverseZ;
}
} }

View File

@ -17,7 +17,7 @@ namespace SceneUtil
static Shader::ShaderManager::DefineMap getShadowsDisabledDefines(); static Shader::ShaderManager::DefineMap getShadowsDisabledDefines();
ShadowManager(osg::ref_ptr<osg::Group> sceneRoot, osg::ref_ptr<osg::Group> rootNode, unsigned int outdoorShadowCastingMask, unsigned int indoorShadowCastingMask, Shader::ShaderManager &shaderManager, bool reverseZ); ShadowManager(osg::ref_ptr<osg::Group> sceneRoot, osg::ref_ptr<osg::Group> rootNode, unsigned int outdoorShadowCastingMask, unsigned int indoorShadowCastingMask, Shader::ShaderManager &shaderManager);
void setupShadowSettings(); void setupShadowSettings();
@ -26,11 +26,8 @@ namespace SceneUtil
void enableIndoorMode(); void enableIndoorMode();
void enableOutdoorMode(); void enableOutdoorMode();
bool getReverseZ() const;
protected: protected:
bool mEnableShadows; bool mEnableShadows;
bool mReverseZ;
osg::ref_ptr<osgShadow::ShadowedScene> mShadowedScene; osg::ref_ptr<osgShadow::ShadowedScene> mShadowedScene;
osg::ref_ptr<osgShadow::ShadowSettings> mShadowSettings; osg::ref_ptr<osgShadow::ShadowSettings> mShadowSettings;

View File

@ -4,16 +4,31 @@
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
#include <SDL_opengl_glext.h>
#include <osg/Node> #include <osg/Node>
#include <osg/NodeVisitor> #include <osg/NodeVisitor>
#include <osg/TexGen> #include <osg/TexGen>
#include <osg/TexEnvCombine> #include <osg/TexEnvCombine>
#include <osg/Version> #include <osg/Version>
#include <osg/Fog>
#include <components/resource/imagemanager.hpp> #include <components/resource/imagemanager.hpp>
#include <components/resource/scenemanager.hpp> #include <components/resource/scenemanager.hpp>
#include <components/settings/settings.hpp> #include <components/settings/settings.hpp>
#include <components/debug/debuglog.hpp>
namespace
{
bool isReverseZSupported()
{
if (!Settings::Manager::mDefaultSettings.count({"Camera", "reverse z"}))
return false;
auto ext = osg::GLExtensions::Get(0, false);
return Settings::Manager::getBool("reverse z", "Camera") && ext && ext->isClipControlSupported;
}
}
namespace SceneUtil namespace SceneUtil
{ {
@ -151,7 +166,12 @@ void GlowUpdater::setDuration(float duration)
mDuration = duration; mDuration = duration;
} }
AttachMultisampledDepthColorCallback::AttachMultisampledDepthColorCallback(const osg::ref_ptr<osg::Texture2D>& colorTex, const osg::ref_ptr<osg::Texture2D>& depthTex, int samples, int colorSamples) // Allows camera to render to a color and floating point depth texture with a multisampled framebuffer.
// Must be set on a camera's cull callback.
class AttachMultisampledDepthColorCallback : public osg::NodeCallback
{
public:
AttachMultisampledDepthColorCallback(osg::Texture2D* colorTex, osg::Texture2D* depthTex, int samples, int colorSamples)
{ {
int width = colorTex->getTextureWidth(); int width = colorTex->getTextureWidth();
int height = colorTex->getTextureHeight(); int height = colorTex->getTextureHeight();
@ -168,7 +188,7 @@ AttachMultisampledDepthColorCallback::AttachMultisampledDepthColorCallback(const
mFbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment(depthTex)); mFbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment(depthTex));
} }
void AttachMultisampledDepthColorCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) void operator()(osg::Node* node, osg::NodeVisitor* nv) override
{ {
osgUtil::RenderStage* renderStage = nv->asCullVisitor()->getCurrentRenderStage(); osgUtil::RenderStage* renderStage = nv->asCullVisitor()->getCurrentRenderStage();
@ -178,6 +198,11 @@ void AttachMultisampledDepthColorCallback::operator()(osg::Node* node, osg::Node
traverse(node, nv); traverse(node, nv);
} }
private:
osg::ref_ptr<osg::FrameBufferObject> mFbo;
osg::ref_ptr<osg::FrameBufferObject> mMsaaFbo;
};
void transformBoundingSphere (const osg::Matrixf& matrix, osg::BoundingSphere& bsphere) void transformBoundingSphere (const osg::Matrixf& matrix, osg::BoundingSphere& bsphere)
{ {
osg::BoundingSphere::vec_type xdash = bsphere._center; osg::BoundingSphere::vec_type xdash = bsphere._center;
@ -313,9 +338,37 @@ bool attachAlphaToCoverageFriendlyFramebufferToCamera(osg::Camera* camera, osg::
return addMSAAIntermediateTarget; return addMSAAIntermediateTarget;
} }
osg::ref_ptr<osg::Depth> createDepth(bool reverseZ) void attachAlphaToCoverageFriendlyDepthColor(osg::Camera* camera, osg::Texture2D* colorTex, osg::Texture2D* depthTex, GLenum depthFormat)
{ {
return new osg::Depth(reverseZ ? osg::Depth::GEQUAL : osg::Depth::LEQUAL); bool addMSAAIntermediateTarget = Settings::Manager::getBool("antialias alpha test", "Shaders") && Settings::Manager::getInt("antialiasing", "Video") > 1;
if (isFloatingPointDepthFormat(depthFormat) && addMSAAIntermediateTarget)
{
camera->attach(osg::Camera::COLOR_BUFFER0, colorTex);
camera->attach(osg::Camera::DEPTH_BUFFER, depthTex);
camera->addCullCallback(new AttachMultisampledDepthColorCallback(colorTex, depthTex, 2, 1));
}
else
{
attachAlphaToCoverageFriendlyFramebufferToCamera(camera, osg::Camera::COLOR_BUFFER, colorTex);
camera->attach(osg::Camera::DEPTH_BUFFER, depthTex);
}
}
bool getReverseZ()
{
static bool reverseZ = isReverseZSupported();
return reverseZ;
}
void setCameraClearDepth(osg::Camera* camera)
{
camera->setClearDepth(getReverseZ() ? 0.0 : 1.0);
}
osg::ref_ptr<osg::Depth> createDepth()
{
return new osg::Depth(getReverseZ() ? osg::Depth::GEQUAL : osg::Depth::LEQUAL);
} }
osg::Matrix getReversedZProjectionMatrixAsPerspectiveInf(double fov, double aspect, double near) osg::Matrix getReversedZProjectionMatrixAsPerspectiveInf(double fov, double aspect, double near)
@ -352,7 +405,14 @@ osg::Matrix getReversedZProjectionMatrixAsOrtho(double left, double right, doubl
bool isFloatingPointDepthFormat(GLenum format) bool isFloatingPointDepthFormat(GLenum format)
{ {
return format == GL_DEPTH_COMPONENT32F || format == GL_DEPTH_COMPONENT32F_NV; constexpr std::array<GLenum, 4> formats = {
GL_DEPTH_COMPONENT32F,
GL_DEPTH_COMPONENT32F_NV,
GL_DEPTH32F_STENCIL8,
GL_DEPTH32F_STENCIL8_NV,
};
return std::find(formats.cbegin(), formats.cend(), format) != formats.cend();
} }
} }

View File

@ -8,7 +8,6 @@
#include <osg/Texture2D> #include <osg/Texture2D>
#include <osg/Vec4f> #include <osg/Vec4f>
#include <osg/Depth> #include <osg/Depth>
#include <osg/FrameBufferObject>
#include <components/resource/resourcesystem.hpp> #include <components/resource/resourcesystem.hpp>
@ -49,22 +48,6 @@ namespace SceneUtil
bool mDone; bool mDone;
}; };
// Allows camera to render to a color and floating point depth texture with a multisampled framebuffer.
// Must be set on a camera's 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<osg::Texture2D>& colorTex, const osg::ref_ptr<osg::Texture2D>& depthTex, int samples, int colorSamples);
void operator()(osg::Node* node, osg::NodeVisitor* nv) override;
private:
osg::ref_ptr<osg::FrameBufferObject> mFbo;
osg::ref_ptr<osg::FrameBufferObject> mMsaaFbo;
};
// Transform a bounding sphere by a matrix // Transform a bounding sphere by a matrix
// based off private code in osg::Transform // based off private code in osg::Transform
// TODO: patch osg to make public // TODO: patch osg to make public
@ -83,9 +66,15 @@ namespace SceneUtil
// Alpha-to-coverage requires a multisampled framebuffer, so we need to set that up for RTTs // Alpha-to-coverage requires a multisampled framebuffer, so we need to set that up for RTTs
bool attachAlphaToCoverageFriendlyFramebufferToCamera(osg::Camera* camera, osg::Camera::BufferComponent buffer, osg::Texture* texture, unsigned int level = 0, unsigned int face = 0, bool mipMapGeneration = false); bool attachAlphaToCoverageFriendlyFramebufferToCamera(osg::Camera* camera, osg::Camera::BufferComponent buffer, osg::Texture* texture, unsigned int level = 0, unsigned int face = 0, bool mipMapGeneration = false);
void attachAlphaToCoverageFriendlyDepthColor(osg::Camera* camera, osg::Texture2D* colorTex, osg::Texture2D* depthTex, GLenum depthFormat);
bool getReverseZ();
void setCameraClearDepth(osg::Camera* camera);
// Returns a suitable depth state attribute dependent on whether a reverse-z // Returns a suitable depth state attribute dependent on whether a reverse-z
// depth buffer is in use. // depth buffer is in use.
osg::ref_ptr<osg::Depth> createDepth(bool reverseZ); osg::ref_ptr<osg::Depth> createDepth();
// Returns a perspective projection matrix for use with a reversed z-buffer // Returns a perspective projection matrix for use with a reversed z-buffer
// and an infinite far plane. This is derived by mapping the default z-range // and an infinite far plane. This is derived by mapping the default z-range

View File

@ -64,7 +64,7 @@ namespace SceneUtil
return waterGeom; return waterGeom;
} }
osg::ref_ptr<osg::StateSet> createSimpleWaterStateSet(float alpha, int renderBin, bool reverseZ) osg::ref_ptr<osg::StateSet> createSimpleWaterStateSet(float alpha, int renderBin)
{ {
osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet); osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet);
@ -78,7 +78,7 @@ namespace SceneUtil
stateset->setMode(GL_BLEND, osg::StateAttribute::ON); stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setMode(GL_CULL_FACE, osg::StateAttribute::OFF); stateset->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
auto depth = createDepth(reverseZ); auto depth = createDepth();
depth->setWriteMask(false); depth->setWriteMask(false);
stateset->setAttributeAndModes(depth, osg::StateAttribute::ON); stateset->setAttributeAndModes(depth, osg::StateAttribute::ON);

View File

@ -13,7 +13,7 @@ namespace SceneUtil
{ {
osg::ref_ptr<osg::Geometry> createWaterGeometry(float size, int segments, float textureRepeats); osg::ref_ptr<osg::Geometry> createWaterGeometry(float size, int segments, float textureRepeats);
osg::ref_ptr<osg::StateSet> createSimpleWaterStateSet(float alpha, int renderBin, bool reverseZ); osg::ref_ptr<osg::StateSet> createSimpleWaterStateSet(float alpha, int renderBin);
} }
#endif #endif

View File

@ -163,7 +163,7 @@ std::vector<osg::ref_ptr<osg::StateSet> > ChunkManager::createPasses(float chunk
float blendmapScale = mStorage->getBlendmapScale(chunkSize); float blendmapScale = mStorage->getBlendmapScale(chunkSize);
return ::Terrain::createPasses(useShaders, mSceneManager, layers, blendmapTextures, blendmapScale, blendmapScale); return ::Terrain::createPasses(useShaders, &mSceneManager->getShaderManager(), layers, blendmapTextures, blendmapScale, blendmapScale);
} }
osg::ref_ptr<osg::Node> ChunkManager::createChunk(float chunkSize, const osg::Vec2f &chunkCenter, unsigned char lod, unsigned int lodFlags, bool compile) osg::ref_ptr<osg::Node> ChunkManager::createChunk(float chunkSize, const osg::Vec2f &chunkCenter, unsigned char lod, unsigned int lodFlags, bool compile)
@ -219,7 +219,7 @@ osg::ref_ptr<osg::Node> ChunkManager::createChunk(float chunkSize, const osg::Ve
layer.mDiffuseMap = compositeMap->mTexture; layer.mDiffuseMap = compositeMap->mTexture;
layer.mParallax = false; layer.mParallax = false;
layer.mSpecular = false; layer.mSpecular = false;
geometry->setPasses(::Terrain::createPasses(mSceneManager->getForceShaders() || !mSceneManager->getClampLighting(), mSceneManager, std::vector<TextureLayer>(1, layer), std::vector<osg::ref_ptr<osg::Texture2D> >(), 1.f, 1.f)); geometry->setPasses(::Terrain::createPasses(mSceneManager->getForceShaders() || !mSceneManager->getClampLighting(), &mSceneManager->getShaderManager(), std::vector<TextureLayer>(1, layer), std::vector<osg::ref_ptr<osg::Texture2D> >(), 1.f, 1.f));
} }
else else
{ {

View File

@ -7,7 +7,6 @@
#include <osg/TexMat> #include <osg/TexMat>
#include <osg/BlendFunc> #include <osg/BlendFunc>
#include <components/resource/scenemanager.hpp>
#include <components/shader/shadermanager.hpp> #include <components/shader/shadermanager.hpp>
#include <components/sceneutil/util.hpp> #include <components/sceneutil/util.hpp>
@ -97,17 +96,17 @@ namespace
class LequalDepth class LequalDepth
{ {
public: public:
static const osg::ref_ptr<osg::Depth>& value(bool reverseZ) static const osg::ref_ptr<osg::Depth>& value()
{ {
static LequalDepth instance(reverseZ); static LequalDepth instance;
return instance.mValue; return instance.mValue;
} }
private: private:
osg::ref_ptr<osg::Depth> mValue; osg::ref_ptr<osg::Depth> mValue;
LequalDepth(bool reverseZ) LequalDepth()
: mValue(SceneUtil::createDepth(reverseZ)) : mValue(SceneUtil::createDepth())
{ {
} }
}; };
@ -171,10 +170,9 @@ namespace
namespace Terrain namespace Terrain
{ {
std::vector<osg::ref_ptr<osg::StateSet> > createPasses(bool useShaders, Resource::SceneManager* sceneManager, const std::vector<TextureLayer> &layers, std::vector<osg::ref_ptr<osg::StateSet> > createPasses(bool useShaders, Shader::ShaderManager* shaderManager, const std::vector<TextureLayer> &layers,
const std::vector<osg::ref_ptr<osg::Texture2D> > &blendmaps, int blendmapScale, float layerTileSize) const std::vector<osg::ref_ptr<osg::Texture2D> > &blendmaps, int blendmapScale, float layerTileSize)
{ {
Shader::ShaderManager* shaderManager = &sceneManager->getShaderManager();
std::vector<osg::ref_ptr<osg::StateSet> > passes; std::vector<osg::ref_ptr<osg::StateSet> > passes;
unsigned int blendmapIndex = 0; unsigned int blendmapIndex = 0;
@ -197,7 +195,7 @@ namespace Terrain
else else
{ {
stateset->setAttributeAndModes(BlendFuncFirst::value(), osg::StateAttribute::ON); stateset->setAttributeAndModes(BlendFuncFirst::value(), osg::StateAttribute::ON);
stateset->setAttributeAndModes(LequalDepth::value(sceneManager->getReverseZ()), osg::StateAttribute::ON); stateset->setAttributeAndModes(LequalDepth::value(), osg::StateAttribute::ON);
} }
} }
@ -240,7 +238,7 @@ namespace Terrain
if (!vertexShader || !fragmentShader) if (!vertexShader || !fragmentShader)
{ {
// Try again without shader. Error already logged by above // Try again without shader. Error already logged by above
return createPasses(false, sceneManager, layers, blendmaps, blendmapScale, layerTileSize); return createPasses(false, shaderManager, layers, blendmaps, blendmapScale, layerTileSize);
} }
stateset->setAttributeAndModes(shaderManager->getProgram(vertexShader, fragmentShader)); stateset->setAttributeAndModes(shaderManager->getProgram(vertexShader, fragmentShader));

View File

@ -10,9 +10,9 @@ namespace osg
class Texture2D; class Texture2D;
} }
namespace Resource namespace Shader
{ {
class SceneManager; class ShaderManager;
} }
namespace Terrain namespace Terrain
@ -26,7 +26,7 @@ namespace Terrain
bool mSpecular; bool mSpecular;
}; };
std::vector<osg::ref_ptr<osg::StateSet> > createPasses(bool useShaders, Resource::SceneManager* sceneManager, std::vector<osg::ref_ptr<osg::StateSet> > createPasses(bool useShaders, Shader::ShaderManager* shaderManager,
const std::vector<TextureLayer>& layers, const std::vector<TextureLayer>& layers,
const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize); const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize);

View File

@ -67,7 +67,7 @@ head bobbing height = 3.0
# Maximum camera roll angle (degrees) # Maximum camera roll angle (degrees)
head bobbing roll = 0.2 head bobbing roll = 0.2
# Reverse the depth range from [0,1] to [1,0]. # Reverse the depth range, reduces z-fighting of distant objects and terrain
reverse z = true reverse z = true
[Cells] [Cells]

View File

@ -1,8 +1,8 @@
#version 120 #version 120
varying vec4 passColor; #include "vertexcolors.glsl"
void main() void main()
{ {
gl_FragData[0] = passColor; gl_FragData[0] = getDiffuseColor();
} }

View File

@ -51,6 +51,8 @@ const float WIND_SPEED = 0.2f;
const vec3 WATER_COLOR = vec3(0.090195, 0.115685, 0.12745); const vec3 WATER_COLOR = vec3(0.090195, 0.115685, 0.12745);
const float WOBBLY_SHORE_FADE_DISTANCE = 6200.0; // fade out wobbly shores to mask precision errors, the effect is almost impossible to see at a distance
// ---------------- rain ripples related stuff --------------------- // ---------------- rain ripples related stuff ---------------------
const float RAIN_RIPPLE_GAPS = 5.0; const float RAIN_RIPPLE_GAPS = 5.0;
@ -270,10 +272,10 @@ void main(void)
vec3 normalShoreRippleRain = texture2D(normalMap,normalCoords(UV, 2.0, 2.7, -1.0*waterTimer, 0.05, 0.1, normal3)).rgb - 0.5 vec3 normalShoreRippleRain = texture2D(normalMap,normalCoords(UV, 2.0, 2.7, -1.0*waterTimer, 0.05, 0.1, normal3)).rgb - 0.5
+ texture2D(normalMap,normalCoords(UV, 2.0, 2.7, waterTimer, 0.04, -0.13, normal4)).rgb - 0.5; + texture2D(normalMap,normalCoords(UV, 2.0, 2.7, waterTimer, 0.04, -0.13, normal4)).rgb - 0.5;
float verticalWaterDepth = realWaterDepth * mix(abs(vVec.z), 1.0, 0.2); // an estimate float verticalWaterDepth = realWaterDepth * mix(abs(vVec.z), 1.0, 0.2); // an estimate
float shoreOffset = verticalWaterDepth - (normal2.r + mix(0, normalShoreRippleRain.r, rainIntensity) + 0.15)*8; float shoreOffset = verticalWaterDepth - (normal2.r + mix(0.0, normalShoreRippleRain.r, rainIntensity) + 0.15)*8.0;
float fuzzFactor = min(1.0, 1000.0/surfaceDepth) * mix(abs(vVec.z), 1, 0.2); float fuzzFactor = min(1.0, 1000.0/surfaceDepth) * mix(abs(vVec.z), 1.0, 0.2);
shoreOffset *= fuzzFactor; shoreOffset *= fuzzFactor;
shoreOffset = clamp(shoreOffset, 0, 1); shoreOffset = clamp(mix(shoreOffset, 1.0, clamp(linearDepth / WOBBLY_SHORE_FADE_DISTANCE, 0.0, 1.0)), 0.0, 1.0);
gl_FragData[0].xyz = mix(rawRefraction, gl_FragData[0].xyz, shoreOffset); gl_FragData[0].xyz = mix(rawRefraction, gl_FragData[0].xyz, shoreOffset);
#else #else
gl_FragData[0].xyz = mix(reflection, waterColor, (1.0-fresnel)*0.5) + specular * sunSpec.xyz + vec3(rainRipple.w) * 0.7; gl_FragData[0].xyz = mix(reflection, waterColor, (1.0-fresnel)*0.5) + specular * sunSpec.xyz + vec3(rainRipple.w) * 0.7;