Silverlight Tutorials: Silverlight Basic – SpinningCube
This article is written by Einar Ingebrigtsen (Silverlight MVP). It is a part of “Silverlight Basic”, the third chapter of “Silverlight Tutorials for Beginners.”
Download : SpinningCube.zip (12.9 KB)
Silverlight represents a subset of WPF and it’s features, one of the features that aren’t part of the subset is 3D. Silverlight 3 introduces the ability to project any UIElement in 3D by rotating them around the X,Y and Z axises. But geometries that consist on anything else but planes, typically triangles would not perform well using the PlaneProjection. Silverlight has primitives that can be used instead, such as Polygon. The Polygon can have N number of sides, at least 3, which is perfect for representing 3D models.
This tutorial is very basic and does not involve any heavy geometry math. If you want to do more advanced geometry stuff with cameras, texturemapping, lighting and such, Balder could be place to look. Balder is a full 3D engine I started writing back in early 2007 and is opensource, you can find it here.
Getting started
First thing you need to do is to create yourself a Silverlight project and leave all the defaults in.
Rendering container
This sample needs a container to add polygons to. When you first created the project a MainPage.xaml file was added to the project, opening it you’ll see there is a
![]()
Now we have a container that we can add polygons to that has a dimension of 640 by 480 pixels and the background is black.
The rendering loop
The first thing we will be needing is a timer that gets called on a regular basis for handling all the calculations and generation of polygons that will be visible on the display. It is very important that this timer is running in the same thread context as the main Silverlight thread. Manipulation of the visual tree is not allowed from other threads. Silverlight has something called the DispatcherTimer that we can use for this. The DispatcherTimer can run with the same framerate as the plugin has been specified to run with, default being 60 frames per second.
In the MainPage.xaml.cs file in your project, we need to add the following code in the constructor, right beneath the InitializeComponent() method call:

In the Tick event we are specifying a method called Render, which will be a method you have to add in the same class that will be called everytime rendering will occur:

A cube
To define our cube we need points in 3D space, these are called Vertices (one vertex, several vertices). We need to add a simple class to represent a vertex. The class represents the original vertex before we have done any calculations on it and also contains the finished calculated vertex ready to be used on our 2D screen.

As you can see, the vertex contains an X,Y and Z representing the coordinate in 3D space. In addition it contains the rotated version and the translated
(2D) version. Now that we have the definition of a vertex, we need the definition of a triangle that hooks itself on 3 vertices, we call these Faces. A face
contains 3 integers representing an index into the array of vertices for the object we are rotating. Add a class called Face and make it look like this:

Next, we need the actual cube and methods for rendering itself. We create a class called Cube. In the Cube class we need to define the vertices that defines the cube. Add the following at the top of the class:

The purpose the constants at the top is to be able to change the size of the cube with ease.
And we need an array of faces that hooks up to these vertices :

Next we need to actually render the cube. The rendering is divided into two processes, one for calculating all the vertices and one for generating the polygons. First we need to define a method for calculating the vertices:

Note that the method takes the rotation around the 3 axises in as parameters, and also something called xoffset and yoffset.
The offsets are used for centering the finished result, which will be shown later in the render method. Now we need a method for generating the polygons:

We now have all the methods we need to finish the rendering. In the Cube class, we expose a public method to tie it all together:

The method takes a panel as the container in which it will render all the polygons to and the rotation around the 3 axises.
The xoffset and yoffset is calculated by taking the containers width and height and divided by 2, which is the center of the container.
All we need now is to go back to our MainPage.xaml.cs file and create our cube and call the rendering method.
Add a private field holding the Cube instance, like this:
![]()
In the constructor we can now create the cube:
![]()
Then we define some variables that we can manipulate every time we render, these will be used for rotating the cube. In the MainPage class, add the following fields:

Now, we can add a body to the Render method:

That should be just about everything we need to get a spinning cube on the screen.
The result should be something like this:

Conclusion
Silverlight has come a long way since its initial first release in 2007, but still real 3D is missing. In Silverlight 3 there has been done some work by Microsoft on the subject with the introduction of plane projection. For a simple cube like in this tutorial, one would be better off using that technique. But, if you want to represent more advanced geometries, the technique in this tutorial is better. There are some limitations though. Due to the way Silverlight works internally with the main rendering engine and the CLR being separated completely, there are speed limitations to how many polygons it is possible to render. So really complex geometries is something that would not perform well. Another issue is texturemapping. Silverlight does not give you any ability to represent the U,V coordinates within the texture for every point in a polygon. It is possible though to get this working by manipulating the matrix of the imagebrush specified, but again, performance will be impacted. In the Balder engine, we have overcome these limitations by creating our own drawing routines, not using any Silverlight primitives at all. Since the CLR is quite speedy, this enables us to draw quite complex objects.













