Tutorial 10         Splish Splash I was taking a bath

Splish Splash I was taking a bath

Figured I would add one last thing into this tutorial set.

Go to the bottom of the TutorialApplication::createScene method. Now add the following code:

// Water
Entity *pWaterEntity;
Plane nWaterPlane;

// create a water plane/scene node
nWaterPlane.normal = Vector3::UNIT_Y;
nWaterPlane.d = -1.5;
MeshManager::getSingleton().createPlane(
    "WaterPlane",
    ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
    nWaterPlane,
    8000, 8000,
    20, 20,
    true, 1,
    10, 10,
    Vector3::UNIT_Z);


To simulate water, we want to create a flat plane with a texture. Here we make a plane that is 8000 units by 8000 units. Each side of the plane has 20 control points. Now we have to add the water to the scene. We do that like this:

pWaterEntity = mSceneMgr->createEntity("water", "WaterPlane");
pWaterEntity->setMaterialName("Examples/TextureEffect4");
SceneNode *waterNode =
    mSceneMgr->getRootSceneNode()->createChildSceneNode("WaterNode");
waterNode->attachObject(pWaterEntity);
waterNode->translate(4000, 50, 4000);


The material Examples/TextureEffect4 requires a light before you can see it. So we have to add a light as well.

// Create a light
Light* pLight = mSceneMgr->createLight("MainLight");
pLight->setType( Light::LT_DIRECTIONAL );
pLight->setDirection( 0, -100, 0 );


So we create a light and projects parallel rays of light in the downward direction. Now go to the TutorialFrameListner.cpp and add the following two lines before the constructor.

#define FLOW_SPEED 0.4
#define FLOW_HEIGHT 10

Last bit of code. YEH! Go to the bottom of the TutorialFrameListener::frameStarted() method. Enter the following code before the
return true.

float fWaterFlow = FLOW_SPEED * evt.timeSinceLastFrame;
static float fFlowAmount = 0.0f;
static bool fFlowUp = true;
SceneNode *pWaterNode = static_cast<SceneNode*>(
    mCamera->getSceneManager()->getRootSceneNode()->getChild("WaterNode"));
if(pWaterNode)
{
    if(fFlowUp)
        fFlowAmount += fWaterFlow;
    else
        fFlowAmount -= fWaterFlow;

    if(fFlowAmount >= FLOW_HEIGHT)
        fFlowUp = false;
    else if(fFlowAmount <= 0.0f)
        fFlowUp = true;

    pWaterNode->translate(0, (fFlowUp ? fWaterFlow : -fWaterFlow), 0);
}

Some explanation is required here. Basically what this code does is gently moves the water up and down. Giving the illusion of a tide, or swell. The other bit of interesting code, is how we get the Water SceneNode. Instead of passing the node in, you can query the scene manager for the node with a specific name. In this case ‘WaterNode’. I would not suggest doing it this way. If you had passed the SceneNode in, then you don’t have to run the query each frame, thus speeding up the frames. So Compile and Run the code, you should see a screen something like:

xorekis10_1.png

This completes this pack of tutorials. I have lots in mind I still want to cover. Anyway, play with what we have created. If you do something interesting, I would be interested in adding it to the tutorials.

Until next tutorial.... wink

'Xorekis'