流程" dotnet"创建OpenTK纹理时崩溃

时间:2017-09-02 01:04:47

标签: c# opengl .net-core textures opentk

我正在使用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);
        }
    }    
}

0 个答案:

没有答案