0

我试图让 OpenGL 中的四边形有纹理,但我的尝试是徒劳的。我的四边形只显示为白色。这是代码:

using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpGL;
using System.Drawing.Imaging;
using System.Threading;

namespace Project
{
    public static class Render
    {
        private static List<Sprite> sprites = new List<Sprite>();
        private static Bitmap imageStorage = null;
        private static BitmapData imgDataStorage;
        private static uint[] imageTexArray = new uint[9999];

        public static int SpriteCount
        {
            get {return sprites.Count;}
        }
        public static void AddSprite(Sprite sprite)
        {
            sprites.Add(sprite);
            sprite.indexNum = imageTexArray.Length-1;
            Thread.Yield();
            OpenGL gl = Program.mainWin.openGLControl.OpenGL;
            //  Load the identity matrix.
            gl.LoadIdentity();
            imageStorage = (Bitmap)sprite.sprite;
            imgDataStorage = imageStorage.LockBits(new Rectangle(0, 0, imageStorage.Width, imageStorage.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            imageStorage.UnlockBits(imgDataStorage);

            gl.GenTextures(1, imageTexArray); // Create a texture to generate to
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, imageTexArray[sprite.indexNum]);
            gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGB8, imageStorage.Width, imageStorage.Height, 0, OpenGL.GL_BGR_EXT, OpenGL.GL_UNSIGNED_BYTE, imgDataStorage.Scan0);
            float[] array = new float[] { OpenGL.GL_NEAREST };
            // Assign some texure parameters
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, array);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, array);
            Console.WriteLine("IMPICKLERICKWUBADUBADUBDUBREEEEE Aka A sprite was added");
        }
        public static void RenderSprites()
        {
            OpenGL gl = Program.mainWin.openGLControl.OpenGL;
            //  Clear the color and depth buffer.
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
            gl.LoadIdentity();
            foreach (Sprite s in sprites)
            {
                //  Draw the sprite
                gl.Enable(OpenGL.GL_TEXTURE_2D);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, imageTexArray[s.indexNum]);
                gl.Color(1.0, 1.0f, 1.0f, 1.0f);
                gl.Begin(OpenGL.GL_QUADS);
                gl.TexCoord(1, 0); gl.Vertex(s.position.X + s.sprite.Width/2, s.position.Y - s.sprite.Height/2); // Bottom Right
                gl.TexCoord(0, 0); gl.Vertex(s.position.X - s.sprite.Width/2, s.position.Y - s.sprite.Height/2); // Bottom Left
                gl.TexCoord(0, 1); gl.Vertex(s.position.X - s.sprite.Width/2, s.position.Y + s.sprite.Height/2); // Top Left
                gl.TexCoord(1, 1); gl.Vertex(s.position.X + s.sprite.Width/2, s.position.Y + s.sprite.Height/2); // Top Right
                // etc!
                gl.End();
                gl.Disable(OpenGL.GL_TEXTURE_2D);
            }
        }

    }
    public class Sprite
    {
        /// <summary>
        /// The sprite
        /// </summary>
        public Image sprite;
        /// <summary>
        /// The directory to the sprite
        /// </summary>
        public string spritePath;
        public int renderNumber;
        public int indexNum = 0;
        public Vector position;
        public Sprite(string path)
        {
            sprite = Image.FromFile(path);
            if (sprite == null) throw new DirectoryNotFoundException("Sprite could not be found");
            spritePath = path;
            renderNumber = Render.SpriteCount;
            position = Vector.Zero;
            Render.AddSprite(this);
            Init();
        }
        public Sprite(string path, Vector pos)
        {
            sprite = Image.FromFile(path);
            if (sprite == null) throw new DirectoryNotFoundException("Sprite could not be found");
            spritePath = path;
            renderNumber = Render.SpriteCount;
            position = pos;
            Render.AddSprite(this);
            Init();
        }
        private void Init()
        {
        }
    }
}
4

0 回答 0