openmw-tes3coop/apps/openmw/mwrender/water.cpp

115 lines
3.2 KiB
C++
Raw Normal View History

2012-01-14 23:34:14 +00:00
#include "water.hpp"
namespace MWRender {
2012-01-22 01:05:08 +00:00
Water::Water (Ogre::Camera *camera, const ESM::Cell* cell) : mCamera (camera), mViewport (camera->getViewport()), mSceneManager (camera->getSceneManager()) {
2012-01-14 23:34:14 +00:00
try {
Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "Water", -1);
Ogre::CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", false);
} catch(...) {
}
2012-01-22 01:05:08 +00:00
mTop = cell->water;
std::cout << "Making water\n";
2012-01-14 23:34:14 +00:00
mIsUnderwater = false;
mCamera->addListener(this);
2012-01-20 22:59:56 +00:00
mWaterPlane = Ogre::Plane(Ogre::Vector3::UNIT_Y, mTop);
2012-01-22 01:05:08 +00:00
Ogre::MeshManager::getSingleton().createPlane("water", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, mWaterPlane, CELL_SIZE*3 + 10000, CELL_SIZE * 3 + 10000, 10, 10, true, 1, 3,5, Ogre::Vector3::UNIT_Z);
2012-01-20 04:29:07 +00:00
mWater = mSceneManager->createEntity("water");
2012-01-20 22:59:56 +00:00
2012-01-20 04:29:07 +00:00
mWater->setMaterialName("Examples/Water0");
2012-01-19 03:42:07 +00:00
2012-01-14 23:34:14 +00:00
mWaterNode = mSceneManager->getRootSceneNode()->createChildSceneNode();
2012-01-22 01:05:08 +00:00
//if(!(cell->data.flags & cell->Interior))
//{
mWaterNode->setPosition(getSceneNodeCoordinates(cell->data.gridX, cell->data.gridY));
//}
//else
// mWaterNode->setPosition(10000, 0, 10000); //Don't mess with y
2012-01-14 23:34:14 +00:00
mWaterNode->attachObject(mWater);
}
Water::~Water() {
2012-01-20 04:29:07 +00:00
mCamera->removeListener(this);
mWaterNode->detachObject(mWater);
mSceneManager->destroyEntity(mWater);
mSceneManager->destroySceneNode(mWaterNode);
2012-01-14 23:34:14 +00:00
Ogre::MeshManager::getSingleton().remove("water");
2012-01-19 03:42:07 +00:00
//Ogre::TextureManager::getSingleton().remove("refraction");
//Ogre::TextureManager::getSingleton().remove("reflection");
2012-01-14 23:34:14 +00:00
Ogre::CompositorManager::getSingleton().removeCompositorChain(mViewport);
}
void Water::preRenderTargetUpdate(const Ogre::RenderTargetEvent& evt) {
mWater->setVisible(false);
if (evt.source == mReflectionTarget) {
mCamera->enableReflection(mWaterPlane);
} else {
}
}
void Water::postRenderTargetUpdate(const Ogre::RenderTargetEvent& evt) {
mWater->setVisible(true);
if (evt.source == mReflectionTarget) {
mCamera->disableReflection();
} else {
}
}
2012-01-20 22:59:56 +00:00
void Water::checkUnderwater(float y) {
if (mIsUnderwater && y > mTop) {
2012-01-14 23:34:14 +00:00
try {
Ogre::CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", false);
} catch(...) {
}
mIsUnderwater = false;
}
2012-01-20 22:59:56 +00:00
if (!mIsUnderwater && y < mTop) {
2012-01-14 23:34:14 +00:00
try {
Ogre::CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", true);
} catch(...) {
}
mIsUnderwater = true;
}
}
void Water::cameraPreRenderScene(Ogre::Camera *cam) {
Ogre::Vector3 pos = cam->getPosition();
if (pos != mOldCameraPos) {
mWaterNode->setPosition(pos.x, 0, pos.z);
mOldCameraPos = pos;
}
}
void Water::cameraPostRenderScene(Ogre::Camera *cam) {
}
void Water::cameraDestroyed(Ogre::Camera *cam) {
}
2012-01-22 01:05:08 +00:00
Ogre::Vector3 Water::getSceneNodeCoordinates(int gridX, int gridY){
Ogre::Vector3 out = Ogre::Vector3(gridX * CELL_SIZE + (CELL_SIZE / 2), 0, -gridY * CELL_SIZE - (CELL_SIZE / 2));
return out;
}
2012-01-14 23:34:14 +00:00
}