From 79c2a2e465a13bbe3d0cdbbcfaaffa4358152175 Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 29 Mar 2012 18:33:08 +0200 Subject: [PATCH] implemented toggleWater (twa); formatting again --- apps/openmw/mwrender/renderingmanager.cpp | 17 ++- apps/openmw/mwrender/renderingmanager.hpp | 2 + apps/openmw/mwrender/water.cpp | 127 ++++++++++++---------- apps/openmw/mwrender/water.hpp | 43 ++++---- apps/openmw/mwscript/docs/vmformat.txt | 3 +- apps/openmw/mwscript/miscextensions.cpp | 17 +++ apps/openmw/mwworld/world.cpp | 5 + apps/openmw/mwworld/world.hpp | 1 + 8 files changed, 126 insertions(+), 89 deletions(-) diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index 4b197205a..775048110 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -92,19 +92,26 @@ OEngine::Render::Fader* RenderingManager::getFader() return mRendering.getFader(); } -void RenderingManager::removeCell (MWWorld::Ptr::CellStore *store){ - - mObjects.removeCell(store); +void RenderingManager::removeCell (MWWorld::Ptr::CellStore *store) +{ + mObjects.removeCell(store); mActors.removeCell(store); - } -void RenderingManager::removeWater (){ + +void RenderingManager::removeWater () +{ if(mWater){ delete mWater; mWater = 0; } } +void RenderingManager::toggleWater() +{ + if (mWater) + mWater->toggle(); +} + void RenderingManager::cellAdded (MWWorld::Ptr::CellStore *store) { mObjects.buildStaticGeometry (*store); diff --git a/apps/openmw/mwrender/renderingmanager.hpp b/apps/openmw/mwrender/renderingmanager.hpp index 8a6a7aed1..7d66aa350 100644 --- a/apps/openmw/mwrender/renderingmanager.hpp +++ b/apps/openmw/mwrender/renderingmanager.hpp @@ -91,8 +91,10 @@ class RenderingManager: private RenderingInterface { void moveObject (const MWWorld::Ptr& ptr, const Ogre::Vector3& position); void scaleObject (const MWWorld::Ptr& ptr, const Ogre::Vector3& scale); void rotateObject (const MWWorld::Ptr& ptr, const::Ogre::Quaternion& orientation); + void checkUnderwater(); void setWaterHeight(const float height); + void toggleWater(); /// \param store Cell the object was in previously (\a ptr has already been updated to the new cell). void moveObjectToCell (const MWWorld::Ptr& ptr, const Ogre::Vector3& position, MWWorld::Ptr::CellStore *store); diff --git a/apps/openmw/mwrender/water.cpp b/apps/openmw/mwrender/water.cpp index c77a99006..9de55e3a5 100644 --- a/apps/openmw/mwrender/water.cpp +++ b/apps/openmw/mwrender/water.cpp @@ -2,87 +2,94 @@ namespace MWRender { - Water::Water (Ogre::Camera *camera, const ESM::Cell* cell) : mCamera (camera), mViewport (camera->getViewport()), mSceneManager (camera->getSceneManager()) - { - try - { - Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "Water", -1); - Ogre::CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", false); - } catch(...) {} - mTop = cell->water; +Water::Water (Ogre::Camera *camera, const ESM::Cell* cell) : + mCamera (camera), mViewport (camera->getViewport()), mSceneManager (camera->getSceneManager()), + mIsUnderwater(false) +{ + try + { + Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "Water", -1); + Ogre::CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", false); + } catch(...) {} - mIsUnderwater = false; + mTop = cell->water; - mWaterPlane = Ogre::Plane(Ogre::Vector3::UNIT_Y, 0); + mIsUnderwater = false; - Ogre::MeshManager::getSingleton().createPlane("water", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, mWaterPlane, CELL_SIZE*5, CELL_SIZE * 5, 10, 10, true, 1, 3,5, Ogre::Vector3::UNIT_Z); + mWaterPlane = Ogre::Plane(Ogre::Vector3::UNIT_Y, 0); + Ogre::MeshManager::getSingleton().createPlane("water", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, mWaterPlane, CELL_SIZE*5, CELL_SIZE * 5, 10, 10, true, 1, 3,5, Ogre::Vector3::UNIT_Z); - mWater = mSceneManager->createEntity("water"); + mWater = mSceneManager->createEntity("water"); - mWater->setMaterialName("Examples/Water0"); + mWater->setMaterialName("Examples/Water0"); - mWaterNode = mSceneManager->getRootSceneNode()->createChildSceneNode(); - mWaterNode->setPosition(0, mTop, 0); + mWaterNode = mSceneManager->getRootSceneNode()->createChildSceneNode(); + mWaterNode->setPosition(0, mTop, 0); - if(!(cell->data.flags & cell->Interior)) - { - mWaterNode->setPosition(getSceneNodeCoordinates(cell->data.gridX, cell->data.gridY)); - } - mWaterNode->attachObject(mWater); + if(!(cell->data.flags & cell->Interior)) + { + mWaterNode->setPosition(getSceneNodeCoordinates(cell->data.gridX, cell->data.gridY)); } + mWaterNode->attachObject(mWater); +} - Water::~Water() - { - Ogre::MeshManager::getSingleton().remove("water"); +Water::~Water() +{ + Ogre::MeshManager::getSingleton().remove("water"); - mWaterNode->detachObject(mWater); - mSceneManager->destroyEntity(mWater); - mSceneManager->destroySceneNode(mWaterNode); + mWaterNode->detachObject(mWater); + mSceneManager->destroyEntity(mWater); + mSceneManager->destroySceneNode(mWaterNode); - Ogre::CompositorManager::getSingleton().removeCompositorChain(mViewport); - } + Ogre::CompositorManager::getSingleton().removeCompositorChain(mViewport); +} - void Water::changeCell(const ESM::Cell* cell) - { - mTop = cell->water; +void Water::changeCell(const ESM::Cell* cell) +{ + mTop = cell->water; - if(!(cell->data.flags & cell->Interior)) - mWaterNode->setPosition(getSceneNodeCoordinates(cell->data.gridX, cell->data.gridY)); - else - setHeight(mTop); - } + if(!(cell->data.flags & cell->Interior)) + mWaterNode->setPosition(getSceneNodeCoordinates(cell->data.gridX, cell->data.gridY)); + else + setHeight(mTop); +} - void Water::setHeight(const float height) - { - mTop = height; - mWaterNode->setPosition(0, height, 0); - } +void Water::setHeight(const float height) +{ + mTop = height; + mWaterNode->setPosition(0, height, 0); +} +void Water::toggle() +{ + mWater->setVisible(!mWater->getVisible()); +} - void Water::checkUnderwater(float y) +void Water::checkUnderwater(float y) +{ + if ((mIsUnderwater && y > mTop) || !mWater->isVisible()) { - if (mIsUnderwater && y > mTop) - { - try { - Ogre::CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", false); - } catch(...) {} - mIsUnderwater = false; - } - - if (!mIsUnderwater && y < mTop) - { - try { - Ogre::CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", true); - } catch(...) {} - mIsUnderwater = true; - } - } + try { + Ogre::CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", false); + } catch(...) {} + mIsUnderwater = false; + } - Ogre::Vector3 Water::getSceneNodeCoordinates(int gridX, int gridY) + if (!mIsUnderwater && y < mTop && mWater->isVisible()) { - return Ogre::Vector3(gridX * CELL_SIZE + (CELL_SIZE / 2), mTop, -gridY * CELL_SIZE - (CELL_SIZE / 2)); + try { + Ogre::CompositorManager::getSingleton().setCompositorEnabled(mViewport, "Water", true); + } catch(...) {} + mIsUnderwater = true; } } + +Ogre::Vector3 Water::getSceneNodeCoordinates(int gridX, int gridY) +{ + return Ogre::Vector3(gridX * CELL_SIZE + (CELL_SIZE / 2), mTop, -gridY * CELL_SIZE - (CELL_SIZE / 2)); +} + +} // namespace diff --git a/apps/openmw/mwrender/water.hpp b/apps/openmw/mwrender/water.hpp index fc644e265..5a5d1cca0 100644 --- a/apps/openmw/mwrender/water.hpp +++ b/apps/openmw/mwrender/water.hpp @@ -7,37 +7,34 @@ namespace MWRender { /// Water rendering - class Water : Ogre::RenderTargetListener, Ogre::Camera::Listener { - static const int CELL_SIZE = 8192; - Ogre::Camera *mCamera; - Ogre::SceneManager *mSceneManager; - Ogre::Viewport *mViewport; + class Water : Ogre::RenderTargetListener, Ogre::Camera::Listener + { + static const int CELL_SIZE = 8192; + Ogre::Camera *mCamera; + Ogre::SceneManager *mSceneManager; + Ogre::Viewport *mViewport; - + Ogre::Plane mWaterPlane; + Ogre::SceneNode *mWaterNode; + Ogre::Entity *mWater; - Ogre::Plane mWaterPlane; - Ogre::SceneNode *mWaterNode; - Ogre::Entity *mWater; + bool mIsUnderwater; + int mTop; - Ogre::Vector3 mOldCameraPos; - bool mIsUnderwater; - int mTop; + Ogre::Vector3 getSceneNodeCoordinates(int gridX, int gridY); + public: + Water (Ogre::Camera *camera, const ESM::Cell* cell); + ~Water(); + void toggle(); - - Ogre::Vector3 getSceneNodeCoordinates(int gridX, int gridY); - - public: - - Water (Ogre::Camera *camera, const ESM::Cell* cell); - ~Water(); + void checkUnderwater(float y); + void changeCell(const ESM::Cell* cell); + void setHeight(const float height); - void checkUnderwater(float y); - void changeCell(const ESM::Cell* cell); - void setHeight(const float height); + }; - }; } #endif diff --git a/apps/openmw/mwscript/docs/vmformat.txt b/apps/openmw/mwscript/docs/vmformat.txt index 64bd43c6c..7ff60a203 100644 --- a/apps/openmw/mwscript/docs/vmformat.txt +++ b/apps/openmw/mwscript/docs/vmformat.txt @@ -126,4 +126,5 @@ op 0x2000140: ChangeWeather op 0x2000141: GetWaterLevel op 0x2000142: SetWaterLevel op 0x2000143: ModWaterLevel -opcodes 0x2000142-0x3ffffff unused +op 0x2000144: ToggleWater, twa +opcodes 0x2000145-0x3ffffff unused diff --git a/apps/openmw/mwscript/miscextensions.cpp b/apps/openmw/mwscript/miscextensions.cpp index 5bfffd3a2..5109319ed 100644 --- a/apps/openmw/mwscript/miscextensions.cpp +++ b/apps/openmw/mwscript/miscextensions.cpp @@ -175,6 +175,19 @@ namespace MWScript } }; + class OpToggleWater : public Interpreter::Opcode0 + { + public: + + virtual void execute (Interpreter::Runtime& runtime) + { + InterpreterContext& context = + static_cast (runtime.getContext()); + + context.getWorld().toggleWater(); + } + }; + const int opcodeXBox = 0x200000c; const int opcodeOnActivate = 0x200000d; const int opcodeActivate = 0x2000075; @@ -187,6 +200,7 @@ namespace MWScript const int opcodeFadeIn = 0x200013c; const int opcodeFadeOut = 0x200013d; const int opcodeFadeTo = 0x200013e; + const int opcodeToggleWater = 0x2000144; void registerExtensions (Compiler::Extensions& extensions) { @@ -204,6 +218,8 @@ namespace MWScript extensions.registerInstruction ("fadein", "f", opcodeFadeIn); extensions.registerInstruction ("fadeout", "f", opcodeFadeOut); extensions.registerInstruction ("fadeto", "ff", opcodeFadeTo); + extensions.registerInstruction ("togglewater", "", opcodeToggleWater); + extensions.registerInstruction ("twa", "", opcodeToggleWater); } void installOpcodes (Interpreter::Interpreter& interpreter) @@ -220,6 +236,7 @@ namespace MWScript interpreter.installSegment5 (opcodeFadeIn, new OpFadeIn); interpreter.installSegment5 (opcodeFadeOut, new OpFadeOut); interpreter.installSegment5 (opcodeFadeTo, new OpFadeTo); + interpreter.installSegment5 (opcodeToggleWater, new OpToggleWater); } } } diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index c62e2e05d..e6d44dd8c 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -761,4 +761,9 @@ namespace MWWorld mRendering->setWaterHeight(height); } + void World::toggleWater() + { + mRendering->toggleWater(); + } + } diff --git a/apps/openmw/mwworld/world.hpp b/apps/openmw/mwworld/world.hpp index 5025fded4..bfc33ae3d 100644 --- a/apps/openmw/mwworld/world.hpp +++ b/apps/openmw/mwworld/world.hpp @@ -114,6 +114,7 @@ namespace MWWorld Ptr::CellStore *getInterior (const std::string& name); void setWaterHeight(const float height); + void toggleWater(); void adjustSky();