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