⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boundary.cs

📁 功能:基于windows mobile 的地图查看器。使用vs2005开发
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;

using Microsoft.WindowsMobile.DirectX;
using Microsoft.WindowsMobile.DirectX.Direct3D;

using WorldWindow;

namespace cfWorldWind
{
	class Boundary
	{
		private Material boundaryMaterial;

		public Boundary()
		{
			boundaryMaterial = new Material();
		}

		public class BoundaryList
		{
			public Vector3[] rawVertices;
			public VertexBuffer vertexBuffer;
			public int numberOfPrimitives;
			public Color color;
		}

		public List<BoundaryList> InitializeVertex(Device device, string boundaryDir)
		{
			List<BoundaryList> alBoundaries = new List<BoundaryList>();
			//string pathListIdx = boundaryDir + "pathlist.idx";
			//string pathListPkg = boundaryDir + "pathlist.pkg";
			string pathListIdx = boundaryDir + "pathlistSmall.idx";
			string pathListPkg = boundaryDir + "pathlistSmall.pkg";
			//pathlist.idx //too long to load, lines dont show until zoom
			//*** pathListSmall.idx //its #1 for all except US, US uses #160
			//0pathlistSmall.idx //us doesnt show up at all
			//1pathlistSmall.idx //US shows up half
			//5pathlistSmall.idx //
			//10pathlistSmall.idx //
			//20pathlistSmall.idx //lines still fragmented at high altitude
			//40pathlistSmall.idx
			//80pathlistSmall.idx //us is better, but sparse
			//160pathlistSmall.idx //us better, the rest is still sparse
			FileInfo indexFileInfo = new FileInfo(pathListIdx);
			FileInfo dataFileInfo = new FileInfo(pathListPkg);

			if (indexFileInfo.Exists && dataFileInfo.Exists)
			{
				FileStream fsIndex = indexFileInfo.OpenRead();
				//BufferedStream indexFileStream = new BufferedStream(fsIndex);
				//BinaryReader indexFileReader = new BinaryReader(indexFileStream, System.Text.Encoding.ASCII);
				BinaryReader indexFileReader = new BinaryReader(fsIndex, System.Text.Encoding.ASCII);
				FileStream fsData = dataFileInfo.OpenRead();
				//BufferedStream dataFileStream = new BufferedStream(fsData);
				//BinaryReader dataArchiveReader = new BinaryReader(dataFileStream, System.Text.Encoding.ASCII);
				BinaryReader dataArchiveReader = new BinaryReader(fsData, System.Text.Encoding.ASCII);

				int count = indexFileReader.ReadInt32();
				//richTextBox1.AppendText("count: " + count.ToString() + "\r\n");

				for (int i = 0; i < count; i++) //974
				{
					string fileName = indexFileReader.ReadString(); //"af106_6_6_1_1.wwb"
					double west = indexFileReader.ReadDouble(); //decimal degrees
					double south = indexFileReader.ReadDouble();
					double east = indexFileReader.ReadDouble();
					double north = indexFileReader.ReadDouble();
					long offset = indexFileReader.ReadInt64(); //37601
					//long size = indexFileReader.ReadInt64();

					//richTextBox1.AppendText(fileName + "\r\n");
					//richTextBox1.AppendText("W " + (int)west + "\r\n");
					//richTextBox1.AppendText("S " + (int)south + "\r\n");
					//richTextBox1.AppendText("E " + (int)east + "\r\n");
					//richTextBox1.AppendText("N " + (int)north + "\r\n");
					//richTextBox1.AppendText("offset: " + offset.ToString() + "\r\n");

					dataArchiveReader.BaseStream.Seek(offset, SeekOrigin.Begin);

					int numCoords = dataArchiveReader.ReadInt32();
					if (numCoords == 0) //not a line
						continue; 
					//richTextBox1.AppendText("coords: " + numCoords.ToString() + "\r\n");

					byte numElements = dataArchiveReader.ReadByte();
					if (numElements != 3 && numElements != 2)
						throw new Exception("Boundary: unknown # of elements");
					//richTextBox1.AppendText("elems: " + numElements.ToString() + "\r\n");
					/*
					Vector3[] sphericalCoordinates = new Vector3[numCoords];
					for (int j = 0; j < numCoords; j++)
					{
						sphericalCoordinates[j].X = (float)dataArchiveReader.ReadDouble();
						sphericalCoordinates[j].Y = (float)dataArchiveReader.ReadDouble();
						if (numElements == 3)
						{
							sphericalCoordinates[j].Z = dataArchiveReader.ReadInt16();
						}
					}
					 */

					/*
					double radius = Settings.Radius;
					//if (drawArgs.device.RenderState.ZBufferEnable)
					if (device.RenderState.ZBufferEnable)
					{
						double bRadius = radius * 1.01f;
						//double nRadius = radius + 0.015f * drawArgs.WorldCamera.Altitude;
						double nRadius = radius + 0.015f * Settings.Radius;
						radius = nRadius < bRadius ? nRadius : bRadius;
					}
					 */

					//CustomVertex.PositionColored[] newLinePoints = new CustomVertex.PositionColored[numCoords];
					Vector3[] newLinePoints = new Vector3[numCoords];
					for (int j = 0; j < numCoords; j++)
					{
						float lat = (float)dataArchiveReader.ReadDouble();
						float lon = (float)dataArchiveReader.ReadDouble();
						short alt; //ignored
						if (numElements == 3)
						{
							alt = dataArchiveReader.ReadInt16();
						}
						double radius = Settings.Radius + 3000;
						Vector3 curPoint = MathEngine.SphericalToCartesian(lat, lon, radius);
						//Vector3 curPoint = MathEngine.SphericalToCartesian(Angle.FromRadians(lat), Angle.FromRadians(lon), radius);
						newLinePoints[j].X = curPoint.X; //6343601
						newLinePoints[j].Y = curPoint.Y; //133218
						newLinePoints[j].Z = curPoint.Z; //678147
						//newLinePoints[j].Color = Color.Gray.ToArgb();
					}

					//VertexBuffer vb = new VertexBuffer(typeof(CustomVertex.PositionColored), newLinePoints.Length, device, 0, CustomVertex.PositionColored.Format, Pool.SystemMemory);
					//GraphicsStream stm = vb.Lock(0, 0, 0);
					//stm.Write(newLinePoints);
					//vb.Unlock();

					BoundaryList bl = new BoundaryList();
					bl.rawVertices = newLinePoints;
					//bl.vertexBuffer = vb;
					//bl.numberOfPrimitives = newLinePoints.Length - 1;
					alBoundaries.Add(bl);
				}
				dataArchiveReader.Close();
				//dataFileStream.Close();
				fsData.Close();
				indexFileReader.Close();
				//indexFileStream.Close();
				fsIndex.Close();
				//MessageBox.Show("done");
			}
			return alBoundaries;
		}

