Making this work
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <OgreMeshLodGenerator.h>
|
||||
#include <OgreCompositorManager.h>
|
||||
#include <OgreMaterialManager.h>
|
||||
#include <OgreBullet.h>
|
||||
#include "water.h"
|
||||
|
||||
/* TODO: use blender glb model for water shape.
|
||||
@@ -20,12 +21,19 @@ Water::Water()
|
||||
, mWindow(nullptr)
|
||||
, mCameraNode(nullptr)
|
||||
, mWaterPlane(Ogre::Vector3::UNIT_Y, 0)
|
||||
, mReflectionPlane(Ogre::Vector3(0.0, 1.0, 0.0), 0.0f /* water height */)
|
||||
, mReflectionClipPlaneAbove(Ogre::Vector3(0.0, 1.0, 0.0), 0.0f /* water height */ - 2.0f)
|
||||
, mReflectionClipPlaneBelow(Ogre::Vector3(0.0, -1.0, 0.0), -(0.0f /* water height */ + 2.0))
|
||||
, mRefractionClipPlaneAbove(Ogre::Vector3(0.0, -1.0, 0.0), -(0.0f /* water height */ + 2.0))
|
||||
, mRefractionClipPlaneBelow(Ogre::Vector3(0.0, 1.0, 0.0), 0.0f /* water height */ - 2.0)
|
||||
, mReflectionPlane(Ogre::Vector3(0.0, 1.0, 0.0),
|
||||
0.0f /* water height */)
|
||||
, mReflectionClipPlaneAbove(Ogre::Vector3(0.0, 1.0, 0.0),
|
||||
0.0f /* water height */ - 2.0f)
|
||||
, mReflectionClipPlaneBelow(Ogre::Vector3(0.0, -1.0, 0.0),
|
||||
-(0.0f /* water height */ + 2.0))
|
||||
, mRefractionClipPlaneAbove(Ogre::Vector3(0.0, -1.0, 0.0),
|
||||
-(0.0f /* water height */ + 2.0))
|
||||
, mRefractionClipPlaneBelow(Ogre::Vector3(0.0, 1.0, 0.0),
|
||||
0.0f /* water height */ - 2.0)
|
||||
, mAbove(true)
|
||||
, mWaterBody(nullptr)
|
||||
, mDynWorld(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -33,8 +41,16 @@ Water::~Water()
|
||||
{
|
||||
if (mWaterNode)
|
||||
mScnMgr->destroySceneNode(mWaterNode);
|
||||
if(mReflectionTexture)
|
||||
if (mReflectionTexture)
|
||||
mReflectionTexture->removeAllListeners();
|
||||
if (mWaterBody) {
|
||||
if (mWaterBody->getWorldArrayIndex() >= 0)
|
||||
if (mDynWorld)
|
||||
mDynWorld->getBtWorld()->removeCollisionObject(
|
||||
mWaterBody);
|
||||
delete mWaterBody;
|
||||
mWaterBody = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Water::create_cameras()
|
||||
@@ -48,9 +64,11 @@ void Water::create_cameras()
|
||||
mReflectionCamera->enableCustomNearClipPlane(mReflectionClipPlaneAbove);
|
||||
mReflectionCamera->enableReflection(mReflectionPlane);
|
||||
|
||||
Ogre::Viewport * reflectionViewport = mReflectionTexture->addViewport(mReflectionCamera, 0, 0, 0, 0.5f, 1.0f);
|
||||
Ogre::Viewport *reflectionViewport = mReflectionTexture->addViewport(
|
||||
mReflectionCamera, 0, 0, 0, 0.5f, 1.0f);
|
||||
reflectionViewport->setClearEveryFrame(true);
|
||||
reflectionViewport->setBackgroundColour(Ogre::ColourValue(0.0, 0.0, 1.0, 1.0));
|
||||
reflectionViewport->setBackgroundColour(
|
||||
Ogre::ColourValue(0.0, 0.0, 1.0, 1.0));
|
||||
reflectionViewport->setOverlaysEnabled(false);
|
||||
reflectionViewport->setSkiesEnabled(true);
|
||||
reflectionViewport->setAutoUpdated(false);
|
||||
@@ -63,9 +81,11 @@ void Water::create_cameras()
|
||||
mRefractionCamera->setFarClipDistance(mCamera->getFarClipDistance());
|
||||
mRefractionCamera->enableCustomNearClipPlane(mRefractionClipPlaneAbove);
|
||||
|
||||
Ogre::Viewport * refractionViewport = mReflectionTexture->addViewport(mRefractionCamera, 1, 0.5, 0, 0.5f, 1.0f);
|
||||
Ogre::Viewport *refractionViewport = mReflectionTexture->addViewport(
|
||||
mRefractionCamera, 1, 0.5, 0, 0.5f, 1.0f);
|
||||
refractionViewport->setClearEveryFrame(true);
|
||||
refractionViewport->setBackgroundColour(Ogre::ColourValue(0.0, 0.5, 1.0, 1.0));
|
||||
refractionViewport->setBackgroundColour(
|
||||
Ogre::ColourValue(0.0, 0.5, 1.0, 1.0));
|
||||
refractionViewport->setOverlaysEnabled(false);
|
||||
refractionViewport->setSkiesEnabled(false);
|
||||
refractionViewport->setAutoUpdated(false);
|
||||
@@ -74,13 +94,12 @@ void Water::create_cameras()
|
||||
|
||||
void Water::create_textures()
|
||||
{
|
||||
Ogre::TexturePtr reflectionTexture = Ogre::TextureManager::getSingleton().createManual(
|
||||
"ReflectionRefractionTexture",
|
||||
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
||||
Ogre::TEX_TYPE_2D, 512, 512,
|
||||
0,
|
||||
Ogre::PF_R8G8B8A8,
|
||||
Ogre::TU_RENDERTARGET);
|
||||
Ogre::TexturePtr reflectionTexture =
|
||||
Ogre::TextureManager::getSingleton().createManual(
|
||||
"ReflectionRefractionTexture",
|
||||
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
||||
Ogre::TEX_TYPE_2D, 512, 512, 0, Ogre::PF_R8G8B8A8,
|
||||
Ogre::TU_RENDERTARGET);
|
||||
|
||||
mReflectionTexture = reflectionTexture->getBuffer()->getRenderTarget();
|
||||
mReflectionTexture->setAutoUpdated(false);
|
||||
@@ -115,10 +134,12 @@ void Water::init()
|
||||
{ -1, 0, 1 }, { 1, 0, -1 }, { 1, 0, 1 } };
|
||||
mWaterNode = mScnMgr->getRootSceneNode()->createChildSceneNode("Water");
|
||||
|
||||
auto mat = Ogre::MaterialManager::getSingleton().getByName("Water/Above");
|
||||
auto mat =
|
||||
Ogre::MaterialManager::getSingleton().getByName("Water/Above");
|
||||
mat->load();
|
||||
mat->setReceiveShadows(false);
|
||||
auto mat2 = Ogre::MaterialManager::getSingleton().getByName("Water/Below");
|
||||
auto mat2 =
|
||||
Ogre::MaterialManager::getSingleton().getByName("Water/Below");
|
||||
mat2->load();
|
||||
mat2->setReceiveShadows(false);
|
||||
#if 0
|
||||
@@ -146,9 +167,11 @@ void Water::init()
|
||||
water_ent->setMaterial(mat);
|
||||
mWaterNode->attachObject(water_ent);
|
||||
mWaterMeshes.push_back(water_ent);
|
||||
mDynWorld->attachCollisionObject(mWaterBody, water_ent, 1, 0x7FFFFFFF);
|
||||
}
|
||||
|
||||
void Water::createWater(Ogre::RenderWindow * window, Ogre::Camera *camera)
|
||||
void Water::createWater(Ogre::RenderWindow *window, Ogre::Camera *camera,
|
||||
Ogre::Bullet::DynamicsWorld *world)
|
||||
{
|
||||
int i;
|
||||
mCamera = camera;
|
||||
@@ -158,6 +181,17 @@ void Water::createWater(Ogre::RenderWindow * window, Ogre::Camera *camera)
|
||||
mWindow = window;
|
||||
mCameraPosition = mCameraNode->getPosition();
|
||||
create_textures();
|
||||
mDynWorld = world;
|
||||
mWaterBody = new btGhostObject;
|
||||
btBoxShape *boxShape = new btBoxShape(btVector3(1000, 1000, 1000));
|
||||
btCompoundShape *shape = new btCompoundShape;
|
||||
shape->addChildShape(
|
||||
btTransform(btQuaternion(), btVector3(0, -1000, 0)), boxShape);
|
||||
mWaterBody->setCollisionShape(shape);
|
||||
mWaterBody->setCollisionFlags(
|
||||
mWaterBody->getCollisionFlags() |
|
||||
btCollisionObject::CF_NO_CONTACT_RESPONSE |
|
||||
btCollisionObject::CF_STATIC_OBJECT);
|
||||
}
|
||||
|
||||
void Water::updateWater(float delta)
|
||||
|
||||
@@ -2,37 +2,38 @@
|
||||
#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::RenderWindow *mWindow;
|
||||
Ogre::SceneNode *mWaterNode, *mCameraNode;
|
||||
Ogre::Vector3 mCameraPosition;
|
||||
Ogre::Camera *mCamera;
|
||||
Ogre::Plane mWaterPlane,
|
||||
mReflectionPlane,
|
||||
mReflectionClipPlaneAbove,
|
||||
mReflectionClipPlaneBelow,
|
||||
mRefractionClipPlaneAbove,
|
||||
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;
|
||||
Ogre::RenderTexture *mReflectionTexture;
|
||||
Ogre::Camera *mReflectionCamera;
|
||||
// Refraction
|
||||
// Ogre::RenderTexture * mRefractionTexture;
|
||||
Ogre::Camera * mRefractionCamera;
|
||||
Ogre::Camera *mRefractionCamera;
|
||||
bool mInRefTexUpdate;
|
||||
Ogre::Timer mtexture_dump;
|
||||
void create_cameras();
|
||||
btGhostObject *mWaterBody;
|
||||
Ogre::Bullet::DynamicsWorld *mDynWorld;
|
||||
|
||||
public:
|
||||
Water();
|
||||
virtual ~Water();
|
||||
void create_textures();
|
||||
void dump_textures();
|
||||
void createWater(Ogre::RenderWindow * window, Ogre::Camera *camera);
|
||||
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; */
|
||||
|
||||
Reference in New Issue
Block a user