■ 3D 라인 그리기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="3D 라인 그리기"

    Width="800"

    Height="600">

    <Grid x:Name="grid" />

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

using System.Windows.Media.Media3D;

 

using _3DTools;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 뷰포트 3D

        /// </summary>

        private Viewport3D viewport3D;

 

        /// <summary>

        /// 투시도법 카메라

        /// </summary>

        private PerspectiveCamera perspectiveCamera;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.viewport3D = new Viewport3D();

 

            Content = this.grid;

 

            this.grid.Children.Add(this.viewport3D);

 

            this.perspectiveCamera = GetPerspectiveCamera(new Point3D(15, 20, 15), new Vector3D(-1, -1, -1), new Vector3D(0, 1, 0), 100);

 

            this.viewport3D.Camera = this.perspectiveCamera;

 

            DrawAxisLine(this.viewport3D);

 

            DrawXZPlane(this.viewport3D);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 투시도법 카메라 구하기 - GetPerspectiveCamera(position, lookDirection, upDirection, fieldOfView)

 

        /// <summary>

        /// 투시도법 카메라 구하기

        /// </summary>

        /// <param name="position">위치</param>

        /// <param name="lookDirection">LOOK 방향</param>

        /// <param name="upDirection">UP 방향</param>

        /// <param name="fieldOfView">뷰 필드</param>

        /// <returns>투시도법 카메라</returns>

        private PerspectiveCamera GetPerspectiveCamera(Point3D position, Vector3D lookDirection, Vector3D upDirection, double fieldOfView)

        {

            PerspectiveCamera camera = new PerspectiveCamera();

 

            camera.Position      = position;

            camera.LookDirection = lookDirection;

            camera.UpDirection   = upDirection;

            camera.FieldOfView   = fieldOfView;

 

            return camera;

        }

 

        #endregion

        #region 3D 라인 구하기 - GetLine(startPoint, endPoint, thickness, color)

 

        /// <summary>

        /// 3D 라인 구하기

        /// </summary>

        /// <param name="startPoint">시작 위치</param>

        /// <param name="endPoint">종료 위치</param>

        /// <param name="thickness">두께</param>

        /// <param name="color">색상</param>

        /// <returns>스크린 공간 라인 3D</returns>

        private ScreenSpaceLines3D GetLine3D(Point3D startPoint, Point3D endPoint, double thickness, Color color)

        {

            ScreenSpaceLines3D screenSpaceLines3D = new ScreenSpaceLines3D();

 

            screenSpaceLines3D.Points.Add(startPoint);

            screenSpaceLines3D.Points.Add(endPoint  );

 

            screenSpaceLines3D.Thickness = thickness;

            screenSpaceLines3D.Color     = color;

 

            return screenSpaceLines3D;

        }

 

        #endregion

        #region 축 선 그리기 - DrawAxisLine(viewport3D)

 

        /// <summary>

        /// 축 선 그리기

        /// </summary>

        /// <param name="viewport3D">뷰포트 3D</param>

        private void DrawAxisLine(Viewport3D viewport3D)

        {

            ScreenSpaceLines3D xAxisLine = GetLine3D(new Point3D(0, 0, 0), new Point3D(10, 0, 0), 2, Colors.Red  );

            ScreenSpaceLines3D yAxisLine = GetLine3D(new Point3D(0, 0, 0), new Point3D(0, 10, 0), 2, Colors.Blue );

            ScreenSpaceLines3D zAxisLine = GetLine3D(new Point3D(0, 0, 0), new Point3D(0, 0, 10), 2, Colors.Black);

 

            viewport3D.Children.Add(xAxisLine);

            viewport3D.Children.Add(yAxisLine);

            viewport3D.Children.Add(zAxisLine);

        }

 

        #endregion

        #region XZ 평면 그리기 - DrawXZPlane(viewport3D)

 

        /// <summary>

        /// XZ 평면 그리기

        /// </summary>

        /// <param name="viewport3D">뷰포트 3D</param>

        private void DrawXZPlane(Viewport3D viewport3D)

        {

            for(int i = -20; i < 20; i++)

            {

                ScreenSpaceLines3D xLine = GetLine3D(new Point3D(-20  , 0, i * 1), new Point3D(20   , 0, i * 1), 0.5, Colors.Green);

                ScreenSpaceLines3D zLine = GetLine3D(new Point3D(i * 1, 0, -20  ), new Point3D(i * 1, 0, 20   ), 1  , Colors.Green);

 

                viewport3D.Children.Add(xLine);

                viewport3D.Children.Add(zLine);

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요