Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ImageSharp加载的纹理显示白色(opentk)_C#_.net Core_Cross Platform_Opentk_Imagesharp - Fatal编程技术网

C# 使用ImageSharp加载的纹理显示白色(opentk)

C# 使用ImageSharp加载的纹理显示白色(opentk),c#,.net-core,cross-platform,opentk,imagesharp,C#,.net Core,Cross Platform,Opentk,Imagesharp,在ImageSharp的帮助下加载图像,然后将其加载到vram中后,它只渲染一个白色四边形。我基本上不知道我会做错什么,这是第一次使用opentk跨平台而不是abel使用BitmapData 纹理类别: public struct Texture2D { public readonly int Handle; public readonly int Width; public readonly int Height; public Texture2D(int[]

在ImageSharp的帮助下加载图像,然后将其加载到vram中后,它只渲染一个白色四边形。我基本上不知道我会做错什么,这是第一次使用opentk跨平台而不是abel使用
BitmapData

纹理类别:

public struct Texture2D
{
    public readonly int Handle;
    public readonly int Width;
    public readonly int Height;

    public Texture2D(int[] data, int width, int height)
    {
        Width = width;
        Height = height;

        GL.Enable(EnableCap.Texture2D);
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
        Handle = GL.GenTexture();
        GL.BindTexture(TextureTarget.Texture2D, Handle);

        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

        unsafe
        {
            fixed (int* dataptr = data)
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, (IntPtr)dataptr);
            GL.BindTexture(TextureTarget.Texture2D, 0);
        }

        GL.Disable(EnableCap.Texture2D);
    }

    public static implicit operator int(Texture2D texture) => texture.Handle;

    public static Texture2D FromFile(string file) => FromImage(Image.Load(Path.Combine("resources", file)));

    public static Texture2D FromImage(Image<Rgba32> image)
    {
        var xL = image.Width;
        var yL = image.Height;
        var data = new int[image.Width * image.Height];
        for (var x = 0; x < xL; x++)
            for (var y = 0; y < yL; y++)
                data[y * xL + x] = image[x, y].R << 24 | image[x, y].G << 16 | image[x, y].B << 8 | image[x, y].A;
        return new Texture2D(data, image.Width, image.Height);
    }
}
loadingTex
是引用纹理的静态变量

这只是.net内核,它适用于.net框架,但有更好的解决方案

经过大量的研究,我找到了答案

简短的回答是:
GL.TexImage2D
接受图像中所有颜色的数组

答案很长:

  • 从图像中获取这样的数组
  • 嗯,实际上这很容易。但是这需要你使用SixLabors的ImageSharp库,但是如果你找到一个类似的库,你可以随意使用它

    第一步是,将图像加载到as
    image
    中。 然后需要将图像数据放入1D(int)数组中。每个int的结构应与此ABGR类似

    例如:

    var xL = image.Width;
    var yL = image.Height;
    var data = new int[image.Width * image.Height];
    for (var x = 0; x < xL; x++)
        for (var y = 0; y < yL; y++)
        {
            var color = image[x, y];
            data[y * xL + x] = (color.A << 24) | (color.B << 16) | (color.G << 8) | (color.R << 0);
        }
    
    这个声明需要一个主体

    这一个将其加载到VRam中。让我们跳过前两个参数,对于这个问题,我不需要解释它们。第三个参数
    PixelInternalFormat.Rgba
    告诉gpu如何将每个像素存储到V-Ram中。接下来的2个是告诉gpu图像的尺寸。论点6目前并不重要。下一个参数
    PixelFormat.Rgba
    告诉gpu你传递像素的格式。
    现在我们有了PixelType.UnsignedByte什么告诉gpu像素的一部分有多大(红色、蓝色、绿色和Alpha都是像素的一部分),而不是像素本身的大小。最后,将数据以IntPtr的形式传递给它自己。基本上,这是一个可以传递给本机库的通用指针。

    您可能想了解Veldrid是如何做到这一点的。另外,在调用GL.begin之后,永远不要绑定纹理。你在这里做了很多不需要的额外工作
    Rgba32
    已正确排序,您可以使用
    Marshal
    类直接访问像素范围,而不是创建阵列。
    var xL = image.Width;
    var yL = image.Height;
    var data = new int[image.Width * image.Height];
    for (var x = 0; x < xL; x++)
        for (var y = 0; y < yL; y++)
        {
            var color = image[x, y];
            data[y * xL + x] = (color.A << 24) | (color.B << 16) | (color.G << 8) | (color.R << 0);
        }
    
    fixed (int* dataptr = data)
    
    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr) dataptr);