diff --git a/RecrownedGTK/Graphics/IDrawable.cs b/RecrownedGTK/Graphics/IDrawable.cs index 715cc08..1641aa1 100644 --- a/RecrownedGTK/Graphics/IDrawable.cs +++ b/RecrownedGTK/Graphics/IDrawable.cs @@ -1,6 +1,12 @@ +using OpenTK; namespace RecrownedGTK.Graphics { public interface IDrawable { - void Draw(out float[] vertices, out float[] indices, out TextureData textureData); + /// + /// Fills the parameters with the information needed to draw this texture.!-- + /// + /// The vertices in pairs of 3 normalized in the order x, y, and z. + /// The texture data to be used. Can be null. + void Draw(out VertexInformation[] vertices, out TextureData textureData); } } \ No newline at end of file diff --git a/RecrownedGTK/Graphics/Texture.cs b/RecrownedGTK/Graphics/Texture.cs index 561f30e..67fa4ec 100644 --- a/RecrownedGTK/Graphics/Texture.cs +++ b/RecrownedGTK/Graphics/Texture.cs @@ -3,7 +3,7 @@ namespace RecrownedGTK.Graphics { public Texture() { } - public void Draw(out float[] vertices, out float[] indices, out TextureData textureData) { + public void Draw(out VertexInformation[] vertices, out TextureData textureData) { throw new System.NotImplementedException(); //TODO: Implement this. } diff --git a/RecrownedGTK/Graphics/TextureData.cs b/RecrownedGTK/Graphics/TextureData.cs index 5a349ed..88ba50c 100644 --- a/RecrownedGTK/Graphics/TextureData.cs +++ b/RecrownedGTK/Graphics/TextureData.cs @@ -1,35 +1,70 @@ using System.Drawing; using System.IO; using OpenTK.Graphics.OpenGL; +using OpenTK.Graphics; +using System; namespace RecrownedGTK.Graphics { - public class TextureData { + public class TextureData : IDisposable { + private bool disposed; int handle; - byte[] textureData; - public byte[] ColorData { - get { - return textureData; - } - } + + public TextureWrapMode textureWrapModeWidth, textureWrapModeHeight; + public Color4 borderColor; + public TextureMinFilter textureMinFilter; + public TextureMagFilter textureMagFilter; public TextureData(string path) { - LoadFromPNG(path); - } - public TextureData() { - //TODO: Finish this class. - } - public TextureData(byte[] textureData, int width, int height, int mipmap = 0) { - this.textureData = textureData; - GL.TexImage2D(TextureTarget.Texture2D, mipmap, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, textureData); - } - - public void LoadFromPNG(string path) { + byte[] textureData = null; + int width; + int height; using (FileStream file = new FileStream(path, FileMode.Open)) { using (Bitmap bitmap = new Bitmap(file)) { ImageConverter converter = new ImageConverter(); textureData = (byte[]) converter.ConvertTo(bitmap, typeof(byte[])); + width = bitmap.Width; + height = bitmap.Height; } } + GenerateTexture(textureData, width, height); + } + + public TextureData(byte[] textureData, int width, int height) { + textureWrapModeHeight = TextureWrapMode.ClampToBorder; + textureWrapModeWidth = TextureWrapMode.ClampToBorder; + textureMinFilter = TextureMinFilter.LinearMipmapLinear; + textureMagFilter = TextureMagFilter.Linear; + + GenerateTexture(textureData, width, height); + } + + 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); + } + + internal void Use() { + GL.BindTexture(TextureTarget.Texture2D, handle); + } + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing = false) { + if (disposed) return; + if (disposing) { + } + GL.BindTexture(TextureTarget.Texture2D, 0); + GL.DeleteTexture(handle); + disposed = true; + } + + ~TextureData() { + Dispose(false); } } } \ No newline at end of file diff --git a/RecrownedGTK/Graphics/VertexInformation.cs b/RecrownedGTK/Graphics/VertexInformation.cs new file mode 100644 index 0000000..5b2be67 --- /dev/null +++ b/RecrownedGTK/Graphics/VertexInformation.cs @@ -0,0 +1,10 @@ +using OpenTK; +using OpenTK.Graphics; +namespace RecrownedGTK.Graphics { + public struct VertexInformation { + public Vector3 vertexCoords; + public uint[] indices; + public Color4 color; + public Vector2 textureCoords; + } +} \ No newline at end of file