[GDAL]在三维场景中显示DEM
粗糙实现了个版本
存储波段的基本信息和数据:
namespace RGeos.Terrain
{
//存储波段的基本信息和数据
public class RasterBandData
{
public double[] data;
public int Columns;
public int Rows;
public double NoDataValue;
public double MaxValue;
public double MinValue;
}
}
RasterBandData
显示用的渲染对象:
using System;
using RGeos.SlimScene.Core;
using SlimDX.Direct3D9;
using System.Drawing;
using SlimDX;
using CustomVertex;
using System.IO; namespace RGeos.Terrain
{
public class RTerrain : RenderableObject
{
Device device = null;
private Bitmap bitMap = null;
private RasterBandData DataDem = null;
//定义行列数目
private int Cols = , Rows = ;
//定义像素的大小
private float cellHeight = 10f, cellWidth = 10f;
//纹理
private Texture texture = null;
//材质
private Material material;
//顶点缓冲变量
private VertexBuffer vertexBuffer;
//索引缓冲变量
private IndexBuffer indexBuffer;
// 顶点变量
private CustomVertex.PositionTextured[] vertices;
//索引号变量
private int[] indices; public RTerrain(string name, RasterBandData dem, Bitmap bitmap)
: base(name)
{
DataDem = dem;
Cols = dem.Columns;
Rows = dem.Rows;
bitMap = bitmap;
} public override void Initialize(DrawArgs drawArgs)
{
try
{
device = drawArgs.Device;
LoadTexturesAndMaterials();
VertexDeclaration();
IndicesDeclaration();//定义索引缓冲
isInitialized = true;
}
catch (Exception ex)
{
isInitialized = false;
throw ex;
} }
//导入贴图和材质
private void LoadTexturesAndMaterials()
{
material = new Material();
material.Diffuse = Color.White;
material.Specular = Color.LightGray;
material.Power = 15.0F;
device.Material = material;
System.IO.MemoryStream memory = new System.IO.MemoryStream();
bitMap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Seek(, SeekOrigin.Begin);
texture = Texture.FromStream(device, memory);
} public override void Update(DrawArgs drawArgs)
{
if (!isInitialized && isOn)
Initialize(drawArgs);
}
//定义顶点
private void VertexDeclaration()
{
vertexBuffer = new VertexBuffer(device, Cols * Rows * CustomVertex.PositionTextured.SizeBytes, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
DataStream vs = vertexBuffer.Lock(, Cols * Rows * CustomVertex.PositionTextured.SizeBytes, LockFlags.None);
vertices = new CustomVertex.PositionTextured[Cols * Rows];//定义顶点 for (int i = ; i < Rows; i++)
{
for (int j = ; j < Cols; j++)
{
//Color color = bitMap.GetPixel((int)(j * cellWidth), (int)(i * cellHeight));
float height = (float)DataDem.data[i * Cols + j];
if (height == DataDem.NoDataValue)
{
height = ;
}
vertices[j + i * Cols].Position = new Vector3(j * cellWidth, height, -i * cellHeight);
vertices[j + i * Cols].Tu = (float)j / (Cols - );
vertices[j + i * Cols].Tv = (float)i / (Rows - );
}
}
vs.WriteRange(vertices);
vertexBuffer.Unlock();
vs.Dispose();
}
//定义索引
private void IndicesDeclaration()
{
indexBuffer = new IndexBuffer(device, * * (Cols - ) * (Rows - ), Usage.WriteOnly, Pool.Default, true);
DataStream ds = indexBuffer.Lock(, * * (Cols - ) * (Rows - ), LockFlags.None);
indices = new int[ * (Cols - ) * (Rows - )];
int index = ;
for (int i = ; i < Rows - ; i++)
{
for (int j = ; j < Cols - ; j++)
{
indices[index] = j + i * (Cols);
indices[index + ] = j + (i + ) * Cols;
indices[index + ] = j + i * Cols + ;
indices[index + ] = j + i * Cols + ;
indices[index + ] = j + (i + ) * Cols;
indices[index + ] = j + (i + ) * Cols + ;
index += ;
}
}
ds.WriteRange(indices);
indexBuffer.Unlock();
ds.Dispose();
} public override void Render(DrawArgs drawArgs)
{
if (!isInitialized || !isOn)
return;
VertexFormat curFormat = drawArgs.Device.VertexFormat;
int curZEnable = drawArgs.Device.GetRenderState(RenderState.ZEnable);
int curLighting = drawArgs.Device.GetRenderState(RenderState.Lighting);
int curCull = drawArgs.Device.GetRenderState(RenderState.CullMode);
int curAlphaBlendEnable = drawArgs.Device.GetRenderState(RenderState.AlphaBlendEnable);
int curDepthBias = drawArgs.Device.GetRenderState(RenderState.DepthBias);
int curColorOperation = drawArgs.Device.GetTextureStageState(, TextureStage.ColorOperation);
try
{
drawArgs.Device.SetRenderState(RenderState.Lighting, false);
drawArgs.Device.VertexFormat = PositionTextured.Format;
drawArgs.Device.SetRenderState(RenderState.ZEnable, true);
drawArgs.Device.SetRenderState(RenderState.CullMode, Cull.None);
drawArgs.Device.SetTextureStageState(, TextureStage.ColorOperation, TextureOperation.Modulate);
drawArgs.Device.SetTextureStageState(, TextureStage.ColorArg1, TextureArgument.Texture);
drawArgs.Device.SetTextureStageState(, TextureStage.ColorArg2, TextureArgument.Diffuse);
drawArgs.Device.SetTextureStageState(, TextureStage.AlphaOperation, TextureOperation.Disable);
device.SetTexture(, texture);//设置贴图 device.SetStreamSource(, vertexBuffer, , PositionTextured.SizeBytes);
device.Indices = indexBuffer;
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, , , (Cols * Rows), , indices.Length / );
}
catch (Exception ex)
{
}
finally
{
drawArgs.Device.VertexFormat = curFormat;
drawArgs.Device.SetRenderState(RenderState.ZEnable, curZEnable);
drawArgs.Device.SetRenderState(RenderState.Lighting, curLighting);
drawArgs.Device.SetRenderState(RenderState.CullMode, curCull);
drawArgs.Device.SetRenderState(RenderState.AlphaBlendEnable, curAlphaBlendEnable);
drawArgs.Device.SetRenderState(RenderState.DepthBias, curDepthBias);
drawArgs.Device.SetTextureStageState(, TextureStage.ColorOperation, curColorOperation);
}
} public override void Dispose()
{
if (vertexBuffer != null)
{
vertexBuffer.Dispose();
vertexBuffer = null;
texture = null;
vertices = null;
}
if (indexBuffer != null)
{
indexBuffer.Dispose();
indexBuffer = null;
indices = null;
}
} public override bool PerformSelectionAction(DrawArgs drawArgs)
{
throw new NotImplementedException();
}
}
}
RTerrain
调用方法:
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "";
dlg.Filter = "Img(*.img)|*.img";
if (dlg.ShowDialog() == DialogResult.OK)
{
string file = dlg.FileName;
string NameOf = System.IO.Path.GetFileNameWithoutExtension(file);
DemHelper dem = new DemHelper();
dem.Start();
dem.Read(file);
RasterBandData bandata = dem.ReadDate(, );
Bitmap bitmap = dem.MakeGrayScale(, );
Vector3 position = new Vector3(-100f, 0f, 100f);
//SimpleRasterShow simRaster = new SimpleRasterShow(NameOf, position, bitmap.Width, bitmap.Height);
//simRaster.IsOn = true;
//simRaster.RenderPriority = RenderPriority.Custom;
//simRaster.bitmap = bitmap;
//mSceneControl.CurrentWorld.RenderableObjects.ChildObjects.Add(simRaster);
RTerrain terrain = new RTerrain(NameOf, bandata, bitmap);
terrain.IsOn = true;
mSceneControl.CurrentWorld.RenderableObjects.ChildObjects.Add(terrain);
}
[GDAL]在三维场景中显示DEM的更多相关文章
- 三维场景中使用BillBoard技术
三维场景中对于渲染效果不是很精致的物体可以使用BillBoard技术实现,使用该技术需要将物体实时朝向摄像机,即计算billboard的旋转矩阵M. 首先根据摄像机位置cameraPos和billBo ...
- MATLAB在三维坐标中显示图片 并 使得图片部分透明
要画一个光路图,本来可以用proe,但是鼠标不好用,有些操作也忘了,用MATLAB画了个.下面是用到的图片. 但是三维坐标中显示彩色图片的目标没有搞定,做了个灰度图,然后用仿射程序将彩色图片贴到了二维 ...
- ArcMap应用——三维场景中井盖的属性配置
在精细三维场景中,有地面(包括道路面.马路牙子).有部件数据(包括井盖).我们会发现有马路牙子的地方比道路面要高出一部分,比如0.1米,但是雨水井盖却有些在路面上.有些在道路以外.就是说在道路面上的井 ...
- 在三维场景中加载shp(skyline)
在场景中添加shp图层有两个方法: (1)直接调用Command命令,SGWorld.Command.Execute(1013,5);这样的话,和在场景中的工程树中右键添加特征图层的过程是一样的.有个 ...
- 三维计算机视觉 — 中层次视觉 — Point Pair Feature
机器人视觉中有一项重要人物就是从场景中提取物体的位置,姿态.图像处理算法借助Deep Learning 的东风已经在图像的物体标记领域耍的飞起了.而从三维场景中提取物体还有待研究.目前已有的思路是先提 ...
- [Unity3D]Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果
大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei. 在<仙剑奇侠传>.<古剑奇谭>等游戏中,常常须要玩家在一个3D场景中 ...
- 从WW中剥离一个三维场景框架
从WW中剥离一个三维场景框架,初步实现的一个.可以绘制一个三角形,但是不能够控制摄像机,没有增加鼠标事件.没有投影,世界变幻之类的东西.以后会不断学习逐步增加进来. 下载地址 下载V1.0.0.2
- 3D游戏引擎中常见的三维场景管理方法
对于一个有很多物体的3D场景来说,渲染这个场景最简单的方式就是用一个List将这些物体进行存储,并送入GPU进行渲染.当然,这种做法在效率上来说是相当低下的,因为真正需要渲染的物体应该是视椎体内的物体 ...
- 三维场景如何嵌入到PPT中展示?
今天要跟大家一起交流的大体内容如标题所示,日常生活中,ppt已经成为人们工作学习生活中不可或缺的工具之一,那么三维场景是如何在ppt中加载展示的呢?请大家慢慢往下看. 1.创建命令按钮和web bro ...
随机推荐
- linux下如何关闭防火墙、查看当前的状态、开放端口
从配置菜单关闭防火墙是不起作用的,索性在安装的时候就不要装防火墙查看防火墙状态:/etc/init.d/iptables status暂时关闭防火墙:/etc/init.d/iptables stop ...
- chrome浏览器开发者工具使用教程[转]
转自:http://www.cr173.com/html/16930_1.html 更多资源:https://developers.google.com/chrome-developer-tools/ ...
- OpenCV学习:Mat结构中的数据共享机制
使用Mat类,内存管理变得简单,不再像使用IplImage那样需要自己申请和释放内存. Mat是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩 ...
- 超全面的JavaWeb笔记day19<Service>
今日内容 l Service事务 l 客户关系管理系统 Service事务 在Service中使用ThreadLocal来完成事务,为将来学习Spring事务打基础! 1 DAO中的事务 在DAO中处 ...
- 什么是LTE?
LTE是英文Long Term Evolution的缩写.LTE也被通俗的称为3.9G,具有100Mbps的数据下载能力,被视作从3G向4G演进的主流技术.它改进并增强了3G的空中接入技术,采用OFD ...
- Linux的网卡相关
检测linux下网卡是否正常 1.dmesg | grep eth 如果出现以下 eth0: link up 说明是网卡正常的 eth0: registered as PCnet/PCI II 79 ...
- Android使用百度定位API时获取的地址信息为null
option.setAddrType("all"); //加上这个配置后才可以取到详细地址信息
- C++11新特性之八——函数对象function
详细请看<C++ Primer plus>(第六版中文版) http://www.cnblogs.com/lvpengms/archive/2011/02/21/1960078.html ...
- AndroidのUI布局之layout weight
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Linux命令行常用光标移动快捷键
Linux 命令行快捷键 涉及在Linux命令行下进行快速移动光标.命令编辑.编辑后执行历史命令.Bang(!)命令.控制命令等.让basher更有效率. 常用 ctrl+左右键:在单词之间跳转 ct ...