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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
1
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
2
EIE360 Integrated Project
I. A Brief Introduction to OGRE
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
3
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Combining Video and Graphics by Dr Daniel Lun
4
Frequently Used Core Objects
in OGRE
The game world
Text overlaid on
the graphics
Light
projected in
this direction
Entities attached
to Scene Nodes
A Plane filled with
grass Texture
Everything are controlled by the SceneManager
5
Basic Execution Flow of OGRE
Update
screen
Initialization
Create all core objects
Determine the motion
of the
objects in each update
:
Finish
update
screen
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Combining Video and Graphics by Dr Daniel Lun
6
EIE360 Integrated Project
II. Animation in OGRE
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
7
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
8
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
9
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
10
A Simple Example
The aquarium
An Entity based on fish.mesh
is attached to a sceneNode
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
Detected by the
frameListener of
Ogre
Step-by-step …
Initialization
EIE360ProjectApp( … )
createScene( … )
0. Define member variables
1. Create entity and
scene node
2. Generate swimming route
3a. Define animation state
Update
screen
Screen Update
frameRenderingQueued( … )
3b. Update animation state
4. Translate the scene nodes and
determine orientation
Finish
update
screen
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
Step-by-step …
Initialization
0. Define member variables
1. Create entity and
scene node
2. Generate swimming route
3a. Define animation state
Update
screen
Screen Update
3b. Update the animation state
4. Translate the scene nodes and
determine orientation
Finish
update
screen
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: Entity * mCharacterEntity;
SceneNode * mCharacterNode
Then
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));
Department of
14
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
Step-by-step …
Initialization
0. Define member variables
1. Create entity and
scene node
2. Generate swimming route
3a. Define animation state
Update
screen
Screen Update
3b. Update the animation state
4. Translate the scene nodes and
determine orientation
Finish
update
screen
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
16
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
17
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
//that
the fish will swim
2. Animation by Dr Daniel Lun
Second Step: Generate
Generate a random
swimming route
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; Keep y-axis unchanged. So the fish will
}
not swim up-and-down frequently
//Close the spline
//The last pos is just the first pos
mFishSplines.addPoint(mFishSplines.getPoint(0));
//Ask
Ogre to curve fit all waypoints
Department of
ELECTRONIC AND INFORMATION ENGINEERING
mFishSplines.recalcTangents();
2. Animation by Dr Daniel Lun
19
Step-by-step …
Initialization
0. Define member variables
1. Create entity and
scene node
2. Generate swimming route
3a. Define animation state
Update
screen
Screen Update
3b. Update the animation state
4. Translate the scene nodes and
determine orientation
Finish
update
screen
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
21
Third Step (a): Get and Set
Animation State (cont)
Assume the following variable is defined:
AnimationState * mAnimationState;
Then
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
Department
//of you disable it or set loop to false
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
22
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);
Show time of
Frame n
t1
Show time of
Frame n+1
addtime
(t1)
t2
Show time of
Frame n+2
addtime
(t2)
Since there may
not be a
keyframe at that
time, Ogre will
interpolate one
based on the
available
keyframes
t
Keyframes/Time positions defined by the designer
23
Step-by-step …
Initialization
0. Define member variables
1. Create entity and
scene node
2. Generate swimming route
3a. Define animation state
Update
screen
Screen Update
3b. Update the animation state
4. Translate the scene nodes and
determine orientation
Finish
update
screen
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 predefined in step 2, it is a closed path
Department of
ELECTRONIC AND INFORMATION ENGINEERING
2. Animation by Dr Daniel Lun
25
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;
To indicate how far
while (mAnimTime > fish_path_length) has the fish gone
from the starting
mAnimTime
-= fish_path_length;
Department
of
26
ELECTRONIC AND INFORMATION ENGINEERING
point of the path
//Limit to be within 0 to 30
2. Animation by Dr Daniel Lun
Fourth Step (a): Translate the
Return an interpolated 3D
scene node
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
Move the scene node to
representing the parametric
the new position
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 27
Department of
ELECTRONIC AND INFORMATION ENGINEERING
mean the ending position.
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
Original position of the fish
New position after update
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
28
Fourth Step (b): Determine the
The original orientation
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
Y-axis is masked since there
else
should not be changes in y-axis
mCharacterNode->
rotate(src.getRotationTo(direction)
*
Department
of
29
ELECTRONIC AND INFORMATION ENGINEERING
Ogre::Vector3(1,0,1));
2. Animation by Dr Daniel Lun
© Copyright 2026 Paperzz