ITutorial01.h /* ----------------------------------------------------------------------------- Filename: ITutorial01.h ----------------------------------------------------------------------------- This source file is generated by the ___ _ __ __ _ _ /___\__ _ _ __ ___ /_\ _ __ _ __/ / /\ \ (_)______ _ _ __ __| | // // _` | '__/ _ \ //_\\| '_ \| '_ \ \/ \/ / |_ / _` | '__/ _` | / \_// (_| | | | __/ / _ \ |_) | |_) \ /\ /| |/ / (_| | | | (_| | \___/ \__, |_| \___| \_/ \_/ .__/| .__/ \/ \/ |_/___\__,_|_| \__,_| |___/ |_| |_| Ogre 1.7.x Application Wizard for VC9 (January 2010) http://code.google.com/p/ogreappwizards/ ----------------------------------------------------------------------------- */ #ifndef __ITutorial01_h_ #define __ITutorial01_h_ #include "BaseApplication.h" class ITutorial01 : public BaseApplication { public: ITutorial01(void); virtual ~ITutorial01(void); protected: virtual void createScene(void); virtual void createFrameListener(void); virtual bool nextLocation(void); virtual bool frameRenderingQueued(const Ogre::FrameEvent &evt); Ogre::Real mDistance; // The distance the object has left to travel Ogre::Vector3 mDirection; // The direction the object is moving Ogre::Vector3 mDestination; // The destination the object is moving towards Ogre::AnimationState *mAnimationState; // The current animation state of the object Ogre::Entity *mEntity; // The Entity we are animating Ogre::SceneNode *mNode; // The SceneNode that the Entity is attached to std::deque<Ogre::Vector3> mWalkList; // The list of points we are walking to Ogre::Real mWalkSpeed; // The speed at which the object is moving }; #endif // #ifndef __ITutorial01_h_
ITutorial01.cpp #include "ITutorial01.h" using namespace std; //------------------------------------------------------------------------------------- ITutorial01::ITutorial01(void) { } //------------------------------------------------------------------------------------- ITutorial01::~ITutorial01(void) { } //------------------------------------------------------------------------------------- void ITutorial01::createScene(void) { // Set the default lighting. mSceneMgr->setAmbientLight(Ogre::ColourValue(1.0f, 1.0f, 1.0f)); // Create the entity mEntity = mSceneMgr->createEntity("Robot", "robot.mesh"); // Create the scene node mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("RobotNode", Ogre::Vector3(0.0f, 0.0f, 25.0f)); mNode->attachObject(mEntity); // Create the walking list mWalkList.push_back(Ogre::Vector3(550.0f, 0.0f, 50.0f )); mWalkList.push_back(Ogre::Vector3(-100.0f, 0.0f, -200.0f)); mWalkList.push_back(Ogre::Vector3(250.0f, 0.0f, -200.0f)); mWalkList.push_back(Ogre::Vector3(250.0f, 0.0f, 100.0f)); // Create objects so we can see movement Ogre::Entity *ent; Ogre::SceneNode *node; ent = mSceneMgr->createEntity("Knot1", "knot.mesh"); node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot1Node", Ogre::Vector3(0.0f, -10.0f, 25.0f)); node->attachObject(ent); node->setScale(0.1f, 0.1f, 0.1f); ent = mSceneMgr->createEntity("Knot2", "knot.mesh"); node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot2Node", Ogre::Vector3(550.0f, -10.0f, 50.0f)); node->attachObject(ent); node->setScale(0.1f, 0.1f, 0.1f); ent = mSceneMgr->createEntity("Knot3", "knot.mesh"); node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot3Node", Ogre::Vector3(-100.0f, -10.0f,-200.0f)); node->attachObject(ent); node->setScale(0.1f, 0.1f, 0.1f); ent = mSceneMgr->createEntity("Knot4", "knot.mesh"); node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot4Node", Ogre::Vector3(250.0f, -10.0f,-200.0f)); node->attachObject(ent); node->setScale(0.1f, 0.1f, 0.1f); ent = mSceneMgr->createEntity("Knot5", "knot.mesh"); node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot5Node", Ogre::Vector3(250.0f, -10.0f, 100.0f)); node->attachObject(ent); node->setScale(0.1f, 0.1f, 0.1f); // Set the camera to look at our handiwork mCamera->setPosition(90.0f, 280.0f, 535.0f); mCamera->pitch(Ogre::Degree(-30.0f)); mCamera->yaw(Ogre::Degree(-15.0f)); } void ITutorial01::createFrameListener(void) { BaseApplication::createFrameListener(); // Set idle animation mAnimationState = mEntity->getAnimationState("Idle"); mAnimationState->setLoop(true); mAnimationState->setEnabled(true); // Set default values for variables mWalkSpeed = 35.0f; mDirection = Ogre::Vector3::ZERO; } bool ITutorial01::nextLocation(void) { if (mWalkList.empty()) return false; mDestination = mWalkList.front(); // this gets the front of the deque mWalkList.pop_front(); // this removes the front of the deque mDirection = mDestination - mNode->getPosition(); mDistance = mDirection.normalise(); return true; } bool ITutorial01::frameRenderingQueued(const Ogre::FrameEvent &evt) { mAnimationState->addTime(evt.timeSinceLastFrame); if (mDirection == Ogre::Vector3::ZERO) { if (nextLocation()) { // Set walking animation mAnimationState = mEntity->getAnimationState("Walk"); mAnimationState->setLoop(true); mAnimationState->setEnabled(true); } } else { Ogre::Real move = mWalkSpeed * evt.timeSinceLastFrame; mDistance -= move; if (mDistance <= 0.0f) { mNode->setPosition(mDestination); mDirection = Ogre::Vector3::ZERO; // Set animation based on if the robot has another point to walk to. if (! nextLocation()) { // Set Idle animation mAnimationState = mEntity->getAnimationState("Idle"); mAnimationState = mEntity->getAnimationState("Die"); mAnimationState->setLoop(true); mAnimationState->setEnabled(true); mDirection += 0.5f; mDestination += 100.0f; mWalkList.push_back(mDestination); ::Sleep(1000); mAnimationState = mEntity->getAnimationState("Walk"); } else { // Rotation Code will go here later Ogre::Vector3 src = mNode->getOrientation() * Ogre::Vector3::UNIT_X; if ((1.0f + src.dotProduct(mDirection)) < 0.0001f) { mNode->yaw(Ogre::Degree(180)); } else { Ogre::Quaternion quat = src.getRotationTo(mDirection); mNode->rotate(quat); } // else } } else { mNode->translate(mDirection * move); } // else } // if mAnimationState->addTime(evt.timeSinceLastFrame); return BaseApplication::frameRenderingQueued(evt); } #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN #include "windows.h" #endif #ifdef __cplusplus extern "C" { #endif #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) #else int main(int argc, char *argv[]) #endif { // Create application object ITutorial01 app; try { app.go(); } catch( Ogre::Exception& e ) { #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else std::cerr << "An exception has occured: " << e.getFullDescription().c_str() << std::endl; #endif } return 0; } #ifdef __cplusplus } #endif