16.09.2020
~ Fixed shading import ~ Fixed "Plate" mesh ~ Move the dll files into "SMCode"
This commit is contained in:
parent
421d03f91d
commit
9889366317
27 changed files with 30 additions and 17 deletions
50
SMCode/SM.OGL/Shaders/GenericShader.cs
Normal file
50
SMCode/SM.OGL/Shaders/GenericShader.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace SM.OGL.Shaders
|
||||
{
|
||||
public class GenericShader : GLObject
|
||||
{
|
||||
protected ShaderFileCollection ShaderFileFiles;
|
||||
protected UniformCollection Uniforms;
|
||||
|
||||
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Program;
|
||||
|
||||
public GenericShader(ShaderFileCollection shaderFileFiles)
|
||||
{
|
||||
ShaderFileFiles = shaderFileFiles;
|
||||
}
|
||||
|
||||
public void Compile()
|
||||
{
|
||||
|
||||
_id = GL.CreateProgram();
|
||||
|
||||
ShaderFileFiles.Append(this);
|
||||
GL.LinkProgram(_id);
|
||||
this.Name(GetType().Name);
|
||||
ShaderFileFiles.Detach(this);
|
||||
|
||||
GL.GetProgram(_id, GetProgramParameterName.ActiveUniforms, out int uniformCount);
|
||||
if (uniformCount < 1)
|
||||
throw new Exception("[Critical] No uniforms has been found.");
|
||||
|
||||
Uniforms = new UniformCollection();
|
||||
for (int i = 0; i < uniformCount; i++)
|
||||
{
|
||||
string key = GL.GetActiveUniform(_id, i, out _, out _);
|
||||
int loc = GL.GetUniformLocation(_id, key);
|
||||
|
||||
if (key.StartsWith("[")) key = key.Split('[', ']')[0];
|
||||
Uniforms.Add(key, loc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void DrawObject(Mesh.Mesh mesh, bool bindVAO = false)
|
||||
{
|
||||
if (bindVAO) GL.BindVertexArray(mesh);
|
||||
GL.DrawArrays(mesh.PrimitiveType, 0, mesh.Vertex.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
SMCode/SM.OGL/Shaders/ShaderFile.cs
Normal file
40
SMCode/SM.OGL/Shaders/ShaderFile.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace SM.OGL.Shaders
|
||||
{
|
||||
public class ShaderFile : GLObject
|
||||
{
|
||||
private string _data;
|
||||
|
||||
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Shader;
|
||||
|
||||
public Dictionary<string, string> StringOverrides = new Dictionary<string, string>();
|
||||
public List<ShaderFile> GLSLExtensions = new List<ShaderFile>();
|
||||
|
||||
public ShaderFile(string data)
|
||||
{
|
||||
_data = data;
|
||||
}
|
||||
|
||||
private void GenerateSource()
|
||||
{
|
||||
foreach (KeyValuePair<string, string> kvp in StringOverrides)
|
||||
_data = _data.Replace("//! " + kvp.Key, kvp.Value);
|
||||
}
|
||||
|
||||
internal void Compile(GenericShader shader, ShaderType type)
|
||||
{
|
||||
if (_id < 0)
|
||||
{
|
||||
GenerateSource();
|
||||
|
||||
_id = GL.CreateShader(type);
|
||||
GL.ShaderSource(_id, _data);
|
||||
GL.CompileShader(_id);
|
||||
}
|
||||
GL.AttachShader(shader, _id);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
SMCode/SM.OGL/Shaders/ShaderFileCollection.cs
Normal file
35
SMCode/SM.OGL/Shaders/ShaderFileCollection.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using System.Collections.Generic;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace SM.OGL.Shaders
|
||||
{
|
||||
public struct ShaderFileCollection
|
||||
{
|
||||
public ShaderFile Vertex;
|
||||
public ShaderFile Geometry;
|
||||
public ShaderFile Fragment;
|
||||
|
||||
public ShaderFileCollection(string vertex, string fragment) : this(new ShaderFile(vertex), new ShaderFile(fragment)) {}
|
||||
|
||||
public ShaderFileCollection(ShaderFile vertex, ShaderFile fragment, ShaderFile geometry = default)
|
||||
{
|
||||
Vertex = vertex;
|
||||
Geometry = geometry;
|
||||
Fragment = fragment;
|
||||
}
|
||||
|
||||
internal void Append(GenericShader shader)
|
||||
{
|
||||
Vertex.Compile(shader, ShaderType.VertexShader);
|
||||
Geometry?.Compile(shader, ShaderType.GeometryShader);
|
||||
Fragment.Compile(shader, ShaderType.FragmentShader);
|
||||
}
|
||||
|
||||
internal void Detach(GenericShader shader)
|
||||
{
|
||||
GL.DetachShader(Vertex, shader);
|
||||
if (Geometry != null) GL.DetachShader(Geometry, shader);
|
||||
GL.DetachShader(Fragment, shader);
|
||||
}
|
||||
}
|
||||
}
|
||||
164
SMCode/SM.OGL/Shaders/Uniform.cs
Normal file
164
SMCode/SM.OGL/Shaders/Uniform.cs
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace SM.OGL.Shaders
|
||||
{
|
||||
public struct Uniform
|
||||
{
|
||||
/// <summary>
|
||||
/// This contains the location for the uniform.
|
||||
/// </summary>
|
||||
private int Location;
|
||||
/// <summary>
|
||||
/// This contains the Parent collection of this uniform.
|
||||
/// </summary>
|
||||
internal UniformCollection Parent;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This create a new uniform manager
|
||||
/// </summary>
|
||||
/// <param name="location">Location id</param>
|
||||
/// <param name="parent">Parent collection</param>
|
||||
public Uniform(int location, UniformCollection parent)
|
||||
{
|
||||
Location = location;
|
||||
Parent = parent;
|
||||
}
|
||||
|
||||
#region Uniform1
|
||||
|
||||
public void SetUniform1(bool value)
|
||||
{
|
||||
GL.Uniform1(Location, value ? 1 : 0);
|
||||
}
|
||||
|
||||
public void SetUniform1(int value) { GL.Uniform1(Location, value); }
|
||||
public void SetUniform1(int count, params int[] values) { GL.Uniform1(Location, count, values); }
|
||||
public void SetUniform1(int count, ref int values) { GL.Uniform1(Location, count, ref values); }
|
||||
|
||||
|
||||
public void SetUniform1(uint value) { GL.Uniform1(Location, value); }
|
||||
public void SetUniform1(int count, params uint[] values) { GL.Uniform1(Location, count, values); }
|
||||
public void SetUniform1(int count, ref uint values) { GL.Uniform1(Location, count, ref values); }
|
||||
|
||||
|
||||
public void SetUniform1(float value) { GL.Uniform1(Location, value); }
|
||||
public void SetUniform1(int count, params float[] values) { GL.Uniform1(Location, count, values); }
|
||||
public void SetUniform1(int count, ref float value) { GL.Uniform1(Location, count, ref value); }
|
||||
|
||||
|
||||
public void SetUniform1(double value) { GL.Uniform1(Location, value); }
|
||||
public void SetUniform1(int count, params double[] values) { GL.Uniform1(Location, count, values); }
|
||||
public void SetUniform1(int count, ref double value) { GL.Uniform1(Location, count, ref value); }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Uniform2
|
||||
|
||||
public void SetUniform2(float x, float y) { GL.Uniform2(Location, x, y); }
|
||||
public void SetUniform2(double x, double y) { GL.Uniform2(Location, x, y); }
|
||||
public void SetUniform2(uint x, uint y) { GL.Uniform2(Location, x, y); }
|
||||
public void SetUniform2(int x, int y) { GL.Uniform2(Location, x, y); }
|
||||
|
||||
public void SetUniform2(int count, params float[] values) { GL.Uniform2(Location, count, values); }
|
||||
public void SetUniform2(int count, params double[] values) { GL.Uniform2(Location, count, values); }
|
||||
public void SetUniform2(int count, params int[] values) { GL.Uniform2(Location, count, values); }
|
||||
public void SetUniform2(int count, params uint[] values) { GL.Uniform2(Location, count, values); }
|
||||
|
||||
public void SetUniform2(int count, ref float values) { GL.Uniform2(Location, count, ref values); }
|
||||
public void SetUniform2(int count, ref double values) { GL.Uniform2(Location, count, ref values); }
|
||||
public void SetUniform2(int count, ref uint values) { GL.Uniform2(Location, count, ref values); }
|
||||
|
||||
public void SetUniform2(Vector2 vector2) { GL.Uniform2(Location, vector2); }
|
||||
public void SetUniform2(ref Vector2 vector2) { GL.Uniform2(Location, ref vector2); }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Uniform3
|
||||
|
||||
public void SetUniform3(float x, float y, float z) { GL.Uniform3(Location, x, y, z); }
|
||||
public void SetUniform3(double x, double y, double z) { GL.Uniform3(Location, x, y, z); }
|
||||
public void SetUniform3(uint x, uint y, uint z) { GL.Uniform3(Location, x, y, z); }
|
||||
public void SetUniform3(int x, int y, int z) { GL.Uniform3(Location, x, y, z); }
|
||||
|
||||
public void SetUniform3(int count, params float[] values) { GL.Uniform3(Location, count, values); }
|
||||
public void SetUniform3(int count, params double[] values) { GL.Uniform3(Location, count, values); }
|
||||
public void SetUniform3(int count, params int[] values) { GL.Uniform3(Location, count, values); }
|
||||
public void SetUniform3(int count, params uint[] values) { GL.Uniform3(Location, count, values); }
|
||||
|
||||
public void SetUniform3(int count, ref float values) { GL.Uniform3(Location, count, ref values); }
|
||||
public void SetUniform3(int count, ref double values) { GL.Uniform3(Location, count, ref values); }
|
||||
public void SetUniform3(int count, ref uint values) { GL.Uniform3(Location, count, ref values); }
|
||||
|
||||
public void SetUniform3(Vector3 vector) { GL.Uniform3(Location, vector); }
|
||||
public void SetUniform3(ref Vector3 vector) { GL.Uniform3(Location, ref vector); }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Uniform4
|
||||
|
||||
public void SetUniform4(float x, float y, float z, float w) { GL.Uniform4(Location, x, y, z, w); }
|
||||
public void SetUniform4(double x, double y, double z, double w) { GL.Uniform4(Location, x, y, z, w); }
|
||||
public void SetUniform4(uint x, uint y, uint z, uint w) { GL.Uniform4(Location, x, y, z, w); }
|
||||
public void SetUniform4(int x, int y, int z, int w) { GL.Uniform4(Location, x, y, z, w); }
|
||||
|
||||
public void SetUniform4(int count, params float[] values) { GL.Uniform4(Location, count, values); }
|
||||
public void SetUniform4(int count, params double[] values) { GL.Uniform4(Location, count, values); }
|
||||
public void SetUniform4(int count, params int[] values) { GL.Uniform4(Location, count, values); }
|
||||
public void SetUniform4(int count, params uint[] values) { GL.Uniform4(Location, count, values); }
|
||||
|
||||
public void SetUniform4(int count, ref float values) { GL.Uniform4(Location, count, ref values); }
|
||||
public void SetUniform4(int count, ref double values) { GL.Uniform4(Location, count, ref values); }
|
||||
public void SetUniform4(int count, ref uint values) { GL.Uniform4(Location, count, ref values); }
|
||||
|
||||
public void SetUniform4(Vector4 vector) { GL.Uniform4(Location, vector); }
|
||||
public void SetUniform4(ref Vector4 vector) { GL.Uniform4(Location, ref vector); }
|
||||
|
||||
public void SetUniform4(Color4 color) { GL.Uniform4(Location, color); }
|
||||
public void SetUniform4(Quaternion quaternion) { GL.Uniform4(Location, quaternion); }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Matrix2
|
||||
|
||||
public void SetMatrix2(ref Matrix2 matrix, bool transpose = false) { GL.UniformMatrix2(Location, transpose, ref matrix); }
|
||||
|
||||
public void SetMatrix2(int count, ref double value, bool transpose = false) { GL.UniformMatrix2(Location, count, transpose, ref value); }
|
||||
public void SetMatrix2(int count, ref float value, bool transpose = false) { GL.UniformMatrix2(Location, count, transpose, ref value); }
|
||||
|
||||
public void SetMatrix2(int count, double[] value, bool transpose = false) { GL.UniformMatrix2(Location, count, transpose, value); }
|
||||
public void SetMatrix2(int count, float[] value, bool transpose = false) { GL.UniformMatrix2(Location, count, transpose, value); }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Matrix3
|
||||
|
||||
public void SetMatrix3(ref Matrix3 matrix, bool transpose = false) { GL.UniformMatrix3(Location, transpose, ref matrix); }
|
||||
|
||||
public void SetMatrix3(int count, ref double value, bool transpose = false) { GL.UniformMatrix3(Location, count, transpose, ref value); }
|
||||
public void SetMatrix3(int count, ref float value, bool transpose = false) { GL.UniformMatrix3(Location, count, transpose, ref value); }
|
||||
|
||||
public void SetMatrix3(int count, double[] value, bool transpose = false) { GL.UniformMatrix3(Location, count, transpose, value); }
|
||||
public void SetMatrix3(int count, float[] value, bool transpose = false) { GL.UniformMatrix3(Location, count, transpose, value); }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Matrix4
|
||||
|
||||
public void SetMatrix4(Matrix4 matrix, bool transpose = false)
|
||||
{
|
||||
GL.UniformMatrix4(Location, transpose, ref matrix);
|
||||
}
|
||||
public void SetMatrix4(ref Matrix4 matrix, bool transpose = false) { GL.UniformMatrix4(Location, transpose, ref matrix); }
|
||||
|
||||
public void SetMatrix4(int count, ref double value, bool transpose = false) { GL.UniformMatrix4(Location, count, transpose, ref value); }
|
||||
public void SetMatrix4(int count, ref float value, bool transpose = false) { GL.UniformMatrix4(Location, count, transpose, ref value); }
|
||||
|
||||
public void SetMatrix4(int count, double[] value, bool transpose = false) { GL.UniformMatrix4(Location, count, transpose, value); }
|
||||
public void SetMatrix4(int count, float[] value, bool transpose = false) { GL.UniformMatrix4(Location, count, transpose, value); }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
34
SMCode/SM.OGL/Shaders/UniformCollection.cs
Normal file
34
SMCode/SM.OGL/Shaders/UniformCollection.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace SM.OGL.Shaders
|
||||
{
|
||||
public class UniformCollection : Dictionary<string, Uniform>
|
||||
{
|
||||
internal GenericShader _parentShader;
|
||||
|
||||
public new Uniform this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
return base[key];
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
Console.WriteLine("[Error] Uniform '"+key+"' was not found. Tried to recreate it.");
|
||||
Uniform u = new Uniform(GL.GetUniformLocation(_parentShader, key), this);
|
||||
Add(key, u);
|
||||
return u;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(string key, int location)
|
||||
{
|
||||
base.Add(key, new Uniform(location, this));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue