[3D]绘制线

数据实体:

 1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Drawing;  6 using SlimDX;  7 using RGeos.SlimScene.Core;  8 

 9 namespace RGeos.Framework.OTL.Geometries 10 { 11 

12     /// <summary>

13     /// LineString. 14     /// </summary>

15     public class LineString 16  { 17         public Point3d[] Coordinates = null; 18         public Color Color = Color.Yellow; 19         public float LineWidth = 1.0f; 20         public bool Visible = true; 21         public bool Remove = false; 22         public RenderableObject ParentRenderable = null; 23 

24         public BoundingBox GetBoundingBox() 25  { 26             if (Coordinates == null || Coordinates.Length == 0) 27                 return new BoundingBox(); 28 

29             double minX = Coordinates[0].X; 30             double maxX = Coordinates[0].X; 31             double minY = Coordinates[0].Y; 32             double maxY = Coordinates[0].Y; 33             double minZ = Coordinates[0].Z; 34             double maxZ = Coordinates[0].Z; 35 

36             for (int i = 1; i < Coordinates.Length; i++) 37  { 38                 if (Coordinates[i].X < minX) 39                     minX = Coordinates[i].X; 40                 if (Coordinates[i].X > maxX) 41                     maxX = Coordinates[i].X; 42 

43                 if (Coordinates[i].Y < minY) 44                     minY = Coordinates[i].Y; 45                 if (Coordinates[i].Y > maxY) 46                     maxY = Coordinates[i].Y; 47 

48                 if (Coordinates[i].Z < minZ) 49                     minZ = Coordinates[i].Z; 50                 if (Coordinates[i].Z > maxZ) 51                     maxZ = Coordinates[i].Z; 52  } 53 

54             return new BoundingBox(new Vector3( 55                 (float)maxY, (float)minY, (float)minX), new Vector3((float)maxX, (float)minZ, (float)maxZ)); 56  } 57  } 58 

59 

60 }
LineString

