Advertisement
lucianb

Untitled

Sep 2nd, 2022
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. //----------------------------------------------------
  2. // Simple3DSceneInCode.cs (c) 2007 by Charles Petzold
  3. //----------------------------------------------------
  4. using System;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Controls.Primitives;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Media3D;
  10.  
  11. namespace Petzold.Simple3DSceneInCode
  12. {
  13.     public class Simple3DSceneInCode : Window
  14.     {
  15.         PerspectiveCamera cam;
  16.  
  17.         [STAThread]
  18.         public static void Main()
  19.         {
  20.             Application app = new Application();
  21.             app.Run(new Simple3DSceneInCode());
  22.         }
  23.  
  24.         public Simple3DSceneInCode()
  25.         {
  26.             Title = "Simple 3D Scene in Code";
  27.  
  28.             // Make DockPanel content of window.
  29.             DockPanel dock = new DockPanel();
  30.             Content = dock;
  31.  
  32.             // Create Scrollbar for moving camera.
  33.             ScrollBar scroll = new ScrollBar();
  34.             scroll.Orientation = Orientation.Horizontal;
  35.             scroll.Value = -2;
  36.             scroll.Minimum = -2;
  37.             scroll.Maximum = 2;
  38.             scroll.ValueChanged += ScrollBarOnValueChanged;
  39.             dock.Children.Add(scroll);
  40.             DockPanel.SetDock(scroll, Dock.Bottom);
  41.  
  42.             // Create Viewport3D for 3D scene.
  43.             Viewport3D viewport = new Viewport3D();
  44.             dock.Children.Add(viewport);
  45.  
  46.             // Define the MeshGeometry3D.
  47.             MeshGeometry3D mesh = new MeshGeometry3D();
  48.             mesh.Positions.Add(new Point3D(0, 0, 0));
  49.             mesh.Positions.Add(new Point3D(0, 1, -1));
  50.             mesh.Positions.Add(new Point3D(0, 0, -2));
  51.             mesh.TriangleIndices = new Int32Collection(new int[] { 0, 1, 2 });
  52.  
  53.             // Define the GeometryModel3D.
  54.             GeometryModel3D geomod = new GeometryModel3D();
  55.             geomod.Geometry = mesh;
  56.             geomod.Material = new DiffuseMaterial(Brushes.Cyan);
  57.             geomod.BackMaterial = new DiffuseMaterial(Brushes.Red);
  58.  
  59.             // Create ModelVisual3D for GeometryModel3D.
  60.             ModelVisual3D modvis = new ModelVisual3D();
  61.             modvis.Content = geomod;
  62.             viewport.Children.Add(modvis);
  63.  
  64.             // Create another ModelVisual3D for light.
  65.             modvis = new ModelVisual3D();
  66.             modvis.Content = new AmbientLight(Colors.White);
  67.             viewport.Children.Add(modvis);
  68.  
  69.             // Create the camera.
  70.             cam = new PerspectiveCamera(new Point3D(-2, 0, 5),
  71.                             new Vector3D(0, 0, -1), new Vector3D(0, 1, 0), 45);
  72.             viewport.Camera = cam;
  73.         }
  74.         void ScrollBarOnValueChanged(object sender,
  75.                             RoutedPropertyChangedEventArgs<double> args)
  76.         {
  77.             cam.Position = new Point3D(args.NewValue, 0, 5);
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement