Изображение Opentk для фона не работает

#c# #opengl #2d #.net-5 #opentk

#c# #opengl #2d #.net-5 #opentk

Вопрос:

Здравствуйте, я пытаюсь повторить leason в opentk для рисования фона. но изображение не загружается, и я вижу только базовый цвет. Texture.cs:Базовый класс для загрузки изображения и перемещения в буфер

  using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using OpenTK.Graphics.OpenGL4; using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;   namespace GameTry1  {  public class Texture : IDisposable  {  public Texture(string filePath)  {  if (!File.Exists(filePath))  {  throw new FileNotFoundException($"File not found at content {filePath}");  }  Bitmap bitmap = new Bitmap(filePath);  bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);  BitmapData bitmapData = bitmap.LockBits(  new Rectangle(0, 0, bitmap.Width, bitmap.Height),  ImageLockMode.ReadOnly,  System.Drawing.Imaging.PixelFormat.Format32bppArgb);   ID = GL.GenTexture();  GL.BindTexture(TextureTarget.Texture2D, ID);  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS,  (int)TextureWrapMode.ClampToBorder);  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT,  (int)TextureWrapMode.ClampToBorder);  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,  (int)TextureMinFilter.LinearMipmapLinear);  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,  (int)TextureMagFilter.Linear);  GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, bitmapData.Scan0);  GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);  GL.BindTexture(TextureTarget.Texture2D, 0);  bitmap.UnlockBits(bitmapData);    Coordinates = new[]  {  0.0f, 1.0f, //UL  1.0f, 1.0f, //UR  1.0f, 0.0f, //DR  0.0f, 0.0f //DL  };  BufferID = GL.GenBuffer();  GL.BindBuffer(BufferTarget.ArrayBuffer, BufferID);  GL.BufferData(BufferTarget.ArrayBuffer, Coordinates.Length * sizeof(float), Coordinates,  BufferUsageHint.StaticDraw);  GL.BindBuffer(BufferTarget.ArrayBuffer, 0);  }   public int ID { get; }  public int BufferID { get; }  public float[] Coordinates { get; }    public void Bind()  {  GL.BindTexture(TextureTarget.Texture2D, ID);  GL.BindBuffer(BufferTarget.ArrayBuffer, BufferID);  }   public void Unbind()  {  GL.BindTexture(TextureTarget.Texture2D, 0);  GL.BindBuffer(BufferTarget.ArrayBuffer, 0);  }   public void Dispose()  {  GL.DeleteBuffer(BufferID);  GL.DeleteTexture(ID);  GC.SuppressFinalize(this);  }  }  }  

Background.cs: Класс для рисования фонового объекта

 using OpenTK.Graphics.OpenGL;  using System;  namespace GameTry1 {  public class Background : IDisposable  {  Texture _texture;  private int _vertexBufferId;  private float[] _vertexData;   public Background(int width, int height, string filePath)  {  _texture = new Texture(filePath);  Resize(width, height);    }    public void Resize(int width, int height)  {  _vertexBufferId = GL.GenBuffer();  _vertexData = new[]  {  0.0f, 0.0f, 0.0f,  width, 0.0f, 0.0f,  width, height, 0.0f,  0.0f, height, 0.0f  };  GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferId);  GL.BufferData(BufferTarget.ArrayBuffer, _vertexData.Length * sizeof(float), _vertexData,  BufferUsageHint.DynamicDraw);  GL.BindBuffer(BufferTarget.ArrayBuffer, 0);  }   public void Draw()  {  EnableStates();  _texture.Bind();  GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferId);  GL.VertexPointer(3, VertexPointerType.Float, 0, 0);  GL.DrawArrays(PrimitiveType.Quads, 0, _vertexData.Length);  GL.BindBuffer(BufferTarget.ArrayBuffer, 0);  _texture.Unbind();  DisableStates();  }   private void EnableStates()  {  GL.EnableClientState(ArrayCap.VertexArray);  GL.EnableClientState(ArrayCap.TextureCoordArray);  }   private void DisableStates()  {  GL.DisableClientState(ArrayCap.VertexArray);  GL.DisableClientState(ArrayCap.TextureCoordArray);  }  public void Dispose()  {  _texture.Dispose();  GL.DeleteBuffer(_vertexBufferId);  GC.SuppressFinalize(this);   }  } }  

GameEngine.cs:

 using System; using GameTry.Common; using OpenTK.Graphics.OpenGL; using OpenTK.Mathematics; using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop; using OpenTK.Windowing.GraphicsLibraryFramework;  namespace GameTry1 {  public sealed class GameEngine : GameWindow  {  Background background;  Matrix4 ortho;   private Shader _shader;  public GameEngine(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings) : base(  gameWindowSettings, nativeWindowSettings)  {  GL.Enable(EnableCap.Texture2D);  GL.Enable(EnableCap.PointSmooth);  GL.Enable(EnableCap.LineSmooth);  GL.Enable(EnableCap.PolygonSmooth);  GL.Enable(EnableCap.Multisample);  GL.Enable(EnableCap.DepthTest);  GL.Enable(EnableCap.AlphaTest);  GL.Enable(EnableCap.CullFace);  GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);  GL.FrontFace(FrontFaceDirection.Cw);  GL.CullFace(CullFaceMode.Back);    background = new Background(Size.X, Size.Y, @"back.png");  }   protected override void OnLoad()  {  base.OnLoad();  GL.ClearColor(0.2f, 0.3f,0.3f,1.0f);  GL.Begin(PrimitiveType.Quads);   }   protected override void OnResize(ResizeEventArgs e)  {  base.OnResize(e);  GL.Viewport(0, 0, Size.X, Size.Y);  ortho = Matrix4.CreateOrthographicOffCenter(0, Size.X, Size.Y, 0, -1, 1);  GL.MatrixMode(MatrixMode.Projection);  GL.LoadMatrix(ref ortho);  GL.MatrixMode(MatrixMode.Modelview);  GL.LoadIdentity();  background.Resize(Size.X, Size.Y);  }   protected override void OnUpdateFrame(FrameEventArgs e)  {  base.OnUpdateFrame(e);  HandleKeybord();  }    private void HandleKeybord()  {  var input = KeyboardState;  if (input.IsKeyDown(Keys.Escape)) Close();  _shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");  _shader.Use();   }    protected override void OnRenderFrame(FrameEventArgs e)  {  base.OnRenderFrame(e);  GL.Clear(ClearBufferMask.ColorBufferBit);  background.Draw();  GL.LoadIdentity();  SwapBuffers();  }  } }  

Program.cs:

 // See https://aka.ms/new-console-template for more information  using OpenTK.Mathematics; using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop;  namespace GameTry1 {  internal class Programm  {  public static void Main(string[] args)  {  var nativeWindowSettings = new NativeWindowSettings  {  Size = new Vector2i(1000, 1000),  Title = "FirstGameEngineTry",  Flags = ContextFlags.Offscreen  };  using (GameEngine gameEngine = new(GameWindowSettings.Default, nativeWindowSettings))  {  gameEngine.VSync = VSyncMode.Adaptive;  gameEngine.Run();  }  }  } }  

Что я хочу нарисовать

Что я получаю

Что я делаю не так? Я использую консольное приложение net 5, а версия opentk-4.6.7