#ifndef MAT_WATER_H
#define MAT_WATER_H
#include <Ogre.h>
class App;
class btGhostObject;
class Water : public /* Ogre::FrameListener, */ Ogre::RenderTargetListener {
	Ogre::SceneManager *mScnMgr;
	Ogre::RenderWindow *mWindow;
	Ogre::SceneNode *mWaterNode, *mCameraNode;
	Ogre::Vector3 mCameraPosition;
	Ogre::Camera *mCamera;
	Ogre::Plane mWaterPlane, mReflectionPlane, mReflectionClipPlaneAbove,
		mReflectionClipPlaneBelow, mRefractionClipPlaneAbove,
		mRefractionClipPlaneBelow;
	bool mAbove;
	std::vector<Ogre::Entity *> mWaterMeshes;
	std::vector<Ogre::Viewport *> mViewports;
	// Reflection
	Ogre::RenderTexture *mReflectionTexture;
	Ogre::Camera *mReflectionCamera;
	// Refraction
	// Ogre::RenderTexture * mRefractionTexture;
	Ogre::Camera *mRefractionCamera;
	bool mInRefTexUpdate;
	Ogre::Timer mtexture_dump;
	void create_cameras();
#if 0
	btGhostObject *mWaterBody;
	Ogre::Bullet::DynamicsWorld *mDynWorld;
	std::set<btCollisionObject *> mOverlaps;
#endif

public:
	Water();
	virtual ~Water();
	void create_textures();
	void dump_textures();
	void createWater(
		Ogre::RenderWindow *window,
		Ogre::Camera *camera /*, Ogre::Bullet::DynamicsWorld *world */);
	void init();
	void updateWater(float delta);
	/* bool frameEnded(const Ogre::FrameEvent &evt) override; */
	/* bool frameRenderingQueued(const Ogre::FrameEvent &evt) override; */
	void preRenderTargetUpdate(const Ogre::RenderTargetEvent &evt) override;
	void
	postRenderTargetUpdate(const Ogre::RenderTargetEvent &evt) override;
	void add_submerged_entity(Ogre::Entity *ent);
	void add_surface_entity(Ogre::Entity *ent);
#if 0
	bool isInWater(const btCollisionObject *body) const
	{
		btCollisionObject *test = const_cast<btCollisionObject *>(body);
		return mOverlaps.find(test) != mOverlaps.end();
	}
#endif
};
#endif
