#include <ExampleApplication.h>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class MoveDemoListener : public ExampleFrameListener
{
protected:
Vector3 mDirection;
Vector3 mPosition;
Real mMove;
Real mDistance;
Real mTime;
AnimationState* mAnimationState;
Entity* mEntity;
SceneNode* mNode;
std::vector<Vector3> mWalkList;
int mIndex;
Real mWalkSpeed;
public:
MoveDemoListener(RenderWindow* win, Camera* cam, SceneNode* sn, Entity* ent, std::vector<Vector3>& walk)
: ExampleFrameListener(win, cam, false, false), mNode(sn), mEntity(ent), mWalkList(walk)
{
mAnimationState = ent->getAnimationState("Walk");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
mWalkSpeed = 35.0f;
mIndex = 0;
int nIndex1 = mIndex, nIndex2 = (mIndex + 1) % 10;
mDirection = mWalkList[nIndex2] - mWalkList[nIndex1];
mDistance = mDirection.length();
mDirection.normalise();
Vector3 Direction;
Direction.x = mDirection.z;
Direction.y = 0.0f;
Direction.z = -mDirection.x;
mNode->setDirection(Direction, Ogre::Node::TS_WORLD);
mTime = 0.0f;
}
void nextLocation()
{
mIndex = (mIndex + 1) % 10;
int nIndex1 = mIndex, nIndex2 = (mIndex + 1) % 10;
mDirection = mWalkList[nIndex2] - mWalkList[nIndex1];
mDistance = mDirection.length();
mDirection.normalise();
Vector3 Direction;
Direction.x = mDirection.z;
Direction.y = 0.0f;
Direction.z = -mDirection.x;
mNode->setDirection(Direction, Ogre::Node::TS_WORLD);
mTime = 0.0f;
Matrix4 matRotation;
}
bool frameStarted(const FrameEvent& evt)
{
mTime += evt.timeSinceLastFrame;
mMove = mTime * mWalkSpeed;
mPosition = mWalkList[mIndex] + mMove * mDirection;
mPosition.y = 0.0f;
mNode->setPosition(mPosition);
if(mMove > mDistance) nextLocation();
mAnimationState->addTime(evt.timeSinceLastFrame);
return ExampleFrameListener::frameStarted(evt);
}
};
class MoveDemoApplication : public ExampleApplication
{
public:
MoveDemoApplication()
{
}
~MoveDemoApplication()
{
}
protected:
Entity* mEntity;
SceneNode* mNode;
std::vector<Vector3> mWalkList;
void createScene()
{
srand(1000);
char s[32];
Vector3 v;
int i;
for(i = 0; i < 10; i ++)
{
mWalkList.push_back(Vector3(rand() % 1000, -10.0f, rand() % 1000));
}
v = mWalkList[0];
v.y = 0.0f;
mSceneMgr->setAmbientLight(ColourValue(1.0f, 1.0f, 1.0f));
mEntity = mSceneMgr->createEntity("Robot", "robot.mesh");
mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("RobotNode", v);
mNode->attachObject(mEntity);
Entity* ent;
SceneNode* node;
for(i = 0; i < 10; i ++)
{
sprintf(s, "Knot%d", i + 1);
ent = mSceneMgr->createEntity(s, "knot.mesh");
sprintf(s, "Knot%dNode", i + 1);
node = mSceneMgr->getRootSceneNode()->createChildSceneNode(s, mWalkList[i]);
node->attachObject(ent);
node->setScale(0.1f, 0.1f, 0.1f);
}
mCamera->setPosition(90.0f, 280.0f, 535.0f);
mCamera->pitch(Degree(-30.0f));
mCamera->yaw(Degree(-15.0f));
}
void createFrameListener()
{
mFrameListener = new MoveDemoListener(mWindow, mCamera, mNode, mEntity, mWalkList);
mFrameListener->showDebugOverlay(false);
mRoot->addFrameListener(mFrameListener);
}
};
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char** argv)
#endif
{
MoveDemoApplication app;
try {
app.go();
} catch(Exception& e) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occured!",
MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
fprintf(stderr, "An exception has occured: %s\n", e.getFullDescription().c_str());
#endif
}
return 0;
}