source: OGRE/trunk/ogrenew/Samples/FacialAnimation/src/FacialAnimation.cpp @ 692

Revision 692, 14.2 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

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/**
16    \file
17        FacialAnimation.cpp
18    \brief
19        Demonstration of facial animation features, using Pose animation
20*/
21
22#include <CEGUI/CEGUIImageset.h>
23#include <CEGUI/CEGUISystem.h>
24#include <CEGUI/CEGUILogger.h>
25#include <CEGUI/CEGUISchemeManager.h>
26#include <CEGUI/CEGUIWindowManager.h>
27#include <CEGUI/CEGUIWindow.h>
28#include <CEGUI/elements/CEGUICombobox.h>
29#include <CEGUI/elements/CEGUIListbox.h>
30#include <CEGUI/elements/CEGUIListboxTextItem.h>
31#include <CEGUI/elements/CEGUIPushButton.h>
32#include <CEGUI/elements/CEGUIScrollbar.h>
33#include <CEGUI/elements/CEGUIStaticImage.h>
34#include <CEGUI/elements/CEGUIRadioButton.h>
35#include "OgreCEGUIRenderer.h"
36#include "OgreCEGUIResourceProvider.h"
37
38#include "ExampleApplication.h"
39
40CEGUI::MouseButton convertOgreButtonToCegui(int buttonID)
41{
42    switch (buttonID)
43    {
44    case MouseEvent::BUTTON0_MASK:
45        return CEGUI::LeftButton;
46    case MouseEvent::BUTTON1_MASK:
47        return CEGUI::RightButton;
48    case MouseEvent::BUTTON2_MASK:
49        return CEGUI::MiddleButton;
50    case MouseEvent::BUTTON3_MASK:
51        return CEGUI::X1Button;
52    default:
53        return CEGUI::LeftButton;
54    }
55}
56
57AnimationState* speakAnimState;
58AnimationState* manualAnimState;
59VertexPoseKeyFrame* manualKeyFrame;
60
61enum ScrollbarIndex
62{
63        SI_HAPPY = 0,
64        SI_SAD = 1,
65        SI_ANGRY = 2,
66        SI_A = 3,
67        SI_E = 4,
68        SI_I = 5,
69        SI_O = 6,
70        SI_U = 7,
71        SI_C = 8,
72        SI_W = 9,
73        SI_M = 10,
74        SI_L = 11,
75        SI_F = 12,
76        SI_T = 13,
77        SI_P = 14,
78        SI_R = 15,
79        SI_S = 16,
80        SI_TH = 17,
81        SI_COUNT = 18
82};
83String scrollbarNames[SI_COUNT] = {
84        "Facial/Happy_Scroll",
85        "Facial/Sad_Scroll",
86        "Facial/Angry_Scroll",
87        "Facial/A_Scroll",
88        "Facial/E_Scroll",
89        "Facial/I_Scroll",
90        "Facial/O_Scroll",
91        "Facial/U_Scroll",
92        "Facial/C_Scroll",
93        "Facial/W_Scroll",
94        "Facial/M_Scroll",
95        "Facial/L_Scroll",
96        "Facial/F_Scroll",
97        "Facial/T_Scroll",
98        "Facial/P_Scroll",
99        "Facial/R_Scroll",
100        "Facial/S_Scroll",
101        "Facial/TH_Scroll",
102};
103unsigned short poseIndexes[SI_COUNT] =
104{ 1, 2, 3, 4, 7, 8, 6, 5, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
105
106CEGUI::Scrollbar* scrollbars[SI_COUNT];
107
108
109class GuiFrameListener : public ExampleFrameListener, public MouseMotionListener, public MouseListener
110{
111private:
112    CEGUI::Renderer* mGUIRenderer;
113    bool mShutdownRequested;
114
115public:
116    // NB using buffered input, this is the only change
117    GuiFrameListener(RenderWindow* win, Camera* cam, CEGUI::Renderer* renderer)
118        : ExampleFrameListener(win, cam, true, true),
119          mGUIRenderer(renderer),
120          mShutdownRequested(false)
121    {
122        mEventProcessor->addMouseMotionListener(this);
123        mEventProcessor->addMouseListener(this);
124                mEventProcessor->addKeyListener(this);
125    }
126
127    /// Tell the frame listener to exit at the end of the next frame
128    void requestShutdown(void)
129    {
130        mShutdownRequested = true;
131    }
132
133    bool frameEnded(const FrameEvent& evt)
134    {
135        if (mShutdownRequested)
136            return false;
137        else
138            return ExampleFrameListener::frameEnded(evt);
139    }
140
141    void mouseMoved (MouseEvent *e)
142    {
143        CEGUI::System::getSingleton().injectMouseMove(
144                e->getRelX() * mGUIRenderer->getWidth(),
145                e->getRelY() * mGUIRenderer->getHeight());
146        e->consume();
147    }
148
149    void mouseDragged (MouseEvent *e)
150    {
151        mouseMoved(e);
152    }
153
154    void mousePressed (MouseEvent *e)
155    {
156        CEGUI::System::getSingleton().injectMouseButtonDown(
157          convertOgreButtonToCegui(e->getButtonID()));
158        e->consume();
159    }
160
161    void mouseReleased (MouseEvent *e)
162    {
163        CEGUI::System::getSingleton().injectMouseButtonUp(
164          convertOgreButtonToCegui(e->getButtonID()));
165        e->consume();
166    }
167
168        void mouseClicked(MouseEvent* e) {}
169        void mouseEntered(MouseEvent* e) {}
170        void mouseExited(MouseEvent* e) {}
171
172    void keyPressed(KeyEvent* e)
173    {
174        if(e->getKey() == KC_ESCAPE)
175        {
176            mShutdownRequested = true;
177            e->consume();
178            return;
179        }
180
181                if (e->getKey() == KC_SYSRQ)
182                {
183                        mWindow->writeContentsToTimestampedFile("screenshot", ".png");
184                }
185
186        CEGUI::System::getSingleton().injectKeyDown(e->getKey());
187                CEGUI::System::getSingleton().injectChar(e->getKeyChar());
188        e->consume();
189    }
190
191        void keyReleased(KeyEvent* e)
192        {
193                CEGUI::System::getSingleton().injectKeyUp(e->getKey());
194                e->consume();
195        }
196        void keyClicked(KeyEvent* e)
197        {
198                // Do nothing
199                e->consume();
200        }
201
202        bool frameStarted(const FrameEvent& evt)
203        {
204                speakAnimState->addTime(evt.timeSinceLastFrame);
205                return ExampleFrameListener::frameStarted(evt);
206
207        }
208};
209
210class FacialApplication : public ExampleApplication
211{
212private:
213    CEGUI::OgreCEGUIRenderer* mGUIRenderer;
214    CEGUI::System* mGUISystem;
215    CEGUI::Window* mEditorGuiSheet;
216        CEGUI::Scrollbar* mRed;
217        CEGUI::Scrollbar* mGreen;
218        CEGUI::Scrollbar* mBlue;
219        CEGUI::StaticImage* mPreview;
220        CEGUI::Window* mTip;
221        CEGUI::Listbox* mList;
222        CEGUI::Window* mEditBox;
223
224public:
225    FacialApplication()
226      : mGUIRenderer(0),
227        mGUISystem(0),
228        mEditorGuiSheet(0)
229    {
230
231        }
232
233    ~FacialApplication()
234    {
235        if(mEditorGuiSheet)
236        {
237            CEGUI::WindowManager::getSingleton().destroyWindow(mEditorGuiSheet);
238        }
239        if(mGUISystem)
240        {
241            delete mGUISystem;
242            mGUISystem = 0;
243        }
244        if(mGUIRenderer)
245        {
246            delete mGUIRenderer;
247            mGUIRenderer = 0;
248        }
249    }
250
251protected:
252
253        bool mPlayAnimation;
254
255        // Handle the scrollbars changing
256        bool handleScrollChanged(const CEGUI::EventArgs& e)
257        {
258                if (!mPlayAnimation)
259                {
260                        // Alter the animation
261                        // Find which one it is first
262                        const CEGUI::WindowEventArgs& args = static_cast<const CEGUI::WindowEventArgs&>(e);
263                        String name = args.window->getName().c_str();
264                        // Find which pose was changed
265                        int i;
266                        for (i = 0; i < SI_COUNT; ++i)
267                        {
268                                if (scrollbarNames[i] == name)
269                                {
270                                        break;
271                                }
272                        }
273                        if (i != SI_COUNT)
274                        {
275                                // Update the pose
276                                manualKeyFrame->updatePoseReference(
277                                        poseIndexes[i], scrollbars[i]->getScrollPosition());
278                                // Dirty animation state since we're fudging this manually
279                                manualAnimState->getParent()->_notifyDirty();
280                        }
281
282                }
283                return true;
284
285        }
286
287        // Handle play animation / manual tweaking event
288        bool handleRadioChanged(const CEGUI::EventArgs& e)
289        {
290                mPlayAnimation = !mPlayAnimation;
291                speakAnimState->setEnabled(mPlayAnimation);
292                manualAnimState->setEnabled(!mPlayAnimation);
293                for (int i = 0; i < SI_COUNT; ++i)
294                {
295                        // enable / disable scrollbars
296                        scrollbars[i]->setEnabled(!mPlayAnimation);
297                }
298
299                return true;
300
301        }
302
303    // Just override the mandatory create scene method
304    void createScene(void)
305    {
306        // Set ambient light
307        mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
308
309        // Create a light
310        Light* l = mSceneMgr->createLight("MainLight");
311        // Accept default settings: point light, white diffuse, just set position
312        // NB I could attach the light to a SceneNode if I wanted it to move automatically with
313        //  other objects, but I don't
314        l->setPosition(20,80,50);
315                l->setDiffuseColour(1.0, 1.0, 1.0);
316
317                // Create a light
318                l = mSceneMgr->createLight("MainLight2");
319                // Accept default settings: point light, white diffuse, just set position
320                // NB I could attach the light to a SceneNode if I wanted it to move automatically with
321                //  other objects, but I don't
322                l->setPosition(-120,-80,-50);
323                l->setDiffuseColour(0.7, 0.7, 0.6);
324
325
326                // Pre-load the mesh so that we can tweak it with a manual animation
327                MeshPtr mesh = MeshManager::getSingleton().load("facial.mesh", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
328                Animation* anim = mesh->createAnimation("manual", 0);
329                VertexAnimationTrack* track = anim->createVertexTrack(4, VAT_POSE);
330                manualKeyFrame = track->createVertexPoseKeyFrame(0);
331                // create pose references, initially zero
332                for (int i = 0; i < SI_COUNT; ++i)
333                {
334                        manualKeyFrame->addPoseReference(poseIndexes[i], 0.0f);
335                }
336
337                // setup GUI system
338        mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow,
339            Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
340
341        mGUISystem = new CEGUI::System(mGUIRenderer);
342
343        CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
344
345        Entity* head = mSceneMgr->createEntity("Head", "facial.mesh");
346                speakAnimState = head->getAnimationState("Speak");
347                speakAnimState->setEnabled(true);
348                manualAnimState = head->getAnimationState("manual");
349                manualAnimState->setTimePosition(0);
350
351        SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
352        headNode->attachObject(head);
353
354                mCamera->setPosition(-20, 50, 150);
355                mCamera->lookAt(0,35,0);
356
357        // load scheme and set up defaults
358        CEGUI::SchemeManager::getSingleton().loadScheme(
359                (CEGUI::utf8*)"TaharezLookSkin.scheme");
360        mGUISystem->setDefaultMouseCursor(
361                (CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
362        mGUISystem->setDefaultFont((CEGUI::utf8*)"Tahoma-12");
363
364        CEGUI::Window* sheet =
365            CEGUI::WindowManager::getSingleton().loadWindowLayout(
366                (CEGUI::utf8*)"facial.layout");
367        mGUISystem->setGUISheet(sheet);
368
369                CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
370                for (int i = 0; i < SI_COUNT; ++i)
371                {
372                        scrollbars[i] = static_cast<CEGUI::Scrollbar*>(
373                                wmgr.getWindow(scrollbarNames[i]));
374                        scrollbars[i]->subscribeEvent(
375                                CEGUI::Scrollbar::EventScrollPositionChanged,
376                                CEGUI::Event::Subscriber(&FacialApplication::handleScrollChanged, this));
377                        // disable to begin with
378                        scrollbars[i]->setEnabled(false);
379
380                }
381
382                CEGUI::RadioButton* btn = static_cast<CEGUI::RadioButton*>(
383                        wmgr.getWindow((CEGUI::utf8*)"Facial/Radio/Play"));
384                // play animation by default
385                btn->setSelected(true);
386                btn->subscribeEvent(CEGUI::RadioButton::EventSelectStateChanged,
387                        CEGUI::Event::Subscriber(&FacialApplication::handleRadioChanged, this));
388
389                mPlayAnimation = true;
390
391
392
393    }
394
395    // Create new frame listener
396    void createFrameListener(void)
397    {
398        mFrameListener= new GuiFrameListener(mWindow, mCamera, mGUIRenderer);
399        mRoot->addFrameListener(mFrameListener);
400    }
401
402    void setupEventHandlers(void)
403    {
404                /*
405                CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
406        wmgr.getWindow((CEGUI::utf8*)"OgreGuiDemo/TabCtrl/Page1/QuitButton")
407                        ->subscribeEvent(
408                                CEGUI::PushButton::EventClicked,
409                                CEGUI::Event::Subscriber(&FacialApplication::handleQuit, this));
410        wmgr.getWindow((CEGUI::utf8*)"OgreGuiDemo/TabCtrl/Page1/NewButton")
411                        ->subscribeEvent(
412                                CEGUI::PushButton::EventClicked,
413                                CEGUI::Event::Subscriber(&FacialApplication::handleNew, this));
414        wmgr.getWindow((CEGUI::utf8*)"OgreGuiDemo/TabCtrl/Page1/LoadButton")
415                        ->subscribeEvent(
416                                CEGUI::PushButton::EventClicked,
417                                CEGUI::Event::Subscriber(&FacialApplication::handleLoad, this));
418        wmgr.getWindow((CEGUI::utf8*)"OgreGuiDemo/TabCtrl/Page2/ObjectTypeList")
419                        ->subscribeEvent(
420                                CEGUI::Combobox::EventListSelectionAccepted,
421                                CEGUI::Event::Subscriber(&FacialApplication::handleObjectSelection, this));
422                */
423
424    }
425
426
427        void setupLoadedLayoutHandlers(void)
428        {
429                /*
430                CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
431                mRed = static_cast<CEGUI::Scrollbar*>(
432                        wmgr.getWindow((CEGUI::utf8*)"Demo8/Window1/Controls/Red"));
433                mGreen = static_cast<CEGUI::Scrollbar*>(
434                        wmgr.getWindow((CEGUI::utf8*)"Demo8/Window1/Controls/Green"));
435                mBlue = static_cast<CEGUI::Scrollbar*>(
436                        wmgr.getWindow((CEGUI::utf8*)"Demo8/Window1/Controls/Blue"));
437                mPreview = static_cast<CEGUI::StaticImage*>(
438                        wmgr.getWindow((CEGUI::utf8*)"Demo8/Window1/Controls/ColourSample"));
439                mList = static_cast<CEGUI::Listbox*>(
440                        wmgr.getWindow((CEGUI::utf8*)"Demo8/Window1/Listbox"));
441                mEditBox =
442                        wmgr.getWindow((CEGUI::utf8*)"Demo8/Window1/Controls/Editbox");
443       
444                mRed->subscribeEvent(
445                                CEGUI::Scrollbar::EventScrollPositionChanged,
446                                CEGUI::Event::Subscriber(&FacialApplication::handleColourChanged, this));
447                mGreen->subscribeEvent(
448                        CEGUI::Scrollbar::EventScrollPositionChanged,
449                        CEGUI::Event::Subscriber(&FacialApplication::handleColourChanged, this));
450                mBlue->subscribeEvent(
451                        CEGUI::Scrollbar::EventScrollPositionChanged,
452                        CEGUI::Event::Subscriber(&FacialApplication::handleColourChanged, this));
453
454                wmgr.getWindow((CEGUI::utf8*)"Demo8/Window1/Controls/Add")
455                        ->subscribeEvent(
456                        CEGUI::PushButton::EventClicked,
457                        CEGUI::Event::Subscriber(&FacialApplication::handleAdd, this));
458
459                CEGUI::Window* root = wmgr.getWindow("Demo8");
460                setupEnterExitEvents(root);
461                */
462
463
464        }
465
466    bool handleQuit(const CEGUI::EventArgs& e)
467    {
468        static_cast<GuiFrameListener*>(mFrameListener)->requestShutdown();
469        return true;
470    }
471
472
473};
474
475#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
476#define WIN32_LEAN_AND_MEAN
477#include "windows.h"
478
479INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
480#else
481int main(int argc, char *argv[])
482#endif
483{
484
485    // Create application object
486    FacialApplication app;
487
488    try {
489        app.go();
490    } catch( Ogre::Exception& e ) {
491#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
492        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
493#else
494        std::cerr << "An exception has occured: " <<
495            e.getFullDescription().c_str() << std::endl;
496#endif
497    }
498
499
500    return 0;
501}
502
503
Note: See TracBrowser for help on using the repository browser.