相对于上一篇中的unBuffered Input在每一帧监听鼠标和键盘事件,buffered Input则是直接控制鼠标和键盘。
主要的代码:
void TutorialApplication::createScene(void)
{
mSceneMgr->setAmbientLight(Ogre::ColourValue(0.25, 0.25, 0.25));
Ogre::Entity* ninjaEntity = mSceneMgr->createEntity("Ninja", "ninja.mesh");
Ogre::SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode("NinjaNode");
node->attachObject(ninjaEntity);
Ogre::Light* pointLight = mSceneMgr->createLight("pointLight");
pointLight->setType(Ogre::Light::LT_POINT);
pointLight->setPosition(Ogre::Vector3(250, 150, 250));
pointLight->setDiffuseColour(Ogre::ColourValue::White);
pointLight->setSpecularColour(Ogre::ColourValue::White);
// Create the scene node
node = mSceneMgr->getRootSceneNode()->createChildSceneNode("CamNode1", Ogre::Vector3(-400, 200, 400));
// Make it look towards the ninja
node->yaw(Ogre::Degree(-45));
// Create the pitch node
node = node->createChildSceneNode("PitchNode1");
node->attachObject(mCamera);
// create the second camera node/pitch node
node = mSceneMgr->getRootSceneNode()->createChildSceneNode("CamNode2", Ogre::Vector3(0, 200, 400));
node = node->createChildSceneNode("PitchNode2");
}
//-------------------------------------------------------------------------------------
void TutorialApplication::createFrameListener(void)
{
BaseApplication::createFrameListener();
// Populate the camera container
mCamNode = mCamera->getParentSceneNode();
// set the rotation and move speed
mRotate = 0.13;
mMove = 250;
mDirection = Ogre::Vector3::ZERO;
}
// OIS::KeyListener
bool TutorialApplication::keyPressed( const OIS::KeyEvent& evt )
{
switch (evt.key)
{
//esc键退出
case OIS::KC_ESCAPE:
mShutDown = true;
break;
//1,2键切换视角
case OIS::KC_1:
mCamera->getParentSceneNode()->detachObject(mCamera);
mCamNode = mSceneMgr->getSceneNode("CamNode1");
mCamNode->attachObject(mCamera);
break;
case OIS::KC_2:
mCamera->getParentSceneNode()->detachObject(mCamera);
mCamNode = mSceneMgr->getSceneNode("CamNode2");
mCamNode->attachObject(mCamera);
break;
//wsad和上下左右
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z = -mMove;
break;
case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z = mMove;
break;
case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x = -mMove;
break;
case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x = mMove;
break;
case OIS::KC_PGDOWN:
case OIS::KC_E:
mDirection.y = -mMove;
break;
case OIS::KC_PGUP:
case OIS::KC_Q:
mDirection.y = mMove;
break;
default:
break;
}
return true;
}
bool TutorialApplication::keyReleased( const OIS::KeyEvent& evt )
{
switch (evt.key)
{
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z = 0;
break;
case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z = 0;
break;
case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x = 0;
break;
case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x = 0;
break;
case OIS::KC_PGDOWN:
case OIS::KC_E:
mDirection.y = 0;
break;
case OIS::KC_PGUP:
case OIS::KC_Q:
mDirection.y = 0;
break;
default:
break;
}
return true;
}
// OIS::MouseListener
bool TutorialApplication::mouseMoved( const OIS::MouseEvent& evt )
{
if (evt.state.buttonDown(OIS::MB_Right))
{
mCamNode->yaw(Ogre::Degree(-mRotate * evt.state.X.rel), Ogre::Node::TS_WORLD);
mCamNode->pitch(Ogre::Degree(-mRotate * evt.state.Y.rel), Ogre::Node::TS_LOCAL);
}
return true;
}
bool TutorialApplication::mousePressed( const OIS::MouseEvent& evt, OIS::MouseButtonID id )
{
Ogre::Light *light = mSceneMgr->getLight("pointLight");
switch (id)
{
case OIS::MB_Left:
light->setVisible(! light->isVisible());
break;
default:
break;
}
return true;
}
bool TutorialApplication::mouseReleased( const OIS::MouseEvent& evt, OIS::MouseButtonID id ){return true;}
//-------------------------------------------------------------------------------------
bool TutorialApplication::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
mCamNode->translate(mDirection * evt.timeSinceLastFrame, Ogre::Node::TS_LOCAL);
if (mWindow->isClosed()) return false;
if (mShutDown) return false;
mKeyboard->capture();
mMouse->capture();
mTrayMgr->frameRenderingQueued(evt);
return true;
}
最终效果:
2012-7-17