using System.Drawing;
using System.IO;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using System;
namespace RecrownedGTK.Graphics {
///
/// Represents the texture for any given vertice.
/// Contains bytes that form the texture.
///
public class TextureData : IDisposable {
private bool disposed;
int handle;
public readonly int width, height;
public TextureWrapMode textureWrapModeWidth, textureWrapModeHeight;
public Color4 borderColor;
public TextureMinFilter textureMinFilter;
public TextureMagFilter textureMagFilter;
///
/// Generates a texture using a byte array of the texture data.
///
/// The byte array representing the texture.
/// The width of the texture.
/// The height of the texture.
public TextureData(byte[] textureData, int width, int height) {
textureWrapModeHeight = TextureWrapMode.ClampToBorder;
textureWrapModeWidth = TextureWrapMode.ClampToBorder;
textureMinFilter = TextureMinFilter.LinearMipmapLinear;
textureMagFilter = TextureMagFilter.Linear;
this.width = width;
this.height = height;
GenerateTexture(textureData, width, height);
}
///
/// Generates a texture in the OpenGL context.
///
/// Byte array representing the texture.
/// Width of the texture.
/// Height of the texture.
private void GenerateTexture(byte[] textureData, int width, int height) {
if (handle != 0) throw new InvalidOperationException("This texture data already holds a texture.");
handle = GL.GenTexture();
Use();
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, textureData);
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
}
///
/// Binds the OpenGL texture.
///
internal void Use() {
GL.BindTexture(TextureTarget.Texture2D, handle);
}
///
/// Removes texture data from memory.
///
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (disposed) return;
if (disposing) {
}
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.DeleteTexture(handle);
disposed = true;
}
~TextureData() {
Dispose(false);
}
}
}