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

■ 3차원 큐브 이미지 매핑하기

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


TestProject.zip


MainWindows.xaml

 

 

<Window x:Class="TestProject.MainWindow"

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

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

    Width="800"

    Height="600"

    Title="3차원 큐브 이미지 매핑하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <DockPanel Margin="0" >

        <Menu DockPanel.Dock="Top">

            <MenuItem Header="파일(_F)" Click="saveMenuItem_Click">저장하기</MenuItem>

            <MenuItem Header="보기(_V)">

                <MenuItem Header="(1, 1, 1)"    Click="viewMenuItem_Click" />

                <MenuItem Header="(1, 1, -1)"   Click="viewMenuItem_Click" />

                <MenuItem Header="(-1, 1, -1)" Click="viewMenuItem_Click" />

                <MenuItem Header="(-1, 1, 1)"   Click="viewMenuItem_Click" />

                <MenuItem Header="(1, -1, 1)"   Click="viewMenuItem_Click" />

                <MenuItem Header="(1, -1, -1)" Click="viewMenuItem_Click" />

                <MenuItem Header="(-1, -1, -1)" Click="viewMenuItem_Click" />

                <MenuItem Header="(-1, -1, 1)" Click="viewMenuItem_Click" />

            </MenuItem>

        </Menu>

        <ScrollBar Name="horizontalScrollBar"

     DockPanel.Dock="Bottom"

     Orientation="Horizontal"

     Minimum="-180"

          Maximum="180"

          SmallChange="1"

     LargeChange="10"

          Value="0" />

        <ScrollBar Name="verticalScrollBar"

     DockPanel.Dock="Right"

     Orientation="Vertical"

     Minimum="-180"

          Maximum="180"

          SmallChange="1"

     LargeChange="10"

          Value="0" />

        <DockPanel

            Margin="0"

            Background="White">

            <DockPanel Name="cubdDockPanel"

                Margin="0"

                Background="White">

                <Viewport3D Name="viewport3D" Margin="0">

                    <ModelVisual3D>

                        <ModelVisual3D.Content>

                            <Model3DGroup>

                                <AmbientLight Color="Gray" />

                                <DirectionalLight Color="Gray" Direction="1 -2 -3" />

                                <DirectionalLight Color="Gray" Direction="-1 2 3" />

                                <GeometryModel3D>

                                    <GeometryModel3D.Geometry>

                                        <MeshGeometry3D

                                            Positions="-1 1 1, 1 1 1, 1 1 -1, -1 1 -1"

                                       TriangleIndices="0 1 2, 2 3 0"

                                            TextureCoordinates="0 1, 1 1, 1 0, 0 0" />

                                    </GeometryModel3D.Geometry>

                                    <GeometryModel3D.Material>

                                        <DiffuseMaterial>

                                            <DiffuseMaterial.Brush>

                                                <ImageBrush ImageSource="Top.png" />

                                            </DiffuseMaterial.Brush>

                                        </DiffuseMaterial>

                                    </GeometryModel3D.Material>

                                </GeometryModel3D>

                                <GeometryModel3D>

                                    <GeometryModel3D.Geometry>

                                        <MeshGeometry3D

                                            Positions="-1 -1 1, 1 -1 1, 1 1 1, -1 1 1"

                                       TriangleIndices="0 1 2, 2 3 0"

                                            TextureCoordinates="0 1, 1 1, 1 0, 0 0" />

                                    </GeometryModel3D.Geometry>

                                    <GeometryModel3D.Material>

                                        <DiffuseMaterial>

                                            <DiffuseMaterial.Brush>

                                                <ImageBrush ImageSource="Front.png" />

                                            </DiffuseMaterial.Brush>

                                        </DiffuseMaterial>

                                    </GeometryModel3D.Material>

                                </GeometryModel3D>

                                <GeometryModel3D>

                                    <GeometryModel3D.Geometry>

                                        <MeshGeometry3D

                                            Positions="1 -1 1, 1 -1 -1, 1 1 -1, 1 1 1"

                                       TriangleIndices="0 1 2, 2 3 0"

                                            TextureCoordinates="0 1, 1 1, 1 0, 0 0" />

                                    </GeometryModel3D.Geometry>

                                    <GeometryModel3D.Material>

                                        <DiffuseMaterial>

                                            <DiffuseMaterial.Brush>

                                                <ImageBrush ImageSource="Right.png" />

                                            </DiffuseMaterial.Brush>

                                        </DiffuseMaterial>

                                    </GeometryModel3D.Material>

                                </GeometryModel3D>

                                <GeometryModel3D>

                                    <GeometryModel3D.Geometry>

                                        <MeshGeometry3D

                                            Positions="-1 -1 -1, -1 -1 1, -1 1 1, -1 1 -1"

                                       TriangleIndices="0 1 2, 2 3 0"

                                            TextureCoordinates="0 1, 1 1, 1 0, 0 0" />

                                    </GeometryModel3D.Geometry>

                                    <GeometryModel3D.Material>

                                        <DiffuseMaterial>

                                            <DiffuseMaterial.Brush>

                                                <ImageBrush ImageSource="Left.png" />

                                            </DiffuseMaterial.Brush>

                                        </DiffuseMaterial>

                                    </GeometryModel3D.Material>

                                </GeometryModel3D>

                                <GeometryModel3D>

                                    <GeometryModel3D.Geometry>

                                        <MeshGeometry3D

                                            Positions="1 -1 -1, -1 -1 -1, -1 1 -1, 1 1 -1"

                                       TriangleIndices="0 1 2, 2 3 0"

                                            TextureCoordinates="0 1, 1 1, 1 0, 0 0" />

                                    </GeometryModel3D.Geometry>

                                    <GeometryModel3D.Material>

                                        <DiffuseMaterial>

                                            <DiffuseMaterial.Brush>

                                                <ImageBrush ImageSource="Back.png" />

                                            </DiffuseMaterial.Brush>

                                        </DiffuseMaterial>

                                    </GeometryModel3D.Material>

                                </GeometryModel3D>

                                <GeometryModel3D>

                                    <GeometryModel3D.Geometry>

                                        <MeshGeometry3D

                                            Positions="-1 -1 -1, 1 -1 -1, 1 -1 1, -1 -1 1"

                                       TriangleIndices="0 1 2, 2 3 0"

                                            TextureCoordinates="0 1, 1 1, 1 0, 0 0" />

                                    </GeometryModel3D.Geometry>

                                    <GeometryModel3D.Material>

                                        <DiffuseMaterial>

                                            <DiffuseMaterial.Brush>

                                                <ImageBrush ImageSource="Bottom.png" />

                                            </DiffuseMaterial.Brush>

                                        </DiffuseMaterial>

                                    </GeometryModel3D.Material>

                                </GeometryModel3D>

                            </Model3DGroup>

                        </ModelVisual3D.Content>

                    </ModelVisual3D>

                    <Viewport3D.Camera>

                        <PerspectiveCamera

                            Position="1.75 2.75 2.75"

                            LookDirection="-1.75 -2.75 -2.75"

                            UpDirection="0 1 0"

                            FieldOfView="60">

                            <PerspectiveCamera.Transform>

                                <Transform3DGroup>

                                    <RotateTransform3D>

                                        <RotateTransform3D.Rotation>

                                            <AxisAngleRotation3D

                                                Axis="0 1 0"

                                                Angle="{Binding ElementName=horizontalScrollBar, Path=Value}" />

                                        </RotateTransform3D.Rotation>

                                    </RotateTransform3D>

                                    <RotateTransform3D>

                                        <RotateTransform3D.Rotation>

                                            <AxisAngleRotation3D

                                                Axis="1 0 0"

                                                Angle="{Binding ElementName=verticalScrollBar, Path=Value}" />

                                        </RotateTransform3D.Rotation>

                                    </RotateTransform3D>

                                </Transform3DGroup>

                            </PerspectiveCamera.Transform>

                        </PerspectiveCamera>

                    </Viewport3D.Camera>

                </Viewport3D>

            </DockPanel>

        </DockPanel>

    </DockPanel>

