我正在使用ImageSharp和OpenTk
在dotnet core 2.0中构建一个opengl应用程序当我用指向我的图像数据的指针调用GL.TexImage2D
时,dotnet运行时崩溃
如何解决或解决此问题?
using OpenTK.Graphics.OpenGL4;
using System;
using ImageSharp;
namespace Processing.OpenTk.Core.Textures
{
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;
Handle = GL.GenTexture();
GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, Handle);
unsafe
{
fixed (int* dataptr = &data[0])
{
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Bgra, PixelType.Int, (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(file));
public static Texture2d FromImage(Image<Rgba32> image)
{
var pixels = new int[image.Pixels.Length];
int[] data = new int[image.Pixels.Length];
for (int i = 0; i < image.Pixels.Length; i++)
{
data[i] = image.Pixels[i].R << 24 | image.Pixels[i].G << 16 | image.Pixels[i].B << 8 | image.Pixels[i].A;
}
return new Texture2d(data, image.Width, image.Height);
}
}
}