■ 3차원 표면에 격자를 그리는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<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차원 표면 격자 그리기" Loaded="Window_Loaded" KeyDown="Window_KeyDown"> <Grid> <Viewport3D Name="mainViewport" /> </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 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 |
using System; using System.Collections.Generic; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Media.Media3D; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 모델 3D 그룹 /// </summary> private Model3DGroup model3Dgroup = new Model3DGroup(); /// <summary> /// 카메라 /// </summary> private PerspectiveCamera camera; /// <summary> /// 카메라 R /// </summary> private double cameraR = 3.0; /// <summary> /// 카메라 파이 /// </summary> private double cameraPhi = Math.PI / 6.0; /// <summary> /// 카메라 세타 /// </summary> private double cameraTheta = Math.PI / 6.0; /// <summary> /// 카메라 델타 파이 /// </summary> private const double CAMERA_DELTA_PHI = 0.1; /// <summary> /// 카메라 델타 세타 /// </summary> private const double CAMERA_DELTA_THETA = 0.1; /// <summary> /// 카메라 델타 R /// </summary> private const double CAMERA_DELTA_R = 0.1; /// <summary> /// X 최소값 /// </summary> private const double X_MINIMUM = -1.5; /// <summary> /// X 최대값 /// </summary> private const double X_MAXIMUM = 1.5; /// <summary> /// X 델타 /// </summary> private const double DELTA_X = 0.03; /// <summary> /// Z 최소값 /// </summary> private const double Z_MINIMUM = -1.5; /// <summary> /// Z 최대값 /// </summary> private const double Z_MAXIMUM = 1.5; /// <summary> /// Z 델타 /// </summary> private const double DELTA_Z = 0.03; /// <summary> /// 텍스처 X 스케일 /// </summary> private const double TEXTURE_X_SCALE = (X_MAXIMUM - X_MINIMUM); /// <summary> /// 텍스처 Z 스케일 /// </summary> private const double TEXTURE_Z_SCALE = (Z_MAXIMUM - Z_MINIMUM); /// <summary> /// 3차원 포인트 딕셔너리 /// </summary> private Dictionary<Point3D, int> point3DDictionary = new Dictionary<Point3D, int>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { this.camera = new PerspectiveCamera(); this.camera.FieldOfView = 60; this.mainViewport.Camera = this.camera; SetCameraPosition(); DefineLight(); DefineModel(this.model3Dgroup); ModelVisual3D modelVisual3D = new ModelVisual3D(); modelVisual3D.Content = this.model3Dgroup; this.mainViewport.Children.Add(modelVisual3D); } #endregion #region 윈도우 키 DOWN 처리하기 - Window_KeyDown(sender, e) /// <summary> /// 윈도우 키 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_KeyDown(object sender, KeyEventArgs e) { switch(e.Key) { case Key.Up : this.cameraPhi += CAMERA_DELTA_PHI; if(this.cameraPhi > Math.PI / 2.0) { this.cameraPhi = Math.PI / 2.0; } break; case Key.Down : this.cameraPhi -= CAMERA_DELTA_PHI; if(this.cameraPhi < -Math.PI / 2.0) { this.cameraPhi = -Math.PI / 2.0; } break; case Key.Left : this.cameraTheta += CAMERA_DELTA_THETA; break; case Key.Right : this.cameraTheta -= CAMERA_DELTA_THETA; break; case Key.Add : case Key.OemPlus : this.cameraR -= CAMERA_DELTA_R; if(this.cameraR < CAMERA_DELTA_R) { this.cameraR = CAMERA_DELTA_R; } break; case Key.Subtract : case Key.OemMinus : this.cameraR += CAMERA_DELTA_R; break; } SetCameraPosition(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 카메라 위치 설정하기 - SetCameraPosition() /// <summary> /// 카메라 위치 설정하기 /// </summary> private void SetCameraPosition() { double y = this.cameraR * Math.Sin(this.cameraPhi); double hyp = this.cameraR * Math.Cos(this.cameraPhi); double x = hyp * Math.Cos(this.cameraTheta); double z = hyp * Math.Sin(this.cameraTheta); this.camera.Position = new Point3D(x, y, z); this.camera.LookDirection = new Vector3D(-x, -y, -z); this.camera.UpDirection = new Vector3D(0, 1, 0); } #endregion #region 조명 정의하기 - DefineLight() /// <summary> /// 조명 정의하기 /// </summary> private void DefineLight() { AmbientLight ambientLight = new AmbientLight(Colors.Gray); DirectionalLight directionalLight = new DirectionalLight(Colors.Gray, new Vector3D(-1.0, -3.0, -2.0)); this.model3Dgroup.Children.Add(ambientLight); this.model3Dgroup.Children.Add(directionalLight); } #endregion #region Y 좌표 구하기 - GetY(x, z) /// <summary> /// Y 좌표 구하기 /// </summary> /// <param name="x">X 좌표</param> /// <param name="z">Z 좌표</param> /// <returns>Y 좌표</returns> private double GetY(double x, double z) { const double TWO_PI = 2 * 3.14159265; double r2 = x * x + z * z; double r = Math.Sqrt(r2); double theta = Math.Atan2(z, x); return Math.Exp(-r2) * Math.Sin(TWO_PI * r) * Math.Cos(3 * theta); } #endregion #region 포인트 추가하기 - AddPoint(point3DCollection, pointCollection, point3D) /// <summary> /// 포인트 추가하기 /// </summary> /// <param name="point3DCollection">3차원 포인트 컬렉션</param> /// <param name="pointCollection">포인트 컬렉션</param> /// <param name="point3D">3차원 포인트</param> /// <returns>3차원 포인트 인덱스</returns> private int AddPoint(Point3DCollection point3DCollection, PointCollection pointCollection, Point3D point3D) { if(this.point3DDictionary.ContainsKey(point3D)) { return this.point3DDictionary[point3D]; } point3DCollection.Add(point3D); this.point3DDictionary.Add(point3D, point3DCollection.Count - 1); pointCollection.Add ( new Point ( (point3D.X - X_MINIMUM) * TEXTURE_X_SCALE, (point3D.Z - Z_MINIMUM) * TEXTURE_Z_SCALE ) ); return point3DCollection.Count - 1; } #endregion #region 삼각형 추가하기 - AddTriangle(meshGeometry3D, point3D1, point3D2, point3D3) /// <summary> /// 삼각형 추가하기 /// </summary> /// <param name="meshGeometry3D">메쉬 기하 3D</param> /// <param name="point3D1">3차원 포인트 1</param> /// <param name="point3D2">3차원 포인트 2</param> /// <param name="point3D3">3차원 포인트 3</param> private void AddTriangle(MeshGeometry3D meshGeometry3D, Point3D point3D1, Point3D point3D2, Point3D point3D3) { int index1 = AddPoint(meshGeometry3D.Positions, meshGeometry3D.TextureCoordinates, point3D1); int index2 = AddPoint(meshGeometry3D.Positions, meshGeometry3D.TextureCoordinates, point3D2); int index3 = AddPoint(meshGeometry3D.Positions, meshGeometry3D.TextureCoordinates, point3D3); meshGeometry3D.TriangleIndices.Add(index1); meshGeometry3D.TriangleIndices.Add(index2); meshGeometry3D.TriangleIndices.Add(index3); } #endregion #region 모델 정의하기 - DefineModel(model3DGroup) /// <summary> /// 모델 정의하기 /// </summary> /// <param name="model3DGroup">모델 3D 그룹</param> private void DefineModel(Model3DGroup model3DGroup) { MeshGeometry3D meshGeometry3D = new MeshGeometry3D(); for(double x = X_MINIMUM; x <= X_MAXIMUM - DELTA_X; x += DELTA_X) { for(double z = Z_MINIMUM; z <= Z_MAXIMUM - DELTA_Z; z += DELTA_X) { Point3D p00 = new Point3D(x , GetY(x , z ), z ); Point3D p10 = new Point3D(x + DELTA_X, GetY(x + DELTA_X, z ), z ); Point3D p01 = new Point3D(x , GetY(x , z + DELTA_Z), z + DELTA_Z); Point3D p11 = new Point3D(x + DELTA_X, GetY(x + DELTA_X, z + DELTA_Z), z + DELTA_Z); AddTriangle(meshGeometry3D, p00, p01, p11); AddTriangle(meshGeometry3D, p00, p11, p10); } } ImageBrush imageBrush = new ImageBrush(); imageBrush.ImageSource = new BitmapImage(new Uri("Grid.png", UriKind.Relative)); DiffuseMaterial diffuseMaterial = new DiffuseMaterial(imageBrush); GeometryModel3D geometryModel3D = new GeometryModel3D(meshGeometry3D, diffuseMaterial); geometryModel3D.BackMaterial = diffuseMaterial; model3DGroup.Children.Add(geometryModel3D); } #endregion } } |