Migrate to Factory/Wrapper pattern - Texture complete

This commit is contained in:
2017-03-01 01:30:06 -05:00
parent f51140b7db
commit 22392e7d83
5 changed files with 70 additions and 64 deletions

View File

@@ -38,10 +38,10 @@ namespace Diamond.Buffers
public class GLBuffer<T> : GLObject where T : struct
{
private GLBufferWrapper _buffer;
private readonly GLBufferWrapper _buffer;
internal override GLWrapper Wrapper => _buffer;
private int _size;
private readonly int _size;
public BufferTarget Target => _buffer.Target;

View File

@@ -91,7 +91,7 @@ namespace Diamond.Buffers
}
}
private static Dictionary<Type, VertexDataInfo> attribCache =
private static readonly Dictionary<Type, VertexDataInfo> attribCache =
new Dictionary<Type, VertexDataInfo>();
public static VertexDataInfo GetInfo<T>() where T : struct

View File

@@ -17,7 +17,7 @@ namespace Diamond
public string Name { get; set; }
private static VertexDataInfo tVdi;
private static readonly VertexDataInfo tVdi;
private static List<VertexPointerAttribute> attribs;
static Mesh()

View File

@@ -404,7 +404,7 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Instance of that is used to format log messages.</xs:documentation>
<xs:documentation>Instance of that is used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="lineEnding" shaderType="LineEndingMode">
@@ -464,7 +464,7 @@
</xs:attribute>
<xs:attribute name="includeSourceInfo" shaderType="xs:boolean">
<xs:annotation>
<xs:documentation>Indicates whether to include source info (file name and line number) in the information sent over the network.</xs:documentation>
<xs:documentation>Indicates whether to include source info (file name and line number) in the ininternalFormation sent over the network.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="includeNLoggerData" shaderType="xs:boolean">
@@ -479,7 +479,7 @@
</xs:attribute>
<xs:attribute name="includeCallSite" shaderType="xs:boolean">
<xs:annotation>
<xs:documentation>Indicates whether to include call site (class and method name) in the information sent over the network.</xs:documentation>
<xs:documentation>Indicates whether to include call site (class and method name) in the ininternalFormation sent over the network.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="appInfo" shaderType="xs:string">
@@ -957,7 +957,7 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Layout used to format log messages.</xs:documentation>
<xs:documentation>Layout used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="optimizeBufferReuse" shaderType="xs:boolean">
@@ -991,7 +991,7 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Layout used to format log messages.</xs:documentation>
<xs:documentation>Layout used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="category" shaderType="SimpleLayoutAttribute">
@@ -1170,7 +1170,7 @@
</xs:attribute>
<xs:attribute name="enableArchiveFileCompression" shaderType="xs:boolean">
<xs:annotation>
<xs:documentation>Indicates whether to compress archive files into the zip archive format.</xs:documentation>
<xs:documentation>Indicates whether to compress archive files into the zip archive internalFormat.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="maxArchiveFiles" shaderType="xs:integer">
@@ -1220,7 +1220,7 @@
</xs:attribute>
<xs:attribute name="archiveDateFormat" shaderType="xs:string">
<xs:annotation>
<xs:documentation>Value specifying the date format to use when archiving files.</xs:documentation>
<xs:documentation>Value specifying the date internalFormat to use when archiving files.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="archiveOldFileOnStartup" shaderType="xs:boolean">
@@ -1771,7 +1771,7 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Layout used to format log messages.</xs:documentation>
<xs:documentation>Layout used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="optimizeBufferReuse" shaderType="xs:boolean">
@@ -1804,7 +1804,7 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Layout used to format log messages.</xs:documentation>
<xs:documentation>Layout used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="encoding" shaderType="xs:string">
@@ -1814,7 +1814,7 @@
</xs:attribute>
<xs:attribute name="useXmlEncoding" shaderType="xs:boolean">
<xs:annotation>
<xs:documentation>Indicates whether to use the XML format when serializing message. This will also disable creating queues.</xs:documentation>
<xs:documentation>Indicates whether to use the XML internalFormat when serializing message. This will also disable creating queues.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="checkIfQueueExists" shaderType="xs:boolean">
@@ -1909,7 +1909,7 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Layout used to format log messages.</xs:documentation>
<xs:documentation>Layout used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="encoding" shaderType="xs:string">
@@ -2014,7 +2014,7 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Instance of that is used to format log messages.</xs:documentation>
<xs:documentation>Instance of that is used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="lineEnding" shaderType="LineEndingMode">
@@ -2074,7 +2074,7 @@
</xs:attribute>
<xs:attribute name="includeSourceInfo" shaderType="xs:boolean">
<xs:annotation>
<xs:documentation>Indicates whether to include source info (file name and line number) in the information sent over the network.</xs:documentation>
<xs:documentation>Indicates whether to include source info (file name and line number) in the ininternalFormation sent over the network.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="includeNLoggerData" shaderType="xs:boolean">
@@ -2089,7 +2089,7 @@
</xs:attribute>
<xs:attribute name="includeCallSite" shaderType="xs:boolean">
<xs:annotation>
<xs:documentation>Indicates whether to include call site (class and method name) in the information sent over the network.</xs:documentation>
<xs:documentation>Indicates whether to include call site (class and method name) in the ininternalFormation sent over the network.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="appInfo" shaderType="xs:string">
@@ -2116,7 +2116,7 @@
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="name" minOccurs="0" maxOccurs="1" shaderType="xs:string" />
<xs:element name="layout" minOccurs="0" maxOccurs="1" shaderType="Layout" />
<xs:element name="formatMessage" minOccurs="0" maxOccurs="1" shaderType="xs:boolean" />
<xs:element name="internalFormatMessage" minOccurs="0" maxOccurs="1" shaderType="xs:boolean" />
<xs:element name="optimizeBufferReuse" minOccurs="0" maxOccurs="1" shaderType="xs:boolean" />
</xs:choice>
<xs:attribute name="name" shaderType="xs:string">
@@ -2126,10 +2126,10 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Layout used to format log messages.</xs:documentation>
<xs:documentation>Layout used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="formatMessage" shaderType="xs:boolean">
<xs:attribute name="internalFormatMessage" shaderType="xs:boolean">
<xs:annotation>
<xs:documentation>Indicates whether to perform layout calculation.</xs:documentation>
</xs:annotation>
@@ -2157,7 +2157,7 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Layout used to format log messages.</xs:documentation>
<xs:documentation>Layout used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="optimizeBufferReuse" shaderType="xs:boolean">
@@ -2438,7 +2438,7 @@
</xs:attribute>
<xs:attribute name="layout" shaderType="SimpleLayoutAttribute">
<xs:annotation>
<xs:documentation>Layout used to format log messages.</xs:documentation>
<xs:documentation>Layout used to internalFormat log messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="optimizeBufferReuse" shaderType="xs:boolean">

