public void ExtenSize(IDevice device, int vsize) { if (!CheckBufferExten(vsize)) { return; } int newvsize = m_vertCount * m_scaleTime; while (newvsize < vsize) { newvsize *= m_scaleTime; } m_buffer.Dispose(); m_vertCount = newvsize; m_bufferSize = GetIndicesCount(m_vertCount); m_desc.SizeInByte = m_bufferSize * sizeof(uint); m_buffer = device.CreateBuffer(m_desc, CreateDataArray(m_vertCount), m_buffer); Console.WriteLine($"exten buffer to:{m_vertCount} - {m_bufferSize}"); }
private void DrawFlat(GraphicsDevice graphicsDevice, SectorBounds bounds, ISector rootSector) { RenderTarget2D renderTarget = renderTargets[rootSector]; // Set the render target graphicsDevice.SetRenderTarget(renderTarget); graphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true }; graphicsDevice.Clear(Pallete.OCEAN_BLUE); double relativeCameraZoom = camera.cameraZoom - Math.Log(ZCoords.GetSectorManager().GetTopmostOSMSectors().Count, 4) + (Game1.RECORDING ? 1 : 0); int zoomLevel = Math.Min(Math.Max((int)(relativeCameraZoom - 3), 0), ZCoords.GetSectorManager().GetHighestOSMZoom()); List <ISector> containedSectors = rootSector.GetSectorsInRange(bounds.minX, bounds.maxX, bounds.minY, bounds.maxY, zoomLevel); List <ISector> sorted = containedSectors.Where(x => x.GetRoot().Equals(rootSector)).ToList(); sorted.Sort((x, y) => x.Zoom.CompareTo(y.Zoom)); foreach (var sector in sorted) { IGraphicsBuffer buffer = loadedMaps[sector]; if (!(buffer is ImageTileBuffer)) { continue; } Matrixd projection = Matrixd.CreateOrthographicOffCenter(bounds.minX * (1 << sector.Zoom) - sector.X, bounds.maxX * (1 << sector.Zoom) - sector.X, bounds.maxY * (1 << sector.Zoom) - sector.Y, bounds.minY * (1 << sector.Zoom) - sector.Y, -1, 0.01f); // TODO: why negative? RenderContext context = new RenderContext(graphicsDevice, projection, bounds.minX * (1 << sector.Zoom) - sector.X, bounds.maxX * (1 << sector.Zoom) - sector.X, bounds.minY * (1 << sector.Zoom) - sector.Y, bounds.maxY * (1 << sector.Zoom) - sector.Y, camera.cameraZoom, RenderContext.LayerPass.MAIN_PASS); buffer.Draw(context); } }
public void Render(IGraphicsBuffer frame) { ClearConsoleWindow(); SetConsoleWindowFrame(frame); UpdateConsoleWindow(); Renderings++; }
private void RebuildImage(GraphicsDevice graphicsDevice, ISector sector) { // combination images using (Texture2D rendered = new RenderTarget2D(graphicsDevice, 512, 512, false, graphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24)) { int highestZoom = ZCoords.GetSectorManager().GetHighestCacheZoom(); foreach (var parent in sector.GetAllParents().OrderBy(x => - x.Zoom).Where(x => x.Zoom <= highestZoom)) { List <ISector> roadSectors = parent.GetChildrenAtLevel(parent.Zoom == highestZoom - 1 ? ZCoords.GetSectorManager().GetHighestOSMZoom() : parent.Zoom + 1); Texture2D[] textures = new Texture2D[roadSectors.Count]; for (int i = 0; i < roadSectors.Count; i++) { IGraphicsBuffer buffer = null; if (File.Exists(OSMPaths.GetSectorImagePath(roadSectors[i]))) { using (var reader = File.OpenRead(OSMPaths.GetSectorImagePath(roadSectors[i]))) { buffer = new ImageTileBuffer(graphicsDevice, Texture2D.FromStream(graphicsDevice, reader), roadSectors[i]); } } else { throw new NotImplementedException(); } textures[i] = buffer.GetImage(graphicsDevice); } if (textures.Any(x => x != null)) { graphicsDevice.SetRenderTarget((RenderTarget2D)rendered); for (int i = 0; i < roadSectors.Count; i++) { int size, x, y; size = 512 >> (roadSectors[i].Zoom - parent.Zoom); x = parent.GetRelativeXOf(roadSectors[i]) * size; y = parent.GetRelativeYOf(roadSectors[i]) * size; if (textures[i] != null) { GraphicsBasic.DrawSpriteRect(graphicsDevice, x, y, size, size, textures[i], BlendState.AlphaBlend, Microsoft.Xna.Framework.Color.White); } } } for (int i = 0; i < textures.Length; i++) { if (textures[i] != null && textures[i] != GlobalContent.Error) { textures[i].Dispose(); } } SuperSave(rendered, OSMPaths.GetSectorImagePath(parent)); } } }
public GUIGraphicsIndicesBuffer(IDevice device, int vsize = 512) { m_vertCount = vsize; m_bufferSize = GetIndicesCount(vsize); m_desc = new GraphicsBufferDesc(); m_desc.BindFlags = GraphicsBindFlag.IndicesBuffer; m_desc.CPUAccessFlags = GraphicsBufferCPUAccessFlag.None; m_desc.SizeInByte = m_bufferSize * sizeof(uint); m_desc.StructureByteStride = 0; m_desc.Usage = GraphicsResourceUsage.Default; m_buffer = device.CreateBuffer(m_desc, CreateDataArray(vsize)); }
private void SetConsoleWindowFrame(IGraphicsBuffer frame) { if (frame != null) { for (int x = 0; x < frame.Width; x++) { for (int y = 0; y < frame.Height; y++) { _consoleWindow.Set(x, y, frame.CharacterBuffer[x, y], frame.ColorBuffer[x, y]); } } } }
private void Draw3D(GraphicsDevice graphicsDevice, SectorBounds bounds, ISector rootSector, RenderContext.LayerPass layerPass) { double relativeCameraZoom = camera.cameraZoom - Math.Log(ZCoords.GetSectorManager().GetTopmostOSMSectors().Count, 4) + (Game1.RECORDING ? 1 : 0); int zoomLevel = Math.Min(Math.Max((int)(relativeCameraZoom - 3), 0), ZCoords.GetSectorManager().GetHighestOSMZoom()); List <ISector> containedSectors = rootSector.GetSectorsInRange(bounds.minX, bounds.maxX, bounds.minY, bounds.maxY, zoomLevel); List <ISector> sorted = containedSectors.Where(x => x.GetRoot().Equals(rootSector)).ToList(); sorted.Sort((x, y) => x.Zoom.CompareTo(y.Zoom)); foreach (var sector in sorted) { IGraphicsBuffer buffer = loadedMaps[sector]; if (buffer is ImageTileBuffer) { continue; } SectorBounds b = new SectorBounds(bounds.minX * (1 << sector.Zoom) - sector.X, bounds.maxX * (1 << sector.Zoom) - sector.X, bounds.minY * (1 << sector.Zoom) - sector.Y, bounds.maxY * (1 << sector.Zoom) - sector.Y); SectorBounds limitedB = new SectorBounds(Math.Max(0, Math.Min(1, b.minX)), Math.Max(0, Math.Min(1, b.maxX)), Math.Max(0, Math.Min(1, b.minY)), Math.Max(0, Math.Min(1, b.maxY))); BasicEffect basicEffect = new BasicEffect(graphicsDevice); camera.ApplyMatrices(basicEffect); // going to make it easy and assume the shape is perfectly parallel (it's not) // the sector plane is constructed by flattening the visible portion of the sphere, basically Vector3d v1 = sector.ProjectToSphereCoordinates(new Vector2d(limitedB.minX, limitedB.minY)); Vector3d v2 = sector.ProjectToSphereCoordinates(new Vector2d(limitedB.maxX, limitedB.minY)); Vector3d v3 = sector.ProjectToSphereCoordinates(new Vector2d(limitedB.minX, limitedB.maxY)); Vector3d xAxis = (v2 - v1) / (limitedB.maxX - limitedB.minX); Vector3d yAxis = (v3 - v1) / (limitedB.maxY - limitedB.minY); Vector3d start = v1 - xAxis * limitedB.minX - yAxis * limitedB.minY; Vector3d zAxis = start * (xAxis.Length() + yAxis.Length()) / start.Length() / 2; // make this roughly the same length // matrixes copied over Matrixd world = Matrixd.CreateRotationZ(-camera.cameraRotX) * Matrixd.CreateRotationX(camera.cameraRotY); // eh.... think hard on this later double distance = 9 * Math.Pow(0.5, camera.cameraZoom); Matrixd view = CameraMatrixManager.GetWorldViewd(distance); Matrixd projection = CameraMatrixManager.GetWorldProjectiond(distance, graphicsDevice.Viewport.AspectRatio); Matrixd transformMatrix = new Matrixd(xAxis.X, xAxis.Y, xAxis.Z, 0, yAxis.X, yAxis.Y, yAxis.Z, 0, zAxis.X, zAxis.Y, zAxis.Z, 0, start.X, start.Y, start.Z, 1); // turns our local coordinates into 3d spherical coordinates, based on the sector Matrixd WVP = Normalize(transformMatrix * world * view * projection); // combine them all to allow for higher precision RenderContext context = new RenderContext(graphicsDevice, WVP, b.minX, b.maxX, b.minY, b.maxY, camera.cameraZoom, layerPass); buffer.Draw(context); } }
private void InitDraw(GraphicsDevice graphicsDevice, SectorBounds bounds, ISector rootSector) { double relativeCameraZoom = camera.cameraZoom - Math.Log(ZCoords.GetSectorManager().GetTopmostOSMSectors().Count, 4) + (Game1.RECORDING ? 1 : 0); // autoload stuff // TODO: move to update step? int zoomLevel = Math.Min(Math.Max((int)(relativeCameraZoom - 3), 0), ZCoords.GetSectorManager().GetHighestOSMZoom()); List <ISector> containedSectors = rootSector.GetSectorsInRange(bounds.minX, bounds.maxX, bounds.minY, bounds.maxY, zoomLevel); foreach (var pair in loadedMaps.Where(x => AllowUnload(x.Key, rootSector, containedSectors)).ToList()) { loadedMaps[pair.Key].Dispose(); loadedMaps.Remove(pair.Key); } // end autoload stuff if (toLoad != null || Constants.TO_LOAD != null) { if (Constants.TO_LOAD != null) { toLoad = ZCoords.GetSectorManager().FromString(Constants.TO_LOAD); } Stopwatch sw = new Stopwatch(); sw.Start(); foreach (var sector in toLoad.GetChildrenAtLevel(ZCoords.GetSectorManager().GetHighestOSMZoom())) { osmSectorLoader.GetGraphicsBuffer(graphicsDevice, sector).Dispose(); } Console.WriteLine($"Total load time for {toLoad} is {sw.Elapsed.TotalHours} h"); toLoad = null; if (Constants.TO_LOAD != null) { Constants.TERMINATE = true; Constants.TO_LOAD = null; } } bool loadCache = !(relativeCameraZoom - 4 > ZCoords.GetSectorManager().GetHighestOSMZoom()); foreach (var l in containedSectors) { if (loadCache) { if (!loadedMaps.ContainsKey(l)) { loadedMaps[l] = osmSectorLoader.GetCacheBuffer(graphicsDevice, l); } } else { if (!loadedMaps.ContainsKey(l) || loadedMaps[l] is ImageTileBuffer) { if (loadedMaps.ContainsKey(l)) { loadedMaps[l].Dispose(); } loadedMaps[l] = osmSectorLoader.GetGraphicsBuffer(graphicsDevice, l); } } } List <ISector> sorted = containedSectors.Where(x => x.GetRoot().Equals(rootSector)).ToList(); sorted.Sort((x, y) => x.Zoom.CompareTo(y.Zoom)); foreach (var sector in sorted) { IGraphicsBuffer buffer = loadedMaps[sector]; Matrixd projection = Matrixd.CreateOrthographicOffCenter(bounds.minX * (1 << sector.Zoom) - sector.X, bounds.maxX * (1 << sector.Zoom) - sector.X, bounds.maxY * (1 << sector.Zoom) - sector.Y, bounds.minY * (1 << sector.Zoom) - sector.Y, -1, 0.01f); // TODO: why negative? RenderContext context = new RenderContext(graphicsDevice, projection, bounds.minX * (1 << sector.Zoom) - sector.X, bounds.maxX * (1 << sector.Zoom) - sector.X, bounds.minY * (1 << sector.Zoom) - sector.Y, bounds.maxY * (1 << sector.Zoom) - sector.Y, camera.cameraZoom, RenderContext.LayerPass.MAIN_PASS); buffer.InitDraw(context); } }