1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 08:49:55 +00:00
openmw-tes3mp/apps/openmw/mwrender/water.cpp

236 lines
7.8 KiB
C++
Raw Normal View History

2012-01-14 23:34:14 +00:00
#include "water.hpp"
#include <components/settings/settings.hpp>
2012-04-03 19:16:43 +00:00
#include "sky.hpp"
using namespace Ogre;
2012-01-14 23:34:14 +00:00
2012-03-29 13:49:24 +00:00
namespace MWRender
{
2012-04-03 19:16:43 +00:00
Water::Water (Ogre::Camera *camera, SkyManager* sky, const ESM::Cell* cell) :
mCamera (camera), mViewport (camera->getViewport()), mSceneManager (camera->getSceneManager()),
2012-04-03 13:13:47 +00:00
mIsUnderwater(false), mReflectDistance(0), mVisibilityFlags(0), mOldCameraFarClip(0),
mReflectionTarget(0), mActive(1)
{
2012-04-03 19:16:43 +00:00
mSky = sky;
try
{
CompositorManager::getSingleton().addCompositor(mViewport, "Water", -1);
CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", false);
} catch(...) {}
2012-03-29 13:49:24 +00:00
mTop = cell->water;
2012-03-29 13:49:24 +00:00
mIsUnderwater = false;
2012-03-29 13:49:24 +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
mWater = mSceneManager->createEntity("water");
2012-04-03 13:13:47 +00:00
mWater->setVisibilityFlags(RV_Water);
mWater->setRenderQueueGroup(RQG_Water);
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
mReflectDistance = Settings::Manager::getInt("reflect distance", "Water");
mWaterNode = mSceneManager->getRootSceneNode()->createChildSceneNode();
mWaterNode->setPosition(0, mTop, 0);
2012-03-29 13:49:24 +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
}
mWaterNode->attachObject(mWater);
2012-04-03 19:16:43 +00:00
// Create rendertarget for reflection
int rttsize = Settings::Manager::getInt("rtt size", "Water");
if (Settings::Manager::getBool("shader", "Water"))
{
TexturePtr tex = TextureManager::getSingleton().createManual("WaterReflection",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, rttsize, rttsize, 0, PF_R8G8B8, TU_RENDERTARGET);
RenderTarget* rtt = tex->getBuffer()->getRenderTarget();
Viewport* vp = rtt->addViewport(mCamera);
vp->setOverlaysEnabled(false);
vp->setBackgroundColour(ColourValue(0.8f, 0.9f, 1.0f));
vp->setShadowsEnabled(false);
vp->setVisibilityMask( mVisibilityFlags );
rtt->addListener(this);
rtt->setActive(true);
mReflectionTarget = rtt;
}
2012-04-06 13:51:57 +00:00
createMaterial();
mWater->setMaterial(mMaterial);
}
2012-03-29 13:49:24 +00:00
2012-04-03 13:13:47 +00:00
void Water::setActive(bool active)
{
mActive = active;
if (mReflectionTarget) mReflectionTarget->setActive(active);
mWater->setVisible(active);
}
2012-01-22 21:38:10 +00:00
Water::~Water()
{
MeshManager::getSingleton().remove("water");
2012-01-14 23:34:14 +00:00
mWaterNode->detachObject(mWater);
mSceneManager->destroyEntity(mWater);
mSceneManager->destroySceneNode(mWaterNode);
2012-01-14 23:34:14 +00:00
CompositorManager::getSingleton().removeCompositorChain(mViewport);
}
2012-01-20 22:59:56 +00:00
void Water::changeCell(const ESM::Cell* cell)
{
mTop = cell->water;
2012-01-14 23:34:14 +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
void Water::setHeight(const float height)
{
mTop = height;
mWaterNode->setPosition(0, height, 0);
}
2012-01-14 23:34:14 +00:00
void Water::toggle()
{
2012-04-03 13:13:47 +00:00
if (mActive)
mWater->setVisible(!mWater->getVisible());
}
2012-01-14 23:34:14 +00:00
void Water::checkUnderwater(float y)
{
2012-04-03 13:13:47 +00:00
if (!mActive) 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
{
try {
CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", false);
} catch(...) {}
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));
if (mReflectionTarget)
mReflectionTarget->setActive(mActive);
mIsUnderwater = false;
}
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
{
try {
CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", true);
} catch(...) {}
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));
if (mReflectionTarget)
mReflectionTarget->setActive(false);
mIsUnderwater = true;
2012-03-29 13:49:24 +00:00
}
2012-01-14 23:34:14 +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)
{
//mOldCameraFarClip = mCamera->getFarClipDistance();
//if (mReflectDistance != 0)
// mCamera->setFarClipDistance(mReflectDistance);
2012-04-03 13:13:47 +00:00
2012-04-05 13:30:55 +00:00
if (evt.source == mReflectionTarget)
{
2012-04-06 13:17:54 +00:00
mWater->setVisible(false);
2012-04-06 13:51:57 +00:00
// Some messy code to get the skybox to show up at all
// The problem here is that it gets clipped by the water plane
// Therefore scale it up a bit
2012-04-05 13:30:55 +00:00
Vector3 pos = mCamera->getRealPosition();
pos.y = mTop*2 - pos.y;
mSky->setSkyPosition(pos);
mSky->scaleSky(mCamera->getFarClipDistance() / 1000.f);
2012-04-06 13:51:57 +00:00
2012-04-05 13:30:55 +00:00
mCamera->enableCustomNearClipPlane(Plane(Vector3::UNIT_Y, mTop));
mCamera->enableReflection(Plane(Vector3::UNIT_Y, mTop));
}
}
void Water::postRenderTargetUpdate(const RenderTargetEvent& evt)
{
mWater->setVisible(true);
//mCamera->setFarClipDistance(mOldCameraFarClip);
2012-04-03 13:13:47 +00:00
2012-04-05 13:30:55 +00:00
if (evt.source == mReflectionTarget)
{
mSky->resetSkyPosition();
mSky->scaleSky(1);
2012-04-05 13:30:55 +00:00
mCamera->disableReflection();
mCamera->disableCustomNearClipPlane();
}
}
2012-04-06 13:51:57 +00:00
void Water::createMaterial()
{
2012-04-06 13:51:57 +00:00
mMaterial = MaterialManager::getSingleton().getByName("Water");
// these have to be set in code
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 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
if (Settings::Manager::getBool("shader", "Water"))
{
2012-04-05 06:58:21 +00:00
CompositorInstance* compositor = CompositorManager::getSingleton().getCompositorChain(mViewport)->getCompositor("gbuffer");
TexturePtr colorTexture = compositor->getTextureInstance("mrt_output", 0);
2012-04-06 13:51:57 +00:00
TextureUnitState* tus = mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState("refractionMap");
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");
if (tus != 0)
tus->setTexture(depthTexture);
}
}
2012-04-05 13:30:55 +00:00
void Water::setViewportBackground(const ColourValue& bg)
{
if (mReflectionTarget)
mReflectionTarget->getViewport(0)->setBackgroundColour(bg);
}
} // namespace