Refactored types into appropriate namespaces
This commit is contained in:
@@ -2,40 +2,12 @@
|
|||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using Diamond.Shaders;
|
using Diamond.Shaders;
|
||||||
using Diamond.Util;
|
using Diamond.Util;
|
||||||
|
using Diamond.Wrappers;
|
||||||
using NLog;
|
using NLog;
|
||||||
using OpenTK.Graphics.OpenGL4;
|
using OpenTK.Graphics.OpenGL4;
|
||||||
|
|
||||||
namespace Diamond.Buffers
|
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<T>(int size, T[] data) where T : struct
|
|
||||||
{
|
|
||||||
Bind();
|
|
||||||
GL.BufferData(Target, (IntPtr) (size * data.Length), data, Usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SubData<T>(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<T> : GLObject where T : struct
|
public class GLBuffer<T> : GLObject where T : struct
|
||||||
{
|
{
|
||||||
private readonly GLBufferWrapper _buffer;
|
private readonly GLBufferWrapper _buffer;
|
||||||
|
|||||||
@@ -52,13 +52,17 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Buffers\GLBuffer.cs" />
|
<Compile Include="Buffers\GLBuffer.cs" />
|
||||||
|
<Compile Include="Wrappers\GLBufferWrapper.cs" />
|
||||||
<Compile Include="GLObject.cs" />
|
<Compile Include="GLObject.cs" />
|
||||||
|
<Compile Include="Wrappers\ProgramWrapper.cs" />
|
||||||
|
<Compile Include="Wrappers\ShaderWrapper.cs" />
|
||||||
|
<Compile Include="Wrappers\TextureWrapper.cs" />
|
||||||
<Compile Include="Util\SubArray.cs" />
|
<Compile Include="Util\SubArray.cs" />
|
||||||
<Compile Include="Buffers\VertexDataAttribute.cs" />
|
<Compile Include="Buffers\VertexDataAttribute.cs" />
|
||||||
<Compile Include="GLWrapper.cs" />
|
<Compile Include="Wrappers\GLWrapper.cs" />
|
||||||
<Compile Include="Level\TileData.cs" />
|
<Compile Include="Level\TileData.cs" />
|
||||||
<Compile Include="Mesh.cs" />
|
<Compile Include="Util\Mesh.cs" />
|
||||||
<Compile Include="ObjVertex.cs" />
|
<Compile Include="Util\ObjVertex.cs" />
|
||||||
<Compile Include="Shaders\Program.cs" />
|
<Compile Include="Shaders\Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Shaders\Shader.cs" />
|
<Compile Include="Shaders\Shader.cs" />
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using Diamond.Wrappers;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
|
||||||
namespace Diamond
|
namespace Diamond
|
||||||
|
|||||||
@@ -1,52 +1,11 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using Diamond.Wrappers;
|
||||||
using NLog;
|
using NLog;
|
||||||
using OpenTK.Graphics.OpenGL4;
|
using OpenTK.Graphics.OpenGL4;
|
||||||
|
|
||||||
namespace Diamond.Shaders
|
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
|
public class Program : GLObject
|
||||||
{
|
{
|
||||||
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||||
|
|||||||
@@ -1,48 +1,11 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using Diamond.Wrappers;
|
||||||
using NLog;
|
using NLog;
|
||||||
using OpenTK.Graphics.OpenGL4;
|
using OpenTK.Graphics.OpenGL4;
|
||||||
|
|
||||||
namespace Diamond.Shaders
|
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
|
public class Shader : GLObject
|
||||||
{
|
{
|
||||||
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||||
|
|||||||
@@ -1,40 +1,12 @@
|
|||||||
using System;
|
using System.Drawing;
|
||||||
using System.Drawing;
|
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
|
using Diamond.Wrappers;
|
||||||
using NLog;
|
using NLog;
|
||||||
using OpenTK.Graphics.OpenGL4;
|
using OpenTK.Graphics.OpenGL4;
|
||||||
using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;
|
using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;
|
||||||
|
|
||||||
namespace Diamond.Textures
|
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
|
public class Texture : GLObject
|
||||||
{
|
{
|
||||||
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||||
|
|||||||
@@ -2,11 +2,10 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Diamond.Buffers;
|
using Diamond.Buffers;
|
||||||
using Diamond.Util;
|
|
||||||
using OpenTK;
|
using OpenTK;
|
||||||
using OpenTK.Graphics.OpenGL4;
|
using OpenTK.Graphics.OpenGL4;
|
||||||
|
|
||||||
namespace Diamond
|
namespace Diamond.Util
|
||||||
{
|
{
|
||||||
public class Mesh<T> where T : struct
|
public class Mesh<T> where T : struct
|
||||||
{
|
{
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using Diamond.Buffers;
|
using Diamond.Buffers;
|
||||||
using OpenTK;
|
using OpenTK;
|
||||||
|
|
||||||
namespace Diamond
|
namespace Diamond.Util
|
||||||
{
|
{
|
||||||
[VertexData]
|
[VertexData]
|
||||||
public struct ObjVertex
|
public struct ObjVertex
|
||||||
34
Diamond/Wrappers/GLBufferWrapper.cs
Normal file
34
Diamond/Wrappers/GLBufferWrapper.cs
Normal file
@@ -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<T>(int size, T[] data) where T : struct
|
||||||
|
{
|
||||||
|
Bind();
|
||||||
|
GL.BufferData(Target, (IntPtr) (size * data.Length), data, Usage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SubData<T>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using OpenTK.Graphics;
|
|
||||||
using NLog;
|
using NLog;
|
||||||
|
using OpenTK.Graphics;
|
||||||
|
|
||||||
namespace Diamond
|
namespace Diamond.Wrappers
|
||||||
{
|
{
|
||||||
internal abstract class GLWrapper : IDisposable
|
internal abstract class GLWrapper : IDisposable
|
||||||
{
|
{
|
||||||
46
Diamond/Wrappers/ProgramWrapper.cs
Normal file
46
Diamond/Wrappers/ProgramWrapper.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
42
Diamond/Wrappers/ShaderWrapper.cs
Normal file
42
Diamond/Wrappers/ShaderWrapper.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
Diamond/Wrappers/TextureWrapper.cs
Normal file
33
Diamond/Wrappers/TextureWrapper.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user