첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 코드로 삼각형 3D 만들기

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


TestProject.zip



MainWindow.cs

 

 

using System.Windows;

using System.Windows.Controls;

using System.Windows.Controls.Primitives;

using System.Windows.Media;

using System.Windows.Media.Media3D;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 투시도법 카메라

        /// </summary>

        private PerspectiveCamera perspectiveCamera;

 

        #endregion

 

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            #region 스크롤바를 설정합니다.

 

            ScrollBar scrollBar = new ScrollBar();

 

            scrollBar.Orientation = Orientation.Horizontal;

            scrollBar.Value       = -2;

            scrollBar.Minimum     = -2;

            scrollBar.Maximum     = 2;

 

            scrollBar.ValueChanged += scrollBar_ValueChanged;

 

            #endregion

            #region 삼각형 MeshGeometry3D를 설정합니다.

 

            MeshGeometry3D triangleMeshGeometry3D = new MeshGeometry3D();

 

            triangleMeshGeometry3D.Positions.Add(new Point3D(0, 0,  0));

            triangleMeshGeometry3D.Positions.Add(new Point3D(0, 1, -1));

            triangleMeshGeometry3D.Positions.Add(new Point3D(0, 0, -2));

 

            triangleMeshGeometry3D.TriangleIndices = new Int32Collection(new int[] { 0, 1, 2 });

 

            #endregion

            #region 삼각형 GeometryModel3D를 설정합니다.

 

            GeometryModel3D triangleGeometryModel3D = new GeometryModel3D();

 

            triangleGeometryModel3D.Geometry     = triangleMeshGeometry3D;

            triangleGeometryModel3D.Material     = new DiffuseMaterial(Brushes.Blue);

            triangleGeometryModel3D.BackMaterial = new DiffuseMaterial(Brushes.Red );

 

            #endregion

            #region 삼각형 ModelVisual3D를 설정합니다.

 

            ModelVisual3D triangleModelVisual3D = new ModelVisual3D();

 

            triangleModelVisual3D.Content = triangleGeometryModel3D;

 

            #endregion

            #region 환경광을 설정합니다.

 

            AmbientLight ambientLight = new AmbientLight(Colors.White);

 

            #endregion

            #region 환경광 ModelVisual3D를 설정합니다.

 

            ModelVisual3D lightModelVisual3D = new ModelVisual3D();

 

            lightModelVisual3D.Content = ambientLight;

 

            #endregion

            #region 투시도법 카메라를 설정합니다.

 

            this.perspectiveCamera = new PerspectiveCamera(new Point3D(-2, 0, 5), new Vector3D(0, 0, -1), new Vector3D(0, 1, 0), 45);

 

            #endregion

            #region Viewport3D를 설정합니다.

 

            Viewport3D viewport3D = new Viewport3D();

 

            viewport3D.Camera = this.perspectiveCamera;

 

            viewport3D.Children.Add(triangleModelVisual3D);

            viewport3D.Children.Add(lightModelVisual3D);

 

            #endregion

            #region 도킹 패널을 설정합니다.

 

            DockPanel dockPanel = new DockPanel();

 

            dockPanel.Children.Add(scrollBar);

 

            DockPanel.SetDock(scrollBar, Dock.Bottom);

 

            dockPanel.Children.Add(viewport3D);

 

            Content = dockPanel;

 

            #endregion

        }

 

        #endregion

 

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

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

 

        #region 스크롤바 값 변경시 처리하기 - scrollBar_ValueChanged(sender, e)

 

        /// <summary>

        /// 스크롤바 값 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void scrollBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

        {

            this.perspectiveCamera.Position = new Point3D(e.NewValue, 0, 5);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요