public CountryInfo(Continent continent, string administrativeName, IGeometry geomentryData, Uri smallFlag, Uri largeFlag, bool longitudeWrap, int zIndex, double toleranceLow, double toleranceHi) { Continent = continent; AdministrativeName = administrativeName; if (geomentryData is Polygon) { var delta = ((Polygon)geomentryData).GetArea().Value; GeomentryData = new List <GeoPolygon> { ConvertPolygon((Polygon)geomentryData, false, longitudeWrap) }; } else if (geomentryData is MultiPolygon) { var polygons = ((MultiPolygon)geomentryData).Geometries .Cast <Polygon>() .Where(p => p.GetArea().Value > 0) .Select(p => new { Polygon = p, Area = p.GetArea().Value, Bounds = p.GetBounds() }) .ToList(); var countryBounds = geomentryData.GetBounds(); var area = polygons.MaxBy(a => a.Area); var dist = Math.Sqrt( Math.Pow(countryBounds.MaxLon - countryBounds.MinLon - area.Bounds.MaxLon + area.Bounds.MinLon, 2) + Math.Pow(countryBounds.MaxLat - countryBounds.MinLat - area.Bounds.MaxLat + area.Bounds.MinLat, 2) ); var total = polygons.Sum(a => a.Area); GeomentryData = polygons .Select(p => ConvertPolygon(p.Polygon, p.Area / total < (toleranceLow + toleranceHi * GetDistance(area.Bounds, p.Bounds) / dist), longitudeWrap)) .ToList(); } else { GeomentryData = new List <GeoPolygon>(); } var bounds = GeomentryData.Where(p => !p.Ignored); Bounds = new GeoBounds( bounds.Min(p => p.Bounds.Left), bounds.Min(p => p.Bounds.Top), bounds.Max(p => p.Bounds.Right), bounds.Max(p => p.Bounds.Bottom)); SmallFlag = smallFlag; LargeFlag = largeFlag; Borders = new List <IBorderInfo>(); ZIndex = zIndex; IsSmall = GeomentryData.Max(g => g.Area) < SmallCountryAreaThreshold; }
protected override void unloadUnmanagedResources() { colorBuffer.Dispose(); blurredRt1.Dispose(); nrmDepthBuffer.Dispose(); edgeResultBuffer.Dispose(); indexBuffer.Dispose(); quad.Dispose(); smallQuad.Dispose(); quadOp = null; smallQuadOp = null; quad = null; smallQuad = null; indexBuffer = null; colorBuffer = null; blurredRt1 = null; //colorTarget = null; //bloom = null; }
/// <summary> /// This implements the method in resource, to load the mesh data(vertex buffer, index buffer) /// of this terrain tile. /// </summary> protected override void load() { // 读取地形数据 // This line will load the terrain data in this tile. float[] data = TerrainData.Instance.GetData(tileX, tileY, terrEdgeSize); float radtc = MathEx.Degree2Radian(tileCol); float radtl = MathEx.Degree2Radian(tileLat); float radSpan = MathEx.Degree2Radian(10); int vertexCount = terrEdgeSize * terrEdgeSize; int terrEdgeLen = terrEdgeSize - 1; if (terrEdgeSize == 33) material.SetEffect(EffectManager.Instance.GetModelEffect(TerrainEffect33Factory.Name)); else material.SetEffect(EffectManager.Instance.GetModelEffect(TerrainEffect17Factory.Name)); #region 顶点数据 vtxDecl = factory.CreateVertexDeclaration(TerrainVertex.Elements); vtxBuffer = factory.CreateVertexBuffer(vertexCount, vtxDecl, BufferUsage.WriteOnly); TerrainVertex[] vtxArray = new TerrainVertex[vertexCount]; float cellAngle = radSpan / (float)terrEdgeLen; #region 计算顶点坐标 // Caluclate the position of each vertex // i为纬度方向 // i is in the latitude direction for (int i = 0; i < terrEdgeSize; i++) { // j为经度方向 // j is in the longitude direction for (int j = 0; j < terrEdgeSize; j++) { Vector3 pos = PlanetEarth.GetPosition(radtc + j * cellAngle, radtl - i * cellAngle); int index = i * terrEdgeSize + j; // 计算海拔高度 // calculate the elevation float height = (data[index] - TerrainMeshManager.PostZeroLevel) * TerrainMeshManager.PostHeightScale; //if (height > 0) //{ // height = (height - 0) * TerrainMeshManager.PostHeightScale; //} //else //{ // height *= TerrainMeshManager.PostHeightScale; // height -= 10; // //if (height < -30) // // height = -30; //} Vector3 normal = pos; normal.Normalize(); vtxArray[index].Position = pos + normal * height; // this index is used to generate detailed texture coordinate in vertex shader vtxArray[index].Index = index; // map the texture coordinate for global texturing float curCol = radtc + j * cellAngle; float curLat = radSpan + radtl - i * cellAngle; curCol += MathEx.PIf; curLat -= MathEx.Degree2Radian(10); vtxArray[index].u = 0.5f * curCol / MathEx.PIf; vtxArray[index].v = (-curLat + MathEx.PiOver2) / MathEx.PIf; } } #endregion #endregion #region 索引数据 SharedIndexData sindexData = TerrainMeshManager.Instance.GetIndexData(terrEdgeSize); indexBuffer = sindexData.Index; #endregion #region 构造GeomentryData defGeometryData = new GeomentryData(); defGeometryData.VertexDeclaration = vtxDecl; defGeometryData.VertexSize = TerrainVertex.Size; defGeometryData.VertexBuffer = vtxBuffer; defGeometryData.IndexBuffer = indexBuffer; defGeometryData.PrimCount = indexBuffer.IndexCount / 3; defGeometryData.VertexCount = terrEdgeSize * terrEdgeSize; defGeometryData.PrimitiveType = RenderPrimitiveType.TriangleList; defGeometryData.BaseVertex = 0; #endregion vtxBuffer.SetData<TerrainVertex>(vtxArray); }
protected unsafe override void loadUnmanagedResources() { Viewport vp = renderSys.Viewport; Size blmSize = new Size(vp.Width / 2, vp.Height / 2); Size scrnSize = new Size(vp.Width, vp.Height); blurredRt1 = factory.CreateRenderTarget(blmSize.Width, blmSize.Height, ImagePixelFormat.A8R8G8B8); blurredRt2 = factory.CreateRenderTarget(blmSize.Width, blmSize.Height, ImagePixelFormat.A8R8G8B8); colorBuffer = factory.CreateRenderTarget(scrnSize.Width, scrnSize.Height, ImagePixelFormat.A8R8G8B8); nrmDepthBuffer = factory.CreateRenderTarget(scrnSize.Width, scrnSize.Height, ImagePixelFormat.A8R8G8B8); edgeResultBuffer = factory.CreateRenderTarget(scrnSize.Width, scrnSize.Height, ImagePixelFormat.A8R8G8B8); #region 计算参数 guassFilter = new GuassBlurFilter(SampleCount, BlurAmount, blmSize.Width, blmSize.Height); #endregion #region 建立屏幕quad quad = factory.CreateVertexBuffer(4, vtxDecl, BufferUsage.Static); float adj = -0.5f; RectVertex* vdst = (RectVertex*)quad.Lock(0, 0, LockMode.None); vdst[0].Position = new Vector4(adj, adj, 0, 1); vdst[0].TexCoord = new Vector2(0, 0); vdst[1].Position = new Vector4(scrnSize.Width + adj, adj, 0, 1); vdst[1].TexCoord = new Vector2(1, 0); vdst[2].Position = new Vector4(adj, scrnSize.Height + adj, 0, 1); vdst[2].TexCoord = new Vector2(0, 1); vdst[3].Position = new Vector4(scrnSize.Width + adj, scrnSize.Height + adj, 0, 1); vdst[3].TexCoord = new Vector2(1, 1); quad.Unlock(); #endregion #region 建立小quad smallQuad = factory.CreateVertexBuffer(4, vtxDecl, BufferUsage.Static); vdst = (RectVertex*)smallQuad.Lock(0, 0, LockMode.None); vdst[0].Position = new Vector4(adj, adj, 0, 1); vdst[0].TexCoord = new Vector2(0, 0); vdst[1].Position = new Vector4(blmSize.Width + adj, adj, 0, 1); vdst[1].TexCoord = new Vector2(1, 0); vdst[2].Position = new Vector4(adj, blmSize.Height + adj, 0, 1); vdst[2].TexCoord = new Vector2(0, 1); vdst[3].Position = new Vector4(blmSize.Width + adj, blmSize.Height + adj, 0, 1); vdst[3].TexCoord = new Vector2(1, 1); smallQuad.Unlock(); #endregion indexBuffer = factory.CreateIndexBuffer(IndexBufferType.Bit32, 6, BufferUsage.Static); int* idst = (int*)indexBuffer.Lock(0, 0, LockMode.None); idst[0] = 3; idst[1] = 1; idst[2] = 0; idst[3] = 2; idst[4] = 3; idst[5] = 0; indexBuffer.Unlock(); quadOp = new GeomentryData(); quadOp.BaseIndexStart = 0; quadOp.BaseVertex = 0; quadOp.IndexBuffer = indexBuffer; quadOp.PrimCount = 2; quadOp.PrimitiveType = RenderPrimitiveType.TriangleList; quadOp.VertexBuffer = quad; quadOp.VertexCount = 4; quadOp.VertexDeclaration = vtxDecl; quadOp.VertexSize = RectVertex.Size; smallQuadOp = new GeomentryData(); smallQuadOp.BaseIndexStart = 0; smallQuadOp.BaseVertex = 0; smallQuadOp.IndexBuffer = indexBuffer; smallQuadOp.PrimCount = 2; smallQuadOp.PrimitiveType = RenderPrimitiveType.TriangleList; smallQuadOp.VertexBuffer = smallQuad; smallQuadOp.VertexCount = 4; smallQuadOp.VertexDeclaration = vtxDecl; smallQuadOp.VertexSize = RectVertex.Size; }
public OceanWaterData(RenderSystem rs, int size, float lat) { this.Size = size; geoData = new GeomentryData(); ObjectFactory fac = rs.ObjectFactory; vtxDecl = fac.CreateVertexDeclaration(WaterVertex.Elements); int len = size - 1; int vertexCount = size * size; vertexBuffer = fac.CreateVertexBuffer(vertexCount, vtxDecl, BufferUsage.Static); float rad10 = PlanetEarth.DefaultTileSpan; float radtl = MathEx.Degree2Radian(lat); #region 顶点数据 WaterVertex[] vtxArray = new WaterVertex[vertexCount]; float cellAngle = rad10 / (float)len; float invSize = 1 / (float)size; // i为经度方向 for (int i = 0; i < size; i++) { // j为纬度方向 for (int j = 0; j < size; j++) { Vector3 pos = PlanetEarth.GetPosition(j * cellAngle, radtl - i * cellAngle); pos.Normalize(); pos *= PlanetEarth.PlanetRadius; int index = i * size + j; vtxArray[index].Position = pos; vtxArray[index].NormalCoord = new Vector2(i * invSize, j * invSize); // = index; } } vertexBuffer.SetData <WaterVertex>(vtxArray); #endregion #region 索引数据 int indexCount = MathEx.Sqr(len) * 2 * 3; int[] indexArray = new int[indexCount]; indexBuffer = fac.CreateIndexBuffer(IndexBufferType.Bit32, indexCount, BufferUsage.WriteOnly); for (int i = 0, index = 0; i < len; i++) { for (int j = 0; j < len; j++) { int x = i; int y = j; indexArray[index++] = y * size + x; indexArray[index++] = y * size + (x + 1); indexArray[index++] = (y + 1) * size + (x + 1); indexArray[index++] = y * size + x; indexArray[index++] = (y + 1) * size + (x + 1); indexArray[index++] = (y + 1) * size + x; } } indexBuffer.SetData <int>(indexArray); #endregion #region 构造GeomentryData geoData = new GeomentryData(); geoData.VertexDeclaration = vtxDecl; geoData.VertexSize = WaterVertex.Size; geoData.VertexBuffer = vertexBuffer; geoData.IndexBuffer = indexBuffer; geoData.PrimCount = MathEx.Sqr(len) * 2; geoData.VertexCount = MathEx.Sqr(size); geoData.PrimitiveType = RenderPrimitiveType.TriangleList; geoData.BaseVertex = 0; #endregion }
protected unsafe override void loadUnmanagedResources() { Viewport vp = renderSys.Viewport; Size blmSize = new Size(vp.Width / 2, vp.Height / 2); Size scrnSize = new Size(vp.Width, vp.Height); blurredRt1 = factory.CreateRenderTarget(blmSize.Width, blmSize.Height, ImagePixelFormat.A8R8G8B8); blurredRt2 = factory.CreateRenderTarget(blmSize.Width, blmSize.Height, ImagePixelFormat.A8R8G8B8); colorBuffer = factory.CreateRenderTarget(scrnSize.Width, scrnSize.Height, ImagePixelFormat.A8R8G8B8); nrmDepthBuffer = factory.CreateRenderTarget(scrnSize.Width, scrnSize.Height, ImagePixelFormat.A8R8G8B8); edgeResultBuffer = factory.CreateRenderTarget(scrnSize.Width, scrnSize.Height, ImagePixelFormat.A8R8G8B8); #region 计算参数 guassFilter = new GuassBlurFilter(SampleCount, BlurAmount, blmSize.Width, blmSize.Height); #endregion #region 建立屏幕quad quad = factory.CreateVertexBuffer(4, vtxDecl, BufferUsage.Static); float adj = -0.5f; RectVertex *vdst = (RectVertex *)quad.Lock(0, 0, LockMode.None); vdst[0].Position = new Vector4(adj, adj, 0, 1); vdst[0].TexCoord = new Vector2(0, 0); vdst[1].Position = new Vector4(scrnSize.Width + adj, adj, 0, 1); vdst[1].TexCoord = new Vector2(1, 0); vdst[2].Position = new Vector4(adj, scrnSize.Height + adj, 0, 1); vdst[2].TexCoord = new Vector2(0, 1); vdst[3].Position = new Vector4(scrnSize.Width + adj, scrnSize.Height + adj, 0, 1); vdst[3].TexCoord = new Vector2(1, 1); quad.Unlock(); #endregion #region 建立小quad smallQuad = factory.CreateVertexBuffer(4, vtxDecl, BufferUsage.Static); vdst = (RectVertex *)smallQuad.Lock(0, 0, LockMode.None); vdst[0].Position = new Vector4(adj, adj, 0, 1); vdst[0].TexCoord = new Vector2(0, 0); vdst[1].Position = new Vector4(blmSize.Width + adj, adj, 0, 1); vdst[1].TexCoord = new Vector2(1, 0); vdst[2].Position = new Vector4(adj, blmSize.Height + adj, 0, 1); vdst[2].TexCoord = new Vector2(0, 1); vdst[3].Position = new Vector4(blmSize.Width + adj, blmSize.Height + adj, 0, 1); vdst[3].TexCoord = new Vector2(1, 1); smallQuad.Unlock(); #endregion indexBuffer = factory.CreateIndexBuffer(IndexBufferType.Bit32, 6, BufferUsage.Static); int *idst = (int *)indexBuffer.Lock(0, 0, LockMode.None); idst[0] = 3; idst[1] = 1; idst[2] = 0; idst[3] = 2; idst[4] = 3; idst[5] = 0; indexBuffer.Unlock(); quadOp = new GeomentryData(); quadOp.BaseIndexStart = 0; quadOp.BaseVertex = 0; quadOp.IndexBuffer = indexBuffer; quadOp.PrimCount = 2; quadOp.PrimitiveType = RenderPrimitiveType.TriangleList; quadOp.VertexBuffer = quad; quadOp.VertexCount = 4; quadOp.VertexDeclaration = vtxDecl; quadOp.VertexSize = RectVertex.Size; smallQuadOp = new GeomentryData(); smallQuadOp.BaseIndexStart = 0; smallQuadOp.BaseVertex = 0; smallQuadOp.IndexBuffer = indexBuffer; smallQuadOp.PrimCount = 2; smallQuadOp.PrimitiveType = RenderPrimitiveType.TriangleList; smallQuadOp.VertexBuffer = smallQuad; smallQuadOp.VertexCount = 4; smallQuadOp.VertexDeclaration = vtxDecl; smallQuadOp.VertexSize = RectVertex.Size; }
/// <summary> /// This implements the method in resource, to load the mesh data(vertex buffer, index buffer) /// of this terrain tile. /// </summary> protected override void load() { // 读取地形数据 // This line will load the terrain data in this tile. float[] data = TerrainData.Instance.GetData(tileX, tileY, terrEdgeSize); float radtc = MathEx.Degree2Radian(tileCol); float radtl = MathEx.Degree2Radian(tileLat); float radSpan = MathEx.Degree2Radian(10); int vertexCount = terrEdgeSize * terrEdgeSize; int terrEdgeLen = terrEdgeSize - 1; if (terrEdgeSize == 33) { material.SetEffect(EffectManager.Instance.GetModelEffect(TerrainEffect33Factory.Name)); } else { material.SetEffect(EffectManager.Instance.GetModelEffect(TerrainEffect17Factory.Name)); } #region 顶点数据 vtxDecl = factory.CreateVertexDeclaration(TerrainVertex.Elements); vtxBuffer = factory.CreateVertexBuffer(vertexCount, vtxDecl, BufferUsage.WriteOnly); TerrainVertex[] vtxArray = new TerrainVertex[vertexCount]; float cellAngle = radSpan / (float)terrEdgeLen; #region 计算顶点坐标 // Caluclate the position of each vertex // i为纬度方向 // i is in the latitude direction for (int i = 0; i < terrEdgeSize; i++) { // j为经度方向 // j is in the longitude direction for (int j = 0; j < terrEdgeSize; j++) { Vector3 pos = PlanetEarth.GetPosition(radtc + j * cellAngle, radtl - i * cellAngle); int index = i * terrEdgeSize + j; // 计算海拔高度 // calculate the elevation float height = (data[index] - TerrainMeshManager.PostZeroLevel) * TerrainMeshManager.PostHeightScale; //if (height > 0) //{ // height = (height - 0) * TerrainMeshManager.PostHeightScale; //} //else //{ // height *= TerrainMeshManager.PostHeightScale; // height -= 10; // //if (height < -30) // // height = -30; //} Vector3 normal = pos; normal.Normalize(); vtxArray[index].Position = pos + normal * height; // this index is used to generate detailed texture coordinate in vertex shader vtxArray[index].Index = index; // map the texture coordinate for global texturing float curCol = radtc + j * cellAngle; float curLat = radSpan + radtl - i * cellAngle; curCol += MathEx.PIf; curLat -= MathEx.Degree2Radian(10); vtxArray[index].u = 0.5f * curCol / MathEx.PIf; vtxArray[index].v = (-curLat + MathEx.PiOver2) / MathEx.PIf; } } #endregion #endregion #region 索引数据 SharedIndexData sindexData = TerrainMeshManager.Instance.GetIndexData(terrEdgeSize); indexBuffer = sindexData.Index; #endregion #region 构造GeomentryData defGeometryData = new GeomentryData(); defGeometryData.VertexDeclaration = vtxDecl; defGeometryData.VertexSize = TerrainVertex.Size; defGeometryData.VertexBuffer = vtxBuffer; defGeometryData.IndexBuffer = indexBuffer; defGeometryData.PrimCount = indexBuffer.IndexCount / 3; defGeometryData.VertexCount = terrEdgeSize * terrEdgeSize; defGeometryData.PrimitiveType = RenderPrimitiveType.TriangleList; defGeometryData.BaseVertex = 0; #endregion vtxBuffer.SetData <TerrainVertex>(vtxArray); }
public OceanWaterData(RenderSystem rs, int size, float lat) { this.Size = size; geoData = new GeomentryData(); ObjectFactory fac = rs.ObjectFactory; vtxDecl = fac.CreateVertexDeclaration(WaterVertex.Elements); int len = size - 1; int vertexCount = size * size; vertexBuffer = fac.CreateVertexBuffer(vertexCount, vtxDecl, BufferUsage.Static); float rad10 = PlanetEarth.DefaultTileSpan; float radtl = MathEx.Degree2Radian(lat); #region 顶点数据 WaterVertex[] vtxArray = new WaterVertex[vertexCount]; float cellAngle = rad10 / (float)len; float invSize = 1 / (float)size; // i为经度方向 for (int i = 0; i < size; i++) { // j为纬度方向 for (int j = 0; j < size; j++) { Vector3 pos = PlanetEarth.GetPosition(j * cellAngle, radtl - i * cellAngle); pos.Normalize(); pos *= PlanetEarth.PlanetRadius; int index = i * size + j; vtxArray[index].Position = pos; vtxArray[index].NormalCoord = new Vector2(i * invSize, j * invSize); // = index; } } vertexBuffer.SetData<WaterVertex>(vtxArray); #endregion #region 索引数据 int indexCount = MathEx.Sqr(len) * 2 * 3; int[] indexArray = new int[indexCount]; indexBuffer = fac.CreateIndexBuffer(IndexBufferType.Bit32, indexCount, BufferUsage.WriteOnly); for (int i = 0, index = 0; i < len; i++) { for (int j = 0; j < len; j++) { int x = i; int y = j; indexArray[index++] = y * size + x; indexArray[index++] = y * size + (x + 1); indexArray[index++] = (y + 1) * size + (x + 1); indexArray[index++] = y * size + x; indexArray[index++] = (y + 1) * size + (x + 1); indexArray[index++] = (y + 1) * size + x; } } indexBuffer.SetData<int>(indexArray); #endregion #region 构造GeomentryData geoData = new GeomentryData(); geoData.VertexDeclaration = vtxDecl; geoData.VertexSize = WaterVertex.Size; geoData.VertexBuffer = vertexBuffer; geoData.IndexBuffer = indexBuffer; geoData.PrimCount = MathEx.Sqr(len) * 2; geoData.VertexCount = MathEx.Sqr(size); geoData.PrimitiveType = RenderPrimitiveType.TriangleList; geoData.BaseVertex = 0; #endregion }