		public void CalculateVertex(Device device, Camera camera, List<BoundaryList> alBoundaries, Color color)
		{
			if (alBoundaries == null)
				return;

			foreach (BoundaryList bl in alBoundaries)
			{
				List<Vector3> renderablePoints = new List<Vector3>();
				Vector3 prevPoint = (Vector3)bl.rawVertices[0];
				renderablePoints.Add(prevPoint);
				for (int i = 1; i < bl.rawVertices.Length-1; i++)
				{
					Vector3 curPoint = (Vector3)bl.rawVertices[i];
					Vector3 projectedCurPoint = curPoint;

					prevPoint.Project(device.Viewport, camera.Projection, camera.View, camera.World);
					projectedCurPoint.Project(device.Viewport, camera.Projection, camera.View, camera.World);

					if (Math.Abs(prevPoint.X - projectedCurPoint.X) > 1.0 ||
						Math.Abs(prevPoint.Y - projectedCurPoint.Y) > 1.0)
					{
						renderablePoints.Add(curPoint);
					}
					prevPoint = curPoint;
				}
				Vector3 lastPoint = (Vector3)bl.rawVertices[bl.rawVertices.Length - 1];
				renderablePoints.Add(lastPoint);

				CustomVertex.PositionColored[] newLinePoints = new CustomVertex.PositionColored[renderablePoints.Count];
				for (int i = 0; i < renderablePoints.Count; i++)
				{
					Vector3 curPoint = (Vector3)renderablePoints[i];
					newLinePoints[i].X = curPoint.X; 
					newLinePoints[i].Y = curPoint.Y; 
					newLinePoints[i].Z = curPoint.Z;
					newLinePoints[i].Color = color.ToArgb(); //TODO this color is ignored
				}

				VertexBuffer vb = new VertexBuffer(typeof(CustomVertex.PositionColored), newLinePoints.Length, device, 0, CustomVertex.PositionColored.Format, Pool.SystemMemory);
				GraphicsStream stm = vb.Lock(0, 0, 0);
				stm.Write(newLinePoints);
				vb.Unlock();

				bl.vertexBuffer = vb;
				bl.numberOfPrimitives = newLinePoints.Length - 1;
				bl.color = color;
			}
		}

		public void PassthroughVertex(Device device, List<BoundaryList> alBoundaries, Color color)
		{
			if (alBoundaries == null)
				return;

			foreach (BoundaryList bl in alBoundaries)
			{
				CustomVertex.PositionColored[] newLinePoints = new CustomVertex.PositionColored[bl.rawVertices.Length];
				for (int i = 0; i < bl.rawVertices.Length; i++)
				{
					Vector3 curPoint = (Vector3)bl.rawVertices[i];
					newLinePoints[i].X = curPoint.X;
					newLinePoints[i].Y = curPoint.Y;
					newLinePoints[i].Z = curPoint.Z;
					newLinePoints[i].Color = color.ToArgb(); //TODO this color is ignored
				}

				VertexBuffer vb = new VertexBuffer(typeof(CustomVertex.PositionColored), newLinePoints.Length, device, 0, CustomVertex.PositionColored.Format, Pool.SystemMemory);
				GraphicsStream stm = vb.Lock(0, 0, 0);
				stm.Write(newLinePoints);
				vb.Unlock();

				bl.vertexBuffer = vb;
				bl.numberOfPrimitives = newLinePoints.Length - 1;
				bl.color = color;
			}
		}

		public void RenderVertex(Device device, List<BoundaryList> alBoundaries)
		{
			if (alBoundaries != null)
			{
				//device.TextureState[0].ColorOperation = TextureOperation.Disable;
				if (alBoundaries.Count > 0)
				{
					BoundaryList bl = (BoundaryList)alBoundaries[0];
					boundaryMaterial.Ambient = bl.color;
					boundaryMaterial.Diffuse = bl.color;
					device.Material = boundaryMaterial;
					device.SetTexture(0, null);
				}

				foreach (BoundaryList bl in alBoundaries)
				{
					device.SetStreamSource(0, bl.vertexBuffer, 0);
					device.DrawPrimitives(PrimitiveType.LineStrip, 0, bl.numberOfPrimitives);
				}
			}
		}

		public void Dispose(List<BoundaryList> boundaryList)
		{
			if (boundaryList != null)
			{
				foreach (BoundaryList bl in boundaryList)
				{
					if (bl.vertexBuffer != null)
					{
						bl.vertexBuffer.Dispose();
						bl.vertexBuffer = null;
					}
				}
				boundaryList.Clear();
				boundaryList = null;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -