1 / 29

EIE360 Integrated Project

Department of ELECTRONIC AND INFORMATION ENGINEERING. 2. Animation by Dr Daniel Lun. EIE360 Integrated Project. Lecture 2 Animation. References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress , 2006 [2] Ogre Tutorials – Ogre Wiki http://www.ogre3d.org/wiki/index.php/Ogre_Tutorials

bishop
Download Presentation

EIE360 Integrated Project

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun EIE360 Integrated Project Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming,Apress, 2006 [2] Ogre Tutorials – Ogre Wiki http://www.ogre3d.org/wiki/index.php/Ogre_Tutorials [3] Microsoft MSDN, C++ reference

  2. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Architecture of the Interactive Virtual Aquarium Computer A USB port Your program Kinect Sensor Device Network Computer B 3D Graphics System Your program 3D Graphics

  3. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun EIE360 Integrated Project I. A Brief Introduction to OGRE

  4. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Combining Video and Graphics by Dr Daniel Lun The Software Development Platform – OGRE • The Interactive Virtual Aquarium is developed in a 3D environment • The 3D graphics engine, OGRE, will be used to simplify the software development tasks • OGRE stands for Object-Oriented Graphics Rendering Engine • OO interface designed to minimize the effort required to render 3D scenes • Independent of 3D implementation e.g. Direct3D, OpenGL, Glide etc. • Contain example frameworks • Common requirements for 3D rendering are done for the user automatically

  5. Frequently Used Core Objects in OGRE The game world Light projected in this direction Text overlaid on the graphics Entities attached to Scene Nodes A Plane filled with grass Texture Everything are controlled by the SceneManager

  6. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Combining Video and Graphics by Dr Daniel Lun Basic Execution Flow of OGRE Update screen Initialization Create all core objects Determine the motion of the objects in each update : Finish update screen

  7. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun EIE360 Integrated Project II. Animation in OGRE

  8. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Animation in OGRE • Animation, in general, is no different in the computer age than it was when some artists first flipped quickly through pages containing a series of slightly different images • In Ogre, the scene is drawn from scratch every frame, whether or not it contains animation • Ogre does not keep track of game characters’ velocity and acceleration vectors • The animation features in Ogre are there only to help position and orient your characters as a function of some arbitrary variable (usually time, although any variable can act as a controller)

  9. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Animation in OGRE (cont) • An animation in Ogre is a collection of possibly (usually) related tracks • An animation track is a set of data values stored as a function of time • The pair of time point and track value composes a keyframe • The term keyframe comes from the days of hand-drawn animation when master artists would provide the junior artists with a set of “key” frames in an animation • Ogre works just like the junior artists to interpolate from your (master artist) keyframes designed using different 3D modeling tools (e.g. 3ds Max) to render the needed animation

  10. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun A Simple Example • Let’s use a simple example, “a swimming fish”, to demonstrate the basic idea of creating animation in Ogre • Fish01 is a skeletally animated mesh object • It has a skeletal animation – Swim • They define the keyframes in these animations • May preview them using OgreMax Win Viewer • The animation is “in-place”, no translation motion • To animate a swimming fish, need to move the scene node to make it look like moving

  11. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun A Simple Example The aquarium An Entity based on fish.mesh is attached to a sceneNode

  12. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Step-by-step … Detected by the frameListener of Ogre Update screen Initialization EIE360ProjectApp( … ) createScene( … ) 0. Define member variables 1. Create entity and scene node 2. Generate swimming route 3a. Define animation state • Screen Update • frameRenderingQueued( … ) • 3b. Update animation state • Translate the scene nodes and • determine orientation Finish update screen

  13. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Step-by-step … Update screen • Initialization • 0. Define member variables • Create entity and • scene node • 2. Generate swimming route • 3a. Define animation state • Screen Update • 3b. Update the animation state • Translate the scene nodes and • determine orientation Finish update screen

  14. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun First Step: Create Entity and SceneNode • Assume the following two variables are defined: • Then Entity * mCharacterEntity; SceneNode * mCharacterNode mCharacterEntity = mSceneMgr-> createEntity("Character", “Fish01.mesh"); mCharacterEntity->setCastShadows(true); //Enable the fish to cast shadow mCharacterNode = mSceneMgr->getRootSceneNode()-> createChildSceneNode("CharacterNode"); mCharacterNode->attachObject(mCharacterEntity); Ogre::Real sizeFactor = 100; mCharacterNode->setScale(Ogre::Vector3(sizeFactor));

  15. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Step-by-step … Update screen • Initialization • 0. Define member variables • Create entity and • scene node • 2. Generate swimming route • 3a. Define animation state • Screen Update • 3b. Update the animation state • Translate the scene nodes and • determine orientation Finish update screen

  16. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Second Step: Generate swimming route Waypoints Ogre will generate the curve that fits all waypoints

  17. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Second Step: Generate swimming route • To better control the movement of the fishes, we often pre-generate their swimming paths • Just need to follow the procedure below: • Define the starting position (the 3D coordinates) • Randomly generate a few waypoints • Connect all waypoints using a splinefunction (curve fitting) • Just give Ogre your waypoints. Ogre will find a smooth curve that goes thru them

  18. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Second Step: Generate swimming route Splines are bendy lines. You define a series of points, and the spline forms a smoother line between the points to eliminate the sharp angles. Ogre::SimpleSpline mFishSplines; //SimpleSpline is a simple spline class mFishSplines.setAutoCalculate(false); //Enable the fish to cast shadow Ogre::Vector3 lastPos = Ogre::Vector3(0,50,0); mFishSplines.addPoint(lastPos); //Define the initial position int room_width = 200; int room_depth = 100; //Define the size of a plane x=±200, z=±100 //that the fish will swim

  19. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Second Step: Generate swimming route Generate a random no. between (-1, 1) //Generate the waypoints for (int waypoint = 1; waypoint < 5; waypoint++) { Ogre::Vector3 pos = Ogre::Vector3( Ogre::Math::SymmetricRandom() * room_width, lastPos.y, Ogre::Math::SymmetricRandom() * room_depth); mFishSplines.addPoint(pos); lastPos = pos; } //Close the spline //The last pos is just the first pos mFishSplines.addPoint(mFishSplines.getPoint(0)); //Ask Ogre to curve fit all waypoints mFishSplines.recalcTangents(); Keep y-axis unchanged. So the fish will not swim up-and-down frequently 19

  20. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Step-by-step … Update screen • Initialization • 0. Define member variables • Create entity and • scene node • 2. Generate swimming route • 3a. Define animation state • Screen Update • 3b. Update the animation state • Translate the scene nodes and • determine orientation Finish update screen

  21. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Third Step: Get and Set Animation State • As part of the export process, different parts of the timeline in an object’s animation can be given names – idle, swim, … One can use these names to “address” different animations on an entity • For each animation, we can obtain or change its current state by calling function getAnimationState() • It returns an animation state that provides access to different properties of an animation: • length – obtain the duration of the animation • time position – get/set the position of an object at a particular time • loop – define if the animation should be played once or loop • enable – define if the animation can be played • weight – define the weighting when blending with other animations

  22. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Third Step (a): Get and Set Animation State (cont) • Assume the following variable is defined: • Then AnimationState * mAnimationState; mAnimationState = mCharacterEntity-> getAnimationState(“swim"); // Get the animation state of “swim” mAnimationState->setLoop(true); // Play “swim” in a loop mAnimationState->setEnabled(true); // The animation can be played // “swim” can then be played forever until // you disable it or set loop to false

  23. Third Step (b): Get and Set Animation State (cont) • In Ogre, the scene will be updated in an irregular period • To synchronize the animation with that update rate, we may use the elapse time between frames to update the time position parameter of the animation state mAnimationState->addTime(evt.timeSinceLastFrame); Since there may not be a keyframe at that time, Ogre will interpolate one based on the available keyframes Show time of Frame n Show time of Frame n+1 Show time of Frame n+2 t1 t2 addtime (t1) addtime (t2) t Keyframes/Time positions defined by the designer

  24. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Step-by-step … Update screen • Initialization • 0. Define member variables • Create entity and • scene node • 2. Generate swimming route • 3a. Define animation state • Screen Update • 3b. Update the animation state • Translate the scene nodes and • determine orientation Finish update screen

  25. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Fourth Step (a): Translate the scene node • The animation of the fish is in-place. Need to move its scene node to animate the swimming motion • The path for the scene node to move has been pre-defined in step 2, it is a closed path

  26. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Fourth Step (a): Translate the scene node • It is convenient to synchronize the swimming speed of the fish with the screen update rate • To make sure the fish will swim in a constant speed irrespective to the speed of the computer, we want • Fast computer  fast update  smaller movement each update • Slow computer  slow update  larger movement each update • To achieve this, use again evt.timeSinceLastFrame Ogre::Real mAnimTime; //Has been initialized to 0 mAnimTime += evt.timeSinceLastFrame; //Faster computer  smaller timeSinceLastFrame int fish_path_length = 30; while (mAnimTime > fish_path_length) mAnimTime -= fish_path_length; //Limit to be within 0 to 30 To indicate how far has the fish gone from the starting point of the path

  27. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Fourth Step (a): Translate the scene node Return an interpolated 3D coordinates on that spline Ogre::Real movePos = mAnimTime / fish_path_length; Ogre::Vector3 newPos = mFishSplines.interpolate(movePos); mCharacterNode->setPosition(newPos); Need a value between 0 and 1 representing the parametric distance along the whole length of the spline. Hence inputting a 0 means you want to get starting position of the spline, and a 1 mean the ending position. Move the scene node to the new position

  28. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Fourth Step (b): Determine the orientation • The orientation of the fish needs to be adjusted from time to time to let it always face at the direction it is swimming New position after update Original position of the fish Ogre::Vector3 direction = mFishLastPosition - newPos; //Compute the direction that the fish is swimming //Difference of two 3D coordinates becomes the //direction between them direction.normalise(); //Compute the norm from the vector direction //At the same time, direction is converted to a //unit vector

  29. Department of ELECTRONIC AND INFORMATION ENGINEERING 2. Animation by Dr Daniel Lun Fourth Step (b): Determine the orientation The original orientation of the model fish01 • A “divide by zero” error will result if we rotate the fish by 180o . Should detect this situation and use yaw() instead Ogre::Vector3 src = mCharacterNode-> getOrientation() * Ogre::Vector3::UNIT_X; //To find the fish orientation if ((1.0f + src.dotProduct(direction) < 0.0001f) mCharacterNode->yaw(Degree(180)); //If the dot product of the current fish // orientation and the desired direction is -1, // yaw the fish 180o else mCharacterNode-> rotate(src.getRotationTo(direction) * Ogre::Vector3(1,0,1)); Y-axis is masked since there should not be changes in y-axis

More Related