#region usings
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Texture;
#endregion
namespace SM.OGL.Shaders
{
///
/// Manages the uniforms.
///
public struct Uniform : IUniform
{
///
/// This contains the location for the uniform.
///
public int Location { get; internal set; }
///
/// This contains the Parent collection of this uniform.
///
public UniformCollection Parent { get; }
#region Constructors
///
/// This creates a new uniform manager, that has a null parent.
///
///
public Uniform(int location) : this(location, null)
{
}
///
/// This creates a new uniform manager, that get the location from the provided shader and with a null parent.
///
///
///
public Uniform(string name, GenericShader shader) : this(GL.GetUniformLocation(shader, name), null)
{
}
///
/// This creates a new uniform manager, that get the location from the provided shader and with a parent.
///
///
///
///
public Uniform(string name, GenericShader shader, UniformCollection parent) : this(GL.GetUniformLocation(shader, name), parent)
{
}
///
/// This create a new uniform manager
///
/// Location id
/// Parent collection
public Uniform(int location, UniformCollection parent)
{
Location = location;
Parent = parent;
}
#endregion
#region Uniform1
///
/// Set a boolean as value.
///
///
public void SetBool(bool value)
{
GL.Uniform1(Location, value ? 1 : 0);
}
///
/// Sets a integer.
///
///
public void SetInt(int value)
{
GL.Uniform1(Location, value);
}
///
/// Sets an array of integers.
///
///
public void SetInt(params int[] values)
{
GL.Uniform1(Location, values.Length, values);
}
///
/// Set a unsigned integer.
///
///
public void SetUInt(uint value)
{
GL.Uniform1(Location, value);
}
///
/// Set an array of unsigned integers.
///
///
public void SetUInt(params uint[] values)
{
GL.Uniform1(Location, values.Length, values);
}
///
/// Sets a float.
///
///
public void SetFloat(float value)
{
GL.Uniform1(Location, value);
}
///
/// Sets an array of floats.
///
///
public void SetFloat(params float[] values)
{
GL.Uniform1(Location, values.Length, values);
}
#endregion
#region Uniform2
///
/// Sets a float vector2 by providing the values.
///
///
///
public void SetVector2(float x, float y)
{
GL.Uniform2(Location, x, y);
}
///
/// Sets a unsigned integer vector2 by providing the values.
///
///
///
public void SetVector2(uint x, uint y)
{
GL.Uniform2(Location, x, y);
}
///
/// Sets a integer vector2 by providing the values.
///
///
///
public void SetVector2(int x, int y)
{
GL.Uniform2(Location, x, y);
}
///
/// Sets a float vector2.
///
///
public void SetVector2(Vector2 vector2)
{
GL.Uniform2(Location, vector2);
}
///
/// Sets a float vector2 by refencing.
///
///
public void SetVector2(ref Vector2 vector2)
{
GL.Uniform2(Location, ref vector2);
}
///
/// Sets a array of vector2.
///
///
public void SetVector2(params Vector2[] values)
{
float[] newValues = new float[values.Length * 2];
for(int i = 0; i < values.Length; i++)
{
Vector2 val = values[i];
int newi = i * 2;
newValues[newi] = val.X;
newValues[newi + 1] = val.Y;
}
GL.Uniform2(Location, values.Length, newValues);
}
///
/// Sets a float array that get converted to a vector2 array.
///
///
public void SetVector2(params float[] values)
{
GL.Uniform2(Location, values.Length / 2, values);
}
///
/// Sets a integer array that get converted to a ivector2 array.
///
///
public void SetVector2(params int[] values)
{
GL.Uniform2(Location, values.Length / 2, values);
}
///
/// Sets a unsigned integer array that get converted to a unsigned integer vector2 array.
///
///
public void SetVector2(params uint[] values)
{
GL.Uniform2(Location, values.Length / 2, values);
}
#endregion
#region Uniform3
///
/// Sets a float vector3 by providing the values.
///
///
///
///
public void SetVector3(float x, float y, float z)
{
GL.Uniform3(Location, x, y, z);
}
///
/// Sets a unsigned integer vector3 by providing the values.
///
///
///
///
public void SetVector3(uint x, uint y, uint z)
{
GL.Uniform3(Location, x, y, z);
}
///
/// Sets a integer vector3 by providing the values.
///
///
///
///
public void SetVector3(int x, int y, int z)
{
GL.Uniform3(Location, x, y, z);
}
///
/// Sets a vector3.
///
///
public void SetVector3(Vector3 vector)
{
GL.Uniform3(Location, vector);
}
///
/// Sets a vector3 by reference.
///
///
public void SetVector3(ref Vector3 vector)
{
GL.Uniform3(Location, ref vector);
}
///
/// Sets a array of vector3.
///
///
public void SetVector3(params Vector3[] values)
{
float[] newValues = new float[values.Length * 3];
for (int i = 0; i < values.Length; i++)
{
Vector3 val = values[i];
int newi = i * 3;
newValues[newi] = val.X;
newValues[newi + 1] = val.Y;
newValues[newi + 2] = val.Z;
}
GL.Uniform3(Location, values.Length, newValues);
}
///
/// Sets a array by providing the components.
///
///
public void SetVector3(params float[] values)
{
GL.Uniform3(Location, values.Length / 3, values);
}
///
/// Sets a array by providing the components.
///
///
public void SetVector3(params int[] values)
{
GL.Uniform3(Location, values.Length / 3, values);
}
///
/// Sets a array by providing the components.
///
///
public void SetVector3(params uint[] values)
{
GL.Uniform3(Location, values.Length / 3, values);
}
#endregion
#region Uniform4
///
/// Sets a vector4 by providing the values.
///
///
///
///
///
public void SetVector4(float x, float y, float z, float w)
{
GL.Uniform4(Location, x, y, z, w);
}
///
/// Sets a vector4 by providing the values.
///
///
///
///
///
public void SetVector4(uint x, uint y, uint z, uint w)
{
GL.Uniform4(Location, x, y, z, w);
}
///
/// Sets a vector4 by providing the values.
///
///
///
///
///
public void SetVector4(int x, int y, int z, int w)
{
GL.Uniform4(Location, x, y, z, w);
}
///
/// Sets a vector4.
///
///
public void SetVector4(Vector4 vector)
{
GL.Uniform4(Location, vector);
}
///
/// Sets a vector4.
///
///
public void SetVector4(ref Vector4 vector)
{
GL.Uniform4(Location, ref vector);
}
///
/// Sets a array of Vector4.
///
///
public void SetVector4(params Vector4[] values)
{
float[] newValues = new float[values.Length * 4];
for (int i = 0; i < values.Length; i++)
{
Vector4 val = values[i];
int newi = i * 3;
newValues[newi] = val.X;
newValues[newi + 1] = val.Y;
newValues[newi + 2] = val.Z;
newValues[newi + 3] = val.W;
}
GL.Uniform3(Location, values.Length, newValues);
}
///
/// Sets a array by providing the components.
///
///
public void SetVector4(params float[] values)
{
GL.Uniform4(Location, values.Length / 4, values);
}
///
/// Sets a array by providing the components.
///
///
public void SetVector4(params int[] values)
{
GL.Uniform4(Location, values.Length / 4, values);
}
///
/// Sets a array by providing the components.
///
///
public void SetVector4(params uint[] values)
{
GL.Uniform4(Location, values.Length / 4, values);
}
///
/// Sets a quaternion.
///
///
public void SetQuaternion(Quaternion quaternion)
{
GL.Uniform4(Location, quaternion);
}
#endregion
#region Matrix2
///
/// Sets a matrix2.
///
///
/// If true, the matrix will be transposed.
public void SetMatrix2(Matrix2 matrix, bool transpose = false)
{
GL.UniformMatrix2(Location, transpose, ref matrix);
}
///
/// Sets a matrix2 array.
///
///
///
/// If true, the matrix will be transposed.
public void SetMatrix2(int count, float[] value, bool transpose = false)
{
GL.UniformMatrix2(Location, count, transpose, value);
}
#endregion
#region Matrix3
///
/// Sets a matrix3.
///
///
/// If true, the matrix will be transposed.
public void SetMatrix3(Matrix3 matrix, bool transpose = false)
{
GL.UniformMatrix3(Location, transpose, ref matrix);
}
///
/// Sets a matrix3 array.
///
///
///
/// If true, the matrix will be transposed.
public void SetMatrix3(int count, float[] value, bool transpose = false)
{
GL.UniformMatrix3(Location, count, transpose, value);
}
#endregion
#region Matrix4
///
/// Sets a matrix4.
///
///
/// If true, the matrix will be transposed.
public void SetMatrix4(Matrix4 matrix, bool transpose = false)
{
GL.UniformMatrix4(Location, transpose, ref matrix);
}
///
/// Sets a matrix4 by reference.
///
///
/// If true, the matrix will be transposed.
public void SetMatrix4(ref Matrix4 matrix, bool transpose = false)
{
GL.UniformMatrix4(Location, transpose, ref matrix);
}
///
/// Sets a matrix4 array.
///
///
///
/// If true, the matrix will be transposed.
public void SetMatrix4(int count, float[] value, bool transpose = false)
{
GL.UniformMatrix4(Location, count, transpose, value);
}
#endregion
///
/// Sets the color.
///
///
public void SetColor(Color4 color)
{
GL.Uniform4(Location, color);
}
///
/// Try to sets the texture at the next possible position and tells the checkUniform, if worked or not.
///
/// The texture you want to add
/// The check uniform.
public void SetTexture(TextureBase texture, Uniform checkUniform)
{
checkUniform.SetBool(texture != null);
if (texture != null) SetTexture(texture);
}
///
/// Try to sets the texture at the specified position and tells the checkUniform, if worked or not.
///
/// The texture you want to add
/// The position
/// The check uniform.
public void SetTexture(TextureBase texture, int pos, Uniform checkUniform)
{
checkUniform.SetBool(texture != null);
if (texture != null) SetTexture(texture);
}
///
/// Sets the texture to the next possible position.
///
///
public void SetTexture(TextureBase texture)
{
if (Parent != null) SetTexture(texture, Parent.NextTexture);
}
///
/// Sets the texture to the specified position.
///
///
///
public void SetTexture(TextureBase texture, int texturePos)
{
Parent.NextTexture = texturePos + 1;
GL.ActiveTexture(TextureUnit.Texture0 + texturePos);
GL.BindTexture(texture.Target, texture);
SetInt(texturePos);
}
///
/// Returns the location from the uniform
///
///
public static implicit operator int(Uniform u)
{
return u.Location;
}
}
}