View File

@@ -1,35 +1,23 @@
using System.Drawing;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using NLog;
using OpenTK.Graphics.OpenGL4;
using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;
namespace Diamond.Textures
{
/// <summary>
/// Wrapper class for gl Textures.
/// </summary>
public class Texture : GLWrapper
internal class TextureWrapper : GLWrapper
{
public TextureTarget Target;
public Texture(TextureTarget target = TextureTarget.Texture2D)
: base((uint) GL.GenTexture())
internal TextureWrapper(TextureTarget target)
{
Id = GL.GenTexture();
Target = target;
}
protected override void Delete() => GL.DeleteTexture(Id);
public readonly TextureTarget Target;
public void Bind()
{
GL.BindTexture(Target, Id);
}
public void Bind(TextureUnit unit)
{
GL.ActiveTexture(unit);
Bind();
}
public void Bind() => GL.BindTexture(Target, Id);
public void Bind(int unit)
{
@@ -37,38 +25,56 @@ namespace Diamond.Textures
Bind();
}
public void Unbind()
public void TexParameter(TextureParameterName parameter, int value) => GL.TexParameter(Target, parameter,
value);
public void Image2D(PixelInternalFormat internalFormat, int width, int height, PixelFormat format,
PixelType type, IntPtr pixels) =>
GL.TexImage2D(Target, 0, internalFormat, width, height, 0, format, type, pixels);
public override void GLDelete() => GL.DeleteTexture(Id);
}
public class Texture : GLObject
{
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly TextureWrapper _texture;
internal override GLWrapper Wrapper => _texture;
internal Texture(TextureWrapper wrapper, string name)
{
GL.BindTexture(Target, 0);
_texture = wrapper;
Name = name;
}
public void Unbind(TextureUnit unit)
{
GL.ActiveTexture(unit);
Unbind();
}
public TextureTarget Target => _texture.Target;
public void Unbind(int unit)
{
GL.ActiveTexture(TextureUnit.Texture0 + unit);
Unbind();
}
public void Bind() => _texture.Bind();
public void Bind(int unit) => _texture.Bind(unit);
public static Texture FromBitmap(Bitmap bmp)
public override string ToString() => Name == null ? $"{Target} ({Id})" : $"{Target} \'{Name}\' ({Id})";
public static Texture FromBitmap(Bitmap bmp, string name = null)
{
var tex = new Texture(TextureTarget.Texture2D);
tex.Bind();
GL.TexParameter(tex.Target, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Nearest);
GL.TexParameter(tex.Target, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Nearest);
var wrapper= new TextureWrapper(TextureTarget.Texture2D);
var service = new Texture(wrapper, null);
Logger.Debug("Created texture {0}", service);
wrapper.Bind();
wrapper.TexParameter(TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
wrapper.TexParameter(TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(tex.Target, 0, PixelInternalFormat.Rgba, bmp.Width, bmp.Height, 0, PixelFormat.Bgra,
PixelType.UnsignedByte, data.Scan0);
wrapper.Image2D(PixelInternalFormat.Rgba, bmp.Width,bmp.Height,PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
bmp.UnlockBits(data);
tex.Unbind();
return tex;
return service;
}
public static Texture FromFile(string path)
{
return FromBitmap(new Bitmap(path));