Example #1
0
        public static Texture1D LoadFromBytes(GraphicsContext graphics, byte[] bytes, int length, TextureParams parameters)
        {
            if (graphics == null)
                throw new ArgumentNullException("graphics");

            if (bytes == null)
                throw new ArgumentNullException("bytes");

            Texture1D texture = new Texture1D(graphics);

            Texture.Initialize(texture, graphics, GLContext.Texture1D, bytes, parameters);

            graphics.GL.TexImage1D(
                GLContext.Texture1D,
                0,
                (int)GLContext.Rgba8,
                length,
                0,
                GLContext.Rgba,
                (int)GLContext.UnsignedByte,
                bytes);

            texture.Length = length;

            return texture;
        }
Example #2
0
        public static Texture2D LoadFromBytes(GraphicsContext graphics, byte[] bytes, int width, int height, TextureParams parameters)
        {
            if (graphics == null)
                throw new ArgumentNullException("graphics");

            if (bytes == null)
                throw new ArgumentNullException("bytes");

            Texture2D texture = new Texture2D(graphics);

            Texture.Initialize(texture, graphics, GLContext.Texture2D, bytes, parameters);

            graphics.GL.TexImage2D(
                GLContext.Texture2D,
                0,
                (int)GLContext.Rgba8,
                width,
                height,
                0,
                GLContext.Rgba,
                (int)GLContext.UnsignedByte,
                bytes);

            texture.Width = width;
            texture.Height = height;

            return texture;
        }
Example #3
0
        public static Texture1D LoadFromFile(GraphicsContext graphics, string fileName, TextureParams parameters)
        {
            if (graphics == null)
                throw new ArgumentNullException("graphics");

            if (fileName == null)
                throw new ArgumentNullException("fileName");

            using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                return LoadFromStream(graphics, file, parameters);
        }
Example #4
0
        public static Texture1D LoadFromStream(GraphicsContext graphics, Stream stream, TextureParams parameters)
        {
            if (graphics == null)
                throw new ArgumentNullException("graphics");

            if (stream == null)
                throw new ArgumentNullException("stream");

            using (Bitmap bitmap = (Bitmap)Bitmap.FromStream(stream))
            {
                byte[] bytes = BitmapHelper.GetBytes(bitmap);
                return LoadFromBytes(graphics, bytes, bitmap.Width * bitmap.Height, parameters);
            }
        }
Example #5
0
        public static Texture LoadFromBytes(GraphicsContext graphics, byte[] bytes, int width, int height, TextureParams parameters)
        {
            if (graphics == null)
                throw new ArgumentNullException("graphics");

            if (bytes == null)
                throw new ArgumentNullException("bytes");

            Texture texture = new Texture(graphics);

            graphics.GL.ActiveTexture(GLContext.Texture0 + texture.Index);
            graphics.GL.BindTexture(GLContext.Texture2D, texture.Handle);

            if (parameters.ColorKey != null)
            {
                for (int i = 0; i < bytes.Length; i += 4)
                {
                    uint pixel = GLHelper.MakePixelRGBA(bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]);

                    if (pixel == parameters.ColorKey.Value.ToRgba())
                        GLHelper.DecomposePixelRGBA(parameters.TransparentPixel.ToRgba(), out bytes[i], out bytes[i + 1], out bytes[i + 2], out bytes[i + 3]);
                }
            }

            graphics.GL.TexParameteri(GLContext.Texture2D, GLContext.TextureMagFilter, (int)parameters.MagFilter);
            graphics.GL.TexParameteri(GLContext.Texture2D, GLContext.TextureMinFilter, (int)parameters.MinFilter);
            graphics.GL.TexParameteri(GLContext.Texture2D, GLContext.TextureWrapS, (int)parameters.WrapS);
            graphics.GL.TexParameteri(GLContext.Texture2D, GLContext.TextureWrapT, (int)parameters.WrapT);

            graphics.GL.TexImage2D(
                GLContext.Texture2D,
                0,
                (int)GLContext.Rgba8,
                width,
                height,
                0,
                GLContext.Rgba,
                (int)GLContext.UnsignedByte,
                bytes);

            texture.Width = width;
            texture.Height = height;

            return texture;
        }