日期:2014-05-18 浏览次数:20977 次
| | | | | | | ------------------- | | | | | | | | | | ------------------- | | | | |
public partial class MainWindow : Window { int i = 0; MatrixTransform3D mt3d = new MatrixTransform3D(); public MainWindow() { InitializeComponent(); Viewport3D viewport = new Viewport3D(); ModelVisual3D modelvisual = new ModelVisual3D(); Model3DGroup modelgroup = new Model3DGroup(); GeometryModel3D geomodel = new GeometryModel3D(); GeometryModel3D geomodel2 = new GeometryModel3D(); MeshGeometry3D mgeo = new MeshGeometry3D(); MeshGeometry3D mgeo2 = new MeshGeometry3D(); viewport.Camera = new PerspectiveCamera(new Point3D(0, 0.5, 8), new Vector3D(0, 0, -1), new Vector3D(0.5, 0, -1), 45); mgeo.Positions.Add(new Point3D(-0.5, -0.5, -0.5)); mgeo.Positions.Add(new Point3D(0.5, -0.5, 0.5)); mgeo.Positions.Add(new Point3D(0.5, 0.5, 0.5)); mgeo.Positions.Add(new Point3D(0.5, 0.5, 0.5)); mgeo.Positions.Add(new Point3D(-0.5, 0.5, -0.5)); mgeo.Positions.Add(new Point3D(-0.5, -0.5, -0.5)); mgeo2.Positions.Add(new Point3D(-1.5, 1, -1)); mgeo2.Positions.Add(new Point3D(1.5, 1, -1)); mgeo2.Positions.Add(new Point3D(1.5, 1.1, -1)); mgeo2.Positions.Add(new Point3D(1.5, 1.1, -1)); mgeo2.Positions.Add(new Point3D(-1.5, 1.1, -1)); mgeo2.Positions.Add(new Point3D(-1.5, 1, -1)); geomodel.Geometry = mgeo; geomodel.Material = new DiffuseMaterial(Brushes.Black); geomodel.Transform = mt3d; geomodel.BackMaterial = new DiffuseMaterial(Brushes.Black); geomodel2.Geometry = mgeo2; geomodel2.Material = new DiffuseMaterial(Brushes.Black); modelgroup.Children.Add(geomodel); modelgroup.Children.Add(geomodel2); modelvisual.Content = modelgroup; viewport.Children.Add(modelvisual); grid1.Children.Add(viewport); CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering); } void CompositionTarget_Rendering(object sender, EventArgs e) { double m11 = Math.Cos(i * 0.01); double m12 = Math.Sin(i * 0.01); Matrix3D matrix = new Matrix3D(); matrix.M11 = 0; matrix.M12 = m11; matrix.M13 = m12; matrix.M21 = 1; matrix.M22 = 0; matrix.M23 = 0; matrix.M31 = 0; matrix.M32 = -m12; matrix.M33 = m11; matrix.OffsetY = 0.9 * (1 - m11 + m12); matrix.OffsetZ = 0.9 * (1 - m12 - m11); mt3d.Matrix = matrix; i++; } }