2012년 7월 22일 일요일

[WPF] 3D 객체 등록 및 카메라 관련 소스

ㆍ언어대상 : WPF   ㆍ실행대상 : WPF   ㆍ작성자 : 홍석인  

  public partial class Ani_2 : Window
    {
        Viewport3D view3d = new Viewport3D();

        // 카메라 설정
        OrthographicCamera camera = new OrthographicCamera();

        // 각 모델들의 집합
        ModelVisual3D _ModelGroup = new ModelVisual3D();


        double x = 5;
        double y = 5;
        double z = 5;

       
        

        public Ani_2()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 카메라 보는 방향을 설정하는 구문
            camera.Position = new Point3D(5, 5, 5);
            camera.LookDirection = new Vector3D(-1, -1, -1);
            camera.Width = 10;

            // Pro WPF in c# 2008 page 877 설명
            view3d.ClipToBounds = false;

            // 위 지붕
            GeometryModel3D Model1 = new GeometryModel3D();
            #region 윗 지붕
            #region 그릴 객체의 색을 정의 하는 구간
            DiffuseMaterial meterial = new DiffuseMaterial(new SolidColorBrush(Colors.Pink));
            #endregion

            #region 그릴 포지션을 정의 하는 구간
            MeshGeometry3D pointer = new MeshGeometry3D();
            pointer.Positions.Add(new Point3D(-1,1,1));
            pointer.Positions.Add(new Point3D(0, 2, 1));
            pointer.Positions.Add(new Point3D(0, 2, -1));
            pointer.Positions.Add(new Point3D(-1,1,-1));
            pointer.Positions.Add(new Point3D(0, 2, 1));
            pointer.Positions.Add(new Point3D(1, 1, 1));
            pointer.Positions.Add(new Point3D(1, 1, -1));
            pointer.Positions.Add(new Point3D(0, 2, -1));
            pointer.TriangleIndices.Add(0);
            pointer.TriangleIndices.Add(1);
            pointer.TriangleIndices.Add(2);
            pointer.TriangleIndices.Add(0);
            pointer.TriangleIndices.Add(2);
            pointer.TriangleIndices.Add(3);
            pointer.TriangleIndices.Add(4);
            pointer.TriangleIndices.Add(5);
            pointer.TriangleIndices.Add(6);
            pointer.TriangleIndices.Add(4);
            pointer.TriangleIndices.Add(6);
            pointer.TriangleIndices.Add(7);
            #endregion

            Model1.Geometry = pointer;
            Model1.Material = meterial;

            #endregion
           
            // 옆면
            GeometryModel3D Model2 = new GeometryModel3D();
            #region 옆면
            DiffuseMaterial meterial2 = new DiffuseMaterial(new SolidColorBrush(Colors.Red));
            MeshGeometry3D pointer2 = new MeshGeometry3D();
            pointer2.Positions.Add(new Point3D(-1,1,1));
            pointer2.Positions.Add(new Point3D(-1,1,-1));
            pointer2.Positions.Add(new Point3D(-1,-1,-1));
            pointer2.Positions.Add(new Point3D(-1,-1,1));
            pointer2.Positions.Add(new Point3D(1,1,-1));
            pointer2.Positions.Add(new Point3D(1,1,1));
            pointer2.Positions.Add(new Point3D(1,-1,1));
            pointer2.Positions.Add(new Point3D(1,-1,-1));
            pointer2.TriangleIndices.Add(0);
            pointer2.TriangleIndices.Add(1);
            pointer2.TriangleIndices.Add(2);
            pointer2.TriangleIndices.Add(0);
            pointer2.TriangleIndices.Add(2);
            pointer2.TriangleIndices.Add(3);
            pointer2.TriangleIndices.Add(4);
            pointer2.TriangleIndices.Add(5);
            pointer2.TriangleIndices.Add(6);
            pointer2.TriangleIndices.Add(4);
            pointer2.TriangleIndices.Add(6);
            pointer2.TriangleIndices.Add(7);
            Model2.Geometry = pointer2;
            Model2.Material = meterial2;
            #endregion

            // 옆면
            GeometryModel3D Model3 = new GeometryModel3D();
            #region 옆면
            DiffuseMaterial meterial3 = new DiffuseMaterial(new SolidColorBrush(Colors.Green));
            MeshGeometry3D pointer3 = new MeshGeometry3D();
            pointer3.Positions.Add(new Point3D(-0.25,0,1));
            pointer3.Positions.Add(new Point3D(-1,1,1));
            pointer3.Positions.Add(new Point3D(-1,-1,1));
            pointer3.Positions.Add(new Point3D(-0.25,-1,1));
            pointer3.Positions.Add(new Point3D(-0.25,0,1));
            pointer3.Positions.Add(new Point3D(-1,-1,1));
            pointer3.Positions.Add(new Point3D(0.25,0,1));
            pointer3.Positions.Add(new Point3D(1,-1,1));
            pointer3.Positions.Add(new Point3D(1,1,1));
            pointer3.Positions.Add(new Point3D(0.25,0,1));
            pointer3.Positions.Add(new Point3D(0.25,-1,1));
            pointer3.Positions.Add(new Point3D(1,-1,1));
            pointer3.Positions.Add(new Point3D(1,1,1));
            pointer3.Positions.Add(new Point3D(0,2,1));
            pointer3.Positions.Add(new Point3D(-1,1,1));
            pointer3.Positions.Add(new Point3D(-1,1,1));
            pointer3.Positions.Add(new Point3D(-0.25,0,1));
            pointer3.Positions.Add(new Point3D(0.25,0,1));
            pointer3.Positions.Add(new Point3D(1,1,1));
            pointer3.Positions.Add(new Point3D(1,1,-1));
            pointer3.Positions.Add(new Point3D(1,-1,-1));
            pointer3.Positions.Add(new Point3D(-1,-1,-1));
            pointer3.Positions.Add(new Point3D(-1,1,-1));
            pointer3.Positions.Add(new Point3D(1,1,-1));
            pointer3.Positions.Add(new Point3D(-1,1,-1));
            pointer3.Positions.Add(new Point3D(0,2,-1));

            int[] data = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 15, 17, 18, 19, 20, 21, 19, 21, 22, 23, 24, 25 };

            for (int i = 0; i < data.Length; i++)
            {
                pointer3.TriangleIndices.Add(data[i]);
            }
            Model3.Geometry = pointer3;
            Model3.Material = meterial3;
            #endregion


           

            ModelVisual3D show3d = new ModelVisual3D();
            ModelVisual3D show3d2 = new ModelVisual3D();
            ModelVisual3D show3d3 = new ModelVisual3D();
            ModelVisual3D trns = new ModelVisual3D();
        

            ModelVisual3D Light = new ModelVisual3D(); // 불빛을 설정해줘야함 , 이것이 없으면 색은 검은색으로 표현

            AmbientLight ll = new AmbientLight();
            Light.Content = ll;
           
            show3d.Content = Model1;
            show3d2.Content = Model2;
            show3d3.Content = Model3;
           

            _ModelGroup.Children.Add(show3d);
            _ModelGroup.Children.Add(show3d2);
            _ModelGroup.Children.Add(show3d3);
            _ModelGroup.Children.Add(Light);


        

            // 각종 모델의 변환의 그룹
            Transform3DGroup TransGroup = new Transform3DGroup();


           
            RotateTransform3D ro3d = new RotateTransform3D();
            AxisAngleRotation3D axisro = new AxisAngleRotation3D();
            axisro.Angle = 45;
            axisro.Axis = new Vector3D(0, 1, 0);
            ro3d.Rotation = axisro;

            ScaleTransform3D scale3d = new ScaleTransform3D();

            scale3d.ScaleX = 1.5;
            scale3d.ScaleY = 1.5;
            scale3d.ScaleZ = 1.5;


            TransGroup.Children.Add(scale3d);
            TransGroup.Children.Add(ro3d);

            Model1.Transform = TransGroup;
            Model2.Transform = TransGroup;
            Model3.Transform = TransGroup;


            view3d.Children.Add(_ModelGroup);
            // 카메라 보는 값 세팅 추가
            view3d.Camera = camera;

            Page Page = new Page();
            Page.Background = Brushes.Black;

            Page.Width = 100;
            Page.Height = 100;

            Page.Content = view3d;
            this.AddChild(Page);
       
        }

     
        private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta / 120 > 0)
            {
                camera.Position = new Point3D(x, y, z++);
                //camera.UpDirection = new Vector3D(x, y, z++);
            }
            else
            {
                camera.Position = new Point3D(x, y, z--);
                //camera.UpDirection = new Vector3D(x, y, z--);
            }
            camera.LookDirection = new Point3D(0, 0, 0) - camera.Position;
            view3d.Camera = camera;
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Left)
            {
                // 카메라 보는 방향을 설정하는 구문
                camera.Position = new Point3D(x++, y, z);
                camera.Width = 10;
                // 카메라 보는 값 세팅 추가
                view3d.Camera = camera;
            }
            else if (e.Key == Key.Right)
            {
                // 카메라 보는 방향을 설정하는 구문
                camera.Position = new Point3D(x--, y, z);
                camera.Width = 10;
                // 카메라 보는 값 세팅 추가
                view3d.Camera = camera;
            }
            else if (e.Key == Key.Down)
            {
                // 카메라 보는 방향을 설정하는 구문
                camera.Position = new Point3D(x, y++, z);
                camera.Width = 10;
                // 카메라 보는 값 세팅 추가
                view3d.Camera = camera;
            }
            else if (e.Key == Key.Up)
            {
                // 카메라 보는 방향을 설정하는 구문
                camera.Position = new Point3D(x, y--, z);
                camera.Width = 10;
                // 카메라 보는 값 세팅 추가
                view3d.Camera = camera;
            }

            // 특정한 좌표값을 중심으로 움직임!!
            camera.LookDirection = new Point3D(0, 0, 0) - camera.Position;
        }
    }



해당 내용은 (주)데브존의 웹봇이 자동으로 저희 홈페이지와 API로 동기화한 내용입니다. 자세한 내용은 홈페이지 http://www.devzone.co.kr 또는 모바일 폰에서 http://devzone.co.kr을 접속 하여 확인해 보시기 비랍니다.
-  대표 전화 : 02) 2061 - 0753   - Fax : 02) 2061 - 0759   - Skype : devzone24
-  주소 : 서울특별시 양천구 목1동 923-14. 현대드림타워 1024호
-  기술 문의: 02) 2061-1259 김민석 기술이사


댓글 없음:

댓글 쓰기