New water WIP

Changes compared to old (Ogre) water:
- Uses depth-texture readback to handle the underwater fog in the water shader, instead of handling it in the object shader
- Different clipping mechanism (glClipPlane instead of a skewed viewing frustum)
- Fixed bug where the reflection camera would look strange when the viewer was very close to the water surface
- Toned down light scattering, made the waterColor a bit darker at night
- Fixed flipped water normals and strange resulting logic in the shader

Still to do: see comments...
openmw-37
scrawl 9 years ago
parent 843225996c
commit e13eb625d3

@ -513,7 +513,7 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings)
// Create the world
mEnvironment.setWorld( new MWWorld::World (mViewer, rootNode, mResourceSystem.get(),
mFileCollections, mContentFiles, mEncoder, mFallbackMap,
mActivationDistanceOverride, mCellName, mStartupScript));
mActivationDistanceOverride, mCellName, mStartupScript, mResDir.string()));
mEnvironment.getWorld()->setupPlayer();
input->setPlayer(&mEnvironment.getWorld()->getPlayer());

@ -32,6 +32,7 @@
#include "camera.hpp"
#include "rotatecontroller.hpp"
#include "renderbin.hpp"
#include "vismask.hpp"
namespace
{
@ -323,9 +324,9 @@ public:
virtual void drawImplementation(osgUtil::RenderBin* bin, osg::RenderInfo& renderInfo, osgUtil::RenderLeaf*& previous)
{
renderInfo.getState()->applyAttribute(mDepth);
//renderInfo.getState()->applyAttribute(mDepth);
glClear(GL_DEPTH_BUFFER_BIT);
//glClear(GL_DEPTH_BUFFER_BIT);
bin->drawImplementation(renderInfo, previous);
}
@ -441,6 +442,7 @@ void NpcAnimation::updateNpcBase()
}
else
{
mObjectRoot->setNodeMask(Mask_FirstPerson);
if(isWerewolf)
addAnimSource(smodel);
else

@ -122,7 +122,8 @@ namespace MWRender
bool mWireframe;
};
RenderingManager::RenderingManager(osgViewer::Viewer* viewer, osg::ref_ptr<osg::Group> rootNode, Resource::ResourceSystem* resourceSystem, const MWWorld::Fallback* fallback)
RenderingManager::RenderingManager(osgViewer::Viewer* viewer, osg::ref_ptr<osg::Group> rootNode, Resource::ResourceSystem* resourceSystem,
const MWWorld::Fallback* fallback, const std::string& resourcePath)
: mViewer(viewer)
, mRootNode(rootNode)
, mResourceSystem(resourceSystem)
@ -145,7 +146,7 @@ namespace MWRender
mEffectManager.reset(new EffectManager(lightRoot, mResourceSystem));
mWater.reset(new Water(lightRoot, mResourceSystem, mViewer->getIncrementalCompileOperation(), fallback));
mWater.reset(new Water(mRootNode, lightRoot, mResourceSystem, mViewer->getIncrementalCompileOperation(), fallback, resourcePath));
mTerrain.reset(new Terrain::TerrainGrid(lightRoot, mResourceSystem, mViewer->getIncrementalCompileOperation(),
new TerrainStorage(mResourceSystem->getVFS(), false), Mask_Terrain));
@ -197,6 +198,39 @@ namespace MWRender
mFieldOfView = Settings::Manager::getFloat("field of view", "General");
updateProjectionMatrix();
mStateUpdater->setFogEnd(mViewDistance);
/*
osg::Texture2D* texture = new osg::Texture2D;
texture->setSourceFormat(GL_DEPTH_COMPONENT);
texture->setInternalFormat(GL_DEPTH_COMPONENT24_ARB);
texture->setSourceType(GL_UNSIGNED_INT);
mViewer->getCamera()->attach(osg::Camera::DEPTH_BUFFER, texture);
osg::ref_ptr<osg::Camera> camera (new osg::Camera);
camera->setProjectionMatrix(osg::Matrix::identity());
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrix::identity());
camera->setClearMask(0);
camera->setRenderOrder(osg::Camera::NESTED_RENDER);
camera->setAllowEventFocus(false);
osg::ref_ptr<osg::Geode> geode (new osg::Geode);
osg::ref_ptr<osg::Geometry> geom = osg::createTexturedQuadGeometry(osg::Vec3f(-1,-1,0), osg::Vec3f(0.5,0,0), osg::Vec3f(0,0.5,0));
geode->addDrawable(geom);
camera->addChild(geode);
osg::StateSet* stateset = geom->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
stateset->setRenderBinDetails(20, "RenderBin");
stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
mLightRoot->addChild(camera);
*/
}
RenderingManager::~RenderingManager()
@ -260,6 +294,7 @@ namespace MWRender
{
// need to wrap this in a StateUpdater?
mSunLight->setDiffuse(colour);
mSunLight->setSpecular(colour);
}
void RenderingManager::setSunDirection(const osg::Vec3f &direction)

@ -57,7 +57,8 @@ namespace MWRender
class RenderingManager : public MWRender::RenderingInterface
{
public:
RenderingManager(osgViewer::Viewer* viewer, osg::ref_ptr<osg::Group> rootNode, Resource::ResourceSystem* resourceSystem, const MWWorld::Fallback* fallback);
RenderingManager(osgViewer::Viewer* viewer, osg::ref_ptr<osg::Group> rootNode, Resource::ResourceSystem* resourceSystem,
const MWWorld::Fallback* fallback, const std::string& resourcePath);
~RenderingManager();
MWRender::Objects& getObjects();

@ -1,6 +1,9 @@
#include "sky.hpp"
#include <cmath>
#include <iostream>
#include <osg/io_utils>
#include <osg/Transform>
#include <osg/Geode>
@ -250,6 +253,8 @@ public:
// That's not a problem though, children of this node can be culled just fine
// Just make sure you do not place a CameraRelativeTransform deep in the scene graph
setCullingActive(false);
addCullCallback(new CullCallback);
}
CameraRelativeTransform(const CameraRelativeTransform& copy, const osg::CopyOp& copyop)
@ -259,7 +264,7 @@ public:
META_Node(MWRender, CameraRelativeTransform)
virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix, osg::NodeVisitor*) const
virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix, osg::NodeVisitor* nv) const
{
if (_referenceFrame==RELATIVE_RF)
{
@ -277,6 +282,48 @@ public:
{
return osg::BoundingSphere(osg::Vec3f(0,0,0), 0);
}
class CullCallback : public osg::NodeCallback
{
public:
virtual void operator() (osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cv = static_cast<osgUtil::CullVisitor*>(nv);
// XXX have to remove unwanted culling plane of the water reflection camera
// Remove all planes that aren't from the standard frustum
unsigned int numPlanes = 4;
if (cv->getCullingMode() & osg::CullSettings::NEAR_PLANE_CULLING)
++numPlanes;
if (cv->getCullingMode() & osg::CullSettings::FAR_PLANE_CULLING)
++numPlanes;
int mask = 0x1;
int resultMask = cv->getProjectionCullingStack().back().getFrustum().getResultMask();
for (unsigned int i=0; i<cv->getProjectionCullingStack().back().getFrustum().getPlaneList().size(); ++i)
{
if (i >= numPlanes)
{
// turn off this culling plane
resultMask &= (~mask);
}
mask <<= 1;
}
cv->getProjectionCullingStack().back().getFrustum().setResultMask(resultMask);
cv->getCurrentCullingSet().getFrustum().setResultMask(resultMask);
cv->getProjectionCullingStack().back().pushCurrentMask();
cv->getCurrentCullingSet().pushCurrentMask();
traverse(node, nv);
cv->getProjectionCullingStack().back().popCurrentMask();
cv->getCurrentCullingSet().popCurrentMask();
}
};
};
class ModVertexAlphaVisitor : public osg::NodeVisitor
@ -1014,6 +1061,7 @@ SkyManager::SkyManager(osg::Group* parentNode, Resource::SceneManager* sceneMana
, mSunEnabled(true)
{
osg::ref_ptr<CameraRelativeTransform> skyroot (new CameraRelativeTransform);
skyroot->setNodeMask(Mask_Sky);
parentNode->addChild(skyroot);
@ -1021,6 +1069,9 @@ SkyManager::SkyManager(osg::Group* parentNode, Resource::SceneManager* sceneMana
// By default render before the world is rendered
mRootNode->getOrCreateStateSet()->setRenderBinDetails(RenderBin_Sky, "RenderBin");
// Prevent unwanted clipping by water reflection camera's clipping plane
mRootNode->getOrCreateStateSet()->setMode(GL_CLIP_PLANE0, osg::StateAttribute::OFF);
}
void SkyManager::create()

@ -17,16 +17,17 @@ namespace MWRender
Mask_Sky = (1<<5),
Mask_Water = (1<<6),
Mask_Terrain = (1<<7),
Mask_FirstPerson = (1<<8),
// top level masks
Mask_Scene = (1<<8),
Mask_GUI = (1<<9),
Mask_Scene = (1<<9),
Mask_GUI = (1<<10),
// Set on a Geode
Mask_ParticleSystem = (1<<10),
Mask_ParticleSystem = (1<<11),
// Set on cameras within the main scene graph
Mask_RenderToTexture = (1<<11)
Mask_RenderToTexture = (1<<12)
// reserved: (1<<16) for SceneUtil::Mask_Lit
};

@ -2,14 +2,24 @@
#include <iomanip>
#include <osg/io_utils>
#include <osg/Depth>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/PositionAttitudeTransform>
#include <osg/Depth>
#include <osg/ClipNode>
#include <osg/MatrixTransform>
#include <osg/FrontFace>
#include <osg/Shader>
#include <osgDB/ReadFile> // XXX remove
#include <osgUtil/IncrementalCompileOperation>
#include <osgUtil/CullVisitor>
#include <components/resource/resourcesystem.hpp>
#include <components/resource/texturemanager.hpp>
@ -17,6 +27,8 @@
#include <components/nifosg/controller.hpp>
#include <components/sceneutil/controller.hpp>
#include <components/settings/settings.hpp>
#include <components/esm/loadcell.hpp>
#include "vismask.hpp"
@ -69,7 +81,7 @@ namespace
return waterGeom;
}
void createWaterStateSet(Resource::ResourceSystem* resourceSystem, osg::ref_ptr<osg::Node> node)
void createSimpleWaterStateSet(Resource::ResourceSystem* resourceSystem, osg::ref_ptr<osg::Node> node)
{
osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet);
@ -111,8 +123,152 @@ namespace MWRender
// --------------------------------------------------------------------------------------------------------------------------------
Water::Water(osg::Group *parent, Resource::ResourceSystem *resourceSystem, osgUtil::IncrementalCompileOperation *ico, const MWWorld::Fallback* fallback)
/// @brief Allows to cull and clip meshes that are below a plane. Useful for reflection & refraction camera effects.
/// Also handles flipping of the plane when the eye point goes below it.
/// To use, simply create the scene as subgraph of this node, then do setPlane(const osg::Plane& plane);
class ClipCullNode : public osg::Group
{
class PlaneCullCallback : public osg::NodeCallback
{
public:
/// @param cullPlane The culling plane (in world space).
PlaneCullCallback(const osg::Plane* cullPlane)
: osg::NodeCallback()
, mCullPlane(cullPlane)
{
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cv = static_cast<osgUtil::CullVisitor*>(nv);
osg::Polytope::PlaneList origPlaneList = cv->getProjectionCullingStack().back().getFrustum().getPlaneList();
// TODO: offset plane towards the viewer to fix bleeding at the water shore
osg::Plane plane = *mCullPlane;
plane.transform(*cv->getCurrentRenderStage()->getInitialViewMatrix());
osg::Vec3d eyePoint = cv->getEyePoint();
if (mCullPlane->intersect(osg::BoundingSphere(osg::Vec3d(0,0,eyePoint.z()), 0)) > 0)
plane.flip();
cv->getProjectionCullingStack().back().getFrustum().add(plane);
traverse(node, nv);
// undo
cv->getProjectionCullingStack().back().getFrustum().set(origPlaneList);
}
private:
const osg::Plane* mCullPlane;
};
class FlipCallback : public osg::NodeCallback
{
public:
FlipCallback(const osg::Plane* cullPlane)
: mCullPlane(cullPlane)
{
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cv = static_cast<osgUtil::CullVisitor*>(nv);
osg::Vec3d eyePoint = cv->getEyePoint();
// flip the below graph if the eye point is above the plane
if (mCullPlane->intersect(osg::BoundingSphere(osg::Vec3d(0,0,eyePoint.z()), 0)) > 0)
{
osg::RefMatrix* modelViewMatrix = new osg::RefMatrix(*cv->getModelViewMatrix());
modelViewMatrix->preMultScale(osg::Vec3(1,1,-1));
cv->pushModelViewMatrix(modelViewMatrix, osg::Transform::RELATIVE_RF);
traverse(node, nv);
cv->popModelViewMatrix();
}
else
traverse(node, nv);
}
private:
const osg::Plane* mCullPlane;
};
public:
ClipCullNode()
{
addCullCallback (new PlaneCullCallback(&mPlane));
mClipNodeTransform = new osg::PositionAttitudeTransform;
mClipNodeTransform->addCullCallback(new FlipCallback(&mPlane));
addChild(mClipNodeTransform);
mClipNode = new osg::ClipNode;
mClipNodeTransform->addChild(mClipNode);
}
void setPlane (const osg::Plane& plane)
{
if (plane == mPlane)
return;
mPlane = plane;
mClipNode->getClipPlaneList().clear();
mClipNode->addClipPlane(new osg::ClipPlane(0, mPlane));
mClipNode->setStateSetModes(*getOrCreateStateSet(), osg::StateAttribute::ON);
}
private:
osg::ref_ptr<osg::PositionAttitudeTransform> mClipNodeTransform;
osg::ref_ptr<osg::ClipNode> mClipNode;
osg::Plane mPlane;
};
// Node callback to entirely skip the traversal.
class NoTraverseCallback : public osg::NodeCallback
{
public:
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
// no traverse()
}
};
void addDebugOverlay(osg::Texture2D* texture, int pos, osg::Group* parent)
{
osg::ref_ptr<osg::Camera> debugCamera (new osg::Camera);
debugCamera->setProjectionMatrix(osg::Matrix::identity());
debugCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
debugCamera->setViewMatrix(osg::Matrix::identity());
debugCamera->setClearMask(0);
debugCamera->setRenderOrder(osg::Camera::NESTED_RENDER);
debugCamera->setAllowEventFocus(false);
const float size = 0.5;
osg::ref_ptr<osg::Geode> debugGeode (new osg::Geode);
osg::ref_ptr<osg::Geometry> geom = osg::createTexturedQuadGeometry(osg::Vec3f(-1 + size*pos, -1, 0), osg::Vec3f(size,0,0), osg::Vec3f(0,size,0));
debugGeode->addDrawable(geom);
debugCamera->addChild(debugGeode);
osg::StateSet* debugStateset = geom->getOrCreateStateSet();
debugStateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
debugStateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
debugStateset->setRenderBinDetails(20, "RenderBin");
debugStateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
parent->addChild(debugCamera);
}
Water::Water(osg::Group *parent, osg::Group* sceneRoot, Resource::ResourceSystem *resourceSystem, osgUtil::IncrementalCompileOperation *ico,
const MWWorld::Fallback* fallback, const std::string& resourcePath)
: mParent(parent)
, mSceneRoot(sceneRoot)
, mResourceSystem(resourceSystem)
, mEnabled(true)
, mToggled(true)
@ -126,17 +282,164 @@ Water::Water(osg::Group *parent, Resource::ResourceSystem *resourceSystem, osgUt
geode->addDrawable(waterGeom);
geode->setNodeMask(Mask_Water);
// TODO: node mask to use simple water for local map
if (ico)
ico->add(geode);
createWaterStateSet(mResourceSystem, geode);
//createSimpleWaterStateSet(mResourceSystem, geode);
mWaterNode = new osg::PositionAttitudeTransform;
mWaterNode->addChild(geode);
mParent->addChild(mWaterNode);
mSceneRoot->addChild(mWaterNode);
setHeight(mTop);
const float waterLevel = -1;
// refraction
unsigned int rttSize = Settings::Manager::getInt("rtt size", "Water");
osg::ref_ptr<osg::Camera> refractionCamera (new osg::Camera);
refractionCamera->setRenderOrder(osg::Camera::PRE_RENDER);
refractionCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
refractionCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
refractionCamera->setReferenceFrame(osg::Camera::RELATIVE_RF);
refractionCamera->setCullMask(Mask_Effect|Mask_Scene|Mask_Terrain|Mask_Actor|Mask_ParticleSystem|Mask_Sky|Mask_Player|(1<<16));
refractionCamera->setNodeMask(Mask_RenderToTexture);
refractionCamera->setViewport(0, 0, rttSize, rttSize);
// No need for Update traversal since the mSceneRoot is already updated as part of the main scene graph
// A double update would mess with the light collection (in addition to being plain redundant)
refractionCamera->setUpdateCallback(new NoTraverseCallback);
// No need for fog here, we are already applying fog on the water surface itself as well as underwater fog
refractionCamera->getOrCreateStateSet()->setMode(GL_FOG, osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
osg::ref_ptr<ClipCullNode> clipNode (new ClipCullNode);
clipNode->setPlane(osg::Plane(osg::Vec3d(0,0,-1), osg::Vec3d(0,0, waterLevel)));
refractionCamera->addChild(clipNode);
clipNode->addChild(mSceneRoot);
// TODO: add ingame setting for texture quality
osg::ref_ptr<osg::Texture2D> refractionTexture = new osg::Texture2D;
refractionTexture->setTextureSize(rttSize, rttSize);
refractionTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
refractionTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
refractionTexture->setInternalFormat(GL_RGB);
refractionTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
refractionTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
refractionCamera->attach(osg::Camera::COLOR_BUFFER, refractionTexture);
osg::ref_ptr<osg::Texture2D> refractionDepthTexture = new osg::Texture2D;
refractionDepthTexture->setSourceFormat(GL_DEPTH_COMPONENT);
refractionDepthTexture->setInternalFormat(GL_DEPTH_COMPONENT24_ARB);
refractionDepthTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
refractionDepthTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
refractionDepthTexture->setSourceType(GL_UNSIGNED_INT);
refractionDepthTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
refractionDepthTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
refractionCamera->attach(osg::Camera::DEPTH_BUFFER, refractionDepthTexture);
mParent->addChild(refractionCamera);
// reflection
osg::ref_ptr<osg::Camera> reflectionCamera (new osg::Camera);
reflectionCamera->setRenderOrder(osg::Camera::PRE_RENDER);
reflectionCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
reflectionCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
reflectionCamera->setReferenceFrame(osg::Camera::RELATIVE_RF);
reflectionCamera->setCullMask(Mask_Effect|Mask_Scene|Mask_Terrain|Mask_Actor|Mask_ParticleSystem|Mask_Sky|Mask_Player|(1<<16));
reflectionCamera->setNodeMask(Mask_RenderToTexture);
reflectionCamera->setViewport(0, 0, rttSize, rttSize);
// No need for Update traversal since the mSceneRoot is already updated as part of the main scene graph
// A double update would mess with the light collection (in addition to being plain redundant)
reflectionCamera->setUpdateCallback(new NoTraverseCallback);
osg::ref_ptr<osg::Texture2D> reflectionTexture = new osg::Texture2D;
reflectionTexture->setInternalFormat(GL_RGB);
reflectionTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
reflectionTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
reflectionTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
reflectionTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
reflectionCamera->attach(osg::Camera::COLOR_BUFFER, reflectionTexture);
reflectionCamera->setViewMatrix(osg::Matrix::translate(0,0,-waterLevel) * osg::Matrix::scale(1,1,-1) * osg::Matrix::translate(0,0,waterLevel));
osg::ref_ptr<osg::MatrixTransform> reflectNode (new osg::MatrixTransform);
// XXX: should really flip the FrontFace on each renderable instead of forcing clockwise.
osg::ref_ptr<osg::FrontFace> frontFace (new osg::FrontFace);
frontFace->setMode(osg::FrontFace::CLOCKWISE);
reflectNode->getOrCreateStateSet()->setAttributeAndModes(frontFace, osg::StateAttribute::ON);
osg::ref_ptr<ClipCullNode> clipNode2 (new ClipCullNode);
clipNode2->setPlane(osg::Plane(osg::Vec3d(0,0,1), osg::Vec3d(0,0,waterLevel)));
reflectNode->addChild(clipNode2);
clipNode2->addChild(mSceneRoot);
reflectionCamera->addChild(reflectNode);
// TODO: add to waterNode so cameras don't get updated when water is hidden?
mParent->addChild(reflectionCamera);
// debug overlay
addDebugOverlay(refractionTexture, 0, mParent);
addDebugOverlay(refractionDepthTexture, 1, mParent);
addDebugOverlay(reflectionTexture, 2, mParent);
// shader
// FIXME: windows utf8 path handling?
osg::ref_ptr<osg::Shader> vertexShader (osg::Shader::readShaderFile(osg::Shader::VERTEX, resourcePath + "/shaders/water_vertex.glsl"));
osg::ref_ptr<osg::Shader> fragmentShader (osg::Shader::readShaderFile(osg::Shader::FRAGMENT, resourcePath + "/shaders/water_fragment.glsl"));
osg::ref_ptr<osg::Program> program (new osg::Program);
program->addShader(vertexShader);
program->addShader(fragmentShader);
osg::ref_ptr<osg::Texture2D> normalMap (new osg::Texture2D(osgDB::readImageFile(resourcePath + "/shaders/water_nm.png")));
normalMap->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
normalMap->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
normalMap->setMaxAnisotropy(16);
normalMap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
normalMap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
normalMap->getImage()->flipVertical();
osg::ref_ptr<osg::StateSet> shaderStateset = new osg::StateSet;
shaderStateset->setAttributeAndModes(program, osg::StateAttribute::ON);
shaderStateset->addUniform(new osg::Uniform("reflectionMap", 0));
shaderStateset->addUniform(new osg::Uniform("refractionMap", 1));
shaderStateset->addUniform(new osg::Uniform("refractionDepthMap", 2));
shaderStateset->addUniform(new osg::Uniform("normalMap", 3));
shaderStateset->setTextureAttributeAndModes(0, reflectionTexture, osg::StateAttribute::ON);
shaderStateset->setTextureAttributeAndModes(1, refractionTexture, osg::StateAttribute::ON);
shaderStateset->setTextureAttributeAndModes(2, refractionDepthTexture, osg::StateAttribute::ON);
shaderStateset->setTextureAttributeAndModes(3, normalMap, osg::StateAttribute::ON);
shaderStateset->setMode(GL_BLEND, osg::StateAttribute::ON); // TODO: set Off when refraction is on
shaderStateset->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
osg::ref_ptr<osg::Depth> depth (new osg::Depth);
depth->setWriteMask(false);
shaderStateset->setAttributeAndModes(depth, osg::StateAttribute::ON);
// TODO: render after transparent bin when refraction is on
shaderStateset->setRenderBinDetails(MWRender::RenderBin_Water, "RenderBin");
geode->setStateSet(shaderStateset);
}
Water::~Water()

@ -9,6 +9,9 @@ namespace osg
{
class Group;
class PositionAttitudeTransform;
class Texture2D;
class Image;
class Camera;
}
namespace osgUtil
@ -37,6 +40,7 @@ namespace MWRender
static const int CELL_SIZE = 8192;
osg::ref_ptr<osg::Group> mParent;
osg::ref_ptr<osg::Group> mSceneRoot;
osg::ref_ptr<osg::PositionAttitudeTransform> mWaterNode;
Resource::ResourceSystem* mResourceSystem;
osg::ref_ptr<osgUtil::IncrementalCompileOperation> mIncrementalCompileOperation;
@ -51,7 +55,9 @@ namespace MWRender
void updateVisible();
public:
Water(osg::Group* parent, Resource::ResourceSystem* resourceSystem, osgUtil::IncrementalCompileOperation* ico, const MWWorld::Fallback* fallback);
Water(osg::Group* parent, osg::Group* sceneRoot,
Resource::ResourceSystem* resourceSystem, osgUtil::IncrementalCompileOperation* ico, const MWWorld::Fallback* fallback,
const std::string& resourcePath);
~Water();
void setEnabled(bool enabled);

@ -153,7 +153,8 @@ namespace MWWorld
const Files::Collections& fileCollections,
const std::vector<std::string>& contentFiles,
ToUTF8::Utf8Encoder* encoder, const std::map<std::string,std::string>& fallbackMap,
int activationDistanceOverride, const std::string& startCell, const std::string& startupScript)
int activationDistanceOverride, const std::string& startCell, const std::string& startupScript,
const std::string& resourcePath)
: mResourceSystem(resourceSystem), mFallback(fallbackMap), mPlayer (0), mLocalScripts (mStore),
mSky (true), mCells (mStore, mEsm),
mGodMode(false), mScriptsEnabled(true), mContentFiles (contentFiles),
@ -163,7 +164,7 @@ namespace MWWorld
{
mPhysics = new MWPhysics::PhysicsSystem(resourceSystem, rootNode);
mProjectileManager.reset(new ProjectileManager(rootNode, resourceSystem, mPhysics));
mRendering = new MWRender::RenderingManager(viewer, rootNode, resourceSystem, &mFallback);
mRendering = new MWRender::RenderingManager(viewer, rootNode, resourceSystem, &mFallback, resourcePath);
mEsm.resize(contentFiles.size());
Loading::Listener* listener = MWBase::Environment::get().getWindowManager()->getLoadingScreen();

@ -167,7 +167,7 @@ namespace MWWorld
const Files::Collections& fileCollections,
const std::vector<std::string>& contentFiles,
ToUTF8::Utf8Encoder* encoder, const std::map<std::string,std::string>& fallbackMap,
int activationDistanceOverride, const std::string& startCell, const std::string& startupScript);
int activationDistanceOverride, const std::string& startCell, const std::string& startupScript, const std::string& resourcePath);
virtual ~World();

@ -1 +1,2 @@
add_subdirectory(mygui)
add_subdirectory(shaders)

@ -0,0 +1,11 @@
# Copy resource files into the build directory
set(SDIR ${CMAKE_CURRENT_SOURCE_DIR})
set(DDIR ${OpenMW_BINARY_DIR}/resources/shaders)
set(SHADER_FILES
water_vertex.glsl
water_fragment.glsl
water_nm.png
)
copy_all_files(${CMAKE_CURRENT_SOURCE_DIR} ${DDIR} "${SHADER_FILES}")

@ -0,0 +1,189 @@
#version 120
// Inspired by Blender GLSL Water by martinsh ( http://devlog-martinsh.blogspot.de/2012/07/waterundewater-shader-wip.html )
#define REFRACTION 1
// tweakables -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
const float VISIBILITY = 1200.0; // how far you can look through water
const float BIG_WAVES_X = 0.1; // strength of big waves
const float BIG_WAVES_Y = 0.1;
const float MID_WAVES_X = 0.1; // strength of middle sized waves
const float MID_WAVES_Y = 0.1;
const float SMALL_WAVES_X = 0.1; // strength of small waves
const float SMALL_WAVES_Y = 0.1;
const float WAVE_CHOPPYNESS = 0.05; // wave choppyness
const float WAVE_SCALE = 75.0; // overall wave scale
const float BUMP = 0.5; // overall water surface bumpiness
const float REFL_BUMP = 0.15; // reflection distortion amount
const float REFR_BUMP = 0.06; // refraction distortion amount
const float SCATTER_AMOUNT = 0.3; // amount of sunlight scattering
const vec3 SCATTER_COLOUR = vec3(0.0,1.0,0.95); // colour of sunlight scattering
const vec3 SUN_EXT = vec3(0.45, 0.55, 0.68); //sunlight extinction
const float SPEC_HARDNESS = 256.0; // specular highlights hardness
const vec2 WIND_DIR = vec2(0.5f, -0.8f);
const float WIND_SPEED = 0.2f;
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta)
{
float c = abs(dot(Incoming, Normal));
float g = eta * eta - 1.0 + c * c;
float result;
if(g > 0.0) {
g = sqrt(g);
float A =(g - c)/(g + c);
float B =(c *(g + c)- 1.0)/(c *(g - c)+ 1.0);
result = 0.5 * A * A *(1.0 + B * B);
}
else
result = 1.0; /* TIR (no refracted component) */
return result;
}
varying vec3 screenCoordsPassthrough;
varying vec4 position;
varying float depthPassthrough;
uniform sampler2D reflectionMap;
#if REFRACTION
uniform sampler2D refractionMap;
uniform sampler2D refractionDepthMap;
#endif
uniform sampler2D normalMap;
uniform float osg_SimulationTime;
void main(void)
{
// FIXME
vec3 worldPos = position.xyz; // ((wMat) * ( position)).xyz;
vec2 UV = worldPos.xy / (8192.0*5.0) * 3.0;
UV.y *= -1.0;
float shadow = 1.0;
vec2 screenCoords = screenCoordsPassthrough.xy / screenCoordsPassthrough.z;
screenCoords.y = (1.0-screenCoords.y);
vec2 nCoord = vec2(0.0,0.0);
#define waterTimer osg_SimulationTime
nCoord = UV * (WAVE_SCALE * 0.05) + WIND_DIR * waterTimer * (WIND_SPEED*0.04);
vec3 normal0 = 2.0 * texture2D(normalMap, nCoord + vec2(-waterTimer*0.015,-waterTimer*0.005)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 0.1) + WIND_DIR * waterTimer * (WIND_SPEED*0.08)-(normal0.xy/normal0.zz)*WAVE_CHOPPYNESS;
vec3 normal1 = 2.0 * texture2D(normalMap, nCoord + vec2(+waterTimer*0.020,+waterTimer*0.015)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 0.25) + WIND_DIR * waterTimer * (WIND_SPEED*0.07)-(normal1.xy/normal1.zz)*WAVE_CHOPPYNESS;
vec3 normal2 = 2.0 * texture2D(normalMap, nCoord + vec2(-waterTimer*0.04,-waterTimer*0.03)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 0.5) + WIND_DIR * waterTimer * (WIND_SPEED*0.09)-(normal2.xy/normal2.z)*WAVE_CHOPPYNESS;
vec3 normal3 = 2.0 * texture2D(normalMap, nCoord + vec2(+waterTimer*0.03,+waterTimer*0.04)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE* 1.0) + WIND_DIR * waterTimer * (WIND_SPEED*0.4)-(normal3.xy/normal3.zz)*WAVE_CHOPPYNESS;
vec3 normal4 = 2.0 * texture2D(normalMap, nCoord + vec2(-waterTimer*0.02,+waterTimer*0.1)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 2.0) + WIND_DIR * waterTimer * (WIND_SPEED*0.7)-(normal4.xy/normal4.zz)*WAVE_CHOPPYNESS;
vec3 normal5 = 2.0 * texture2D(normalMap, nCoord + vec2(+waterTimer*0.1,-waterTimer*0.06)).rgb - 1.0;
vec3 normal = (normal0 * BIG_WAVES_X + normal1 * BIG_WAVES_Y +
normal2 * MID_WAVES_X + normal3 * MID_WAVES_Y +
normal4 * SMALL_WAVES_X + normal5 * SMALL_WAVES_Y);
normal = normalize(vec3(normal.x * BUMP, normal.y * BUMP, normal.z));
normal = vec3(-normal.x, -normal.y, normal.z);
// normal for sunlight scattering
vec3 lNormal = (normal0 * BIG_WAVES_X*0.5 + normal1 * BIG_WAVES_Y*0.5 +
normal2 * MID_WAVES_X*0.2 + normal3 * MID_WAVES_Y*0.2 +
normal4 * SMALL_WAVES_X*0.1 + normal5 * SMALL_WAVES_Y*0.1).xyz;
lNormal = normalize(vec3(lNormal.x * BUMP, lNormal.y * BUMP, lNormal.z));
lNormal = vec3(-lNormal.x, -lNormal.y, lNormal.z);
vec3 lVec = normalize((gl_ModelViewMatrixInverse * vec4(gl_LightSource[0].position.xyz, 0.0)).xyz);
vec3 cameraPos = (gl_ModelViewMatrixInverse * vec4(0,0,0,1)).xyz;
vec3 vVec = normalize(position.xyz - cameraPos.xyz);
float isUnderwater = (cameraPos.z > 0.0) ? 0.0 : 1.0;
// sunlight scattering
vec3 pNormal = vec3(0,0,1);
vec3 lR = reflect(lVec, lNormal);
vec3 llR = reflect(lVec, pNormal);
float sunHeight = lVec.z;
float sunFade = length(gl_LightModel.ambient.xyz);
float s = clamp(dot(lR, vVec)*2.0-1.2, 0.0, 1.0);
float lightScatter = shadow * clamp(dot(lVec,lNormal)*0.7+0.3, 0.0, 1.0) * s * SCATTER_AMOUNT * sunFade * clamp(1.0-exp(-sunHeight), 0.0, 1.0);
vec3 scatterColour = mix(vec3(SCATTER_COLOUR)*vec3(1.0,0.4,0.0), SCATTER_COLOUR, clamp(1.0-exp(-sunHeight*SUN_EXT), 0.0, 1.0));
// fresnel
float ior = (cameraPos.z>0.0)?(1.333/1.0):(1.0/1.333); //air to water; water to air
float fresnel = fresnel_dielectric(vVec, normal, ior);
fresnel = clamp(fresnel, 0.0, 1.0);
// reflection
vec3 reflection = texture2D(reflectionMap, screenCoords+(normal.xy*REFL_BUMP)).rgb;
// refraction
#if REFRACTION
vec3 refraction = texture2D(refractionMap, screenCoords-(normal.xy*REFR_BUMP)).rgb;
// brighten up the refraction underwater
refraction = (cameraPos.z < 0.0) ? clamp(refraction * 1.5, 0.0, 1.0) : refraction;
#endif
// specular
vec3 R = reflect(vVec, normal);
float specular = pow(max(dot(R, lVec), 0.0),SPEC_HARDNESS) * shadow;
#if REFRACTION
float refractionDepth = texture2D(refractionDepthMap, screenCoords-(normal.xy*REFR_BUMP)).x;
// make linear
float zNear = 5; // FIXME
float zFar = 6666; // FIXME
float z_n = 2.0 * refractionDepth - 1.0;
refractionDepth = 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
float waterDepth = refractionDepth - depthPassthrough;
vec3 waterColor = vec3(0.090195, 0.115685, 0.12745);
waterColor = waterColor * length(gl_LightModel.ambient.xyz);
if (cameraPos.z > 0.0)
refraction = mix(refraction, waterColor, clamp(waterDepth/VISIBILITY, 0.0, 1.0));
gl_FragData[0].xyz = mix( mix(refraction, scatterColour, lightScatter), reflection, fresnel) + specular * gl_LightSource[0].specular.xyz;
#else
gl_FragData[0].xyz = mix(reflection, vec3(0.090195, 0.115685, 0.12745), (1.0-fresnel)*0.5) + specular * gl_LightSource[0].specular.xyz;
#endif
// fog
float fogValue = clamp((depthPassthrough - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, gl_Fog.color.xyz, fogValue);
#if REFRACTION
gl_FragData[0].w = 1.0;
#else
gl_FragData[0].w = clamp(fresnel*2.0 + specular, 0.0, 1.0);
#endif
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

@ -0,0 +1,22 @@
#version 120
varying vec3 screenCoordsPassthrough;
varying vec4 position;
varying float depthPassthrough;
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
mat4 scalemat = mat4(0.5, 0.0, 0.0, 0.0,
0.0, -0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
vec4 texcoordProj = ((scalemat) * ( gl_Position));
screenCoordsPassthrough = vec3(texcoordProj.x, texcoordProj.y, texcoordProj.w);
position = gl_Vertex;
depthPassthrough = gl_Position.z;
}
Loading…
Cancel
Save