1 lecture 2 animation references: [1] gregory junker, pro ogre 3d programming, apress, 2006 [2] ogre...

29
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

Upload: eleanor-sparks

Post on 29-Dec-2015

235 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 2: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 3: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

3

I. A Brief Introduction to OGRE

Department of ELECTRONIC AND INFORMATION ENGINEERING

2. Animation by Dr Daniel Lun

EIE360 Integrated Project

Page 4: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 5: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 6: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 7: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

7

II. Animation in OGRE

Department of ELECTRONIC AND INFORMATION ENGINEERING

2. Animation by Dr Daniel Lun

EIE360 Integrated Project

Page 8: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 9: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 10: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 11: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 12: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 13: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 14: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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));

Page 15: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 16: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 17: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 18: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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.

Page 19: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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)

Page 20: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 21: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 22: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 23: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 24: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 25: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 26: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 27: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 28: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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

Page 29: 1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

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