WPF The Easy 3D Way
WPF The Easy 3D Way
Written by Mike James   
Wednesday, 10 June 2015
Article Index
WPF The Easy 3D Way
Model Groups

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 or you can copy and paste the listing below.


The complete program is (omitting the usual using statements):


using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
namespace Cube1
 /// <summary>
 /// Interaction logic for MainWindow.xaml
 /// </summary>
 public partial class MainWindow : Window
  public MainWindow()
  MeshGeometry3D MCube()
   MeshGeometry3D cube = new MeshGeometry3D();
   Point3DCollection corners = new
   corners.Add(new Point3D(0.5, 0.5, 0.5));
   corners.Add(new Point3D(-0.5, 0.5, 0.5));
   corners.Add(new Point3D(-0.5, -0.5, 0.5));
   corners.Add(new Point3D(0.5, -0.5, 0.5));
   corners.Add(new Point3D(0.5, 0.5, -0.5));
   corners.Add(new Point3D(-0.5, 0.5, -0.5));
   corners.Add(new Point3D(-0.5, -0.5, -0.5));
   corners.Add(new Point3D(0.5, -0.5, -0.5));
   cube.Positions = corners;

   Int32[] indices ={

  Int32Collection Triangles =
                        new Int32Collection();
  foreach (Int32 index in indices)
  cube.TriangleIndices = Triangles;
  return cube;

 private void Window_Loaded(object sender,
                           RoutedEventArgs e)
  GeometryModel3D Cube1 =
                       new GeometryModel3D();
  MeshGeometry3D cubeMesh = MCube();
  Cube1.Geometry = cubeMesh;
  Cube1.Material = new DiffuseMaterial(
            new SolidColorBrush(Colors.Red));

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

  PerspectiveCamera Camera1 =
                     new PerspectiveCamera();
  Camera1.FarPlaneDistance = 20;
  Camera1.NearPlaneDistance = 1;
  Camera1.FieldOfView = 45;
  Camera1.Position = new Point3D(2, 2, 3);
  Camera1.LookDirection =
                    new Vector3D(-2, -2, -3);
  Camera1.UpDirection =
                       new Vector3D(0, 1, 0);

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

  Viewport3D myViewport = new Viewport3D();
  myViewport.Camera = Camera1;
  myViewport.Height = 500;
  myViewport.Width = 500;
  Canvas.SetTop(myViewport, 0);
  Canvas.SetLeft(myViewport, 0);
  this.Width = myViewport.Width;
  this.Height = myViewport.Height;

  AxisAngleRotation3D axis =
               new AxisAngleRotation3D(
                 new Vector3D(0, 1, 0), 0);
  RotateTransform3D Rotate =
               new RotateTransform3D(axis);
  Cube1.Transform = Rotate;
  DoubleAnimation RotAngle =
                     new DoubleAnimation();
  RotAngle.From = 0;
  RotAngle.To = 360;
  RotAngle.Duration = new Duration(
  RotAngle.RepeatBehavior =
                         new NameScope());
  Canvas1.RegisterName("cubeaxis", axis);
   new PropertyPath(
  Storyboard RotCube = new Storyboard();






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 [ ... ]

Loading Bitmaps: DoEvents and the closure pattern

Sometimes loading a bitmap causes an asynchronous download and you have to wait for it to complete before using it - but how best to wait? The standard solution is to use an event but this breaks what [ ... ]

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 [ ... ]

BitmapSource: WPF Bitmaps

Although bitmap graphics are not a main focus of WPF it does offer some useful facilities

BitmapImage and local files

When and how to use the BitmapImage class

Other Articles




blog comments powered by Disqus

















Last Updated ( Wednesday, 10 June 2015 )

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