source: OGRE/trunk/ogrenew/Samples/CameraTrack/src/CameraTrack.cpp @ 657

Revision 657, 5.4 KB checked in by mattausch, 18 years ago (diff)

added ogre dependencies and patched ogre sources

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