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

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.

Listing

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()
  {
   InitializeComponent();
  }
 
  MeshGeometry3D MCube()
  {
   MeshGeometry3D cube = new MeshGeometry3D();
   Point3DCollection corners = new
                          Point3DCollection();
   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 ={
   //front
     0,1,2,
     0,2,3,
  //back
     4,7,6,
     4,6,5,
  //Right
     4,0,3,
     4,3,7,
  //Left
     1,5,6,
     1,6,2,
  //Top
     1,0,4,
     1,4,5,
  //Bottom
     2,6,7,
     2,7,3
  };

  Int32Collection Triangles =
                        new Int32Collection();
  foreach (Int32 index in indices)
  {
   Triangles.Add(index);
  }
  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();
  modelGroup.Children.Add(Cube1);
  modelGroup.Children.Add(DirLight1);
  ModelVisual3D modelsVisual =
                         new ModelVisual3D();
  modelsVisual.Content = modelGroup;

  Viewport3D myViewport = new Viewport3D();
  myViewport.Camera = Camera1;
  myViewport.Children.Add(modelsVisual);
  this.Canvas1.Children.Add(myViewport);
  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(
               TimeSpan.FromSeconds(20.0));
  RotAngle.RepeatBehavior =
                   RepeatBehavior.Forever;
  NameScope.SetNameScope(Canvas1,
                         new NameScope());
  Canvas1.RegisterName("cubeaxis", axis);
  Storyboard.SetTargetName(RotAngle,
                             "cubeaxis");
  Storyboard.SetTargetProperty(RotAngle,
   new PropertyPath(
      AxisAngleRotation3D.AngleProperty));
  Storyboard RotCube = new Storyboard();
  RotCube.Children.Add(RotAngle);
  RotCube.Begin(Canvas1);
  }
}

 

 

Banner

 

 


Custom Bitmap Effects - HLSL

In the article Custom Bitmap Effects - Getting started we discovered how to work with HLSL in WPF. Now we are in a position to write more sophisticated shaders and  this means learning some more  [ ... ]



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.



Drawing Bitmaps – DrawingImage and DrawingVisual

 

WPF provides multiple ways to convert vector drawings to bitmaps. Find out how DrawingImage and DrawingVisual work and when to use which. On the way we look at how to create 2D vector drawings.



Custom Bitmap Effects - Getting started

The simplest possible custom effects project is enough for you to see how it all works and to move on to building your own effects that do something useful. This is an introduction to using HLSL in WP [ ... ]



Bitmap Coding and Metatdata in WPF


Having looked at working with raw pixel data we turn our attention to formattted image files and how to code and decode both the pixel data and the meta data they contain.

 


Other Articles

 

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:0201398559>

<ASIN:1556229119>

<ASIN:1848000413>

<ASIN:0470041803>

<ASIN:159863156X>

<ASIN:0596518439>

<ASIN:1932394818>

<ASIN:1590597826>

<ASIN:0596510373>

<ASIN:3866454236@DE>

<ASIN:1590599551>

<ASIN:0735623945>

<ASIN:1430210842>

<ASIN:0672329859>

<ASIN:0979372518>

<ASIN:0672328917>



Last Updated ( Wednesday, 10 June 2015 )