渲染对象

 1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using RGeos.SlimScene.Core;  6 using SlimDX.Direct3D9;  7 using SlimDX;  8 

 9 namespace RGeos.Framework.OTL.Geometries  10 {  11     public class RenderableLineString : RenderableObject  12  {  13         #region Static Members

 14         #endregion

 15 

 16         #region Private Members

 17         double m_distanceAboveSurface = 0;  18         Point3d[] m_points = null;  19         CustomVertex.PositionColoredTextured[] m_wallVertices = null;  20 

 21         CustomVertex.PositionColored[] m_topVertices = null;  22         CustomVertex.PositionColored[] m_bottomVertices = null;  23         CustomVertex.PositionColored[] m_sideVertices = null;  24 

 25         System.Drawing.Color m_lineColor = System.Drawing.Color.Black;  26         float m_verticalExaggeration = 1;  27         double m_minimumDisplayAltitude = 0;  28         double m_maximumDisplayAltitude = double.MaxValue;  29         string m_imageUri = null;  30         Texture m_texture = null;  31         System.Drawing.Color m_polygonColor = System.Drawing.Color.Black;  32         bool m_outline = true;  33         float m_lineWidth = 1.0f;  34         bool m_extrude = false;  35         AltitudeMode m_altitudeMode = AltitudeMode.Absolute;  36         long m_numPoints = 0;  37         #endregion

 38 

 39         /// <summary>

 40         /// Boolean indicating whether or not the line needs rebuilding.  41         /// </summary>

 42         public bool NeedsUpdate = true;  43 

 44         public bool Extrude  45  {  46             get { return m_extrude; }  47             set { m_extrude = value; }  48  }  49 

 50         public AltitudeMode AltitudeMode  51  {  52             get { return m_altitudeMode; }  53             set { m_altitudeMode = value; }  54  }  55 

 56         public System.Drawing.Color LineColor  57  {  58             get { return m_lineColor; }  59             set

 60  {  61                 m_lineColor = value;  62                 NeedsUpdate = true;  63  }  64  }  65 

 66         public float LineWidth  67  {  68             get { return m_lineWidth; }  69             set

 70  {  71                 m_lineWidth = value;  72                 NeedsUpdate = true;  73  }  74  }  75 

 76         public double DistanceAboveSurface  77  {  78             get { return m_distanceAboveSurface; }  79             set

 80  {  81                 m_distanceAboveSurface = value;  82                 if (m_topVertices != null)  83  {  84                     NeedsUpdate = true;  85                     //UpdateVertices();

 86  }  87  }  88  }  89 

 90         public System.Drawing.Color PolygonColor  91  {  92             get { return m_polygonColor; }  93             set

 94  {  95                 m_polygonColor = value;  96                 if (m_topVertices != null)  97  {  98                     NeedsUpdate = true;  99                     //UpdateVertices();

100  } 101  } 102  } 103 

104         public bool Outline 105  { 106             get { return m_outline; } 107             set

108  { 109                 m_outline = value; 110                 if (m_topVertices != null) 111  { 112                     NeedsUpdate = true; 113                     //UpdateVertices();

114  } 115  } 116  } 117 

118         public Point3d[] Points 119  { 120             get

121  { 122                 // if the array size is correct just return it

123                 if (m_numPoints == m_points.LongLength) 124                     return m_points; 125 

126                 // return an array the correct size.

127                 Point3d[] points = new Point3d[m_numPoints]; 128                 for (int i = 0; i < m_numPoints; i++) 129  { 130                     points[i] = m_points[i]; 131  } 132                 return points; 133  } 134             set

135  { 136                 m_points = value; 137                 m_numPoints = m_points.LongLength; 138                 NeedsUpdate = true; 139  } 140  } 141 

142         public long NumPoints 143  { 144             get { return m_numPoints; } 145  } 146 

147         public double MinimumDisplayAltitude 148  { 149             get { return m_minimumDisplayAltitude; } 150             set { m_minimumDisplayAltitude = value; } 151  } 152 

153         public double MaximumDisplayAltitude 154  { 155             get { return m_maximumDisplayAltitude; } 156             set { m_maximumDisplayAltitude = value; } 157  } 158 

159         public override byte Opacity 160  { 161             get

162  { 163                 return base.Opacity; 164  } 165             set

166  { 167                 base.Opacity = value; 168                 if (m_topVertices != null) 169  { 170  UpdateVertices(); 171  } 172  } 173  } 174 

175         public RenderableLineString(string name, World parentWorld, Point3d[] points, System.Drawing.Color lineColor) 176             : base(name, parentWorld) 177  { 178             m_points = points; 179             m_lineColor = lineColor; 180             m_polygonColor = lineColor; 181             m_numPoints = m_points.LongLength; 182 

183             // RenderPriority = WorldWind.Renderable.RenderPriority.LinePaths;

184  } 185 

186         public RenderableLineString(string name, World parentWorld, Point3d[] points, string imageUri) 187             : base(name, parentWorld) 188  { 189             m_points = points; 190             m_imageUri = imageUri; 191             m_numPoints = m_points.LongLength; 192 

193             // RenderPriority = WorldWind.Renderable.RenderPriority.LinePaths;

194  } 195 

196         public override void Dispose() 197  { 198             if (m_texture != null && !m_texture.Disposed) 199  { 200  m_texture.Dispose(); 201                 m_texture = null; 202  } 203 

204             if (m_lineString != null) 205  { 206                 m_lineString.Remove = true; 207                 m_lineString = null; 208  } 209             NeedsUpdate = true; 210  } 211 

212         public override void Initialize(DrawArgs drawArgs) 213  { 214             if (m_points == null) 215  { 216                 isInitialized = true; 217                 return; 218  } 219 

220             if (m_imageUri != null) 221  { 222                 //load image 223                 //if (m_imageUri.ToLower().StartsWith("http://")) 224                 //{ 225                 // string savePath = string.Format("{0}\\image", ConfigurationLoader.GetRenderablePathString(this)); 226                 // System.IO.FileInfo file = new System.IO.FileInfo(savePath); 227                 // if (!file.Exists) 228                 // { 229                 // WorldWind.Net.WebDownload download = new WorldWind.Net.WebDownload(m_imageUri); 230 

231                 // if (!file.Directory.Exists) 232                 // file.Directory.Create(); 233 

234                 // download.DownloadFile(file.FullName, WorldWind.Net.DownloadType.Unspecified); 235                 // } 236 

237                 // m_texture = ImageHelper.LoadTexture(file.FullName); 238                 //} 239                 //else 240                 //{ 241                 // m_texture = ImageHelper.LoadTexture(m_imageUri); 242                 //}

243  } 244 

245  UpdateVertices(); 246 

247             isInitialized = true; 248  } 249 

250         /// <summary>

251         /// Adds a point to the line at the end of the line. 252         /// </summary>

253         /// <param name="point">The Point3d object to add.</param>

254         public void AddPoint(Point3d point) 255  { 256             // if the array is too small grow it.

257             if (m_numPoints >= m_points.LongLength) 258  { 259                 long growSize = m_points.LongLength / 2; 260                 if (growSize < 10) growSize = 10; 261 

262                 Point3d[] points = new Point3d[m_points.LongLength + growSize]; 263 

264                 for (int i = 0; i < m_numPoints; i++) 265  { 266                     points[i] = m_points[i]; 267  } 268                 m_points = points; 269  } 270             m_points[m_numPoints] = point; 271             m_numPoints++; 272             NeedsUpdate = true; 273  } 274 

275         private void UpdateVertices() 276  { 277             try

278  { 279                 // m_verticalExaggeration = World.Settings.VerticalExaggeration;

280 

281  UpdateTexturedVertices(); 282 

283                 if (m_lineString != null && m_outline && m_wallVertices != null && m_wallVertices.Length > m_topVertices.Length) 284  { 285  UpdateOutlineVertices(); 286  } 287 

288                 NeedsUpdate = false; 289  } 290             catch (Exception ex) 291  { 292  Utility.Log.Write(ex); 293  } 294  } 295 

296         private void UpdateOutlineVertices() 297  { 298             m_bottomVertices = new CustomVertex.PositionColored[m_numPoints]; 299             m_sideVertices = new CustomVertex.PositionColored[m_numPoints * 2]; 300 

301             for (int i = 0; i < m_numPoints; i++) 302  { 303                 m_sideVertices[2 * i] = m_topVertices[i]; 304 

305                 Vector3 xyzVertex = new Vector3( 306                     m_wallVertices[2 * i + 1].Position.X, 307                     m_wallVertices[2 * i + 1].Position.Y, 308                     m_wallVertices[2 * i + 1].Position.Z); 309 

310                 m_bottomVertices[i].Position.X = xyzVertex.X; 311                 m_bottomVertices[i].Position.Y = xyzVertex.Y; 312                 m_bottomVertices[i].Position.Z = xyzVertex.Z; 313                 m_bottomVertices[i].Color = m_lineColor.ToArgb(); 314 

315                 m_sideVertices[2 * i + 1] = m_bottomVertices[i]; 316  } 317  } 318 

319         LineString m_lineString = null; 320         private void UpdateTexturedVertices() 321  { 322             if (m_altitudeMode == AltitudeMode.ClampedToGround) 323  { 324                 if (m_lineString != null) 325  { 326                     m_lineString.Remove = true; 327                     m_lineString = null; 328  } 329 

330                 m_lineString = new LineString(); 331                 m_lineString.Coordinates = Points; 332                 m_lineString.Color = LineColor; 333                 m_lineString.LineWidth = LineWidth; 334                 m_lineString.ParentRenderable = this; 335                 // this.World.ProjectedVectorRenderer.Add(m_lineString);

336 

337                 if (m_wallVertices != null) 338                     m_wallVertices = null; 339 

340                 return; 341  } 342 

343             if (m_extrude || m_altitudeMode == AltitudeMode.RelativeToGround) 344  { 345                 m_wallVertices = new CustomVertex.PositionColoredTextured[m_numPoints * 2]; 346  } 347 

348             float textureCoordIncrement = 1.0f / (float)(m_numPoints - 1); 349             // m_verticalExaggeration = World.Settings.VerticalExaggeration;

350             int vertexColor = m_polygonColor.ToArgb(); 351 

352             m_topVertices = new CustomVertex.PositionColored[m_numPoints]; 353 

354             for (int i = 0; i < m_numPoints; i++) 355  { 356                 double terrainHeight = 0; 357 

358 

359                 Vector3 xyzVertex = new Vector3((float)m_points[i].X, (float)m_points[i].Y, (float)m_points[i].Z); 360 

361                 m_topVertices[i].Position.X = xyzVertex.X; 362                 m_topVertices[i].Position.Y = xyzVertex.Y; 363                 m_topVertices[i].Position.Z = xyzVertex.Z; 364                 m_topVertices[i].Color = m_lineColor.ToArgb(); 365 

366                 if (m_extrude || m_altitudeMode == AltitudeMode.RelativeToGround) 367  { 368                     m_wallVertices[2 * i].Position.X = xyzVertex.X; 369                     m_wallVertices[2 * i].Position.Y = xyzVertex.Y; 370                     m_wallVertices[2 * i].Position.Z = xyzVertex.Z; 371                     m_wallVertices[2 * i].Color = vertexColor; 372                     m_wallVertices[2 * i].Tu = i * textureCoordIncrement; 373                     m_wallVertices[2 * i].Tv = 1.0f; 374 

375                     m_wallVertices[2 * i + 1].Position.X = xyzVertex.X; 376                     m_wallVertices[2 * i + 1].Position.Y = xyzVertex.Y; 377                     m_wallVertices[2 * i + 1].Position.Z = xyzVertex.Z; 378                     m_wallVertices[2 * i + 1].Color = vertexColor; 379                     m_wallVertices[2 * i + 1].Tu = i * textureCoordIncrement; 380                     m_wallVertices[2 * i + 1].Tv = 0.0f; 381  } 382  } 383  } 384 

385         public override bool PerformSelectionAction(DrawArgs drawArgs) 386  { 387             return false; 388  } 389 

390         public override void Update(DrawArgs drawArgs) 391  { 392             if (drawArgs.WorldCamera.Distance >= m_minimumDisplayAltitude && drawArgs.WorldCamera.Distance <= m_maximumDisplayAltitude) 393  { 394                 if (!isInitialized) 395  Initialize(drawArgs); 396 

397                 if (NeedsUpdate) 398  UpdateVertices(); 399  } 400 

401  } 402 

403         public override void Render(DrawArgs drawArgs) 404  { 405             if (!isInitialized || drawArgs.WorldCamera.Distance < m_minimumDisplayAltitude || drawArgs.WorldCamera.Distance > m_maximumDisplayAltitude) 406  { 407                 return; 408  } 409 

410             try

411  { 412                 if (m_lineString != null) 413                     return; 414 

415                 int currentCull = drawArgs.Device.GetRenderState(RenderState.CullMode); 416  drawArgs.Device.SetRenderState(RenderState.CullMode, Cull.None); 417 

418                 if (m_wallVertices != null) 419  { 420                     drawArgs.Device.SetRenderState(RenderState.ZEnable, true); 421 

422                     if (m_texture != null && !m_texture.Disposed) 423  { 424                         drawArgs.Device.SetTexture(0, m_texture); 425                         drawArgs.Device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Modulate); 426                         drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Add); 427                         drawArgs.Device.SetTextureStageState(0, TextureStage.AlphaArg1, TextureArgument.Texture); 428  } 429                     else

430  { 431                         drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Disable); 432  } 433 

434                     drawArgs.Device.VertexFormat = CustomVertex.PositionColoredTextured.Format; 435 

436                     drawArgs.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, m_wallVertices.Length - 2, m_wallVertices); 437 

438                     if (m_outline) 439  { 440 

441                         drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Disable); 442                         drawArgs.Device.VertexFormat = CustomVertex.PositionColored.Format; 443                         drawArgs.Device.DrawUserPrimitives(PrimitiveType.LineStrip, m_topVertices.Length - 1, m_topVertices); 444 

445                         if (m_bottomVertices != null) 446                             drawArgs.Device.DrawUserPrimitives(PrimitiveType.LineStrip, m_bottomVertices.Length - 1, m_bottomVertices); 447 

448                         if (m_sideVertices != null) 449                             drawArgs.Device.DrawUserPrimitives(PrimitiveType.LineList, m_sideVertices.Length / 2, m_sideVertices); 450 

451  } 452  } 453                 else

