private unsafe void CreateDeviceObjects() { IO io = ImGui.GetIO(); // Build texture atlas FontTextureData texData = io.FontAtlas.GetTexDataAsAlpha8(); // Create OpenGL texture s_fontTexture = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, s_fontTexture); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Alpha, texData.Width, texData.Height, 0, PixelFormat.Alpha, PixelType.UnsignedByte, new IntPtr(texData.Pixels)); // Store the texture identifier in the ImFontAtlas substructure. io.FontAtlas.SetTexID(s_fontTexture); // Cleanup (don't clear the input data if you want to append new fonts later) //io.Fonts->ClearInputData(); io.FontAtlas.ClearTexData(); GL.BindTexture(TextureTarget.Texture2D, 0); }
/// <summary> /// Recreates the device texture used to render text. /// </summary> public unsafe void RecreateFontDeviceTexture(GraphicsDevice gd) { IO io = ImGui.GetIO(); // Build FontTextureData textureData = io.FontAtlas.GetTexDataAsRGBA32(); // Store our identifier io.FontAtlas.SetTexID(_fontAtlasID); _fontTexture = gd.ResourceFactory.CreateTexture(TextureDescription.Texture2D( (uint)textureData.Width, (uint)textureData.Height, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.Sampled)); _fontTexture.Name = "ImGui.NET Font Texture"; gd.UpdateTexture( _fontTexture, (IntPtr)textureData.Pixels, (uint)(textureData.BytesPerPixel * textureData.Width * textureData.Height), 0, 0, 0, (uint)textureData.Width, (uint)textureData.Height, 1, 0, 0); _fontTextureView = gd.ResourceFactory.CreateTextureView(_fontTexture); io.FontAtlas.ClearTexData(); }
/// <summary> /// 保存字符集的图片到左面 /// </summary> public unsafe void SaveFontText() { IO io = ImGui.GetIO(); FontTextureData textureData = io.FontAtlas.GetTexDataAsRGBA32(); Rgba32[] pixelData = new Rgba32[textureData.Width * textureData.Height]; var data = textureData.Pixels; var datalenfth = textureData.BytesPerPixel; int offset = 0; for (int y = 0; y < textureData.Height; y++) { for (int x = 0; x < textureData.Width; x++) { int index = (int)(y * textureData.Width + x); //连续读取指定长度的 pixelData[index] = new Rgba32(data[offset], data[offset++], data[offset++], data[offset++]); } } //textureData. var image = SixLabors.ImageSharp.Image.LoadPixelData <Rgba32>(pixelData, textureData.Width, textureData.Height); var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "123.jpg"); image.Save(path); //释放相关变量 io.FontAtlas.ClearTexData(); image.Dispose(); }
private unsafe void CreateDeviceObjects() { IO io = ImGui.GetIO(); // Build texture atlas FontTextureData texData = io.FontAtlas.GetTexDataAsRGBA32(); // Create DirectX Texture fontTexture = new Texture2D(Device, new Texture2DDescription() { Width = texData.Width, Height = texData.Height, MipLevels = 1, ArraySize = 1, Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 1), Usage = ResourceUsage.Default, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = 0 }, new SharpDX.DataRectangle(new IntPtr(texData.Pixels), texData.Width)); fontTextureView = new ShaderResourceView(Device, fontTexture, new ShaderResourceViewDescription() { Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Texture2D = new ShaderResourceViewDescription.Texture2DResource() { MipLevels = 1, MostDetailedMip = 0, } }); io.FontAtlas.SetTexID(FontTextureId); // Create texture sampler fontSampler = new SamplerState(Device, new SamplerStateDescription() { Filter = Filter.MinMagMipLinear, AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, MipLodBias = 0.0f, ComparisonFunction = Comparison.Always, MinimumLod = 0.0f, MaximumLod = 0.0f }); // Compile Shader var vertexShaderByteCode = ShaderBytecode.Compile(vertexShaderCode, "vs_4_0", ShaderFlags.None, EffectFlags.None); var vertexShader = new VertexShader(Device, vertexShaderByteCode); var pixelShaderByteCode = ShaderBytecode.Compile(pixelShaderCode, "ps_4_0", ShaderFlags.None, EffectFlags.None); var pixelShader = new PixelShader(Device, pixelShaderByteCode); inputLayout = new InputLayout(Device, ShaderSignature.GetInputSignature(vertexShaderByteCode), new[] { new InputElement("POSITION", 0, Format.R32G32_Float, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 1), new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 2) }); vertexConstantBuffer = new Buffer(Device, SharpDX.Utilities.SizeOf <SharpDX.Matrix>(), ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0); // Create the blending setup var blendStateDesc = new BlendStateDescription(); blendStateDesc.AlphaToCoverageEnable = true; blendStateDesc.RenderTarget[0].IsBlendEnabled = true; blendStateDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.SourceAlpha; blendStateDesc.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha; blendStateDesc.RenderTarget[0].BlendOperation = BlendOperation.Add; blendStateDesc.RenderTarget[0].SourceBlend = BlendOption.InverseSourceAlpha; blendStateDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero; blendStateDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add; blendStateDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; blendState = new BlendState(Device, blendStateDesc); // Create the rasterizer state rasterizerState = new RasterizerState(Device, new RasterizerStateDescription() { FillMode = FillMode.Solid, CullMode = CullMode.None, IsScissorEnabled = true, IsDepthClipEnabled = true, }); // Create depth-stencil State depthStencilState = new DepthStencilState(Device, new DepthStencilStateDescription() { IsDepthEnabled = true, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.Always, IsStencilEnabled = false, FrontFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Keep, PassOperation = StencilOperation.Keep }, BackFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Keep, PassOperation = StencilOperation.Keep } }); }