Copies the specified camera, converts field of view width if necessary. - CSharp System.Windows.Media.Media3D

CSharp examples for System.Windows.Media.Media3D:PerspectiveCamera

Description

Copies the specified camera, converts field of view width if necessary.

Demo Code

//   http://helixtoolkit.codeplex.com, license: MIT
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using System.Windows.Controls;
using System.Windows;
using System.Text;
using System.Globalization;
using System;/* www. j  a v  a2  s  . c o  m*/

public class Main{
        /// <summary>
        /// Copies the specified camera, converts field of view/width if necessary.
        /// </summary>
        /// <param name="source">The source camera.</param>
        /// <param name="dest">The destination camera.</param>
        /// <param name="copyNearFarPlaneDistances">Copy near and far plane distances if set to <c>true</c>.</param>
        public static void Copy(this ProjectionCamera source, ProjectionCamera dest, bool copyNearFarPlaneDistances = true)
        {
            if (source == null || dest == null)
            {
                return;
            }

            dest.LookDirection = source.LookDirection;
            dest.Position = source.Position;
            dest.UpDirection = source.UpDirection;

            if (copyNearFarPlaneDistances)
            {
                dest.NearPlaneDistance = source.NearPlaneDistance;
                dest.FarPlaneDistance = source.FarPlaneDistance;
            }

            var psrc = source as PerspectiveCamera;
            var osrc = source as OrthographicCamera;
            var pdest = dest as PerspectiveCamera;
            var odest = dest as OrthographicCamera;
            if (pdest != null)
            {
                double fov = 45;
                if (psrc != null)
                {
                    fov = psrc.FieldOfView;
                }

                if (osrc != null)
                {
                    double dist = source.LookDirection.Length;
                    fov = Math.Atan(osrc.Width / 2 / dist) * 180 / Math.PI * 2;
                }

                pdest.FieldOfView = fov;
            }

            if (odest != null)
            {
                double width = 100;
                if (psrc != null)
                {
                    double dist = source.LookDirection.Length;
                    width = Math.Tan(psrc.FieldOfView / 180 * Math.PI / 2) * dist * 2;
                }

                if (osrc != null)
                {
                    width = osrc.Width;
                }

                odest.Width = width;
            }
        }
}

Related Tutorials