454  { 455                     drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Disable); 456                     drawArgs.Device.VertexFormat = CustomVertex.PositionColored.Format; 457                     drawArgs.Device.DrawUserPrimitives(PrimitiveType.LineStrip, m_topVertices.Length - 1, m_topVertices); 458  } 459 

460  drawArgs.Device.SetTransform(TransformState.World, drawArgs.WorldCamera.WorldMatrix); 461  drawArgs.Device.SetRenderState(RenderState.CullMode, currentCull); 462  } 463             catch//(Exception ex)

464  { 465                 //Utility.Log.Write(ex);

466  } 467  } 468  } 469 }
RenderableLineString

使用方法:

 private void tspPolyline_Click(object sender, EventArgs e) { Point3d[] pts=new Point3d[5]; Point3d pt1=new Point3d(0,0,0); Point3d pt2=new Point3d(0,10,8); Point3d pt3=new Point3d(0,20,5); Point3d pt4=new Point3d(0,30,4); Point3d pt5=new Point3d(0,40,2); pts[0]=pt1; pts[1]=pt2; pts[2]=pt3; pts[3]=pt4; pts[4]=pt5; RenderableLineString rend = new RenderableLineString("Hello", null, pts, Color.White); rend.IsOn = true; rend.RenderPriority = RenderPriority.Custom; mSceneControl.CurrentWorld.RenderableObjects.ChildObjects.Add(rend); }

 [3D]绘制线

你可能感兴趣的:(3D)