</Window>

 

 

MainWindows.xaml.cs

 

 

using System.IO;

using System.Media;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Media.Media3D;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

        }

 

        #endregion

 

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

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

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 저장하기 메뉴 항목 클릭시 처리하기 - saveMenuItem_Click(sender, e)

 

        /// <summary>

        /// 저장하기 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        private void saveMenuItem_Click(object sender, RoutedEventArgs e)

        {

            RenderTargetBitmap bitmap = new RenderTargetBitmap

            (

                (int)this.cubdDockPanel.ActualWidth,

                (int)this.cubdDockPanel.ActualHeight,

                96,

                96,

                PixelFormats.Pbgra32

            );

 

            bitmap.Render(cubdDockPanel);

 

            PngBitmapEncoder encoder = new PngBitmapEncoder();

 

            encoder.Frames.Add(BitmapFrame.Create(bitmap));

 

            using(FileStream stream = new FileStream("Saved.png", FileMode.Create, FileAccess.Write, FileShare.None))

            {

                encoder.Save(stream);

            }

 

            SystemSounds.Beep.Play();

        }

 

        #endregion

        #region 보기 메뉴 항목 클릭시 처리하기 - viewMenuItem_Click(sender, e)

 

        /// <summary>

        /// 보기 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        private void viewMenuItem_Click(object sender, RoutedEventArgs e)

        {

            MenuItem item = sender as MenuItem;

 

            string text = item.Header.ToString().Replace("(", "").Replace(")", "");

 

            string[] valueArray = text.Split(',');

 

            float x   = 3 * float.Parse(valueArray[0]);

            float y   = 3 * float.Parse(valueArray[1]);

            float z   = 3 * float.Parse(valueArray[2]);

            float yUP = y > 0 ? 1 : -1;

 

            SetCameraPosition(x, y, z, yUP);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 카메라 위치 설정하기 - SetCameraPosition(x, y, z, yUP)

 

        /// <summary>

        /// 카메라 위치 설정하기

        /// </summary>

        /// <param name="x">X 좌표</param>

        /// <param name="y">Y 좌표</param>

        /// <param name="z">Z 좌표</param>

        /// <param name="yUP">Y UP</param>

        private void SetCameraPosition(float x, float y, float z, float yUP)

        {

            this.horizontalScrollBar.Value = 0;

            this.verticalScrollBar.Value   = 0;

 

            PerspectiveCamera camera = this.viewport3D.Camera as PerspectiveCamera;

 

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

            camera.LookDirection = new Vector3D(-x, -y, -z);

            camera.UpDirection   = new Vector3D(0, yUP, 0);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요