[GDAL]在三维场景中显示DEM

粗糙实现了个版本

存储波段的基本信息和数据:

 1 namespace RGeos.Terrain  2 {  3    //存储波段的基本信息和数据

 4     public class RasterBandData  5  {  6         public double[] data;  7         public int Columns;  8         public int Rows;  9         public double NoDataValue; 10         public double MaxValue; 11         public double MinValue; 12  } 13 }
RasterBandData

显示用的渲染对象:

 1 using System;  2 using RGeos.SlimScene.Core;  3 using SlimDX.Direct3D9;  4 using System.Drawing;  5 using SlimDX;  6 using CustomVertex;  7 using System.IO;  8 

 9 namespace RGeos.Terrain  10 {  11     public class RTerrain : RenderableObject  12  {  13         Device device = null;  14         private Bitmap bitMap = null;  15         private RasterBandData DataDem = null;  16         //定义行列数目

 17         private int Cols = 5, Rows = 4;  18         //定义像素的大小 

 19         private float cellHeight = 10f, cellWidth = 10f;  20         //纹理

 21         private Texture texture = null;  22         //材质

 23         private Material material;  24         //顶点缓冲变量

 25         private VertexBuffer vertexBuffer;  26         //索引缓冲变量

 27         private IndexBuffer indexBuffer;  28         // 顶点变量

 29         private CustomVertex.PositionTextured[] vertices;  30         //索引号变量

 31         private int[] indices;  32 

 33         public RTerrain(string name, RasterBandData dem, Bitmap bitmap)  34             : base(name)  35  {  36             DataDem = dem;  37             Cols = dem.Columns;  38             Rows = dem.Rows;  39             bitMap = bitmap;  40  }  41 

 42         public override void Initialize(DrawArgs drawArgs)  43  {  44             try

 45  {  46                 device = drawArgs.Device;  47  LoadTexturesAndMaterials();  48  VertexDeclaration();  49                 IndicesDeclaration();//定义索引缓冲 

 50                 isInitialized = true;  51  }  52             catch (Exception ex)  53  {  54                 isInitialized = false;  55                 throw ex;  56  }  57 

 58  }  59         //导入贴图和材质

 60         private void LoadTexturesAndMaterials()  61  {  62             material = new Material();  63             material.Diffuse = Color.White;  64             material.Specular = Color.LightGray;  65             material.Power = 15.0F;  66             device.Material = material;  67             System.IO.MemoryStream memory = new System.IO.MemoryStream();  68  bitMap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);  69             memory.Seek(0, SeekOrigin.Begin);  70             texture = Texture.FromStream(device, memory);  71  }  72 

 73         public override void Update(DrawArgs drawArgs)  74  {  75             if (!isInitialized && isOn)  76  Initialize(drawArgs);  77  }  78         //定义顶点

 79         private void VertexDeclaration()  80  {  81             vertexBuffer = new VertexBuffer(device, Cols * Rows * CustomVertex.PositionTextured.SizeBytes, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);  82             DataStream vs = vertexBuffer.Lock(0, Cols * Rows * CustomVertex.PositionTextured.SizeBytes, LockFlags.None);  83             vertices = new CustomVertex.PositionTextured[Cols * Rows];//定义顶点 

 84 

 85             for (int i = 0; i < Rows; i++)  86  {  87                 for (int j = 0; j < Cols; j++)  88  {  89                     //Color color = bitMap.GetPixel((int)(j * cellWidth), (int)(i * cellHeight));

 90                     float height = (float)DataDem.data[i * Cols + j];  91                     if (height == DataDem.NoDataValue)  92  {  93                         height = 0;  94  }  95                     vertices[j + i * Cols].Position = new Vector3(j * cellWidth, height, -i * cellHeight);  96                     vertices[j + i * Cols].Tu = (float)j / (Cols - 1);  97                     vertices[j + i * Cols].Tv = (float)i / (Rows - 1);  98  }  99  } 100  vs.WriteRange(vertices); 101  vertexBuffer.Unlock(); 102  vs.Dispose(); 103  } 104         //定义索引

105         private void IndicesDeclaration() 106  { 107             indexBuffer = new IndexBuffer(device, 32 * 6 * (Cols - 1) * (Rows - 1), Usage.WriteOnly, Pool.Default, true); 108             DataStream ds = indexBuffer.Lock(0, 32 * 6 * (Cols - 1) * (Rows - 1), LockFlags.None); 109             indices = new int[6 * (Cols - 1) * (Rows - 1)]; 110             int index = 0; 111             for (int i = 0; i < Rows - 1; i++) 112  { 113                 for (int j = 0; j < Cols - 1; j++) 114  { 115                     indices[index] = j + i * (Cols); 116                     indices[index + 1] = j + (i + 1) * Cols; 117                     indices[index + 2] = j + i * Cols + 1; 118                     indices[index + 3] = j + i * Cols + 1; 119                     indices[index + 4] = j + (i + 1) * Cols; 120                     indices[index + 5] = j + (i + 1) * Cols + 1; 121                     index += 6; 122  } 123  } 124  ds.WriteRange(indices); 125  indexBuffer.Unlock(); 126  ds.Dispose(); 127  } 128 

129         public override void Render(DrawArgs drawArgs) 130  { 131             if (!isInitialized || !isOn) 132                 return; 133             VertexFormat curFormat = drawArgs.Device.VertexFormat; 134             int curZEnable = drawArgs.Device.GetRenderState(RenderState.ZEnable); 135             int curLighting = drawArgs.Device.GetRenderState(RenderState.Lighting); 136             int curCull = drawArgs.Device.GetRenderState(RenderState.CullMode); 137             int curAlphaBlendEnable = drawArgs.Device.GetRenderState(RenderState.AlphaBlendEnable); 138             int curDepthBias = drawArgs.Device.GetRenderState(RenderState.DepthBias); 139             int curColorOperation = drawArgs.Device.GetTextureStageState(0, TextureStage.ColorOperation); 140             try

141  { 142                 drawArgs.Device.SetRenderState(RenderState.Lighting, false); 143              drawArgs.Device.VertexFormat = PositionTextured.Format; 144                 drawArgs.Device.SetRenderState(RenderState.ZEnable, true); 145  drawArgs.Device.SetRenderState(RenderState.CullMode, Cull.None); 146                 drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Modulate); 147                 drawArgs.Device.SetTextureStageState(0, TextureStage.ColorArg1, TextureArgument.Texture); 148                 drawArgs.Device.SetTextureStageState(0, TextureStage.ColorArg2, TextureArgument.Diffuse); 149                 drawArgs.Device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Disable); 150                 device.SetTexture(0, texture);//设置贴图 

151 

152                 device.SetStreamSource(0, vertexBuffer, 0, PositionTextured.SizeBytes); 153                 device.Indices = indexBuffer; 154                 device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, (Cols * Rows), 0, indices.Length / 3); 155  } 156             catch (Exception ex) 157  { 158  } 159             finally

