Easy 3D
Written by Mike James   
Tuesday, 11 August 2009
Article Index
Easy 3D
The Mesh
The scene
Spin and storyboards

Spin and storyboards

At this point there are many different tracks to explore but to finish our example let’s explore a demonstration of how transformations work.


All objects have transformation properties which can be use to modify where and how they appear in the model. To construct a rotation transformation we need an axis and an angle to rotate through:

AxisAngleRotation3D axis = new
new Vector3D(0, 1, 0), 0);

The transform is now created and associated with the cube:

RotateTransform3D Rotate = new
Cube1.Transform = Rotate;

From this point on we can change the Angle property of axis and the cube will be redrawn at this new angle. The correct way to do this is to use WPF’s animation and as this is very poorly explained in the documentation an example is needed.

Animation is achieved using a Storyboard object which can contain any number of Animation objects. When the Storyboard is activated all of the animations it contains are run.

You can think of an Animation object as specifying how a property of the object being animated should change over time. First we need an animation object that can change the Angle property:

DoubleAnimation RotAngle=
new DoubleAnimation();
RotAngle.Duration= new Duration(

This defines a repeated animation of a “double” from an initial value of 0 to a final value of 360 over 20 seconds.

We now have to connect the animation with the object and property being animated. This is slightly complicated in that we have to first register a “friendly name” of the object we want to animate in the “namescope” of its container:

canvas1, new NameScope());
"cubeaxis", axis);

This creates a new NameScope for in canvas1 and adds “cubeaxis” as the friendly name for the axis object.

You can think of this as building a database of all the objects we want to animate within a layout container. Now we have the axis object registered we can specify it and its Angle property as the target of the animation:

RotAngle, "cubeaxis");
new PropertyPath(

The first instruction sets whatever object has been registered as “cubeaxis” as the object to animate, i.e. axis in this case. The second instruction defines the property on the object that will be changed by the animation.

At last we can create a Storyboard object and add the animation:

Storyboard RotCube=new Storyboard();

A Storyboard can support multiple animations on a range of different objects and properties. Finally we can start the animation:


Notice that the animation is specific to the canvas1 layout container and only at this point is the name “cubeaxis” resolved to the real axis object and its property. The advantage of this is that you could apply the Storyboard to another layout container with a different set of objects as long as you register the same names.

It’s a flexible, if slightly confusing system, but we now have a rotating cube.

You can add animations to move the camera’s position, or its zoom say, in exactly the same way and create a “fly past” but to make it look impressive you will need more than a single cube and this is the start of a very big project…

If you would like the code for this project then register and click on CodeBin.


Simple WPF data binding

Find out how WPF data binding really works. It's not the binding object that matters - it's the binding expression.

Custom BitmapSource

Even if you don't anticipate implementing your own custom BitmapSource finding out how to reveals quite a lot about the way that WPF bitmaps work.

ISupportInitialize and XAML

For a class to be instantiated by XAML it has to have a parameter-less constructor. This means that properties that might be essential to creating the instance can be initialized in any order and this [ ... ]

The bitmap transform classes

WPF bitmaps are immutable but that doesn't mean you can't transform them. Find out about rotation, scaling, changing pixel formatting and how to mangage color profiles.

Getting Started with WPF


WPF Windows Presentation Foundation is the new way to do forms and all things graphical in .NET. You can still use  Windows Forms but don't expect anything particularly new to be added to the tec [ ... ]

Other Articles




Last Updated ( Tuesday, 13 August 2013 )

RSS feed of all content
I Programmer - full contents
Copyright © 2015 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.