■ 파노라마 뷰를 만드는 방법을 보여준다.
▶ PanoramaViewer.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Media3D; namespace TestProject { /// <summary> /// 파노라마 뷰어 /// </summary> public class PanoramaViewer : Viewport3D { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 시야각 속성 - FieldOfViewProperty /// <summary> /// 시야각 속성 /// </summary> public static readonly DependencyProperty FieldOfViewProperty = DependencyProperty.Register ( "FieldOfView", typeof(double), typeof(PanoramaViewer), new PropertyMetadata ( (double)0, new PropertyChangedCallback(FieldOfViewPropertyChangedCallback) ) ); #endregion #region X축 회전 속성 - RotationXProperty /// <summary> /// X축 회전 속성 /// </summary> public static readonly DependencyProperty RotationXProperty = DependencyProperty.Register ( "RotationX", typeof(double), typeof(PanoramaViewer), new UIPropertyMetadata ( 0.0, (d, e) => ((PanoramaViewer)d).UpdateRotation() ) ); #endregion #region Y축 회전 속성 - RotationYProperty /// <summary> /// Y축 회전 속성 /// </summary> public static readonly DependencyProperty RotationYProperty = DependencyProperty.Register ( "RotationY", typeof(double), typeof(PanoramaViewer), new UIPropertyMetadata ( 0.0, (d, e) => ((PanoramaViewer)d).UpdateRotation() ) ); #endregion #region Z축 회전 속성 - RotationZProperty /// <summary> /// Z축 회전 속성 /// </summary> public static readonly DependencyProperty RotationZProperty = DependencyProperty.Register ( "RotationZ", typeof(double), typeof(PanoramaViewer), new UIPropertyMetadata ( 0.0, (d, e) => ((PanoramaViewer)d).UpdateRotation() ) ); #endregion #region 파노라마 이미지 속성 - PanoramaImageProperty /// <summary> /// 파노라마 이미지 속성 /// </summary> public static readonly DependencyProperty PanoramaImageProperty = DependencyProperty.Register ( "PanoramaImage", typeof(ImageSource), typeof(PanoramaViewer), new PropertyMetadata ( null, new PropertyChangedCallback(PanoramaImagePropertyChangedCallback) ) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// X축 벡터 /// </summary> private static readonly Vector3D _xAxisVector = new Vector3D(1, 0, 0); /// <summary> /// Y축 벡터 /// </summary> private static readonly Vector3D _yAxisVector = new Vector3D(0, 1, 0); /// <summary> /// Z축 벡터 /// </summary> private static readonly Vector3D _zAxisVector = new Vector3D(0, 0, 1); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 시야각 - FieldOfView /// <summary> /// 시야각 /// </summary> public double FieldOfView { get { return (double)GetValue(FieldOfViewProperty); } set { SetValue(FieldOfViewProperty, value); } } #endregion #region X축 회전 - RotationX /// <summary> /// X축 회전 /// </summary> public double RotationX { get { return (double)GetValue(RotationXProperty); } set { SetValue(RotationXProperty, value); } } #endregion #region Y축 회전 - RotationY /// <summary> /// Y축 회전 /// </summary> public double RotationY { get { return (double)GetValue(RotationYProperty); } set { SetValue(RotationYProperty, value); } } #endregion #region Z축 회전 - RotationZ /// <summary> /// Z축 회전 /// </summary> public double RotationZ { get { return (double)GetValue(RotationZProperty); } set { SetValue(RotationZProperty, value); } } #endregion #region 파노라마 이미지 - PanoramaImage /// <summary> /// 파노라마 이미지 /// </summary> public ImageSource PanoramaImage { get { return (ImageSource)GetValue(PanoramaImageProperty); } set { SetValue(PanoramaImageProperty, value); } } #endregion #region 파노라마 객체 - PanoramaObject /// <summary> /// 파노라마 객체 /// </summary> public ModelVisual3D PanoramaObject { get; set; } #endregion #region 파노라마 지오메트리 - PanoramaGeometry /// <summary> /// 파노라마 지오메트리 /// </summary> public GeometryModel3D PanoramaGeometry { get; set; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 파노라마 회전 - PanoramaRotation /// <summary> /// 파노라마 회전 /// </summary> private QuaternionRotation3D PanoramaRotation { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PanoramaViewer() /// <summary> /// 생성자 /// </summary> public PanoramaViewer() { InitializeViewer(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 시야각 속성 변경시 콜백 처리하기 - FieldOfViewPropertyChangedCallback(sender, e) /// <summary> /// 시야각 속성 변경시 콜백 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void FieldOfViewPropertyChangedCallback(object sender, DependencyPropertyChangedEventArgs e) { PanoramaViewer viewer = sender as PanoramaViewer; PerspectiveCamera camera = viewer.Camera as PerspectiveCamera; camera.FieldOfView = viewer.FieldOfView; } #endregion #region 파노라마 이미지 속성 변경시 콜백 처리하기 - PanoramaImagePropertyChangedCallback(sender, e) /// <summary> /// 파노라마 이미지 속성 변경시 콜백 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void PanoramaImagePropertyChangedCallback(object sender, DependencyPropertyChangedEventArgs e) { PanoramaViewer viewer = sender as PanoramaViewer; ImageBrush brush = new ImageBrush(viewer.PanoramaImage); viewer.PanoramaGeometry.BackMaterial = new DiffuseMaterial(brush); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region 위치 구하기 - GetPosition(theta, y) /// <summary> /// 위치 구하기 /// </summary> /// <param name="theta">세타</param> /// <param name="y">Y</param> /// <returns>위치</returns> private Point3D GetPosition(double theta, double y) { double r = Math.Sqrt(1 - y * y); double x = r * Math.Cos(theta); double z = r * Math.Sin(theta); return new Point3D(x, y, z); } #endregion #region 법선 벡터 구하기 - GetNormalVector(t, y) /// <summary> /// 법선 벡터 구하기 /// </summary> /// <param name="t">T</param> /// <param name="y">Y</param> /// <returns>법선</returns> private Vector3D GetNormalVector(double t, double y) { return (Vector3D)GetPosition(t, y); } #endregion #region 텍스처 좌표 구하기 - GetTextureCoordinate(t, y) /// <summary> /// 텍스처 좌표 구하기 /// </summary> /// <param name="t">T</param> /// <param name="y">Y</param> /// <returns>텍스처 좌표</returns> private Point GetTextureCoordinate(double t, double y) { Matrix matrix = new Matrix(); matrix.Scale(1 / (2 * Math.PI), -0.5); Point point = new Point(t, y); point = point * matrix; return point; } #endregion #region 지오메트리 생성하기 - CreateGeometry() /// <summary> /// 지오메트리 생성하기 /// </summary> /// <returns>지오메트리</returns> private Geometry3D CreateGeometry() { int thetaDivision = 64; int yDivision = 64; double maximumTheta = (360.0 / 180.0) * Math.PI; double minimumY = -1.0; double maximumY = 1.0; double deltaTheta = maximumTheta / thetaDivision; double deltaY = (maximumY - minimumY) / yDivision; MeshGeometry3D meshGeometry = new MeshGeometry3D(); for(int i = 0; i <= yDivision; i++) { double y = minimumY + i * deltaY; for(int j = 0; j <= thetaDivision; j++) { double t = j * deltaTheta; meshGeometry.Positions.Add(GetPosition(t, y)); meshGeometry.Normals.Add(GetNormalVector(t, y)); meshGeometry.TextureCoordinates.Add(GetTextureCoordinate(t, y)); } } for(int i = 0; i < yDivision; i++) { for(int j = 0; j < thetaDivision; j++) { int x0 = j; int x1 = (j + 1); int y0 = i * (thetaDivision + 1); int y1 = (i + 1) * (thetaDivision + 1); meshGeometry.TriangleIndices.Add(x0 + y0); meshGeometry.TriangleIndices.Add(x0 + y1); meshGeometry.TriangleIndices.Add(x1 + y0); meshGeometry.TriangleIndices.Add(x1 + y0); meshGeometry.TriangleIndices.Add(x0 + y1); meshGeometry.TriangleIndices.Add(x1 + y1); } } meshGeometry.Freeze(); return meshGeometry; } #endregion #region 뷰어 초기화 하기 - InitializeViewer() /// <summary> /// 뷰어 초기화 하기 /// </summary> private void InitializeViewer() { PerspectiveCamera perspectiveCamera = new PerspectiveCamera(); perspectiveCamera.Position = new Point3D(0, -0.0, 0); perspectiveCamera.UpDirection = new Vector3D(0, 1, 0); perspectiveCamera.LookDirection = new Vector3D(0, 0, 1); perspectiveCamera.FieldOfView = 80; Camera = perspectiveCamera; FieldOfView = 80; ModelVisual3D lightModelVisual3D = new ModelVisual3D(); lightModelVisual3D.Content = new DirectionalLight(Colors.White, new Vector3D(0, 0, 1)); Children.Add(lightModelVisual3D); PanoramaGeometry = new GeometryModel3D(); PanoramaGeometry.Geometry = CreateGeometry(); PanoramaObject = new ModelVisual3D(); PanoramaObject.Content = PanoramaGeometry; RotateTransform3D rotateTransform = new RotateTransform3D(); ScaleTransform3D scaleTransform = new ScaleTransform3D() { ScaleX = 1, ScaleY = 1.65, ScaleZ = 1 }; Transform3DGroup transformGroup = new Transform3DGroup(); PanoramaRotation = new QuaternionRotation3D(); transformGroup.Children.Add(scaleTransform ); transformGroup.Children.Add(rotateTransform); rotateTransform.Rotation = PanoramaRotation; PanoramaObject.Transform = transformGroup; Children.Add(PanoramaObject); } #endregion #region 회전 갱신하기 - UpdateRotation() /// <summary> /// 회전 갱신하기 /// </summary> private void UpdateRotation() { Quaternion quaternionX = new Quaternion(_xAxisVector, RotationX); Quaternion quaternionY = new Quaternion(_yAxisVector, RotationY); Quaternion quaternionZ = new Quaternion(_zAxisVector, RotationZ); PanoramaRotation.Quaternion = quaternionX * quaternionY * quaternionZ; } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="파노라마 뷰 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Border Margin="10" BorderThickness="1" BorderBrush="Gray"> <Grid> <local:PanoramaViewer x:Name="viewer" Height="350" PanoramaImage="sample1.jpg" /> </Grid> </Border> <StackPanel Margin="10 0 10 0"> <StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" Text="시야각"/> <Slider VerticalAlignment="Center" Margin="10 0 0 0" Width="50" Minimum="30" Maximum="150" Value="{Binding FieldOfView, ElementName=viewer}" /> <TextBlock Margin="10 0 0 0" VerticalAlignment="Center" Text="X축 회전" /> <Slider VerticalAlignment="Center" Margin="10 0 0 0" Width="90" Minimum="-180" Maximum="180" Value="{Binding RotationX, ElementName=viewer}" /> <TextBlock VerticalAlignment="Center" Margin="10 0 0 0" Text="Y축 회전" /> <Slider VerticalAlignment="Center" Margin="10 0 0 0" Width="90" Minimum="-180" Maximum="180" Value="{Binding RotationY, ElementName=viewer}" /> <TextBlock VerticalAlignment="Center" Margin="10 0 0 0" Text="Z축 회전" /> <Slider VerticalAlignment="Center" Margin="10 0 0 0" Width="90" Minimum="-180" Maximum="180" Value="{Binding RotationZ, ElementName=viewer}" /> </StackPanel> </StackPanel> <StackPanel Margin="10 10 10 0" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Name="button1" Margin="10" Padding="5" Content="샘플 이미지 1" /> <Button Name="button2" Margin="10" Padding="5" Content="샘플 이미지 2" /> <Button Name="button3" Margin="10" Padding="5" Content="샘플 이미지 3" /> <Button Name="button4" Margin="10" Padding="5" Content="샘플 이미지 4" /> </StackPanel> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 회전 벡터 /// </summary> private Vector rotationVector = new Vector(); /// <summary> /// 드래그 시작 포인트 /// </summary> private Point dragStartPoint; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.viewer.MouseLeftButtonDown += viewer_MouseLeftButtonDown; this.viewer.MouseMove += viewer_MouseMove; this.button1.Click += button_Click; this.button2.Click += button_Click; this.button3.Click += button_Click; this.button4.Click += button_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 뷰어 마우스 왼쪽 버튼 DOWN 처리하기 - viewer_MouseLeftButtonDown(sender, e) /// <summary> /// 뷰어 마우스 왼쪽 버튼 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void viewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { dragStartPoint = e.GetPosition(this.viewer); this.rotationVector.X = this.viewer.RotationX; this.rotationVector.Y = this.viewer.RotationY; } #endregion #region 뷰어 마우스 이동시 처리하기 - viewer_MouseMove(sender, e) /// <summary> /// 뷰어 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void viewer_MouseMove(object sender, MouseEventArgs e) { if(e.LeftButton == MouseButtonState.Released) { return; } Vector offsetVector = Point.Subtract(e.GetPosition(this.viewer) , this.dragStartPoint) * 0.25; this.viewer.RotationY = this.rotationVector.Y + offsetVector.X; this.viewer.RotationX = this.rotationVector.X + offsetVector.Y; } #endregion #region 버튼 클릭시 처리하기 - button_Click(sender, e) /// <summary> /// 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; string imageFileName = null; switch(button.Name) { case "button1" : imageFileName = "sample1.jpg"; break; case "button2" : imageFileName = "sample2.jpg"; break; case "button3" : imageFileName = "sample3.jpg"; break; case "button4" : imageFileName = "sample4.jpg"; break; } Uri uri = new Uri($"pack://application:,,,/{imageFileName}"); BitmapImage bitmapImage = new BitmapImage(uri); this.viewer.PanoramaImage = bitmapImage; } #endregion } } |