160  { 161                 drawArgs.Device.VertexFormat = curFormat; 162  drawArgs.Device.SetRenderState(RenderState.ZEnable, curZEnable); 163  drawArgs.Device.SetRenderState(RenderState.Lighting, curLighting); 164  drawArgs.Device.SetRenderState(RenderState.CullMode, curCull); 165  drawArgs.Device.SetRenderState(RenderState.AlphaBlendEnable, curAlphaBlendEnable); 166  drawArgs.Device.SetRenderState(RenderState.DepthBias, curDepthBias); 167                 drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, curColorOperation); 168  } 169  } 170 

171         public override void Dispose() 172  { 173             if (vertexBuffer != null) 174  { 175  vertexBuffer.Dispose(); 176                 vertexBuffer = null; 177                 texture = null; 178                 vertices = null; 179  } 180             if (indexBuffer != null) 181  { 182  indexBuffer.Dispose(); 183                 indexBuffer = null; 184                 indices = null; 185  } 186  } 187 

188         public override bool PerformSelectionAction(DrawArgs drawArgs) 189  { 190             throw new NotImplementedException(); 191  } 192  } 193 }
RTerrain

调用方法:

 1   OpenFileDialog dlg = new OpenFileDialog();

 2             dlg.Title = "";

 3             dlg.Filter = "Img(*.img)|*.img";

 4             if (dlg.ShowDialog() == DialogResult.OK)

 5             {

 6                 string file = dlg.FileName;

 7                 string NameOf = System.IO.Path.GetFileNameWithoutExtension(file);

 8                 DemHelper dem = new DemHelper();

 9                 dem.Start();

10                 dem.Read(file);

11                 RasterBandData bandata = dem.ReadDate(50, 40);

12                 Bitmap bitmap = dem.MakeGrayScale(50, 40);

13                 Vector3 position = new Vector3(-100f, 0f, 100f);

14                 //SimpleRasterShow simRaster = new SimpleRasterShow(NameOf, position, bitmap.Width, bitmap.Height);

15                 //simRaster.IsOn = true;

16                 //simRaster.RenderPriority = RenderPriority.Custom;

17                 //simRaster.bitmap = bitmap;

18                 //mSceneControl.CurrentWorld.RenderableObjects.ChildObjects.Add(simRaster);

19                 RTerrain terrain = new RTerrain(NameOf, bandata, bitmap);

20                 terrain.IsOn = true;

21                 mSceneControl.CurrentWorld.RenderableObjects.ChildObjects.Add(terrain);

22             }
View Code

 

 

 

你可能感兴趣的:(em)