OpenMW
apps/openmw/mwrender/water.hpp
Go to the documentation of this file.
00001 #ifndef GAME_MWRENDER_WATER_H
00002 #define GAME_MWRENDER_WATER_H
00003 
00004 #include <OgrePlane.h>
00005 #include <OgreRenderQueue.h>
00006 #include <OgreRenderQueueListener.h>
00007 #include <OgreRenderTargetListener.h>
00008 #include <OgreMaterial.h>
00009 #include <OgreTexture.h>
00010 #include <OgreVector2.h>
00011 
00012 #include <components/esm/loadcell.hpp>
00013 #include <components/settings/settings.hpp>
00014 
00015 #include <extern/shiny/Main/MaterialInstance.hpp>
00016 
00017 
00018 #include "renderconst.hpp"
00019 
00020 #include "../mwworld/ptr.hpp"
00021 
00022 namespace Ogre
00023 {
00024     class Camera;
00025     class SceneManager;
00026     class SceneNode;
00027     class Entity;
00028     class Vector3;
00029     class Rectangle2D;
00030     struct RenderTargetEvent;
00031 }
00032 
00033 namespace MWRender {
00034 
00035     class SkyManager;
00036     class RenderingManager;
00037     class RippleSimulation;
00038     class Refraction;
00039 
00040     class Reflection
00041     {
00042     public:
00043         Reflection(Ogre::SceneManager* sceneManager)
00044             : mSceneMgr(sceneManager)
00045             , mIsUnderwater(false)
00046             , mCamera(NULL)
00047             , mParentCamera(NULL)
00048             {}
00049         virtual ~Reflection() {}
00050 
00051         virtual void setHeight (float height) {}
00052         virtual void setParentCamera (Ogre::Camera* parent) { mParentCamera = parent; }
00053         void setUnderwater(bool underwater) { mIsUnderwater = underwater; }
00054         virtual void setActive (bool active) {}
00055         virtual void setViewportBackground(Ogre::ColourValue colour) {}
00056         virtual void update() {}
00057         virtual void setVisibilityMask (int flags) {}
00058 
00059     protected:
00060         Ogre::Camera* mCamera;
00061         Ogre::Camera* mParentCamera;
00062         Ogre::TexturePtr mTexture;
00063         Ogre::SceneManager* mSceneMgr;
00064         bool mIsUnderwater;
00065     };
00066 
00067     class CubeReflection : public Reflection
00068     {
00069     public:
00070         CubeReflection(Ogre::SceneManager* sceneManager);
00071         virtual ~CubeReflection();
00072 
00073         virtual void update();
00074     protected:
00075         Ogre::RenderTarget* mRenderTargets[6];
00076     };
00077 
00078     class PlaneReflection : public Reflection, public Ogre::RenderQueueListener, public Ogre::RenderTargetListener
00079     {
00080     public:
00081         PlaneReflection(Ogre::SceneManager* sceneManager, SkyManager* sky);
00082         virtual ~PlaneReflection();
00083 
00084         virtual void setHeight (float height);
00085         virtual void setActive (bool active);
00086         virtual void setVisibilityMask (int flags);
00087 
00088         void preRenderTargetUpdate(const Ogre::RenderTargetEvent& evt);
00089         void postRenderTargetUpdate(const Ogre::RenderTargetEvent& evt);
00090 
00091         void renderQueueStarted (Ogre::uint8 queueGroupId, const Ogre::String &invocation, bool &skipThisInvocation);
00092         void renderQueueEnded (Ogre::uint8 queueGroupId, const Ogre::String &invocation, bool &repeatThisInvocation);
00093 
00094         virtual void setViewportBackground(Ogre::ColourValue colour);
00095 
00096     protected:
00097         Ogre::RenderTarget* mRenderTarget;
00098         SkyManager* mSky;
00099         Ogre::Plane mWaterPlane;
00100         Ogre::Plane mErrorPlane;
00101         Ogre::Plane mErrorPlaneUnderwater;
00102         bool mRenderActive;
00103     };
00104 
00106     class Water : public sh::MaterialInstanceListener
00107     {
00108         static const int CELL_SIZE = 8192;
00109         Ogre::Camera *mCamera;
00110         Ogre::SceneManager *mSceneMgr;
00111 
00112         Ogre::Plane mWaterPlane;
00113 
00114         Ogre::SceneNode *mWaterNode;
00115         Ogre::Entity *mWater;
00116 
00117         bool mIsUnderwater;
00118         bool mActive;
00119         bool mToggled;
00120         int mTop;
00121 
00122         float mWaterTimer;
00123 
00124 
00125         Ogre::Vector3 getSceneNodeCoordinates(int gridX, int gridY);
00126 
00127     protected:
00128         void applyRTT();
00129         void applyVisibilityMask();
00130 
00131         void updateVisible();
00132 
00133         RenderingManager* mRendering;
00134         SkyManager* mSky;
00135 
00136         Ogre::MaterialPtr mMaterial;
00137 
00138         bool mUnderwaterEffect;
00139         int mVisibilityFlags;
00140 
00141         Reflection* mReflection;
00142         Refraction* mRefraction;
00143         RippleSimulation* mSimulation;
00144 
00145         Ogre::Vector2 mPlayer;
00146 
00147     public:
00148         Water (Ogre::Camera *camera, RenderingManager* rend);
00149         ~Water();
00150 
00151         void setActive(bool active);
00152 
00153         void toggle();
00154         void update(float dt, Ogre::Vector3 player);
00155         void frameStarted(float dt);
00156 
00158         void addEmitter (const MWWorld::Ptr& ptr, float scale = 1.f, float force = 1.f);
00159         void removeEmitter (const MWWorld::Ptr& ptr);
00160         void updateEmitterPtr (const MWWorld::Ptr& old, const MWWorld::Ptr& ptr);
00161 
00162         void setViewportBackground(const Ogre::ColourValue& bg);
00163 
00164         void processChangedSettings(const Settings::CategorySettingVector& settings);
00165 
00167         void updateUnderwater(bool underwater);
00168         void changeCell(const ESM::Cell* cell);
00169         void setHeight(const float height);
00170 
00171         virtual void requestedConfiguration (sh::MaterialInstance* m, const std::string& configuration);
00172         virtual void createdConfiguration (sh::MaterialInstance* m, const std::string& configuration);
00173 
00174     };
00175 
00176 }
00177 
00178 #endif