Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#

3D Basics using Silverlight-5 and XNA (Part 2)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
12 Dec 2011CPOL8 min read 37.3K   2.4K   16  
Learn 3D programming with Silverlight-5 and XNA
using System.Windows.Controls;
using System.Windows.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Polyhedron
{
  public partial class MainPage : UserControl
  {
    private float aspectRatio = 1f;
    private Polyhedron[] polyhedrons;

    public MainPage()
    {
      InitializeComponent();
    }

    private void drawingSurface1_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
      polyhedrons = new Polyhedron[]
      {
        new Polyhedron(Polyhedra.CubeCorners, Polyhedra.CubeFaces, new Color(0, 0, 1f)),
        new Polyhedron(Polyhedra.TetrahedronCorners, Polyhedra.TetrahedronFaces, new Color(1f, 1f, 0)),
        new Polyhedron(Polyhedra.OctahedronCorners, Polyhedra.OctahedronFaces, new Color(0, 1f, 1f)),
        new Polyhedron(Polyhedra.DodecahedronCorners, Polyhedra.DodecahedronFaces, new Color(0, 1f, 0)),
        new Polyhedron(Polyhedra.IcosahedronCorners, Polyhedra.IcosahedronFaces, new Color(1f, 0, 0))
      };
    }

    private void drawingSurface1_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
    {
      aspectRatio = (float)(drawingSurface1.ActualWidth / drawingSurface1.ActualHeight);
    }

    private void drawingSurface1_Draw(object sender, DrawEventArgs e)
    {
      GraphicsDevice g = GraphicsDeviceManager.Current.GraphicsDevice;
      Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 8.0f), Vector3.Zero, Vector3.Up);
      Matrix projection = Matrix.CreatePerspectiveFieldOfView(0.85f, aspectRatio, 0.01f, 1000.0f);

      Vector3 axis = new Vector3(-0.5f, 1, -0.5f);
      axis.Normalize();
      Matrix rotate1 = Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(axis, (float)e.TotalTime.TotalSeconds * 3));
      Matrix translate = Matrix.CreateTranslation(2, 0, 0);
      for (int i = 0; i < polyhedrons.Length; i++)
      {
        Matrix rotate2 = Matrix.CreateRotationZ(i * MathHelper.TwoPi / 5 + (float)e.TotalTime.TotalSeconds / 3);
        polyhedrons[i].World = rotate1 * translate * rotate2;
      }
      g.Clear(new Color(0.8f, 0.8f, 0.8f, 1.0f));
      foreach (Polyhedron polyhedron in polyhedrons)
        polyhedron.Draw(view, projection);
      e.InvalidateSurface();
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) MetaMapics Ltd.
United Kingdom United Kingdom
Freelance software developer with a focus on Microsoft technologies and specialist experience in mapping and location-based applications.

Comments and Discussions