[1] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | You may use this sample code for anything you like, it is not covered by the |
---|
| 11 | LGPL like the rest of the engine. |
---|
| 12 | ----------------------------------------------------------------------------- |
---|
| 13 | */ |
---|
| 14 | /* |
---|
| 15 | ----------------------------------------------------------------------------- |
---|
| 16 | Filename: CameraTrack.cpp |
---|
| 17 | Description: An example of using AnimationTracks to smoothly make a node |
---|
| 18 | follow a predefined path, with spline interpolation. Also |
---|
| 19 | uses the auto tracking ability of the camera. |
---|
| 20 | ----------------------------------------------------------------------------- |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #include "ExampleApplication.h" |
---|
| 24 | |
---|
| 25 | AnimationState* mAnimState; |
---|
| 26 | |
---|
| 27 | // Event handler |
---|
| 28 | class CameraTrackListener: public ExampleFrameListener |
---|
| 29 | { |
---|
| 30 | protected: |
---|
| 31 | public: |
---|
| 32 | CameraTrackListener(RenderWindow* win, Camera* cam) |
---|
| 33 | : ExampleFrameListener(win, cam) |
---|
| 34 | { |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | bool frameStarted(const FrameEvent& evt) |
---|
| 38 | { |
---|
| 39 | if( ExampleFrameListener::frameStarted(evt) == false ) |
---|
| 40 | return false; |
---|
| 41 | |
---|
| 42 | mAnimState->addTime(evt.timeSinceLastFrame); |
---|
| 43 | |
---|
| 44 | return true; |
---|
| 45 | } |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | class CameraTrackApplication : public ExampleApplication |
---|
| 49 | { |
---|
| 50 | public: |
---|
| 51 | CameraTrackApplication() { |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | ~CameraTrackApplication() { } |
---|
| 58 | |
---|
| 59 | protected: |
---|
| 60 | SceneNode* mFountainNode; |
---|
| 61 | |
---|
| 62 | // Just override the mandatory create scene method |
---|
| 63 | void createScene(void) |
---|
| 64 | { |
---|
| 65 | |
---|
| 66 | // Set ambient light |
---|
| 67 | mSceneMgr->setAmbientLight(ColourValue(0.2, 0.2, 0.2)); |
---|
| 68 | |
---|
| 69 | // Create a skydome |
---|
| 70 | mSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8); |
---|
| 71 | |
---|
| 72 | // Create a light |
---|
| 73 | Light* l = mSceneMgr->createLight("MainLight"); |
---|
| 74 | // Accept default settings: point light, white diffuse, just set position |
---|
| 75 | // NB I could attach the light to a SceneNode if I wanted it to move automatically with |
---|
| 76 | // other objects, but I don't |
---|
| 77 | l->setPosition(20,80,50); |
---|
| 78 | |
---|
| 79 | Entity *ent; |
---|
| 80 | |
---|
| 81 | // Define a floor plane mesh |
---|
| 82 | Plane p; |
---|
| 83 | p.normal = Vector3::UNIT_Y; |
---|
| 84 | p.d = 200; |
---|
| 85 | MeshManager::getSingleton().createPlane( |
---|
| 86 | "FloorPlane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, |
---|
| 87 | p, 200000, 200000, 20, 20, true, 1, 50, 50, Vector3::UNIT_Z); |
---|
| 88 | |
---|
| 89 | // Create an entity (the floor) |
---|
| 90 | ent = mSceneMgr->createEntity("floor", "FloorPlane"); |
---|
| 91 | ent->setMaterialName("Examples/RustySteel"); |
---|
| 92 | // Attach to child of root node, better for culling (otherwise bounds are the combination of the 2) |
---|
| 93 | mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent); |
---|
| 94 | |
---|
| 95 | // Add a head, give it it's own node |
---|
| 96 | SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); |
---|
| 97 | ent = mSceneMgr->createEntity("head", "ogrehead.mesh"); |
---|
| 98 | headNode->attachObject(ent); |
---|
| 99 | |
---|
| 100 | // Make sure the camera track this node |
---|
| 101 | mCamera->setAutoTracking(true, headNode); |
---|
| 102 | |
---|
| 103 | // Create the camera node & attach camera |
---|
| 104 | SceneNode* camNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); |
---|
| 105 | camNode->attachObject(mCamera); |
---|
| 106 | |
---|
| 107 | // set up spline animation of node |
---|
| 108 | Animation* anim = mSceneMgr->createAnimation("CameraTrack", 10); |
---|
| 109 | // Spline it for nice curves |
---|
| 110 | anim->setInterpolationMode(Animation::IM_SPLINE); |
---|
| 111 | // Create a track to animate the camera's node |
---|
| 112 | NodeAnimationTrack* track = anim->createNodeTrack(0, camNode); |
---|
| 113 | // Setup keyframes |
---|
| 114 | TransformKeyFrame* key = track->createNodeKeyFrame(0); // startposition |
---|
| 115 | key = track->createNodeKeyFrame(2.5); |
---|
| 116 | key->setTranslate(Vector3(500,500,-1000)); |
---|
| 117 | key = track->createNodeKeyFrame(5); |
---|
| 118 | key->setTranslate(Vector3(-1500,1000,-600)); |
---|
| 119 | key = track->createNodeKeyFrame(7.5); |
---|
| 120 | key->setTranslate(Vector3(0,-100,0)); |
---|
| 121 | key = track->createNodeKeyFrame(10); |
---|
| 122 | key->setTranslate(Vector3(0,0,0)); |
---|
| 123 | // Create a new animation state to track this |
---|
| 124 | mAnimState = mSceneMgr->createAnimationState("CameraTrack"); |
---|
| 125 | mAnimState->setEnabled(true); |
---|
| 126 | |
---|
| 127 | // Put in a bit of fog for the hell of it |
---|
| 128 | mSceneMgr->setFog(FOG_EXP, ColourValue::White, 0.0002); |
---|
| 129 | |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // Create new frame listener |
---|
| 133 | void createFrameListener(void) |
---|
| 134 | { |
---|
| 135 | mFrameListener= new CameraTrackListener(mWindow, mCamera); |
---|
| 136 | mRoot->addFrameListener(mFrameListener); |
---|
| 137 | |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | }; |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
| 146 | #define WIN32_LEAN_AND_MEAN |
---|
| 147 | #include "windows.h" |
---|
| 148 | #endif |
---|
| 149 | |
---|
| 150 | #ifdef __cplusplus |
---|
| 151 | extern "C" { |
---|
| 152 | #endif |
---|
| 153 | |
---|
| 154 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
| 155 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) |
---|
| 156 | #else |
---|
| 157 | int main(int argc, char **argv) |
---|
| 158 | #endif |
---|
| 159 | { |
---|
| 160 | // Create application object |
---|
| 161 | CameraTrackApplication app; |
---|
| 162 | |
---|
| 163 | try { |
---|
| 164 | app.go(); |
---|
| 165 | } catch( Exception& e ) { |
---|
| 166 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
| 167 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
| 168 | #else |
---|
| 169 | std::cerr << "An exception has occured: " << e.getFullDescription(); |
---|
| 170 | #endif |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | return 0; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | #ifdef __cplusplus |
---|
| 178 | } |
---|
| 179 | #endif |
---|