eie360 integrated project
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
1
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
EIE360 Integrated Project
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
2
Architecture of the Interactive Virtual Aquarium
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
USB port
3D GraphicsSystem
3D Graphics
Your program
Your program
Network
Computer A
Computer B
Kinect Sensor Device
3
I. A Brief Introduction to OGRE
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
EIE360 Integrated Project
4
The Software Development Platform – OGRE
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Combining Video and Graphics by Dr Daniel Lun
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
Entities attached to Scene Nodes
Entities attached to Scene Nodes
A Plane filled with grass Texture
A Plane filled with grass Texture
Text overlaid on the graphics
Text overlaid on the graphics
Light projected in this direction
Light projected in this direction
Everything are controlled by the SceneManagerEverything are controlled by the SceneManager
6
Basic Execution Flow of OGRE
Initialization
Create all core objects
Determine the motion of the
objects in each update
Update screen
Finish update screen
:
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Combining Video and Graphics by Dr Daniel Lun
7
II. Animation in OGRE
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
EIE360 Integrated Project
8
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)
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
9Department 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
10Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
A Simple Example
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
An Entity based on fish.mesh is attached to a sceneNode
The aquarium
Step-by-step …
InitializationEIE360ProjectApp( … )createScene( … )
0. Define member variables1. Create entity and
scene node 2. Generate swimming route3a. Define animation state
Update screen
Finish update screen
Screen UpdateframeRenderingQueued( … )
3b. Update animation state4. Translate the scene nodes and
determine orientation
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
Detected by the frameListener of Ogre
Step-by-step …
Initialization
0. Define member variables1. Create entity and
scene node 2. Generate swimming route3a. Define animation state
Update screen
Finish update screen
Screen Update
3b. Update the animation state4. Translate the scene nodes and
determine orientation
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
14
Entity * mCharacterEntity; SceneNode * mCharacterNode
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
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));
Step-by-step …
Initialization
0. Define member variables1. Create entity and
scene node 2. Generate swimming route3a. Define animation state
Update screen
Finish update screen
Screen Update
3b. Update the animation state4. Translate the scene nodes and
determine orientation
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
Second Step: Generate swimming route
16Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
Waypoints
Ogre will generate the curve that fits all waypoints
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 spline function
(curve fitting) Just give Ogre your waypoints. Ogre will find a
smooth curve that goes thru them
17Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
Second Step: Generate swimming route
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
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
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.
Second Step: Generate swimming route
19Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
//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();
19
Keep y-axis unchanged. So the fish will not swim up-and-down frequently
Generate a random no. between (-1, 1)
Step-by-step …
Initialization
0. Define member variables1. Create entity and
scene node 2. Generate swimming route3a. Define animation state
Update screen
Finish update screen
Screen Update
3b. Update the animation state4. Translate the scene nodes and
determine orientation
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
21Department 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
22
AnimationState * mAnimationState;
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
mAnimationState = mCharacterEntity->getAnimationState(“swim");
// Get the animation state of “swim”mAnimationState->setLoop(true);
// Play “swim” in a loopmAnimationState->setEnabled(true);
// The animation can be played// “swim” can then be played forever until// you disable it or set loop to false
Third Step (b): Get and Set Animation State (cont)
23Keyframes/Time positions defined by the designert
mAnimationState->addTime(evt.timeSinceLastFrame);
Show time of Frame n
Show time of Frame n+1
Show time of Frame n+2t1 t2
addtime(t1)
addtime(t2)
Since there may not be a keyframe at that time, Ogre will interpolate one based on the available keyframes
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
Step-by-step …
Initialization
0. Define member variables1. Create entity and
scene node 2. Generate swimming route3a. Define animation state
Update screen
Finish update screen
Screen Update
3b. Update the animation state4. Translate the scene nodes and
determine orientation
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
24
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
25Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
Fourth Step (a): Translate the scene node
26Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
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
Fourth Step (a): Translate the scene node
27Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
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.
Return an interpolated 3D coordinates on that spline
Move the scene node to the new position
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
28
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
Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
Original position of the fishNew position after update
Fourth Step (b): Determine the orientation
A “divide by zero” error will result if we rotate the fish by 180o . Should detect this situation and use yaw() instead
29Department of ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
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));
The original orientation of the model fish01
Y-axis is masked since there should not be changes in y-axis