diff --git a/Diamond/Buffers/GLBuffer.cs b/Diamond/Buffers/GLBuffer.cs index 0c49a99..a431e46 100644 --- a/Diamond/Buffers/GLBuffer.cs +++ b/Diamond/Buffers/GLBuffer.cs @@ -2,40 +2,12 @@ using System.Runtime.InteropServices; using Diamond.Shaders; using Diamond.Util; +using Diamond.Wrappers; using NLog; using OpenTK.Graphics.OpenGL4; namespace Diamond.Buffers { - internal class GLBufferWrapper : GLWrapper - { - public BufferTarget Target { get; } - public BufferUsageHint Usage { get; set; } - - internal GLBufferWrapper(BufferTarget target, BufferUsageHint usage) - { - Id = GL.GenBuffer(); - Target = target; - Usage = usage; - } - - public void Bind() => GL.BindBuffer(Target, Id); - - public void Data(int size, T[] data) where T : struct - { - Bind(); - GL.BufferData(Target, (IntPtr) (size * data.Length), data, Usage); - } - - public void SubData(int size, int offset, int count, T[] data) where T : struct - { - Bind(); - GL.BufferSubData(Target, (IntPtr) (offset * size), (IntPtr) (count * size), data); - } - - public override void GLDelete() => GL.DeleteBuffer(Id); - } - public class GLBuffer : GLObject where T : struct { private readonly GLBufferWrapper _buffer; diff --git a/Diamond/Diamond.csproj b/Diamond/Diamond.csproj index dffddf1..dc4749a 100644 --- a/Diamond/Diamond.csproj +++ b/Diamond/Diamond.csproj @@ -52,13 +52,17 @@ + + + + - + - - + + diff --git a/Diamond/GLObject.cs b/Diamond/GLObject.cs index 6c30f6b..efbc8c5 100644 --- a/Diamond/GLObject.cs +++ b/Diamond/GLObject.cs @@ -1,4 +1,5 @@ using System; +using Diamond.Wrappers; using NLog; namespace Diamond diff --git a/Diamond/Shaders/Program.cs b/Diamond/Shaders/Program.cs index 8d15e14..cba30ce 100644 --- a/Diamond/Shaders/Program.cs +++ b/Diamond/Shaders/Program.cs @@ -1,52 +1,11 @@ using System.Collections.Generic; using System.Linq; -using System.Text; +using Diamond.Wrappers; using NLog; using OpenTK.Graphics.OpenGL4; namespace Diamond.Shaders { - internal class ProgramWrapper : GLWrapper - { - internal ProgramWrapper() - { - Id = GL.CreateProgram(); - } - - public int ActiveUniforms => Get(GetProgramParameterName.ActiveUniforms); - public int ActiveAttributes => Get(GetProgramParameterName.ActiveAttributes); - public bool Linked => Get(GetProgramParameterName.LinkStatus) != 0; - - public int Get(GetProgramParameterName parameter) - { - GL.GetProgram(Id, parameter, out int res); - return res; - } - - public void Link() => GL.LinkProgram(Id); - public string InfoLog => GL.GetProgramInfoLog(Id).Trim(); - - public void Attach(ShaderWrapper shader) => GL.AttachShader(Id, shader.Id); - - public void Use() => GL.UseProgram(Id); - - public string UniformName(int location) - { - var sb = new StringBuilder(64); - GL.GetActiveUniformName(Id, location, sb.Capacity, out int length, sb); - return sb.ToString(); - } - - public string AttributeName(int location) - { - var sb = new StringBuilder(64); - GL.GetActiveAttrib(Id, location, sb.Capacity, out int length, out int size, out ActiveAttribType type, sb); - return sb.ToString(); - } - - public override void GLDelete() => GL.DeleteProgram(Id); - } - public class Program : GLObject { internal static readonly Logger Logger = LogManager.GetCurrentClassLogger(); diff --git a/Diamond/Shaders/Shader.cs b/Diamond/Shaders/Shader.cs index 761ecf0..3c791ca 100644 --- a/Diamond/Shaders/Shader.cs +++ b/Diamond/Shaders/Shader.cs @@ -1,48 +1,11 @@ using System.Collections.Generic; using System.IO; -using System.Text; +using Diamond.Wrappers; using NLog; using OpenTK.Graphics.OpenGL4; namespace Diamond.Shaders { - internal sealed class ShaderWrapper : GLWrapper - { - internal ShaderWrapper(ShaderType shaderType) - { - Id = GL.CreateShader(shaderType); - ShaderType = shaderType; - } - - public readonly ShaderType ShaderType; - - public string Source - { - get - { - var sb = new StringBuilder(1024); - GL.GetShaderSource(Id, sb.Capacity, out int length, sb); - return sb.ToString(); - } - set { GL.ShaderSource(Id, value); } - } - - public bool Compiled - { - get - { - GL.GetShader(Id, ShaderParameter.CompileStatus, out int res); - return res != 0; - } - } - - public string InfoLog => GL.GetShaderInfoLog(Id); - - public void Compile() => GL.CompileShader(Id); - - public override void GLDelete() => GL.DeleteShader(Id); - } - public class Shader : GLObject { internal static readonly Logger Logger = LogManager.GetCurrentClassLogger(); diff --git a/Diamond/Textures/Texture.cs b/Diamond/Textures/Texture.cs index e304b7e..1ba1cc8 100644 --- a/Diamond/Textures/Texture.cs +++ b/Diamond/Textures/Texture.cs @@ -1,40 +1,12 @@ -using System; -using System.Drawing; +using System.Drawing; using System.Drawing.Imaging; +using Diamond.Wrappers; using NLog; using OpenTK.Graphics.OpenGL4; using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat; namespace Diamond.Textures { - internal class TextureWrapper : GLWrapper - { - internal TextureWrapper(TextureTarget target) - { - Id = GL.GenTexture(); - Target = target; - } - - public readonly TextureTarget Target; - - public void Bind() => GL.BindTexture(Target, Id); - - public void Bind(int unit) - { - GL.ActiveTexture(TextureUnit.Texture0 + unit); - Bind(); - } - - 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(); diff --git a/Diamond/Mesh.cs b/Diamond/Util/Mesh.cs similarity index 99% rename from Diamond/Mesh.cs rename to Diamond/Util/Mesh.cs index 7acb659..112960c 100644 --- a/Diamond/Mesh.cs +++ b/Diamond/Util/Mesh.cs @@ -2,11 +2,10 @@ using System.IO; using System.Linq; using Diamond.Buffers; -using Diamond.Util; using OpenTK; using OpenTK.Graphics.OpenGL4; -namespace Diamond +namespace Diamond.Util { public class Mesh where T : struct { diff --git a/Diamond/ObjVertex.cs b/Diamond/Util/ObjVertex.cs similarity index 95% rename from Diamond/ObjVertex.cs rename to Diamond/Util/ObjVertex.cs index 3da5537..c168a09 100644 --- a/Diamond/ObjVertex.cs +++ b/Diamond/Util/ObjVertex.cs @@ -1,7 +1,7 @@ using Diamond.Buffers; using OpenTK; -namespace Diamond +namespace Diamond.Util { [VertexData] public struct ObjVertex diff --git a/Diamond/Wrappers/GLBufferWrapper.cs b/Diamond/Wrappers/GLBufferWrapper.cs new file mode 100644 index 0000000..81d48ab --- /dev/null +++ b/Diamond/Wrappers/GLBufferWrapper.cs @@ -0,0 +1,34 @@ +using System; +using OpenTK.Graphics.OpenGL4; + +namespace Diamond.Wrappers +{ + internal class GLBufferWrapper : GLWrapper + { + public BufferTarget Target { get; } + public BufferUsageHint Usage { get; set; } + + internal GLBufferWrapper(BufferTarget target, BufferUsageHint usage) + { + Id = GL.GenBuffer(); + Target = target; + Usage = usage; + } + + public void Bind() => GL.BindBuffer(Target, Id); + + public void Data(int size, T[] data) where T : struct + { + Bind(); + GL.BufferData(Target, (IntPtr) (size * data.Length), data, Usage); + } + + public void SubData(int size, int offset, int count, T[] data) where T : struct + { + Bind(); + GL.BufferSubData(Target, (IntPtr) (offset * size), (IntPtr) (count * size), data); + } + + public override void GLDelete() => GL.DeleteBuffer(Id); + } +} \ No newline at end of file diff --git a/Diamond/GLWrapper.cs b/Diamond/Wrappers/GLWrapper.cs similarity index 97% rename from Diamond/GLWrapper.cs rename to Diamond/Wrappers/GLWrapper.cs index 168d8e7..81c5fb8 100644 --- a/Diamond/GLWrapper.cs +++ b/Diamond/Wrappers/GLWrapper.cs @@ -1,8 +1,8 @@ using System; -using OpenTK.Graphics; using NLog; +using OpenTK.Graphics; -namespace Diamond +namespace Diamond.Wrappers { internal abstract class GLWrapper : IDisposable { diff --git a/Diamond/Wrappers/ProgramWrapper.cs b/Diamond/Wrappers/ProgramWrapper.cs new file mode 100644 index 0000000..d9f070b --- /dev/null +++ b/Diamond/Wrappers/ProgramWrapper.cs @@ -0,0 +1,46 @@ +using System.Text; +using OpenTK.Graphics.OpenGL4; + +namespace Diamond.Wrappers +{ + internal class ProgramWrapper : GLWrapper + { + internal ProgramWrapper() + { + Id = GL.CreateProgram(); + } + + public int ActiveUniforms => Get(GetProgramParameterName.ActiveUniforms); + public int ActiveAttributes => Get(GetProgramParameterName.ActiveAttributes); + public bool Linked => Get(GetProgramParameterName.LinkStatus) != 0; + + public int Get(GetProgramParameterName parameter) + { + GL.GetProgram(Id, parameter, out int res); + return res; + } + + public void Link() => GL.LinkProgram(Id); + public string InfoLog => GL.GetProgramInfoLog(Id).Trim(); + + public void Attach(ShaderWrapper shader) => GL.AttachShader(Id, shader.Id); + + public void Use() => GL.UseProgram(Id); + + public string UniformName(int location) + { + var sb = new StringBuilder(64); + GL.GetActiveUniformName(Id, location, sb.Capacity, out int length, sb); + return sb.ToString(); + } + + public string AttributeName(int location) + { + var sb = new StringBuilder(64); + GL.GetActiveAttrib(Id, location, sb.Capacity, out int length, out int size, out ActiveAttribType type, sb); + return sb.ToString(); + } + + public override void GLDelete() => GL.DeleteProgram(Id); + } +} \ No newline at end of file diff --git a/Diamond/Wrappers/ShaderWrapper.cs b/Diamond/Wrappers/ShaderWrapper.cs new file mode 100644 index 0000000..83808ea --- /dev/null +++ b/Diamond/Wrappers/ShaderWrapper.cs @@ -0,0 +1,42 @@ +using System.Text; +using OpenTK.Graphics.OpenGL4; + +namespace Diamond.Wrappers +{ + internal sealed class ShaderWrapper : GLWrapper + { + internal ShaderWrapper(ShaderType shaderType) + { + Id = GL.CreateShader(shaderType); + ShaderType = shaderType; + } + + public readonly ShaderType ShaderType; + + public string Source + { + get + { + var sb = new StringBuilder(1024); + GL.GetShaderSource(Id, sb.Capacity, out int length, sb); + return sb.ToString(); + } + set { GL.ShaderSource(Id, value); } + } + + public bool Compiled + { + get + { + GL.GetShader(Id, ShaderParameter.CompileStatus, out int res); + return res != 0; + } + } + + public string InfoLog => GL.GetShaderInfoLog(Id); + + public void Compile() => GL.CompileShader(Id); + + public override void GLDelete() => GL.DeleteShader(Id); + } +} \ No newline at end of file diff --git a/Diamond/Wrappers/TextureWrapper.cs b/Diamond/Wrappers/TextureWrapper.cs new file mode 100644 index 0000000..a1d632f --- /dev/null +++ b/Diamond/Wrappers/TextureWrapper.cs @@ -0,0 +1,33 @@ +using System; +using OpenTK.Graphics.OpenGL4; + +namespace Diamond.Wrappers +{ + internal class TextureWrapper : GLWrapper + { + internal TextureWrapper(TextureTarget target) + { + Id = GL.GenTexture(); + Target = target; + } + + public readonly TextureTarget Target; + + public void Bind() => GL.BindTexture(Target, Id); + + public void Bind(int unit) + { + GL.ActiveTexture(TextureUnit.Texture0 + unit); + Bind(); + } + + 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); + } +} \ No newline at end of file