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

📄 world.cs

📁 功能:基于windows mobile 的地图查看器。使用vs2005开发
💻 CS
📖 第 1 页 / 共 2 页
字号:
				level = 3;
				tileSize = 4.5f;
				numRows = 39; //TODO should this be 40?
				numCols = 80;
			}

			levelChange = false;
			if (level != lastLevel)
			{
				levelChange = true;
			}
			else
			{
				levelChange = false;
			}

			//determine what row/col i'm currently at
			//int row = MathEngine.GetRowFromLatitude(latitude, tileSize);
			//int col = MathEngine.GetColFromLongitude(longitude, tileSize);
			int row = (int)((camera.Latitude + 90) / tileSize);
			int col = (int)((camera.Longitude + 180) / tileSize);
			if (level == lastLevel && row == lastRow && col == lastCol)
			{
				return;
			}

			List<string> alNeighbors = new List<string>();
			AddNeighbor(alNeighbors, level, row, col, numRows, numCols);
			//now i know its 8 neighbors
			AddNeighbor(alNeighbors, level, row - 1, col, numRows, numCols);
			AddNeighbor(alNeighbors, level, row + 1, col, numRows, numCols);
			AddNeighbor(alNeighbors, level, row, col - 1, numRows, numCols);
			AddNeighbor(alNeighbors, level, row, col + 1, numRows, numCols);
			AddNeighbor(alNeighbors, level, row - 1, col - 1, numRows, numCols);
			AddNeighbor(alNeighbors, level, row - 1, col + 1, numRows, numCols);
			AddNeighbor(alNeighbors, level, row + 1, col - 1, numRows, numCols);
			AddNeighbor(alNeighbors, level, row + 1, col + 1, numRows, numCols);

			//loop through smaller cache and figure out tiles to remove?
			//or do something generation based
			//then add to the cache and get rid of these last
			List<string> alDispose = new List<string>();
			foreach (string key in htRenderItems.Keys)
			{
				if (alNeighbors.Contains(key) == false)
				{
					alDispose.Add(key); //or just dispose now
				}
			}

			string levelPath = Settings.DirectoryBlueMarbleTextures + level.ToString() + @"\";
			//loop through the neighbors
			foreach (string idTile in alNeighbors)
			{
				if (htRenderItems.ContainsKey(idTile) == true)
				{
					continue;
				}
				//else load the tile

				string[] idTiles = idTile.Split(splitChar);
				//int iLevel = Int32.Parse(idTiles[0]); //already known above
				int iRow = Int32.Parse(idTiles[1]);
				int jCol = Int32.Parse(idTiles[2]);

				string rowDirName = string.Format("{0:0000}", iRow);
				string rowDirPath = levelPath + rowDirName + @"\";
				string colFileName = string.Format("{0:0000}", jCol);
				//string textureName = rowDirName + "_" + colFileName + ".dds"; //earth texture DDS
				string textureName = rowDirName + "_" + colFileName + ".bmp"; //earth texture BMP
				string meshName = rowDirName + "_" + colFileName + ".md3dm";

				if (Settings.Tiles == Settings.TileType.RowColumn)
				{
					textureName = rowDirName + "_" + colFileName + ".test.bmp";
				}

				string texturePath = rowDirPath + textureName;
				string meshPath = rowDirPath + meshName;

				Texture texture = defaultTexture;
				bool defaultTextureFlag = false;
				if (File.Exists(texturePath) == true)
				{
					texture = TextureLoader.FromFile(device, texturePath);
				}
				else
				{
					defaultTextureFlag = true;
					System.Diagnostics.Debug.WriteLine("MISSING " + textureName);
				}

				Mesh mesh = null;
				if (File.Exists(meshPath) == true)
				{
					FileStream fs = new FileStream(meshPath, FileMode.Open);
					Material[] outMaterials;
					string[] outTextures;
					mesh = MeshLoader.LoadMesh(device, fs, MeshFlags.SystemMemory, out outMaterials, out outTextures);
					fs.Close();
				}
				else
				{
					System.Diagnostics.Debug.WriteLine("MISSING " + meshPath);
				}

				VertexBuffer vbOutline = null;
				if (Settings.TileOutline == true)
				{
					float lat = iRow * 36 - 90;
					float lon = jCol * 36 - 180;
					if (level != 0)
					{
						lat = iRow * tileSize - 90;
						lon = jCol * tileSize - 180;
					}

					float west = lon;
					float east = lon + tileSize;
					float south = lat;
					float north = lat + tileSize;

					int color = System.Drawing.Color.Blue.ToArgb(); //ignored

					Vector3 northWestV = MathEngine.SphericalToCartesian(north, west, Settings.Radius);
					Vector3 southWestV = MathEngine.SphericalToCartesian(south, west, Settings.Radius);
					Vector3 northEastV = MathEngine.SphericalToCartesian(north, east, Settings.Radius);
					Vector3 southEastV = MathEngine.SphericalToCartesian(south, east, Settings.Radius);

					CustomVertex.PositionColored[] box = new CustomVertex.PositionColored[5]; //5 enclosed
					box[0].X = northWestV.X - 1;
					box[0].Y = northWestV.Y - 1;
					box[0].Z = northWestV.Z;
					box[0].Color = color;

					box[1].X = southWestV.X - 1;
					box[1].Y = southWestV.Y - 1;
					box[1].Z = southWestV.Z;
					box[1].Color = color;

					box[2].X = southEastV.X - 1;
					box[2].Y = southEastV.Y - 1;
					box[2].Z = southEastV.Z;
					box[2].Color = color;

					box[3].X = northEastV.X - 1;
					box[3].Y = northEastV.Y - 1;
					box[3].Z = northEastV.Z;
					box[3].Color = color;

					box[4].X = box[0].X;
					box[4].Y = box[0].Y;
					box[4].Z = box[0].Z;
					box[4].Color = color;

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

				RenderTile rt = new RenderTile();
				rt.mesh = mesh;
				rt.texture = texture;
				rt.defaultTexture = defaultTextureFlag;
				rt.outline = vbOutline;
				htRenderItems.Add(idTile, rt);
			}

			//now delete the unused tiles
			foreach (string idTile in alDispose)
			{
				RenderTile rt = (RenderTile)htRenderItems[idTile];
				if (rt.defaultTexture == false)
				{
					rt.Dispose(); //clear up Texture memory
					rt = null;
				}
				htRenderItems.Remove(idTile);
			}
			//Diagnostic.Instance(device).Add("level", level.ToString());
			Diagnostic.Instance(device).Add("row", row.ToString());
			Diagnostic.Instance(device).Add("col", col.ToString());
			//Diagnostic.Instance(device).Add("tiles", htRenderItems.Count.ToString());
		}

		private void AddNeighbor(List<string> alTiles, int level, int row, int col, int numRows, int numCols)
		{
			if (row >= numRows || row < 0)
			{
				return; //dont add 
				//TODO expand the columns?
			}
			if (col >= numCols)
			{
				col = 0;
			}
			if (col < 0)
			{
				col = numCols - 1;
			}
			string idTile = level.ToString() + ":" + row.ToString() + ":" + col.ToString();
			alTiles.Add(idTile);
		}

		public class RenderTile
		{
			public Mesh mesh;
			public Texture texture;
			public bool defaultTexture = false;
			public VertexBuffer outline;

			//public int level;
			//public int row;
			//public int col;
			//public float tileSize;

			public void Dispose()
			{
				if (texture != null)
				{
					texture.Dispose();
					texture = null;
				}
				if (mesh != null)
				{
					mesh.Dispose();
					mesh = null;
				}
				if (outline != null)
				{
					outline.Dispose();
					outline = null;
				}
			}
		}

		public void RenderTiles(Device device)
		{
			if (htRenderItems != null && htRenderItems.Count > 0)
			{
				//IDictionaryEnumerator ide = htRenderItems.GetEnumerator();
				device.SetTexture(0, null);
				foreach(RenderTile rt in htRenderItems.Values)
				{
				//while (ide.MoveNext() == true)
				//{
					//RenderTile rt = (RenderTile)ide.Value;

					device.Material = meshMaterial;
					if (Settings.Texture == true)
					{
						device.SetTexture(0, rt.texture);
					}
					rt.mesh.DrawSubset(0);

					if (Settings.TileOutline == true)
					{
						if (rt.outline != null)
						{
							//device.RenderState.ZBufferEnable = false;
							device.Material = outlineMaterial;
							device.SetTexture(0, null);
							device.SetStreamSource(0, rt.outline, 0);
							//device.TextureState[0].ColorOperation = TextureOperation.Disable;
							device.DrawPrimitives(PrimitiveType.LineStrip, 0, 4);
							//device.RenderState.ZBufferEnable = true;
						}
					}
				}
			}
		}

		public void Dispose()
		{
			if (mesh != null)
			{
				mesh.Dispose();
				mesh = null;
			}
			if (meshTexture != null)
			{
				meshTexture.Dispose();
				meshTexture = null;
			}
			if (defaultTexture != null)
			{
				defaultTexture.Dispose();
				defaultTexture = null;
			}
			if (htRenderItems != null)
			{
				foreach (string key in htRenderItems.Keys)
				{
					RenderTile rt = (RenderTile) htRenderItems[key];
					rt.Dispose();
					rt = null;
				}
			}
		}
	}
}

⌨️ 快捷键说明

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