Added summeries to SM.OGL

This commit is contained in:
Michel Fedde 2020-09-27 13:01:50 +02:00
parent 2aa12f8d25
commit 16366fa015
15 changed files with 363 additions and 27 deletions

View file

@ -3,18 +3,33 @@ using OpenTK.Graphics.OpenGL4;
namespace SM.OGL.Shaders
{
public class GenericShader : GLObject
/// <summary>
/// Abstract class, that is used to create graphic shader.
/// </summary>
public abstract class GenericShader : GLObject
{
/// <summary>
/// Contains the different files for the shader.
/// </summary>
protected ShaderFileCollection ShaderFileFiles;
/// <summary>
/// Contains and manage the uniforms from the shader.
/// </summary>
protected UniformCollection Uniforms;
/// <inheritdoc />
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Program;
public GenericShader(ShaderFileCollection shaderFileFiles)
/// <inheritdoc />
protected GenericShader(ShaderFileCollection shaderFileFiles)
{
ShaderFileFiles = shaderFileFiles;
}
/// <summary>
/// Loads the shader to the GPU.
/// </summary>
public void Load()
{
@ -43,12 +58,19 @@ namespace SM.OGL.Shaders
}
/// <inheritdoc />
protected override void Compile()
{
Load();
}
public void DrawObject(Mesh.GenericMesh mesh, int amount, bool bindVAO = false)
/// <summary>
/// Draws the mesh.
/// </summary>
/// <param name="mesh">The mesh.</param>
/// <param name="amount">The amounts for instancing.</param>
/// <param name="bindVAO">Binds the vertex array for the mesh.</param>
protected void DrawObject(Mesh.GenericMesh mesh, int amount, bool bindVAO = false)
{
if (bindVAO) GL.BindVertexArray(mesh);
@ -57,7 +79,9 @@ namespace SM.OGL.Shaders
else
GL.DrawArraysInstanced(mesh.PrimitiveType, 0, mesh.Vertex.Count, amount);
}
/// <summary>
/// Resets the shader specific settings to ensure proper workings.
/// </summary>
protected void CleanUp()
{
Uniforms.NextTexture = 0;

View file

@ -4,20 +4,35 @@ using OpenTK.Graphics.OpenGL4;
namespace SM.OGL.Shaders
{
/// <summary>
/// Contains/Represents a file used in shaders.
/// </summary>
public class ShaderFile : GLObject
{
private string _data;
/// <inheritdoc />
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Shader;
/// <summary>
/// Contains overrides, that can be used to import values from the CPU to the shader before it is been send to the GPU.
/// </summary>
public Dictionary<string, string> StringOverrides = new Dictionary<string, string>();
/// <summary>
/// Contains other shader files to allow access to their functions.
/// </summary>
public List<ShaderFile> GLSLExtensions = new List<ShaderFile>();
/// <summary>
/// Creates a file.
/// </summary>
/// <param name="data">The source file.</param>
public ShaderFile(string data)
{
_data = data;
}
private void GenerateSource()
{
foreach (KeyValuePair<string, string> kvp in StringOverrides)

View file

@ -4,25 +4,48 @@ using OpenTK.Graphics.OpenGL4;
namespace SM.OGL.Shaders
{
/// <summary>
/// Collects all files that are needed for a shader.
/// </summary>
public struct ShaderFileCollection
{
/// <summary>
/// Contains the vertex file.
/// </summary>
public ShaderFile Vertex;
/// <summary>
/// Contains the geometry file.
/// </summary>
public ShaderFile Geometry;
/// <summary>
/// Contains the fragment file.
/// </summary>
public ShaderFile Fragment;
public Action<UniformCollection> SetUniforms;
/// <summary>
/// Creating the collection with vertex and fragment files.
/// </summary>
/// <param name="vertex">The vertex source file.</param>
/// <param name="fragment">The fragment source file.</param>
public ShaderFileCollection(string vertex, string fragment) : this(new ShaderFile(vertex), new ShaderFile(fragment)) {}
/// <summary>
/// Creating the collection with shader files.
/// </summary>
/// <param name="vertex"></param>
/// <param name="fragment"></param>
/// <param name="geometry"></param>
public ShaderFileCollection(ShaderFile vertex, ShaderFile fragment, ShaderFile geometry = default)
{
Vertex = vertex;
Geometry = geometry;
Fragment = fragment;
SetUniforms = u => { };
}
/// <summary>
/// Appends the files to the shader.
/// </summary>
/// <param name="shader"></param>
internal void Append(GenericShader shader)
{
Vertex.Compile(shader, ShaderType.VertexShader);
@ -30,6 +53,10 @@ namespace SM.OGL.Shaders
Fragment.Compile(shader, ShaderType.FragmentShader);
}
/// <summary>
/// Removes the files form the shader.
/// </summary>
/// <param name="shader"></param>
internal void Detach(GenericShader shader)
{
GL.DetachShader(Vertex, shader);

View file

@ -5,6 +5,9 @@ using SM.OGL.Texture;
namespace SM.OGL.Shaders
{
/// <summary>
/// Manages the uniforms.
/// </summary>
public struct Uniform
{
/// <summary>
@ -162,19 +165,39 @@ namespace SM.OGL.Shaders
#endregion
/// <summary>
/// Try to sets the texture at the next possible position and tells the checkUniform, if worked or not.
/// </summary>
/// <param name="texture">The texture you want to add</param>
/// <param name="checkUniform">The check uniform.</param>
public void SetTexture(TextureBase texture, Uniform checkUniform)
{
checkUniform.SetUniform1(texture != null);
if (texture != null) SetTexture(texture);
}
/// <summary>
/// Try to sets the texture at the specified position and tells the checkUniform, if worked or not.
/// </summary>
/// <param name="texture">The texture you want to add</param>
/// <param name="pos">The position</param>
/// <param name="checkUniform">The check uniform.</param>
public void SetTexture(TextureBase texture, int pos, Uniform checkUniform)
{
checkUniform.SetUniform1(texture != null);
if (texture != null) SetTexture(texture);
}
/// <summary>
/// Sets the texture to the next possible position.
/// </summary>
/// <param name="texture"></param>
public void SetTexture(TextureBase texture) => SetTexture(texture, Parent.NextTexture++);
/// <summary>
/// Sets the texture to the specified position.
/// </summary>
/// <param name="texture"></param>
/// <param name="texturePos"></param>
public void SetTexture(TextureBase texture, int texturePos)
{
GL.ActiveTexture(TextureUnit.Texture0 + texturePos);
@ -182,6 +205,10 @@ namespace SM.OGL.Shaders
SetUniform1(texturePos);
}
/// <summary>
/// Returns the location from the uniform
/// </summary>
/// <param name="u"></param>
public static implicit operator int(Uniform u) => u.Location;
}
}

View file

@ -4,12 +4,28 @@ using OpenTK.Graphics.OpenGL4;
namespace SM.OGL.Shaders
{
/// <summary>
/// Contains and manages the uniform of the parent shader.
/// </summary>
public class UniformCollection : Dictionary<string, Uniform>
{
/// <summary>
/// The next texture id for the uniform.
/// </summary>
internal int NextTexture = 0;
/// <summary>
/// The parent shader.
/// </summary>
internal GenericShader _parentShader;
/// <summary>
/// Get you the uniform under the variable name.
/// <para>If it don't find the uniform, it tries to recreate it.</para>
/// <para>If the variable doesn't exist in the first place, it will after the recreation send everything to -1, what is the void.</para>
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public new Uniform this[string key]
{
get
@ -27,7 +43,11 @@ namespace SM.OGL.Shaders
}
}
}
/// <summary>
/// Adds a uniform with a location.
/// </summary>
/// <param name="key"></param>
/// <param name="location"></param>
public void Add(string key, int location)
{
base.Add(key, new Uniform(location, this));