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

The scene

Now we have the 3D object and need to create at least one light and a camera object to view it. One of the simplest types of lights to use is the directional light. This creates light of a specific colour in a given direction.


To create a white directional light shining down, to the left and into the scene you would use:

DirectionalLight DirLight1 = new
DirLight1.Color = Colors.White;
DirLight1.Direction = new
Vector3D(-1, -1, -1);

You can define multiple lights of different types but a single directional light is enough to demonstrate how objects change their appearance in response to lighting.

Finally we need a camera. A perspective camera is the most common one to use because it renders a scene complete with perspective effects, but this does make it slightly more complex to define:

PerspectiveCamera Camera1 = new

To set the camera we need to specify its near and far clipping planes – any object closer or further way from the camera isn’t rendered:

Camera1.FarPlaneDistance = 20;
Camera1.NearPlaneDistance = 1;

The camera’s field of view is specified as an angle and corresponds to the angle of the view cone. You can think of this as setting the type of lens the camera is using. A big angle exaggerates perspective and corresponds to a wide-angle lens. A small angle reduces the perspective effect and corresponds to a telephoto lens:

Camera1.FieldOfView = 45;


The camera geometry


In this case we need to position the camera at a positive z direction to be in front of the cube and to the right and above the cube (recall the cube is positioned at the origin):

Camera1.Position = 
new Point3D(2, 2, 3);

The most difficult setting to get right is the LookDirection, i.e. the direction the camera is pointing in. If you get it wrong you the chances of seeing anything are slim. A handy tip to get started is that if you move the camera, which is initially at the origin, to x,y,z then a LookDirection set to –x,-y,-z looks at the origin.

As our cube is at the origin we can use this prescription to give:

Camera1.LookDirection = 
new Vector3D(-2, -2, -3);

Finally the camera needs to know which way is up:

Camera1.UpDirection = 
new Vector3D(0, 1, 0);

We are now almost ready to render the entire scene but first all of the 3D objects that we have created so far, i.e. the cube and the light, have to be gathered together in a single Model3DGroup object:

Model3DGroup modelGroup =
new Model3DGroup();

In what at first appears to be an unnecessary level in the object hierarchy we next have to assign the Model3DGroup to a ModelVisual3D:

ModelVisual3D modelsVisual =
new ModelVisual3D();
modelsVisual.Content = modelGroup;

and then finally we create a viewport which does the actual job of converting everything from 3D to a 2D rendering:

Viewport3D myViewport = new Viewport3D();

The way the conversion is performed is controlled by the camera assigned to the viewport:

myViewport.Camera = Camera1;

We can now add the 3D model to the viewport so that we have something to render:


and assign the viewport to the container that is going to display the 2D bitmap it creates. A standard WPF application creates a form with a Grid layout container as its content. The simplest thing to do to display the viewport is to add a Canvas layout container to the Grid by dragging and dropping from the Toolbox. This adds a component called Canvas1 by default and we can add the viewport to it using:


We can also specify the 2D physical properties of the viewport such as its size and location:

myViewport.Height = 500;
myViewport.Width = 500;
Canvas.SetTop(myViewport, 0);
Canvas.SetLeft(myViewport, 0);
this.Width = myViewport.Width;
this.Height = myViewport.Height;

If you want to use the entire window to display the scene you could simply use:

this.Content = myViewport;


The red cube





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.