[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 ...
随机推荐
- 【Mysql】修改最大连接数
http://www.111cn.net/database/mysql/51934.htm
- Oracle 11g 的bug?: aix 上,expdp 11.2.0.1 导出,impdp 11.2.0.3 导入,Interval 分区的 【Interval】 分区属性成了【N】
如题: Oracle 11g 的bug?: aix 上,expdp 11.2.0.1 导出,impdp 11.2.0.3 导入,Interval 分区的 [Interval] 分区属性成了[N] 谨记 ...
- bat、cmd、dos窗口:后台调用,不显示黑色的控制台dos(命令行)窗口
建立一个windows的vbs脚本文件,内容类似如下:注意末尾的参数0 createobject("wscript.shell").run "VBoxheadless.e ...
- PVS 7.6 部署教程
PVS 7.6 部署教程 1 PVS介绍 Citrix Provisioning Services採用流技术通过网络将单一标准桌面镜像,包含操作系统和软件按需交付给物理虚拟桌面.一方面实现同型号机器单 ...
- day27<反射&JDK5新特性>
反射(类的加载概述和加载时机) 反射(类加载器的概述和分类) 反射(反射概述) 反射(Class.forName()读取配置文件举例) 反射(通过反射获取带参构造方法并使用) 反射(通过反射获取成员变 ...
- 7 -- Spring的基本用法 -- 3... Spring 的核心机制 : 依赖注入
7.3 Spring 的核心机制 : 依赖注入 Spring 框架的核心功能有两个. Spring容器作为超级大工厂,负责创建.管理所有的Java对象,这些Java对象被称为Bean. Spring容 ...
- Extjs学习笔记--(一vs增加extjs智能感知)
1,编写class.js var classList=[ "Ext.layout.container.Absolute", "Ext.layout.container.A ...
- 使用 XPath
XPath 简介: (1) 前面我们爬取一个网页,都是使用正则表达式来提取想要的信息,但是这种方式比较复杂,一旦有一个地方写错,就匹配不出来了,因此我们可以使用 XPath 来进行提取(2) XPat ...
- poj_3579 二分法
题目大意 给定N个数,这些数字两两求差构成C(N,2)(即N*(N-1)/2)个数值,求这C(N,2)个数的中位数.N <= 100000. 题目分析 根据数据规模N最大为100000,可知不能 ...
- HTTP2.0简明笔记
版权声明:本文由史燕飞原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/82 来源:腾云阁https://www.qcloud ...