2012-01-14 23:34:14 +00:00
|
|
|
#include "water.hpp"
|
2012-04-03 12:23:23 +00:00
|
|
|
#include <components/settings/settings.hpp>
|
2012-04-03 19:16:43 +00:00
|
|
|
#include "sky.hpp"
|
2012-04-06 16:22:37 +00:00
|
|
|
#include "renderingmanager.hpp"
|
2012-04-29 01:33:10 +00:00
|
|
|
#include "compositors.hpp"
|
2012-04-03 12:23:23 +00:00
|
|
|
|
|
|
|
using namespace Ogre;
|
2012-01-14 23:34:14 +00:00
|
|
|
|
2012-03-29 13:49:24 +00:00
|
|
|
namespace MWRender
|
|
|
|
{
|
|
|
|
|
2012-04-29 01:33:10 +00:00
|
|
|
Water::Water (Ogre::Camera *camera, RenderingManager* rend, const ESM::Cell* cell) :
|
2012-03-29 16:33:08 +00:00
|
|
|
mCamera (camera), mViewport (camera->getViewport()), mSceneManager (camera->getSceneManager()),
|
2012-04-08 15:44:00 +00:00
|
|
|
mIsUnderwater(false), mVisibilityFlags(0),
|
2012-04-18 23:08:26 +00:00
|
|
|
mReflectionTarget(0), mActive(1), mToggled(1),
|
2012-04-29 01:33:10 +00:00
|
|
|
mReflectionRenderActive(false), mRendering(rend)
|
2012-03-29 16:33:08 +00:00
|
|
|
{
|
2012-04-29 01:33:10 +00:00
|
|
|
mSky = rend->getSkyManager();
|
2012-03-29 13:49:24 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
mTop = cell->water;
|
2012-03-29 13:49:24 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
mIsUnderwater = false;
|
2012-03-29 13:49:24 +00:00
|
|
|
|
2012-04-03 12:23:23 +00:00
|
|
|
mWaterPlane = Plane(Vector3::UNIT_Y, 0);
|
2012-03-29 13:49:24 +00:00
|
|
|
|
2012-04-04 16:53:40 +00:00
|
|
|
MeshManager::getSingleton().createPlane("water", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, mWaterPlane, CELL_SIZE*5, CELL_SIZE * 5, 10, 10, true, 1, 3,3, Vector3::UNIT_Z);
|
2012-03-29 13:49:24 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
mWater = mSceneManager->createEntity("water");
|
2012-04-03 13:13:47 +00:00
|
|
|
mWater->setVisibilityFlags(RV_Water);
|
2012-04-05 17:07:21 +00:00
|
|
|
mWater->setRenderQueueGroup(RQG_Water);
|
2012-04-11 16:53:13 +00:00
|
|
|
mWater->setCastShadows(false);
|
2012-04-03 14:37:54 +00:00
|
|
|
|
2012-04-03 13:13:47 +00:00
|
|
|
mVisibilityFlags = RV_Terrain * Settings::Manager::getBool("reflect terrain", "Water")
|
|
|
|
+ RV_Statics * Settings::Manager::getBool("reflect statics", "Water")
|
|
|
|
+ RV_StaticsSmall * Settings::Manager::getBool("reflect small statics", "Water")
|
|
|
|
+ RV_Actors * Settings::Manager::getBool("reflect actors", "Water")
|
2012-04-05 13:30:55 +00:00
|
|
|
+ RV_Misc * Settings::Manager::getBool("reflect misc", "Water")
|
|
|
|
+ RV_Sky;
|
2012-04-03 13:13:47 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
mWaterNode = mSceneManager->getRootSceneNode()->createChildSceneNode();
|
|
|
|
mWaterNode->setPosition(0, mTop, 0);
|
2012-03-29 13:49:24 +00:00
|
|
|
|
2012-04-11 16:53:13 +00:00
|
|
|
mReflectionCamera = mSceneManager->createCamera("ReflectionCamera");
|
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
if(!(cell->data.flags & cell->Interior))
|
|
|
|
{
|
|
|
|
mWaterNode->setPosition(getSceneNodeCoordinates(cell->data.gridX, cell->data.gridY));
|
2012-03-29 13:49:24 +00:00
|
|
|
}
|
2012-03-29 16:33:08 +00:00
|
|
|
mWaterNode->attachObject(mWater);
|
2012-04-03 12:23:23 +00:00
|
|
|
|
2012-04-03 19:16:43 +00:00
|
|
|
// Create rendertarget for reflection
|
2012-04-03 12:23:23 +00:00
|
|
|
int rttsize = Settings::Manager::getInt("rtt size", "Water");
|
2012-04-03 17:10:02 +00:00
|
|
|
|
2012-04-11 16:53:13 +00:00
|
|
|
TexturePtr tex;
|
2012-04-05 17:27:54 +00:00
|
|
|
if (Settings::Manager::getBool("shader", "Water"))
|
2012-04-03 17:10:02 +00:00
|
|
|
{
|
2012-04-11 16:53:13 +00:00
|
|
|
tex = TextureManager::getSingleton().createManual("WaterReflection",
|
|
|
|
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, rttsize, rttsize, 0, PF_FLOAT16_RGBA, TU_RENDERTARGET);
|
2012-04-03 17:10:02 +00:00
|
|
|
|
|
|
|
RenderTarget* rtt = tex->getBuffer()->getRenderTarget();
|
2012-04-11 16:53:13 +00:00
|
|
|
Viewport* vp = rtt->addViewport(mReflectionCamera);
|
2012-04-03 17:10:02 +00:00
|
|
|
vp->setOverlaysEnabled(false);
|
|
|
|
vp->setBackgroundColour(ColourValue(0.8f, 0.9f, 1.0f));
|
|
|
|
vp->setShadowsEnabled(false);
|
|
|
|
vp->setVisibilityMask( mVisibilityFlags );
|
2012-04-11 16:53:13 +00:00
|
|
|
// use fallback techniques without shadows and without mrt (currently not implemented for sky and terrain)
|
|
|
|
//vp->setMaterialScheme("Fallback");
|
2012-04-03 17:10:02 +00:00
|
|
|
rtt->addListener(this);
|
2012-04-03 12:23:23 +00:00
|
|
|
rtt->setActive(true);
|
|
|
|
|
2012-04-03 17:10:02 +00:00
|
|
|
mReflectionTarget = rtt;
|
|
|
|
}
|
|
|
|
|
2012-04-06 16:22:37 +00:00
|
|
|
mCompositorName = RenderingManager::useMRT() ? "Underwater" : "UnderwaterNoMRT";
|
|
|
|
|
2012-04-06 13:51:57 +00:00
|
|
|
createMaterial();
|
|
|
|
mWater->setMaterial(mMaterial);
|
2012-04-08 15:44:00 +00:00
|
|
|
|
|
|
|
mUnderwaterEffect = Settings::Manager::getBool("underwater effect", "Water");
|
2012-04-11 16:53:13 +00:00
|
|
|
|
2012-04-18 23:08:26 +00:00
|
|
|
mSceneManager->addRenderQueueListener(this);
|
|
|
|
|
2012-04-11 16:53:13 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------
|
|
|
|
// ---------------------------------- reflection debug overlay ----------------------------------
|
|
|
|
// ----------------------------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
if (Settings::Manager::getBool("shader", "Water"))
|
|
|
|
{
|
|
|
|
OverlayManager& mgr = OverlayManager::getSingleton();
|
|
|
|
Overlay* overlay;
|
|
|
|
// destroy if already exists
|
|
|
|
if (overlay = mgr.getByName("ReflectionDebugOverlay"))
|
|
|
|
mgr.destroy(overlay);
|
2012-04-12 14:46:56 +00:00
|
|
|
|
2012-04-11 16:53:13 +00:00
|
|
|
overlay = mgr.create("ReflectionDebugOverlay");
|
2012-04-12 14:46:56 +00:00
|
|
|
|
2012-04-11 16:53:13 +00:00
|
|
|
if (MaterialManager::getSingleton().resourceExists("Ogre/ReflectionDebugTexture"))
|
|
|
|
MaterialManager::getSingleton().remove("Ogre/ReflectionDebugTexture");
|
|
|
|
MaterialPtr debugMat = MaterialManager::getSingleton().create(
|
2012-04-12 14:46:56 +00:00
|
|
|
"Ogre/ReflectionDebugTexture",
|
2012-04-11 16:53:13 +00:00
|
|
|
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
2012-04-12 14:46:56 +00:00
|
|
|
|
2012-04-11 16:53:13 +00:00
|
|
|
debugMat->getTechnique(0)->getPass(0)->setLightingEnabled(false);
|
|
|
|
TextureUnitState *t = debugMat->getTechnique(0)->getPass(0)->createTextureUnitState(tex->getName());
|
|
|
|
t->setTextureAddressingMode(TextureUnitState::TAM_CLAMP);
|
|
|
|
|
|
|
|
OverlayContainer* debugPanel;
|
2012-04-12 14:46:56 +00:00
|
|
|
|
2012-04-11 16:53:13 +00:00
|
|
|
// destroy container if exists
|
|
|
|
try
|
|
|
|
{
|
2012-04-12 14:46:56 +00:00
|
|
|
if (debugPanel =
|
2012-04-11 16:53:13 +00:00
|
|
|
static_cast<OverlayContainer*>(
|
|
|
|
mgr.getOverlayElement("Ogre/ReflectionDebugTexPanel"
|
|
|
|
)))
|
|
|
|
mgr.destroyOverlayElement(debugPanel);
|
|
|
|
}
|
|
|
|
catch (Ogre::Exception&) {}
|
2012-04-12 14:46:56 +00:00
|
|
|
|
2012-04-11 16:53:13 +00:00
|
|
|
debugPanel = (OverlayContainer*)
|
|
|
|
(OverlayManager::getSingleton().createOverlayElement("Panel", "Ogre/ReflectionDebugTexPanel"));
|
|
|
|
debugPanel->_setPosition(0, 0.55);
|
|
|
|
debugPanel->_setDimensions(0.3, 0.3);
|
|
|
|
debugPanel->setMaterialName(debugMat->getName());
|
|
|
|
debugPanel->show();
|
|
|
|
overlay->add2D(debugPanel);
|
|
|
|
overlay->show();
|
|
|
|
}
|
|
|
|
*/
|
2012-03-29 16:33:08 +00:00
|
|
|
}
|
2012-03-29 13:49:24 +00:00
|
|
|
|
2012-04-03 13:13:47 +00:00
|
|
|
void Water::setActive(bool active)
|
|
|
|
{
|
|
|
|
mActive = active;
|
2012-04-12 14:46:56 +00:00
|
|
|
updateVisible();
|
2012-04-03 13:13:47 +00:00
|
|
|
}
|
2012-01-22 21:38:10 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
Water::~Water()
|
|
|
|
{
|
2012-04-03 12:23:23 +00:00
|
|
|
MeshManager::getSingleton().remove("water");
|
2012-01-14 23:34:14 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
mWaterNode->detachObject(mWater);
|
|
|
|
mSceneManager->destroyEntity(mWater);
|
|
|
|
mSceneManager->destroySceneNode(mWaterNode);
|
|
|
|
}
|
2012-01-20 22:59:56 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
void Water::changeCell(const ESM::Cell* cell)
|
|
|
|
{
|
|
|
|
mTop = cell->water;
|
2012-01-14 23:34:14 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
if(!(cell->data.flags & cell->Interior))
|
|
|
|
mWaterNode->setPosition(getSceneNodeCoordinates(cell->data.gridX, cell->data.gridY));
|
|
|
|
else
|
|
|
|
setHeight(mTop);
|
|
|
|
}
|
2012-03-29 13:49:24 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
void Water::setHeight(const float height)
|
|
|
|
{
|
|
|
|
mTop = height;
|
2012-04-18 23:08:26 +00:00
|
|
|
mWaterPlane = Plane(Vector3::UNIT_Y, height);
|
2012-03-29 16:33:08 +00:00
|
|
|
mWaterNode->setPosition(0, height, 0);
|
|
|
|
}
|
2012-01-14 23:34:14 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
void Water::toggle()
|
|
|
|
{
|
2012-04-12 14:46:56 +00:00
|
|
|
mToggled = !mToggled;
|
|
|
|
updateVisible();
|
2012-03-29 16:33:08 +00:00
|
|
|
}
|
2012-01-14 23:34:14 +00:00
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
void Water::checkUnderwater(float y)
|
|
|
|
{
|
2012-04-19 21:25:58 +00:00
|
|
|
if (!mActive)
|
|
|
|
{
|
2012-04-29 01:33:10 +00:00
|
|
|
mRendering->getCompositors()->setCompositorEnabled(mCompositorName, false);
|
2012-04-19 21:25:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-02 12:32:21 +00:00
|
|
|
if ((mIsUnderwater && y > mTop) || !mWater->isVisible() || mCamera->getPolygonMode() != Ogre::PM_SOLID)
|
2012-03-29 13:49:24 +00:00
|
|
|
{
|
2012-04-29 01:33:10 +00:00
|
|
|
mRendering->getCompositors()->setCompositorEnabled(mCompositorName, false);
|
2012-04-06 13:51:57 +00:00
|
|
|
|
|
|
|
// tell the shader we are not underwater
|
|
|
|
Ogre::Pass* pass = mMaterial->getTechnique(0)->getPass(0);
|
|
|
|
if (pass->hasFragmentProgram() && pass->getFragmentProgramParameters()->_findNamedConstantDefinition("isUnderwater", false))
|
|
|
|
pass->getFragmentProgramParameters()->setNamedConstant("isUnderwater", Real(0));
|
|
|
|
|
2012-04-06 14:11:08 +00:00
|
|
|
mWater->setRenderQueueGroup(RQG_Water);
|
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
mIsUnderwater = false;
|
2012-04-12 14:46:56 +00:00
|
|
|
}
|
2012-03-29 13:49:24 +00:00
|
|
|
|
2012-04-02 12:32:21 +00:00
|
|
|
if (!mIsUnderwater && y < mTop && mWater->isVisible() && mCamera->getPolygonMode() == Ogre::PM_SOLID)
|
2012-03-29 13:49:24 +00:00
|
|
|
{
|
2012-04-08 15:44:00 +00:00
|
|
|
if (mUnderwaterEffect)
|
2012-04-29 01:33:10 +00:00
|
|
|
mRendering->getCompositors()->setCompositorEnabled(mCompositorName, true);
|
2012-04-06 13:51:57 +00:00
|
|
|
|
|
|
|
// tell the shader we are underwater
|
|
|
|
Ogre::Pass* pass = mMaterial->getTechnique(0)->getPass(0);
|
|
|
|
if (pass->hasFragmentProgram() && pass->getFragmentProgramParameters()->_findNamedConstantDefinition("isUnderwater", false))
|
|
|
|
pass->getFragmentProgramParameters()->setNamedConstant("isUnderwater", Real(1));
|
|
|
|
|
2012-04-06 14:11:08 +00:00
|
|
|
mWater->setRenderQueueGroup(RQG_UnderWater);
|
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
mIsUnderwater = true;
|
2012-03-29 13:49:24 +00:00
|
|
|
}
|
2012-04-12 14:46:56 +00:00
|
|
|
|
|
|
|
updateVisible();
|
2012-01-14 23:34:14 +00:00
|
|
|
}
|
2012-03-29 16:33:08 +00:00
|
|
|
|
2012-04-03 12:23:23 +00:00
|
|
|
Vector3 Water::getSceneNodeCoordinates(int gridX, int gridY)
|
|
|
|
{
|
|
|
|
return Vector3(gridX * CELL_SIZE + (CELL_SIZE / 2), mTop, -gridY * CELL_SIZE - (CELL_SIZE / 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Water::preRenderTargetUpdate(const RenderTargetEvent& evt)
|
2012-03-29 16:33:08 +00:00
|
|
|
{
|
2012-04-05 13:30:55 +00:00
|
|
|
if (evt.source == mReflectionTarget)
|
|
|
|
{
|
2012-04-12 14:46:56 +00:00
|
|
|
mReflectionCamera->setOrientation(mCamera->getDerivedOrientation());
|
|
|
|
mReflectionCamera->setPosition(mCamera->getDerivedPosition());
|
|
|
|
mReflectionCamera->setNearClipDistance(mCamera->getNearClipDistance());
|
|
|
|
mReflectionCamera->setFarClipDistance(mCamera->getFarClipDistance());
|
|
|
|
mReflectionCamera->setAspectRatio(mCamera->getAspectRatio());
|
|
|
|
mReflectionCamera->setFOVy(mCamera->getFOVy());
|
2012-04-18 23:08:26 +00:00
|
|
|
mReflectionRenderActive = true;
|
2012-04-06 13:51:57 +00:00
|
|
|
|
2012-04-18 23:08:26 +00:00
|
|
|
/// \todo For some reason this camera is delayed for 1 frame, which causes ugly sky reflection behaviour..
|
|
|
|
/// to circumvent this we just scale the sky up, so it's not that noticable
|
2012-04-05 13:30:55 +00:00
|
|
|
Vector3 pos = mCamera->getRealPosition();
|
|
|
|
pos.y = mTop*2 - pos.y;
|
|
|
|
mSky->setSkyPosition(pos);
|
2012-04-18 23:08:26 +00:00
|
|
|
mSky->scaleSky(mCamera->getFarClipDistance() / 5000.f);
|
|
|
|
mReflectionCamera->enableReflection(mWaterPlane);
|
2012-04-05 13:30:55 +00:00
|
|
|
}
|
2012-04-03 12:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Water::postRenderTargetUpdate(const RenderTargetEvent& evt)
|
|
|
|
{
|
2012-04-05 13:30:55 +00:00
|
|
|
if (evt.source == mReflectionTarget)
|
|
|
|
{
|
|
|
|
mSky->resetSkyPosition();
|
2012-04-06 13:05:17 +00:00
|
|
|
mSky->scaleSky(1);
|
2012-04-11 16:53:13 +00:00
|
|
|
mReflectionCamera->disableReflection();
|
2012-04-18 23:08:26 +00:00
|
|
|
mReflectionCamera->disableCustomNearClipPlane();
|
|
|
|
mReflectionRenderActive = false;
|
2012-04-05 13:30:55 +00:00
|
|
|
}
|
2012-03-29 16:33:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 13:51:57 +00:00
|
|
|
void Water::createMaterial()
|
2012-04-03 14:37:54 +00:00
|
|
|
{
|
2012-04-06 13:51:57 +00:00
|
|
|
mMaterial = MaterialManager::getSingleton().getByName("Water");
|
2012-04-03 17:10:02 +00:00
|
|
|
|
|
|
|
// these have to be set in code
|
2012-04-03 14:37:54 +00:00
|
|
|
std::string textureNames[32];
|
|
|
|
for (int i=0; i<32; ++i)
|
|
|
|
{
|
|
|
|
textureNames[i] = "textures\\water\\water" + StringConverter::toString(i, 2, '0') + ".dds";
|
|
|
|
}
|
2012-04-06 13:51:57 +00:00
|
|
|
mMaterial->getTechnique(1)->getPass(0)->getTextureUnitState(0)->setAnimatedTextureName(textureNames, 32, 2);
|
2012-04-03 17:10:02 +00:00
|
|
|
|
2012-04-03 19:16:43 +00:00
|
|
|
// use technique without shaders if reflection is disabled
|
|
|
|
if (mReflectionTarget == 0)
|
2012-04-06 13:51:57 +00:00
|
|
|
mMaterial->removeTechnique(0);
|
2012-04-03 19:16:43 +00:00
|
|
|
|
2012-04-05 17:27:54 +00:00
|
|
|
if (Settings::Manager::getBool("shader", "Water"))
|
2012-04-04 23:21:36 +00:00
|
|
|
{
|
2012-04-05 06:58:21 +00:00
|
|
|
CompositorInstance* compositor = CompositorManager::getSingleton().getCompositorChain(mViewport)->getCompositor("gbuffer");
|
2012-04-04 23:21:36 +00:00
|
|
|
|
|
|
|
TexturePtr colorTexture = compositor->getTextureInstance("mrt_output", 0);
|
2012-04-06 13:51:57 +00:00
|
|
|
TextureUnitState* tus = mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState("refractionMap");
|
2012-04-04 23:21:36 +00:00
|
|
|
if (tus != 0)
|
|
|
|
tus->setTexture(colorTexture);
|
|
|
|
|
|
|
|
TexturePtr depthTexture = compositor->getTextureInstance("mrt_output", 1);
|
2012-04-06 13:51:57 +00:00
|
|
|
tus = mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState("depthMap");
|
2012-04-04 23:21:36 +00:00
|
|
|
if (tus != 0)
|
|
|
|
tus->setTexture(depthTexture);
|
|
|
|
}
|
2012-04-03 14:37:54 +00:00
|
|
|
}
|
|
|
|
|
2012-04-05 13:30:55 +00:00
|
|
|
void Water::setViewportBackground(const ColourValue& bg)
|
|
|
|
{
|
|
|
|
if (mReflectionTarget)
|
|
|
|
mReflectionTarget->getViewport(0)->setBackgroundColour(bg);
|
|
|
|
}
|
|
|
|
|
2012-04-12 14:46:56 +00:00
|
|
|
void Water::updateVisible()
|
|
|
|
{
|
|
|
|
mWater->setVisible(mToggled && mActive);
|
|
|
|
if (mReflectionTarget)
|
|
|
|
mReflectionTarget->setActive(mToggled && mActive && !mIsUnderwater);
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:08:26 +00:00
|
|
|
void Water::renderQueueStarted (Ogre::uint8 queueGroupId, const Ogre::String &invocation, bool &skipThisInvocation)
|
|
|
|
{
|
|
|
|
// We don't want the sky to get clipped by custom near clip plane (the water plane)
|
|
|
|
if (queueGroupId < 20 && mReflectionRenderActive)
|
|
|
|
{
|
|
|
|
mReflectionCamera->disableCustomNearClipPlane();
|
|
|
|
Root::getSingleton().getRenderSystem()->_setProjectionMatrix(mReflectionCamera->getProjectionMatrixRS());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Water::renderQueueEnded (Ogre::uint8 queueGroupId, const Ogre::String &invocation, bool &repeatThisInvocation)
|
|
|
|
{
|
|
|
|
if (queueGroupId < 20 && mReflectionRenderActive)
|
|
|
|
{
|
|
|
|
mReflectionCamera->enableCustomNearClipPlane(mWaterPlane);
|
|
|
|
Root::getSingleton().getRenderSystem()->_setProjectionMatrix(mReflectionCamera->getProjectionMatrixRS());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Water::update()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-29 16:33:08 +00:00
|
|
|
} // namespace
|