Ejemplo n.º 1
0
        public System.Drawing.Image Render(RendererOptions options)
        {
            // The main object model group.
            Model3DGroup group = new Model3DGroup();

            // The camera.
            PerspectiveCamera camera = new PerspectiveCamera();

            Viewport3D viewport = new Viewport3D();

            // Give the camera its initial position.
            camera.FieldOfView = 5.75;
            viewport.Camera    = camera;

            // The camera's current location.

            string textureFilePath = options.TextureFilePath;
            int    size            = (int)options.OutputImageSize;
            double cameraPhi       = ToRadians(options.LatitudeShift);
            double cameraTheta     = ToRadians(options.LongutudeShift);
            double cameraR         = 20;

            // Calculate the camera's position in Cartesian coordinates.
            double y   = cameraR * Math.Sin(cameraPhi);
            double hyp = cameraR * Math.Cos(cameraPhi);
            double x   = hyp * Math.Cos(cameraTheta);
            double z   = hyp * Math.Sin(cameraTheta);

            camera.Position = new Point3D(x, y, z);

            // Look toward the origin.
            camera.LookDirection = new Vector3D(-x, -y, -z);

            // Set the Up direction.
            camera.UpDirection = new Vector3D(0, 1, 0);

            // Define lights.
            AmbientLight ambientLight = new AmbientLight(Colors.White);

            group.Children.Add(ambientLight);

            // Create the model.
            // Globe. Place it in a new model so we can transform it.
            Model3DGroup globe = new Model3DGroup();

            group.Children.Add(globe);

            ImageBrush globeBrush    = new ImageBrush(new BitmapImage(new Uri(textureFilePath, UriKind.RelativeOrAbsolute)));
            Material   globeMaterial = new DiffuseMaterial(globeBrush);

            MakeSphere(globe, globeMaterial, 1, 0, 0, 0, 64, 64);

            // Add the group of models to a ModelVisual3D.
            ModelVisual3D visual = new ModelVisual3D();

            visual.Content = group;

            // Display the main visual to the viewport.
            viewport.Children.Add(visual);

            viewport.Width  = size;
            viewport.Height = size;
            viewport.Measure(new System.Windows.Size(size, size));
            viewport.Arrange(new System.Windows.Rect(0, 0, size, size));
            viewport.InvalidateVisual();

            if (targetBitmap == null)
            {
                targetBitmap = new RenderTargetBitmap(size, size, 96, 96, PixelFormats.Pbgra32);
            }

            targetBitmap.Clear();
            targetBitmap.Render(viewport);

            return(ToWinFormsBitmap(targetBitmap));
        }
Ejemplo n.º 2
0
 public RendererOptionsInOut(RendererOptions opts, Action <Bitmap> onComplete)
 {
     Options    = opts;
     OnComplete = onComplete;
 }