public override void OnCreateVertexBuffer(VertexBuffer11 vb) { ComputeMatrix(); double latMin = 0 + (ScaleY * (Height - PixelCenterY)); double latMax = 0 - (ScaleY * PixelCenterY); double lngMin = 0 + (ScaleX * PixelCenterX); double lngMax = 0 - (ScaleX * (Width - PixelCenterX)); Vector3d TopLeft = GeoTo3d(latMin, lngMin); Vector3d BottomRight = GeoTo3d(latMax, lngMax); Vector3d TopRight = GeoTo3d(latMin, lngMax); Vector3d BottomLeft = GeoTo3d(latMax, lngMin); // Create a vertex buffer PositionNormalTexturedX2[] verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) verts[0].Position = TopLeft; verts[0].Normal = TopLeft; verts[0].Tu = 0; verts[0].Tv = 0; verts[1].Position = TopRight; verts[1].Normal = TopRight; verts[1].Tu = 1; verts[1].Tv = 0; verts[2].Position = BottomRight; verts[2].Normal = BottomRight; verts[2].Tu = 1; verts[2].Tv = 1; verts[3].Position = BottomLeft; verts[3].Normal = BottomLeft; verts[3].Tu = 0; verts[3].Tv = 1; vb.Unlock(); short[] indexArray = (short[])indexBuffer.Lock(); indexArray[0] = 3; indexArray[1] = 1; indexArray[2] = 0; indexArray[3] = 1; indexArray[4] = 3; indexArray[5] = 2; indexBuffer.Unlock(); }
public override void CleanUpGeometryOnly() { base.CleanUpGeometryOnly(); if (this.vertexBuffer != null) { this.vertexBuffer.Dispose(); GC.SuppressFinalize(vertexBuffer); this.vertexBuffer = null; } if (this.indexBuffer != null) { indexBuffer.Dispose(); GC.SuppressFinalize(indexBuffer); indexBuffer = null; } }
public static void ReturnPNTX2VertexBuffer(VertexBuffer11 buff) { if (buff == null) { return; } buff.Dispose(); GC.SuppressFinalize(buff); // BufferMutex.WaitOne(); //todo11 reimplement buffer pooling //int count = buff.Count; //if (!VertexX2Pools.ContainsKey(count)) //{ // VertexX2Pools.Add(count, new Buffers(count)); //} //VertexX2Pools[count].Entries.Push(buff); //BufferMutex.ReleaseMutex(); }
public override bool CreateGeometry(RenderContext11 renderContext, bool uiThread) { if (texture == null) { if (this.texture == null) { if (TextureReady) { texture = BufferPool11.GetTexture(FileName); double aspect = (double)texture.Width / (double)texture.Height; if (aspect < 1.5) { domeMaster = true; } } else { return false; } } iTileBuildCount++; if (vertexBuffer == null) { vertexBuffer = new VertexBuffer11(typeof(PositionNormalTexturedX2), VertexCount, RenderContext11.PrepDevice); indexBuffer = new IndexBuffer11[4]; OnCreateVertexBuffer(vertexBuffer); sphereRadius = 1; sphereCenter = new Vector3d(0, 0, 0); } } ReadyToRender = true; return true; }
public override bool CreateGeometry(RenderContext11 renderContext, bool uiThread) { if (texture == null) { if (this.texture == null) { if (TextureReady) { texture = BufferPool11.GetTexture(FileName); double aspect = (double)texture.Width / (double)texture.Height; if (aspect < 1.5) { domeMaster = true; } } else { return(false); } } iTileBuildCount++; if (vertexBuffer == null) { vertexBuffer = new VertexBuffer11(typeof(PositionNormalTexturedX2), VertexCount, RenderContext11.PrepDevice); indexBuffer = new IndexBuffer11[4]; OnCreateVertexBuffer(vertexBuffer); sphereRadius = 1; sphereCenter = new Vector3d(0, 0, 0); } } ReadyToRender = true; return(true); }
public override void CleanUp(bool removeFromParent) { base.CleanUp(removeFromParent); this.ImageData = null; if (this.texture != null) { this.texture.Dispose(); GC.SuppressFinalize(texture); this.texture = null; } if (this.vertexBuffer != null) { try { this.vertexBuffer.Dispose(); GC.SuppressFinalize(vertexBuffer); this.vertexBuffer = null; } catch { } } if (this.indexBuffer != null) { try { indexBuffer.Dispose(); GC.SuppressFinalize(indexBuffer); indexBuffer = null; } catch { } } }
public override void CleanUp(bool removeFromParent) { base.CleanUp(removeFromParent); ImageData = null; if (texture != null) { texture.Dispose(); GC.SuppressFinalize(texture); texture = null; } if (vertexBuffer != null) { try { vertexBuffer.Dispose(); GC.SuppressFinalize(vertexBuffer); vertexBuffer = null; } catch { } } if (indexBuffer != null) { try { indexBuffer.Dispose(); GC.SuppressFinalize(indexBuffer); indexBuffer = null; } catch { } } }
public static void DisposeBuffers() { foreach (Buffers pool in IndexPools.Values) { foreach (object buf in pool.Entries) { IndexBuffer11 indexBuf = buf as IndexBuffer11; indexBuf.Dispose(); GC.SuppressFinalize(indexBuf); } } IndexPools.Clear(); foreach (Buffers pool in VertexX2Pools.Values) { foreach (object buf in pool.Entries) { VertexBuffer11 vertexBuf = buf as VertexBuffer11; vertexBuf.Dispose(); GC.SuppressFinalize(vertexBuf); } } Vector3dPools.Clear(); // no need to dispose since these are just managed buffers LockX2Pools.Clear(); Vector3dPools.Clear(); IndexU32Pools.Clear(); IndexU32Pools.Clear(); TriangleListPool.Entries.Clear(); foreach (Texture11 texture in TexturePool256) { texture.Dispose(); GC.SuppressFinalize(texture); } TexturePool256.Clear(); }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { ComputeMatrix(); var latMin = 0 + (ScaleY * (Height - PixelCenterY)); var latMax = 0 - (ScaleY * PixelCenterY); var lngMin = 0 + (ScaleX * PixelCenterX); var lngMax = 0 - (ScaleX * (Width - PixelCenterX)); var TopLeft = GeoTo3d(latMin, lngMin); var BottomRight = GeoTo3d(latMax, lngMax); var TopRight = GeoTo3d(latMin, lngMax); var BottomLeft = GeoTo3d(latMax, lngMin); // Create a vertex buffer var verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) verts[0].Position = TopLeft; verts[0].Normal = TopLeft; verts[0].Tu = 0; verts[0].Tv = 0; verts[1].Position = TopRight; verts[1].Normal = TopRight; verts[1].Tu = 1; verts[1].Tv = 0; verts[2].Position = BottomRight; verts[2].Normal = BottomRight; verts[2].Tu = 1; verts[2].Tv = 1; verts[3].Position = BottomLeft; verts[3].Normal = BottomLeft; verts[3].Tu = 0; verts[3].Tv = 1; vb.Unlock(); var indexArray = (short[])indexBuffer.Lock(); indexArray[0] = 3; indexArray[1] = 1; indexArray[2] = 0; indexArray[3] = 1; indexArray[4] = 3; indexArray[5] = 2; indexBuffer.Unlock(); }
public override bool CreateGeometry(RenderContext11 renderContext, bool uiThread) { if (texture == null || (Volitile && !ReadyToRender)) { GetParameters(); if ( ImageData != null) { try { var ms = new MemoryStream(ImageData); var old = texture; texture = Texture11.FromStream(RenderContext11.PrepDevice, ms); ReadyToRender = true; if (old != null) { old.Dispose(); GC.SuppressFinalize(old); } if (Width == 0 && Height == 0) { Width = texture.Width; Height = texture.Height; if (dataset.WcsImage != null) { if (dataset.WcsImage.SizeX != 0) { Width = dataset.WcsImage.SizeX; } if (dataset.WcsImage.SizeY != 0) { Height = dataset.WcsImage.SizeY; } } } ms.Dispose(); ImageData = null; } catch { return false; } } if (TextureReady) { if (dataset.WcsImage != null) { if (dataset.WcsImage is FitsImage) { SetTexture(dataset.WcsImage.GetBitmap()); ReadyToRender = true; } } } if (texture == null) { if (dataset.WcsImage != null) { if (dataset.WcsImage is FitsImage) { SetTexture(dataset.WcsImage.GetBitmap()); ReadyToRender = true; } } if (TextureReady) { paintColor = Color.White; if (dataset.WcsImage != null) { paintColor = dataset.WcsImage.Color; blend = !dataset.WcsImage.ColorCombine; } if (string.IsNullOrEmpty(FileName)) { texture = Texture11.FromBitmap(RenderContext11.PrepDevice, Image); ReadyToRender = true; } else { try { texture = Texture11.FromFile(RenderContext11.PrepDevice, FileName); ReadyToRender = true; ReadyToRender = true; if (Width == 0 && Height == 0) { Width = texture.Width; Height = texture.Height; if (dataset.WcsImage != null) { if (dataset.WcsImage.SizeX != 0) { Width = dataset.WcsImage.SizeX; } if (dataset.WcsImage.SizeY != 0) { Height = dataset.WcsImage.SizeY; } } } } catch { try { //texture = Texture.FromBitmap(prepDevice, bmp, Usage.AutoGenerateMipMap, Tile.PoolToUse); texture = Texture11.FromFile(RenderContext11.PrepDevice, FileName); ReadyToRender = true; Width = texture.Width; Height = texture.Height; if (dataset.WcsImage.SizeX != 0) { Width = dataset.WcsImage.SizeX; } if (dataset.WcsImage.SizeY != 0) { Height = dataset.WcsImage.SizeY; } } catch { errored = true; } } } } } } if (vertexBuffer == null) { GetParameters(); vertexBuffer = new VertexBuffer11(typeof(PositionNormalTexturedX2), 4, RenderContext11.PrepDevice); indexBuffer = new IndexBuffer11(typeof(short), 6, RenderContext11.PrepDevice); OnCreateVertexBuffer(vertexBuffer); } return true; }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { for (int i = 0; i < 4; i++) { indexBuffer[i] = new IndexBuffer11(typeof(short), ((SubDivisions / 2) * (SubDivisions / 2) * 6), RenderContext11.PrepDevice); } try { if (!topDown) { OnCreateVertexBufferBottomsUp(vb); return; } double lat, lng; int index = 0; float tileDegrees = (float)this.dataset.BaseTileDegrees / ((float)Math.Pow(2, this.level)); float latMin = (float)(((double)this.dataset.BaseTileDegrees / 2.0 - (((double)this.y) * tileDegrees)) + this.dataset.OffsetY); float latMax = (float)(((double)this.dataset.BaseTileDegrees / 2.0 - (((double)(this.y + 1)) * tileDegrees)) + this.dataset.OffsetY); float lngMin = (float)((((double)this.x * tileDegrees) - (float)this.dataset.BaseTileDegrees / dataset.WidthFactor) + this.dataset.OffsetX); float lngMax = (float)(((((double)(this.x + 1)) * tileDegrees) - (float)this.dataset.BaseTileDegrees / dataset.WidthFactor) + this.dataset.OffsetX); float tileDegreesX = lngMax - lngMin; float tileDegreesY = latMax - latMin; TopLeft = GeoTo3d(latMin, lngMin, false); BottomRight = GeoTo3d(latMax, lngMax, false); TopRight = GeoTo3d(latMin, lngMax, false); BottomLeft = GeoTo3d(latMax, lngMin, false); // Create a vertex buffer PositionNormalTexturedX2[] verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) int x, y; float textureStep = 1.0f / SubDivisions; for (y = 0; y <= SubDivisions; y++) { if (y != SubDivisions) { lat = latMin + (textureStep * tileDegreesY * y); } else { lat = latMax; } for (x = 0; x <= SubDivisions; x++) { if (x != SubDivisions) { lng = lngMin + (textureStep * tileDegreesX * x); } else { lng = lngMax; } index = y * (SubDivisions + 1) + x; verts[index].Position = GeoTo3d(lat, lng, true); verts[index].Normal = GeoTo3d(lat, lng, false); verts[index].Tu = x * textureStep +.002f; verts[index].Tv = y * textureStep +.002f; } } vb.Unlock(); TriangleCount = (SubDivisions) * (SubDivisions) * 2; int quarterDivisions = SubDivisions / 2; int part = 0; for (int y2 = 0; y2 < 2; y2++) { for (int x2 = 0; x2 < 2; x2++) { short[] indexArray = (short[])this.indexBuffer[part].Lock(); index = 0; for (int y1 = (quarterDivisions * y2); y1 < (quarterDivisions * (y2 + 1)); y1++) { for (int x1 = (quarterDivisions * x2); x1 < (quarterDivisions * (x2 + 1)); x1++) { //index = ((y1 * quarterDivisions * 6) + 6 * x1); // First triangle in quad indexArray[index] = (short)(y1 * (SubDivisions + 1) + x1); indexArray[index + 2] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 1] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); indexArray[index + 5] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 4] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); index += 6; } } this.indexBuffer[part].Unlock(); part++; } } } catch { } }
public virtual void CleanUp(bool removeFromParent) { ReadyToRender = false; TextureReady = false; DemData = null; DemReady = false; hdTile = null; if (this.texture != null) { if (Earth3d.Logging) { Earth3d.WriteLogMessage("Tile:Texture Cleanup"); } BufferPool11.ReturnTexture(texture); this.texture = null; } else { if (Earth3d.Logging) { Earth3d.WriteLogMessage("Tile:Cleanup - no Texture"); } } if (this.vertexBuffer != null) { BufferPool11.ReturnPNTX2VertexBuffer(vertexBuffer); this.vertexBuffer = null; } if (this.indexBuffer != null) { foreach (IndexBuffer11 buffer in indexBuffer) { if (buffer != null) { BufferPool11.ReturnShortIndexBuffer(buffer); } } this.indexBuffer = null; } }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { for (var i = 0; i < 4; i++) { indexBuffer[i] = new IndexBuffer11(typeof(short), ((SubDivisions / 2) * (SubDivisions / 2) * 6), RenderContext11.PrepDevice); } try { if (!topDown) { OnCreateVertexBufferBottomsUp(vb); return; } int index; var tileDegrees = dataset.BaseTileDegrees / (Math.Pow(2, level)); var latMin = (90 - (this.y * tileDegrees)); var latMax = (90 - ((this.y + 1) * tileDegrees)); var lngMin = ((this.x * tileDegrees) - 180.0); var lngMax = (((this.x + 1) * tileDegrees) - 180.0); var tileDegreesX = lngMax - lngMin; var tileDegreesY = latMax - latMin; //bugbug altitude broken TopLeft = GeoTo3dWithAltitude(latMin, lngMin, false); BottomRight = GeoTo3dWithAltitude(latMax, lngMax, false); TopRight = GeoTo3dWithAltitude(latMin, lngMax, false); BottomLeft = GeoTo3dWithAltitude(latMax, lngMin, false); // Create a vertex buffer var verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) int x, y; var textureStep = 1.0f / SubDivisions; for (y = 0; y <= SubDivisions; y++) { double lat; if (y != SubDivisions) { lat = latMin + (textureStep * tileDegreesY * y); } else { lat = latMax; } for (x = 0; x <= SubDivisions; x++) { double lng; if (x != SubDivisions) { lng = lngMin + (textureStep * tileDegreesX * x); } else { lng = lngMax; } index = y * (SubDivisions + 1) + x; verts[index].Position = GeoTo3dWithAltitude(lat, lng, true);// Add Altitude mapping here verts[index].Normal = GeoTo3d(lat, lng, false); verts[index].Tu = x * textureStep; verts[index].Tv = y * textureStep; verts[index].Lat = lat; verts[index].Lng = lng; } } vb.Unlock(); TriangleCount = (SubDivisions) * (SubDivisions) * 2; var quarterDivisions = SubDivisions / 2; var part = 0; for (var y2 = 0; y2 < 2; y2++) { for (var x2 = 0; x2 < 2; x2++) { var indexArray = (short[])indexBuffer[part].Lock(); index = 0; for (var y1 = (quarterDivisions * y2); y1 < (quarterDivisions * (y2 + 1)); y1++) { for (var x1 = (quarterDivisions * x2); x1 < (quarterDivisions * (x2 + 1)); x1++) { //index = ((y1 * quarterDivisions * 6) + 6 * x1); // First triangle in quad indexArray[index] = (short)(y1 * (SubDivisions + 1) + x1); indexArray[index + 1] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 2] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); indexArray[index + 4] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 5] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); index += 6; } } indexBuffer[part].Unlock(); part++; } } } catch { } }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { var dem = DemEnabled; if (!subDivided) { if (vertexList == null) { InitializeGrids(); } for (var i = 0; i < 4; i++) { var count = subDivisionLevel; while (count-- > 1) { var newList = BufferPool11.GetTriagleList(); foreach (var tri in childTriangleList[i]) { tri.SubDivide(newList, vertexList); } BufferPool11.ReturnTriangleList(childTriangleList[i]); childTriangleList[i] = newList; } } subDivided = true; } //for (int i = 0; i < 4; i++) //{ // indexBuffer[i] = BufferPool11.GetShortIndexBuffer(childTriangleList[i].Count * 3); //} var indexCount = childTriangleList[0].Count * 3; demIndex = 0; try { // Create a vertex buffer var verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) var index = 0; if (dem && level > 1) { demArray = new double[17 * 17]; if (backslash) { if (backslashYIndex == null) { tempBackslashYIndex = new byte[demSize]; tempBackslashXIndex = new byte[demSize]; } } else { if (slashYIndex == null) { tempSlashYIndex = new byte[demSize]; tempSlashXIndex = new byte[demSize]; } } } foreach (var vert in vertexList) { if (dem) { // todo map this with backslash as well verts[index++] = GetMappedVertex(vert); } else { verts[index++] = vert.PositionNormalTextured(localCenter, backslash ); } demIndex++; } vb.Unlock(); TriangleCount = childTriangleList[0].Count*4; var quarterDivisions = SubDivisions / 2; var part = 0; foreach (var triList in childTriangleList) { if (GetIndexBuffer(part, 0) == null) { var indexArray = new short[indexCount]; var indexer = 0; //dumpMutex.WaitOne(); //System.Diagnostics.Debug.WriteLine("start Index dump:" + part.ToString() + ";" + (backslash ? "Backslash" : "Slash")); foreach (var tri in triList) { indexArray[indexer++] = (short)tri.A; indexArray[indexer++] = (short)tri.B; indexArray[indexer++] = (short)tri.C; //WriteDebugVertex(tri.A); //WriteDebugVertex(tri.B); //WriteDebugVertex(tri.C); } //System.Diagnostics.Debug.WriteLine("End Index dump"); //dumpMutex.ReleaseMutex(); ProcessIndexBuffer(indexArray, part); } part++; } if (backslash) { if (tempBackslashXIndex != null) { backslashXIndex = tempBackslashXIndex; backslashYIndex = tempBackslashYIndex; tempBackslashXIndex = null; tempBackslashYIndex = null; } } else { if (tempSlashYIndex != null) { slashXIndex = tempSlashXIndex; slashYIndex = tempSlashYIndex; tempSlashYIndex = null; tempSlashXIndex = null; } } } catch { } ReturnBuffers(); }
static void InitSphere() { if (sphereIndexBuffers != null) { foreach (var indexBuf in sphereIndexBuffers) { indexBuf.Dispose(); GC.SuppressFinalize(indexBuf); } } if (sphereVertexBuffers != null) { foreach (var vertBuf in sphereVertexBuffers) { vertBuf.Dispose(); GC.SuppressFinalize(vertBuf); } } sphereVertexBuffers = new VertexBuffer11[sphereCount]; sphereIndexBuffers = new IndexBuffer11[sphereCount]; triangleCountSphere = new int[sphereCount]; vertexCountSphere = new int[sphereCount]; var countX = maxSubDivisionsX; var countY = maxSubDivisionsY; for (var sphereIndex = 0; sphereIndex < sphereCount; sphereIndex++) { triangleCountSphere[sphereIndex] = countX * countY * 2; vertexCountSphere[sphereIndex] = (countX + 1) * (countY + 1); sphereVertexBuffers[sphereIndex] = new VertexBuffer11(typeof(PositionNormalTexturedX2), ((countX + 1) * (countY + 1)),RenderContext11.PrepDevice); sphereIndexBuffers[sphereIndex] = new IndexBuffer11(typeof(int), countX * countY * 6, RenderContext11.PrepDevice); double lat, lng; var index = 0; double latMin = 90; double latMax = -90; double lngMin = -180; double lngMax = 180; // Create a vertex buffer var verts = (PositionNormalTexturedX2[])sphereVertexBuffers[sphereIndex].Lock(0, 0); // Lock the buffer (which will return our structs) int x1, y1; var latDegrees = latMax - latMin; var lngDegrees = lngMax - lngMin; double textureStepX = 1.0f / countX; double textureStepY = 1.0f / countY; for (y1 = 0; y1 <= countY; y1++) { if (y1 != countY) { lat = latMax - (textureStepY * latDegrees * y1); } else { lat = latMin; } for (x1 = 0; x1 <= countX; x1++) { if (x1 != countX) { lng = lngMin + (textureStepX * lngDegrees * x1); } else { lng = lngMax; } index = y1 * (countX + 1) + x1; verts[index].Position = Coordinates.GeoTo3dDouble(lat, lng);// Add Altitude mapping here verts[index].Normal = verts[index].Position;// with altitude will come normal recomputer from adjacent triangles verts[index].Tu = (float)(x1 * textureStepX); verts[index].Tv = (float)(1f - (y1 * textureStepY)); } } sphereVertexBuffers[sphereIndex].Unlock(); var indexArray = (int[])sphereIndexBuffers[sphereIndex].Lock(); for (y1 = 0; y1 < countY; y1++) { var bWinding = (y1 % 2) == 0; for (x1 = 0; x1 < countX; x1++) { index = (y1 * countX * 6) + 6 * x1; if (bWinding) { // First triangle in quad indexArray[index] = y1 * (countX + 1) + x1; //A indexArray[index + 2] = y1 * (countX + 1) + (x1 + 1);//B indexArray[index + 1] = (y1 + 1) * (countX + 1) + x1;//C // Second triangle in quad indexArray[index + 3] = y1 * (countX + 1) + (x1 + 1);//B indexArray[index + 5] = (y1 + 1) * (countX + 1) + (x1 + 1); //D indexArray[index + 4] = (y1 + 1) * (countX + 1) + x1;// C } else { // First triangle in quad indexArray[index] = y1 * (countX + 1) + x1; //A indexArray[index + 2] = (y1 + 1) * (countX + 1) + (x1 + 1); //D indexArray[index + 1] = (y1 + 1) * (countX + 1) + x1;//C // Second triangle in quad indexArray[index + 3] = y1 * (countX + 1) + (x1 + 1);//B indexArray[index + 5] = (y1 + 1) * (countX + 1) + (x1 + 1); //D indexArray[index + 4] = y1 * (countX + 1) + x1; //A } } } sphereIndexBuffers[sphereIndex].Unlock(); countX /= 2; countY /= 2; } }
public override bool CreateGeometry(RenderContext11 renderContext, bool uiThread) { if (texture == null || (Volitile && !ReadyToRender)) { GetParameters(); if (ImageData != null) { try { MemoryStream ms = new MemoryStream(ImageData); Texture11 old = this.texture; texture = Texture11.FromStream(RenderContext11.PrepDevice, ms); ReadyToRender = true; if (old != null) { old.Dispose(); GC.SuppressFinalize(old); } if (Width == 0 && Height == 0) { Width = texture.Width; Height = texture.Height; if (dataset.WcsImage != null) { if (dataset.WcsImage.SizeX != 0) { Width = dataset.WcsImage.SizeX; } if (dataset.WcsImage.SizeY != 0) { Height = dataset.WcsImage.SizeY; } } } ms.Dispose(); ImageData = null; } catch { return(false); } } if (TextureReady) { if (dataset.WcsImage != null) { if (dataset.WcsImage is FitsImage) { SetTexture(dataset.WcsImage.GetBitmap()); ReadyToRender = true; } } } if (this.texture == null) { if (dataset.WcsImage != null) { if (dataset.WcsImage is FitsImage) { SetTexture(dataset.WcsImage.GetBitmap()); ReadyToRender = true; } } if (TextureReady) { paintColor = Color.White; if (dataset.WcsImage != null) { paintColor = dataset.WcsImage.Color; blend = !dataset.WcsImage.ColorCombine; } if (string.IsNullOrEmpty(FileName)) { texture = Texture11.FromBitmap(RenderContext11.PrepDevice, Image); ReadyToRender = true; } else { try { texture = Texture11.FromFile(RenderContext11.PrepDevice, FileName); ReadyToRender = true; ReadyToRender = true; if (Width == 0 && Height == 0) { Width = texture.Width; Height = texture.Height; if (dataset.WcsImage != null) { if (dataset.WcsImage.SizeX != 0) { Width = dataset.WcsImage.SizeX; } if (dataset.WcsImage.SizeY != 0) { Height = dataset.WcsImage.SizeY; } } } } catch { try { //texture = Texture.FromBitmap(prepDevice, bmp, Usage.AutoGenerateMipMap, Tile.PoolToUse); texture = Texture11.FromFile(RenderContext11.PrepDevice, FileName); ReadyToRender = true; Width = texture.Width; Height = texture.Height; if (dataset.WcsImage.SizeX != 0) { Width = dataset.WcsImage.SizeX; } if (dataset.WcsImage.SizeY != 0) { Height = dataset.WcsImage.SizeY; } } catch { errored = true; } } } } } } if (vertexBuffer == null) { GetParameters(); vertexBuffer = new VertexBuffer11(typeof(PositionNormalTexturedX2), 4, RenderContext11.PrepDevice); indexBuffer = new IndexBuffer11(typeof(short), 6, RenderContext11.PrepDevice); this.OnCreateVertexBuffer(vertexBuffer); } return(true); }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { bool dem = DemEnabled; if (!subDivided) { if (vertexList == null) { InitializeGrids(); } for (int i = 0; i < 4; i++) { int count = subDivisionLevel; while (count-- > 1) { List <Triangle> newList = BufferPool11.GetTriagleList(); foreach (Triangle tri in childTriangleList[i]) { tri.SubDivide(newList, vertexList); } BufferPool11.ReturnTriangleList(childTriangleList[i]); childTriangleList[i] = newList; } } subDivided = true; } //for (int i = 0; i < 4; i++) //{ // indexBuffer[i] = BufferPool11.GetShortIndexBuffer(childTriangleList[i].Count * 3); //} int indexCount = childTriangleList[0].Count * 3; demIndex = 0; try { // Create a vertex buffer PositionNormalTexturedX2[] verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) int index = 0; if (dem && level > 1) { demArray = new double[17 * 17]; if (backslash) { if (backslashYIndex == null) { tempBackslashYIndex = new byte[demSize]; tempBackslashXIndex = new byte[demSize]; } } else { if (slashYIndex == null) { tempSlashYIndex = new byte[demSize]; tempSlashXIndex = new byte[demSize]; } } } foreach (PositionTexture vert in vertexList) { if (dem) { // todo map this with backslash as well verts[index++] = GetMappedVertex(vert); } else { verts[index++] = vert.PositionNormalTextured(localCenter, backslash); } demIndex++; } vb.Unlock(); TriangleCount = childTriangleList[0].Count * 4; int quarterDivisions = SubDivisions / 2; int part = 0; foreach (List <Triangle> triList in childTriangleList) { if (GetIndexBuffer(part, 0) == null) { short[] indexArray = new short[indexCount]; int indexer = 0; //dumpMutex.WaitOne(); //System.Diagnostics.Debug.WriteLine("start Index dump:" + part.ToString() + ";" + (backslash ? "Backslash" : "Slash")); foreach (Triangle tri in triList) { indexArray[indexer++] = (short)tri.A; indexArray[indexer++] = (short)tri.B; indexArray[indexer++] = (short)tri.C; //WriteDebugVertex(tri.A); //WriteDebugVertex(tri.B); //WriteDebugVertex(tri.C); } //System.Diagnostics.Debug.WriteLine("End Index dump"); //dumpMutex.ReleaseMutex(); ProcessIndexBuffer(indexArray, part); } part++; } if (backslash) { if (tempBackslashXIndex != null) { backslashXIndex = tempBackslashXIndex; backslashYIndex = tempBackslashYIndex; tempBackslashXIndex = null; tempBackslashYIndex = null; } } else { if (tempSlashYIndex != null) { slashXIndex = tempSlashXIndex; slashYIndex = tempSlashYIndex; tempSlashYIndex = null; tempSlashXIndex = null; } } } catch { } ReturnBuffers(); }
public virtual void OnCreateVertexBuffer(VertexBuffer11 vb) { }
public virtual bool CreateGeometry(RenderContext11 renderContext, bool uiThread) { if (uiThread && !ReadyToRender) { return false; } if (texture == null) { if (PreCreateGeometry(renderContext)) { ReadyToRender = true; TextureReady = true; blendMode = false; if (DemEnabled && DemReady && DemData == null) { if (!LoadDemData()) { if (Earth3d.Logging) { Earth3d.WriteLogMessage("Tile:CreateGeometry:Loading Dem Failed"); } return false; } } return true; } blendMode = (dataset.DataSetType == ImageSetType.Sky || dataset.DataSetType == ImageSetType.Panorama) && !Settings.DomeView; //blendMode = false; if (this.texture == null) { if (TextureReady) { iTileBuildCount++; string localFilename = FileName; if (GrayscaleStyle) { localFilename = UiTools.MakeGrayScaleImage(localFilename); } if (FileExists) { if (Earth3d.Logging) { Earth3d.WriteLogMessage("Tile:CreateGeometry:Loading Texture"); } texture = BufferPool11.GetTexture(localFilename); if (texture == null) { try { // bad texture TextureReady = false; File.Delete(localFilename); } catch { if (Earth3d.Logging) { Earth3d.WriteLogMessage("Tile:CreateGeometry:Loading Texture: Exception"); } errored = true; } return false; } } else { return false; } TexturesLoaded++; } else { return false; } } if (DemEnabled && DemReady && DemData == null) { if (!LoadDemData()) { if (Earth3d.Logging) { Earth3d.WriteLogMessage("Tile:CreateGeometry:Loading Dem Failed"); } return false; } } if (DemEnabled && DemData == null) { return false; } } if (vertexBuffer == null) { vertexBuffer = BufferPool11.GetPNTX2VertexBuffer(VertexCount); vertexBuffer.ComputeSphereOnUnlock = true; indexBuffer = new IndexBuffer11[4]; if (vertexBuffer != null) { this.OnCreateVertexBuffer(vertexBuffer); sphereRadius = vertexBuffer.SphereRadius; sphereCenter = vertexBuffer.SphereCenter; sphereCenter.Add(localCenter); } } ReadyToRender = true; return true; }
public virtual void CleanUpGeometryOnly() { if (this.vertexBuffer != null) { //vertexBuffer.Created -= OnCreateVertexBuffer(); this.vertexBuffer.Dispose(); GC.SuppressFinalize(vertexBuffer); this.vertexBuffer = null; } if (this.indexBuffer != null) { foreach (IndexBuffer11 buffer in indexBuffer) { if (buffer != null) { BufferPool11.ReturnShortIndexBuffer(buffer); } } this.indexBuffer = null; } }
public override void CleanUpGeometryOnly() { base.CleanUpGeometryOnly(); if (vertexBuffer != null) { vertexBuffer.Dispose(); GC.SuppressFinalize(vertexBuffer); vertexBuffer = null; } if (indexBuffer != null) { indexBuffer.Dispose(); GC.SuppressFinalize(indexBuffer); indexBuffer = null; } }
static void InitRings() { if (ringsVertexBuffer != null) { ringsVertexBuffer.Dispose(); GC.SuppressFinalize(ringsVertexBuffer); ringsVertexBuffer = null; } var inner = 1.113; var outer = 2.25; ringsVertexBuffer = new VertexBuffer11(typeof(PositionNormalTextured), ((subDivisionsRings + 1) * 4), RenderContext11.PrepDevice); triangleCountRings = (subDivisionsRings) * 2; var verts = (PositionNormalTexturedX2[])ringsVertexBuffer.Lock(0, 0); // Lock the buffer (which will return our structs) var radStep = Math.PI * 2.0 / subDivisionsRings; var index = 0; for (var x = 0; x <= subDivisionsRings; x += 2) { var rads1 = x * radStep; var rads2 = (x + 1) * radStep; verts[index].Position = new Vector3d((Math.Cos(rads1) * inner), 0, (Math.Sin(rads1) * inner)); verts[index].Normal = new Vector3d(0, 1, 0); verts[index].Tu = 1; verts[index].Tv = 0; index++; verts[index].Position = new Vector3d((Math.Cos(rads1) * outer), 0, (Math.Sin(rads1) * outer)); verts[index].Normal = new Vector3d(0, 1, 0); verts[index].Tu = 0; verts[index].Tv = 0; index++; verts[index].Position = new Vector3d((Math.Cos(rads2) * inner), 0, (Math.Sin(rads2) * inner)); verts[index].Normal = new Vector3d(0, 1, 0); verts[index].Tu = 1; verts[index].Tv = 1; index++; verts[index].Position = new Vector3d((Math.Cos(rads2) * outer), 0, (Math.Sin(rads2) * outer)); verts[index].Normal = new Vector3d(0, 1, 0); verts[index].Tu = 0; verts[index].Tv = 1; index++; } ringsVertexBuffer.Unlock(); }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { for (int i = 0; i < 4; i++) { indexBuffer[i] = new IndexBuffer11(typeof(short), ((SubDivisions / 2) * (SubDivisions / 2) * 6), RenderContext11.PrepDevice); } try { if (!topDown) { OnCreateVertexBufferBottomsUp(vb); return; } double lat, lng; int index = 0; double tileDegrees = this.dataset.BaseTileDegrees / (Math.Pow(2, this.level)); double latMin = (90 - (((double)this.y) * tileDegrees)); double latMax = (90 - (((double)(this.y + 1)) * tileDegrees)); double lngMin = (((double)this.x * tileDegrees) - 180.0); double lngMax = ((((double)(this.x + 1)) * tileDegrees) - 180.0); double tileDegreesX = lngMax - lngMin; double tileDegreesY = latMax - latMin; //bugbug altitude broken TopLeft = GeoTo3dWithAltitude(latMin, lngMin, false); BottomRight = GeoTo3dWithAltitude(latMax, lngMax, false); TopRight = GeoTo3dWithAltitude(latMin, lngMax, false); BottomLeft = GeoTo3dWithAltitude(latMax, lngMin, false); // Create a vertex buffer PositionNormalTexturedX2[] verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) int x, y; float textureStep = 1.0f / SubDivisions; for (y = 0; y <= SubDivisions; y++) { if (y != SubDivisions) { lat = latMin + (textureStep * tileDegreesY * y); } else { lat = latMax; } for (x = 0; x <= SubDivisions; x++) { if (x != SubDivisions) { lng = lngMin + (textureStep * tileDegreesX * x); } else { lng = lngMax; } index = y * (SubDivisions + 1) + x; verts[index].Position = GeoTo3dWithAltitude(lat, lng, true);// Add Altitude mapping here verts[index].Normal = GeoTo3d(lat, lng, false); verts[index].Tu = x * textureStep; verts[index].Tv = y * textureStep; verts[index].Lat = lat; verts[index].Lng = lng; } } vb.Unlock(); TriangleCount = (SubDivisions) * (SubDivisions) * 2; int quarterDivisions = SubDivisions / 2; int part = 0; for (int y2 = 0; y2 < 2; y2++) { for (int x2 = 0; x2 < 2; x2++) { short[] indexArray = (short[])this.indexBuffer[part].Lock(); index = 0; for (int y1 = (quarterDivisions * y2); y1 < (quarterDivisions * (y2 + 1)); y1++) { for (int x1 = (quarterDivisions * x2); x1 < (quarterDivisions * (x2 + 1)); x1++) { //index = ((y1 * quarterDivisions * 6) + 6 * x1); // First triangle in quad indexArray[index] = (short)(y1 * (SubDivisions + 1) + x1); indexArray[index + 1] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 2] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); indexArray[index + 4] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 5] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); index += 6; } } this.indexBuffer[part].Unlock(); part++; } } } catch { } }
public void OnCreateVertexBufferBottomsUp(VertexBuffer11 vb) { double lat, lng; var index = 0; var tileDegrees = dataset.BaseTileDegrees / (Math.Pow(2, level)); var latMin = (-90 + ((this.y + 1) * tileDegrees)); var latMax = (-90 + (this.y * tileDegrees)); var lngMin = ((this.x * tileDegrees) - 180.0); var lngMax = (((this.x + 1) * tileDegrees) - 180.0); var tileDegreesX = lngMax - lngMin; var tileDegreesY = latMax - latMin; // Create a vertex buffer var verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) int x, y; var textureStep = 1.0f / SubDivisions; for (y = 0; y <= SubDivisions; y++) { if (y != SubDivisions) { lat = latMin + (textureStep * tileDegreesY * y); } else { lat = latMax; } for (x = 0; x <= SubDivisions; x++) { if (x != SubDivisions) { lng = lngMin + (textureStep * tileDegreesX * x); } else { lng = lngMax; } index = y * (SubDivisions + 1) + x; verts[index].Position = GeoTo3d(lat, lng, true);// Add Altitude mapping here verts[index].Normal = GeoTo3d(lat, lng, false); verts[index].Tu = x * textureStep; verts[index].Tv = y * textureStep; verts[index].Lat = lat; verts[index].Lng = lng; } } vb.Unlock(); TriangleCount = (SubDivisions) * (SubDivisions) * 2; var quarterDivisions = SubDivisions / 2; var part = 0; for (var y2 = 0; y2 < 2; y2++) { for (var x2 = 0; x2 < 2; x2++) { var indexArray = (short[])indexBuffer[part].Lock(); index = 0; for (var y1 = (quarterDivisions * y2); y1 < (quarterDivisions * (y2 + 1)); y1++) { for (var x1 = (quarterDivisions * x2); x1 < (quarterDivisions * (x2 + 1)); x1++) { //index = ((y1 * quarterDivisions * 6) + 6 * x1); // First triangle in quad indexArray[index] = (short)(y1 * (SubDivisions + 1) + x1); indexArray[index + 1] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 2] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); indexArray[index + 4] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 5] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); index += 6; } } indexBuffer[part].Unlock(); part++; } } }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { StringBuilder sb = new StringBuilder(); try { double lat, lng; int index = 0; double tileDegrees = 360 / (Math.Pow(2, this.level)); latMin = AbsoluteMetersToLatAtZoom(y * 256, level); latMax = AbsoluteMetersToLatAtZoom((y + 1) * 256, level); lngMin = (((double)this.x * tileDegrees) - 180.0); lngMax = ((((double)(this.x + 1)) * tileDegrees) - 180.0); double latCenter = AbsoluteMetersToLatAtZoom(((y * 2) + 1) * 256, level + 1); double lngCenter = (lngMin / lngMax) / 2; demIndex = 0; tileDegrees = lngMax - lngMin; // Create a vertex buffer PositionNormalTexturedX2[] verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) double dGrid = (tileDegrees / SubDivisions); int x1, y1; double textureStep = 1.0f / SubDivisions; double latDegrees = latMax - latCenter; demIndex = 0; for (y1 = 0; y1 < SubDivisions / 2; y1++) { if (y1 != SubDivisions / 2) { //lat = latMax - (2 * textureStep * latDegrees * (double)y1); lat = AbsoluteMetersToLatAtZoom(((y + 1) * 256) - y1 * 8, level); } else { lat = latCenter; } for (x1 = 0; x1 <= SubDivisions; x1++) { if (x1 != SubDivisions) { lng = lngMin + (textureStep * tileDegrees * (double)x1); } else { lng = lngMax; } index = y1 * (SubDivisions + 1) + x1; verts[index].Position = GeoTo3dWithAltitude(lat, lng, true);// Add Altitude mapping here verts[index].Normal = GeoTo3dWithAltitude(lat, lng, false); verts[index].Tu = (float)(x1 * textureStep) + .002f; verts[index].Lat = lat; verts[index].Lng = lng; // For top levels the geometry is vastly out of scale for v so we adjust with real Lat calculation, otherwise // simple linear interpolation will do verts[index].Tv = (float)((AbsoluteLatToMetersAtZoom(lat, level) - (y * 256)) / 256f) + .002f; sb.Append(verts[index].Tv.ToString() + ", " + verts[index].Tu.ToString() + "\n"); demIndex++; } } latDegrees = latMin - latCenter; for (y1 = SubDivisions / 2; y1 <= SubDivisions; y1++) { if (y1 != SubDivisions) { //lat = latCenter + (2 * textureStep * latDegrees * (double)(y1 - (SubDivisions / 2))); lat = AbsoluteMetersToLatAtZoom(((y + 1) * 256) - y1 * 8, level); } else { lat = latMin; } for (x1 = 0; x1 <= SubDivisions; x1++) { if (x1 != SubDivisions) { lng = lngMin + (textureStep * tileDegrees * (double)x1); } else { lng = lngMax; } index = y1 * (SubDivisions + 1) + x1; verts[index].Position = GeoTo3dWithAltitude(lat, lng, true);// Add Altitude mapping here verts[index].Normal = GeoTo3dWithAltitude(lat, lng, false); verts[index].Tu = (float)(x1 * textureStep) + .002f; verts[index].Lat = lat; verts[index].Lng = lng; // For top levels the geometry is vastly out of scale for v so we adjust with real Lat calculation, otherwise // simple linear interpolation will do verts[index].Tv = (float)((AbsoluteLatToMetersAtZoom(lat, level) - (y * 256)) / 256f) + .002f; sb.Append(verts[index].Tv.ToString() + ", " + verts[index].Tu.ToString() + "\n"); demIndex++; } } if (Y == 0) { // Send the tops to the pole to fill in the Bing Hole y1 = SubDivisions; for (x1 = 0; x1 <= SubDivisions; x1++) { index = y1 * (SubDivisions + 1) + x1; verts[index].Position = new Vector3d(0, 1, 0);// Add Altitude mapping here verts[index].Normal = new Vector3d(0, 1, 0); } } if (Y == Math.Pow(2, level) - 1) { // Send the tops to the pole to fill in the Bing Hole y1 = 0; for (x1 = 0; x1 <= SubDivisions; x1++) { index = y1 * (SubDivisions + 1) + x1; verts[index].Position = new Vector3d(0, -1, 0);// Add Altitude mapping here verts[index].Normal = new Vector3d(0, -1, 0); } } vb.Unlock(); #region createIndex TriangleCount = (SubDivisions) * (SubDivisions) * 2; //bool flipFlop = false; //int quarterDivisions = SubDivisions / 2; //int part = 0; //for (int y2 = 0; y2 < 2; y2++) //{ // for (int x2 = 0; x2 < 2; x2++) // { // short[] indexArray = (short[])this.indexBuffer[part].Lock(0, LockFlags.None); // index = 0; // for (y1 = (quarterDivisions * y2); y1 < (quarterDivisions * (y2 + 1)); y1++) // { // for (x1 = (quarterDivisions * x2); x1 < (quarterDivisions * (x2 + 1)); x1++) // { // if (flipFlop) // { // //index = ((y1 * quarterDivisions * 6) + 6 * x1); // // First triangle in quad // indexArray[index] = (short)(y1 * (SubDivisions + 1) + x1); // indexArray[index + 2] = (short)((y1 + 1) * (SubDivisions + 1) + x1); // indexArray[index + 1] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // // Second triangle in quad // indexArray[index + 3] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // indexArray[index + 5] = (short)((y1 + 1) * (SubDivisions + 1) + x1); // indexArray[index + 4] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); // index += 6; // } // else // { // //index = ((y1 * quarterDivisions * 6) + 6 * x1); // // First triangle in quad // indexArray[index] = (short)(y1 * (SubDivisions + 1) + x1); // indexArray[index + 2] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); // indexArray[index + 1] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // // Second triangle in quad // indexArray[index + 3] = (short)(y1 * (SubDivisions + 1) + (x1 )); // indexArray[index + 5] = (short)((y1 + 1) * (SubDivisions + 1) + x1); // indexArray[index + 4] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); // index += 6; // } // flipFlop = !flipFlop; // } // flipFlop = !flipFlop; // } // this.indexBuffer[part].Unlock(); // part++; // } //} #endregion string data = sb.ToString(); // LoadMeshFile(); } catch { } }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { indexBuffer[0] = new IndexBuffer11(typeof(short), subDivisionsX * subDivisionsY * 6, RenderContext11.PrepDevice); double lat, lng; int index = 0; // double tileDegrees = 360; double latMin = 90; double latMax = -90; double lngMin = -180; double lngMax = 180; // Create a vertex buffer PositionNormalTexturedX2[] verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) int x1, y1; double latDegrees = latMax - latMin; double lngDegrees = lngMax - lngMin; double textureStepX = 1.0f / subDivisionsX; double textureStepY = 1.0f / subDivisionsY; for (y1 = 0; y1 <= subDivisionsY; y1++) { if (y1 != subDivisionsY) { lat = latMax - (textureStepY * latDegrees * (double)y1); } else { lat = latMin; } for (x1 = 0; x1 <= subDivisionsX; x1++) { if (x1 != subDivisionsX) { lng = lngMin + (textureStepX * lngDegrees * (double)x1); } else { lng = lngMax; } index = y1 * (subDivisionsX + 1) + x1; verts[index].Position = GeoTo3d(lat, lng, false);// Add Altitude mapping here verts[index].Normal = verts[index].Position; if (domeMaster) { double dist = (90-lat) / 180; verts[index].Tu = (float)(.5 +Math.Sin((lng+180)/180*Math.PI)*dist); verts[index].Tv = (float)(.5 + Math.Cos((lng+180) / 180 * Math.PI) * dist); } else { verts[index].Tu = (float)(x1 * textureStepX); verts[index].Tv = (float)(1f - (y1 * textureStepY)); } } } vb.Unlock(); TriangleCount = (subDivisionsX) * (subDivisionsY) * 2; short[] indexArray = (short[])this.indexBuffer[0].Lock(); for (y1 = 0; y1 < subDivisionsY; y1++) { if (!(domeMaster && y1 < subDivisionsY / 2)) { for (x1 = 0; x1 < subDivisionsX; x1++) { index = (y1 * subDivisionsX * 6) + 6 * x1; // First triangle in quad indexArray[index] = (short)(y1 * (subDivisionsX + 1) + x1); indexArray[index + 2] = (short)((y1 + 1) * (subDivisionsX + 1) + x1); indexArray[index + 1] = (short)(y1 * (subDivisionsX + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (short)(y1 * (subDivisionsX + 1) + (x1 + 1)); indexArray[index + 5] = (short)((y1 + 1) * (subDivisionsX + 1) + x1); indexArray[index + 4] = (short)((y1 + 1) * (subDivisionsX + 1) + (x1 + 1)); } } } this.indexBuffer[0].Unlock(); }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { indexBuffer[0] = new IndexBuffer11(typeof(short), subDivisionsX * subDivisionsY * 6, RenderContext11.PrepDevice); double lat, lng; int index = 0; // double tileDegrees = 360; double latMin = 90; double latMax = -90; double lngMin = -180; double lngMax = 180; // Create a vertex buffer PositionNormalTexturedX2[] verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) int x1, y1; double latDegrees = latMax - latMin; double lngDegrees = lngMax - lngMin; double textureStepX = 1.0f / subDivisionsX; double textureStepY = 1.0f / subDivisionsY; for (y1 = 0; y1 <= subDivisionsY; y1++) { if (y1 != subDivisionsY) { lat = latMax - (textureStepY * latDegrees * (double)y1); } else { lat = latMin; } for (x1 = 0; x1 <= subDivisionsX; x1++) { if (x1 != subDivisionsX) { lng = lngMin + (textureStepX * lngDegrees * (double)x1); } else { lng = lngMax; } index = y1 * (subDivisionsX + 1) + x1; verts[index].Position = GeoTo3d(lat, lng, false);// Add Altitude mapping here verts[index].Normal = verts[index].Position; if (domeMaster) { double dist = (90 - lat) / 180; verts[index].Tu = (float)(.5 + Math.Sin((lng + 180) / 180 * Math.PI) * dist); verts[index].Tv = (float)(.5 + Math.Cos((lng + 180) / 180 * Math.PI) * dist); } else { verts[index].Tu = (float)(x1 * textureStepX); verts[index].Tv = (float)(1f - (y1 * textureStepY)); } } } vb.Unlock(); TriangleCount = (subDivisionsX) * (subDivisionsY) * 2; short[] indexArray = (short[])this.indexBuffer[0].Lock(); for (y1 = 0; y1 < subDivisionsY; y1++) { if (!(domeMaster && y1 < subDivisionsY / 2)) { for (x1 = 0; x1 < subDivisionsX; x1++) { index = (y1 * subDivisionsX * 6) + 6 * x1; // First triangle in quad indexArray[index] = (short)(y1 * (subDivisionsX + 1) + x1); indexArray[index + 2] = (short)((y1 + 1) * (subDivisionsX + 1) + x1); indexArray[index + 1] = (short)(y1 * (subDivisionsX + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (short)(y1 * (subDivisionsX + 1) + (x1 + 1)); indexArray[index + 5] = (short)((y1 + 1) * (subDivisionsX + 1) + x1); indexArray[index + 4] = (short)((y1 + 1) * (subDivisionsX + 1) + (x1 + 1)); } } } this.indexBuffer[0].Unlock(); }
public override void OnCreateVertexBuffer(VertexBuffer11 vb) { var sb = new StringBuilder(); try { double lat, lng; var index = 0; var tileDegrees = 360 / (Math.Pow(2, level)); latMin = AbsoluteMetersToLatAtZoom(y * 256, level); latMax = AbsoluteMetersToLatAtZoom((y + 1) * 256, level); lngMin = ((x * tileDegrees) - 180.0); lngMax = (((x + 1) * tileDegrees) - 180.0); var latCenter = AbsoluteMetersToLatAtZoom(((y * 2) + 1) * 256, level + 1); var lngCenter = (lngMin / lngMax) / 2; demIndex = 0; tileDegrees = lngMax - lngMin; // Create a vertex buffer var verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) var dGrid = (tileDegrees / SubDivisions); int x1, y1; double textureStep = 1.0f / SubDivisions; var latDegrees = latMax - latCenter; demIndex = 0; for (y1 = 0; y1 < SubDivisions / 2; y1++) { if (y1 != SubDivisions / 2) { //lat = latMax - (2 * textureStep * latDegrees * (double)y1); lat = AbsoluteMetersToLatAtZoom(((y + 1) * 256) - y1 * 8, level); } else { lat = latCenter; } for (x1 = 0; x1 <= SubDivisions; x1++) { if (x1 != SubDivisions) { lng = lngMin + (textureStep * tileDegrees * x1); } else { lng = lngMax; } index = y1 * (SubDivisions + 1) + x1; verts[index].Position = GeoTo3dWithAltitude(lat, lng, true);// Add Altitude mapping here verts[index].Normal = GeoTo3dWithAltitude(lat, lng, false); verts[index].Tu = (float)(x1 * textureStep) + .002f; verts[index].Lat = lat; verts[index].Lng = lng; // For top levels the geometry is vastly out of scale for v so we adjust with real Lat calculation, otherwise // simple linear interpolation will do verts[index].Tv = (float)((AbsoluteLatToMetersAtZoom(lat, level) - (y * 256)) / 256f) + .002f; sb.Append(verts[index].Tv + ", " + verts[index].Tu + "\n"); demIndex++; } } latDegrees = latMin - latCenter; for (y1 = SubDivisions / 2; y1 <= SubDivisions; y1++) { if (y1 != SubDivisions) { //lat = latCenter + (2 * textureStep * latDegrees * (double)(y1 - (SubDivisions / 2))); lat = AbsoluteMetersToLatAtZoom(((y + 1) * 256) - y1 * 8, level); } else { lat = latMin; } for (x1 = 0; x1 <= SubDivisions; x1++) { if (x1 != SubDivisions) { lng = lngMin + (textureStep * tileDegrees * x1); } else { lng = lngMax; } index = y1 * (SubDivisions + 1) + x1; verts[index].Position = GeoTo3dWithAltitude(lat, lng, true);// Add Altitude mapping here verts[index].Normal = GeoTo3dWithAltitude(lat, lng, false); verts[index].Tu = (float)(x1 * textureStep) + .002f; verts[index].Lat = lat; verts[index].Lng = lng; // For top levels the geometry is vastly out of scale for v so we adjust with real Lat calculation, otherwise // simple linear interpolation will do verts[index].Tv = (float)((AbsoluteLatToMetersAtZoom(lat, level) - (y * 256)) / 256f) + .002f; sb.Append(verts[index].Tv + ", " + verts[index].Tu + "\n"); demIndex++; } } if (Y == 0) { // Send the tops to the pole to fill in the Bing Hole y1 = SubDivisions; for (x1 = 0; x1 <= SubDivisions; x1++) { index = y1 * (SubDivisions + 1) + x1; verts[index].Position = new Vector3d(0, 1, 0);// Add Altitude mapping here verts[index].Normal = new Vector3d(0, 1, 0); } } if (Y == Math.Pow(2, level)-1) { // Send the tops to the pole to fill in the Bing Hole y1 = 0; for (x1 = 0; x1 <= SubDivisions; x1++) { index = y1 * (SubDivisions + 1) + x1; verts[index].Position = new Vector3d(0, -1, 0);// Add Altitude mapping here verts[index].Normal = new Vector3d(0, -1, 0); } } vb.Unlock(); #region createIndex TriangleCount = (SubDivisions) * (SubDivisions) * 2; //bool flipFlop = false; //int quarterDivisions = SubDivisions / 2; //int part = 0; //for (int y2 = 0; y2 < 2; y2++) //{ // for (int x2 = 0; x2 < 2; x2++) // { // short[] indexArray = (short[])this.indexBuffer[part].Lock(0, LockFlags.None); // index = 0; // for (y1 = (quarterDivisions * y2); y1 < (quarterDivisions * (y2 + 1)); y1++) // { // for (x1 = (quarterDivisions * x2); x1 < (quarterDivisions * (x2 + 1)); x1++) // { // if (flipFlop) // { // //index = ((y1 * quarterDivisions * 6) + 6 * x1); // // First triangle in quad // indexArray[index] = (short)(y1 * (SubDivisions + 1) + x1); // indexArray[index + 2] = (short)((y1 + 1) * (SubDivisions + 1) + x1); // indexArray[index + 1] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // // Second triangle in quad // indexArray[index + 3] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // indexArray[index + 5] = (short)((y1 + 1) * (SubDivisions + 1) + x1); // indexArray[index + 4] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); // index += 6; // } // else // { // //index = ((y1 * quarterDivisions * 6) + 6 * x1); // // First triangle in quad // indexArray[index] = (short)(y1 * (SubDivisions + 1) + x1); // indexArray[index + 2] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); // indexArray[index + 1] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // // Second triangle in quad // indexArray[index + 3] = (short)(y1 * (SubDivisions + 1) + (x1 )); // indexArray[index + 5] = (short)((y1 + 1) * (SubDivisions + 1) + x1); // indexArray[index + 4] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); // index += 6; // } // flipFlop = !flipFlop; // } // flipFlop = !flipFlop; // } // this.indexBuffer[part].Unlock(); // part++; // } //} #endregion var data = sb.ToString(); // LoadMeshFile(); } catch { } }
public void OnCreateVertexBufferBottomsUp(VertexBuffer11 vb) { double lat, lng; int index = 0; float tileDegrees = (float)this.dataset.BaseTileDegrees / ((float)Math.Pow(2, this.level)); float latMin = (float)((-90 + (((double)(this.y + 1)) * tileDegrees)) + dataset.OffsetY); float latMax = (float)((-90 + (((double)this.y) * tileDegrees)) + dataset.OffsetY); float lngMin = (float)((((double)this.x * tileDegrees) - 180.0) + dataset.OffsetX); float lngMax = (float)(((((double)(this.x + 1)) * tileDegrees) - 180.0) + dataset.OffsetX); float tileDegreesX = lngMax - lngMin; float tileDegreesY = latMax - latMin; // Create a vertex buffer PositionNormalTexturedX2[] verts = (PositionNormalTexturedX2[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) int x, y; float textureStep = 1.0f / SubDivisions; for (y = 0; y <= SubDivisions; y++) { if (y != SubDivisions) { lat = latMin + (textureStep * tileDegreesY * y); } else { lat = latMax; } for (x = 0; x <= SubDivisions; x++) { if (x != SubDivisions) { lng = lngMin + (textureStep * tileDegreesX * x); } else { lng = lngMax; } index = y * (SubDivisions + 1) + x; verts[index].Position = GeoTo3d(lat, lng, true);// Add Altitude mapping here verts[index].Normal = GeoTo3d(lat, lng, false); verts[index].Tu = x * textureStep; verts[index].Tv = y * textureStep; } } vb.Unlock(); TriangleCount = (SubDivisions) * (SubDivisions) * 2; int quarterDivisions = SubDivisions / 2; int part = 0; for (int y2 = 0; y2 < 2; y2++) { for (int x2 = 0; x2 < 2; x2++) { short[] indexArray = (short[])this.indexBuffer[part].Lock(); index = 0; for (int y1 = (quarterDivisions * y2); y1 < (quarterDivisions * (y2 + 1)); y1++) { for (int x1 = (quarterDivisions * x2); x1 < (quarterDivisions * (x2 + 1)); x1++) { //index = ((y1 * quarterDivisions * 6) + 6 * x1); // First triangle in quad indexArray[index] = (short)(y1 * (SubDivisions + 1) + x1); indexArray[index + 1] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 2] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (short)(y1 * (SubDivisions + 1) + (x1 + 1)); indexArray[index + 4] = (short)((y1 + 1) * (SubDivisions + 1) + x1); indexArray[index + 5] = (short)((y1 + 1) * (SubDivisions + 1) + (x1 + 1)); index += 6; } } this.indexBuffer[part].Unlock(); part++; } } }