Tutorial1: Animation

#include 
#include 
using namespace Ogre;
class MyTestApp : public OgreBites::ApplicationContext, public OgreBites::InputListener
{
public:
    MyTestApp();
    void setup();
    bool keyPressed(const OgreBites::KeyboardEvent& evt);
private:
    bool frameRenderingQueued(const Ogre::FrameEvent& evt);
    Ogre::AnimationState* mAnimationState;
};

//! [constructor]
MyTestApp::MyTestApp() : OgreBites::ApplicationContext("OgreTutorialApp")
{
}
//! [constructor]

//! [key_handler]
bool MyTestApp::keyPressed(const OgreBites::KeyboardEvent& evt)
{
    if (evt.keysym.sym == OgreBites::SDLK_ESCAPE)
    {
        getRoot()->queueEndRendering();
    }
    return true;
}
//! [key_handler]

//! [setup]
void MyTestApp::setup(void)
{
    // do not forget to call the base first
    OgreBites::ApplicationContext::setup();
    
    // register for input events
    addInputListener(this);

    // get a pointer to the already created root
    Ogre::Root* root = getRoot();
    Ogre::SceneManager* scnMgr = root->createSceneManager();

    // register our scene with the RTSS
    Ogre::RTShader::ShaderGenerator* shadergen = Ogre::RTShader::ShaderGenerator::getSingletonPtr();
    shadergen->addSceneManager(scnMgr);

    // set shadow properties
    // scnMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE);
    // scnMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
    // scnMgr->setShadowTextureSize(1024);
    // scnMgr->setShadowTextureCount(1);

    //! [lightingsset]
    scnMgr->setAmbientLight(ColourValue(0, 0, 0));
    scnMgr->setShadowTechnique(ShadowTechnique::SHADOWTYPE_STENCIL_MODULATIVE);

    // without light we would just get a black screen    
    Ogre::Light* light = scnMgr->createLight("MainLight");
    Ogre::SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
    lightNode->setPosition(10, 20, 15);
    lightNode->attachObject(light);

    // also need to tell where we are
    Ogre::SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();
    //camNode->setPosition(0, 0, 15);
    //camNode->lookAt(Ogre::Vector3(0, 0, -1), Ogre::Node::TS_PARENT);
    camNode->setPosition(0, 8, 30);
    camNode->lookAt(Vector3(0, 0, 0), Node::TransformSpace::TS_WORLD);

    // create the camera
    Ogre::Camera* cam = scnMgr->createCamera("myCam");
    cam->setNearClipDistance(5); // specific to this sample
    //cam->setAutoAspectRatio(true);
    camNode->attachObject(cam);

    // and tell it to render into the main window
    //getRenderWindow()->addViewport(cam);

      //! [addviewport]
    Viewport* vp = getRenderWindow()->addViewport(cam);
    //! [addviewport]

    //! [viewportback]
    vp->setBackgroundColour(ColourValue(0, 0, 0));
    //! [viewportback]

    //! [cameraratio]
    cam->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));

    // finally something to render
    Ogre::Entity* ent = scnMgr->createEntity("Sinbad.mesh");
    Ogre::SceneNode* node = scnMgr->getRootSceneNode()->createChildSceneNode(Vector3::UNIT_Y * 5);
    node->attachObject(ent);

    // create a floor mesh resource
    Ogre::MeshManager::getSingleton().createPlane("floor", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
            Plane(Vector3::UNIT_Y, 0), 100, 100, 20, 20, true, 1, 5, 5, Vector3::UNIT_Z);
    
    // create a floor entity, give it a material, and place it at the origin
    Entity* floor = scnMgr->createEntity("Floor", "floor");
    floor->setMaterialName("Examples/Rockwall");
    floor->setCastShadows(false);
    scnMgr->getRootSceneNode()->attachObject(floor);

    mAnimationState = ent->getAnimationState("Dance");
    mAnimationState->setEnabled(true);
    mAnimationState->setLoop(true);
}
//! [setup]

bool MyTestApp::frameRenderingQueued(const Ogre::FrameEvent& evt) {
    mAnimationState->addTime(evt.timeSinceLastFrame);
    return true;
}

//! [main]
int main(int argc, char *argv[])
{
    MyTestApp app;
    app.initApp();
    app.getRoot()->startRendering();
    app.closeApp();
    return 0;
}
//! [main]

你可能感兴趣的:(Tutorial1: Animation)