+ comments to the SM.Base

+ Length, Normalize methods for CVectors
This commit is contained in:
Michel Fedde 2020-12-19 20:49:16 +01:00
parent 5d4b360b05
commit 9b917ac181
18 changed files with 248 additions and 33 deletions

View file

@ -11,6 +11,9 @@ namespace SM.Base.Drawing
/// </summary> /// </summary>
public abstract class GenericTransformation public abstract class GenericTransformation
{ {
/// <summary>
/// If true, ignores the transformation and sends <see cref="Matrix4.Identity"/>, when requested.
/// </summary>
public bool Ignore = false; public bool Ignore = false;
/// <summary> /// <summary>

View file

@ -54,6 +54,10 @@ namespace SM.Base.Drawing.Particles
/// </summary> /// </summary>
public abstract Func<TDirection, ParticleContext, TDirection> MovementCalculation { get; set; } public abstract Func<TDirection, ParticleContext, TDirection> MovementCalculation { get; set; }
/// <summary>
/// Sets up the timer.
/// </summary>
/// <param name="duration">Duration how long the particles should live</param>
protected ParticleDrawingBasis(TimeSpan duration) protected ParticleDrawingBasis(TimeSpan duration)
{ {
timer = new Timer(duration); timer = new Timer(duration);

View file

@ -1,5 +1,6 @@
#region usings #region usings
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Mesh; using SM.OGL.Mesh;
#endregion #endregion
@ -12,14 +13,15 @@ namespace SM.Base.Objects
/// <summary> /// <summary>
/// While initializing, it will add the <see cref="Color" /> to the data index. /// While initializing, it will add the <see cref="Color" /> to the data index.
/// </summary> /// </summary>
public Mesh() public Mesh(PrimitiveType type)
{ {
PrimitiveType = type;
Attributes.Add(3, "color", Color); Attributes.Add(3, "color", Color);
} }
/// <summary> /// <summary>
/// Contains vertex colors /// Contains vertex colors
/// </summary> /// </summary>
public virtual VBO Color { get; } public virtual VBO Color { get; protected set; }
} }
} }

View file

@ -4,13 +4,24 @@ using SM.OGL.Mesh;
namespace SM.Base.Objects.Static namespace SM.Base.Objects.Static
{ {
/// <summary>
/// An AxisHelper-Model
/// <para>White: -X, -Y, -Z</para>
/// <para>Red: +X </para>
/// <para>Green: +Y </para>
/// <para>Blue: +Z </para>
/// </summary>
public class AxisHelper : Mesh public class AxisHelper : Mesh
{ {
/// <summary>
/// Object
/// </summary>
public static AxisHelper Object = new AxisHelper(); public static AxisHelper Object = new AxisHelper();
private AxisHelper() {} private AxisHelper() : base(PrimitiveType.Lines) {}
public override VBO Vertex { get; } = new VBO() /// <inheritdoc />
public override VBO Vertex { get; protected set; } = new VBO()
{ {
{0, 0, 0}, {0, 0, 0},
{.5f, 0, 0}, {.5f, 0, 0},
@ -20,7 +31,8 @@ namespace SM.Base.Objects.Static
{0, 0, .5f}, {0, 0, .5f},
}; };
public override VBO Color { get; } = new VBO(pointerSize:4) /// <inheritdoc />
public override VBO Color { get; protected set; } = new VBO(pointerSize:4)
{ {
{Color4.White}, {Color4.White},
{Color4.Red}, {Color4.Red},
@ -29,7 +41,5 @@ namespace SM.Base.Objects.Static
{Color4.White}, {Color4.White},
{Color4.DarkBlue}, {Color4.DarkBlue},
}; };
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Lines;
} }
} }

View file

@ -25,7 +25,7 @@ namespace SM.Base.Objects.Static
} }
/// <inheritdoc /> /// <inheritdoc />
public override VBO Vertex { get; } = new VBO public override VBO Vertex { get; protected set; } = new VBO
{ {
{-.5f, -.5f, 0}, {-.5f, -.5f, 0},
{-.5f, .5f, 0}, {-.5f, .5f, 0},
@ -34,7 +34,7 @@ namespace SM.Base.Objects.Static
}; };
/// <inheritdoc /> /// <inheritdoc />
public override VBO UVs { get; } = new VBO(pointerSize: 2) public override VBO UVs { get; protected set; } = new VBO(pointerSize: 2)
{ {
{0, 1}, {0, 1},
{0, 0}, {0, 0},
@ -43,7 +43,7 @@ namespace SM.Base.Objects.Static
}; };
/// <inheritdoc /> /// <inheritdoc />
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Quads; public override PrimitiveType PrimitiveType { get; protected set; } = PrimitiveType.Quads;
/// <inheritdoc /> /// <inheritdoc />
public override BoundingBox BoundingBox { get; } = public override BoundingBox BoundingBox { get; } =

View file

@ -10,25 +10,39 @@ using SM.OGL.Shaders;
namespace SM.Base.PostProcess namespace SM.Base.PostProcess
{ {
/// <summary>
/// Basis for a post process effect
/// </summary>
public abstract class PostProcessEffect public abstract class PostProcessEffect
{ {
internal static Matrix4 Mvp; internal static Matrix4 Mvp;
internal static Matrix4 Model; internal static Matrix4 Model;
public virtual ICollection<Framebuffer> RequiredFramebuffers { get; }
/// <summary>
/// Method, to initialize the shader.
/// </summary>
public virtual void Init() {} public virtual void Init() {}
/// <summary>
/// Method, to initialize the shader.
/// </summary>
public virtual void Init(Framebuffer main) public virtual void Init(Framebuffer main)
{ {
Init(); Init();
} }
public virtual void Draw(Framebuffer main, Framebuffer target) /// <summary>
{ /// Method to draw the actual effect.
/// </summary>
} /// <param name="main">The framebuffer, that was used.</param>
/// <param name="target">The framebuffer, the system should draw to.</param>
public abstract void Draw(Framebuffer main, Framebuffer target);
/// <summary>
/// Event, when the scene changed.
/// </summary>
public virtual void SceneChanged(GenericScene scene) public virtual void SceneChanged(GenericScene scene)
{ {

View file

@ -41,6 +41,9 @@ namespace SM.Base.Textures
get => _height ?? Map.Height; get => _height ?? Map.Height;
protected set => _height = value; protected set => _height = value;
} }
/// <summary>
/// Aspect ratio of Width and Height of the texture
/// </summary>
public float Aspect { get; private set; } public float Aspect { get; private set; }
/// <summary> /// <summary>

View file

@ -12,8 +12,6 @@ namespace SM.Base.Time
/// </summary> /// </summary>
public class Interval : Timer public class Interval : Timer
{ {
private bool _stop;
/// <inheritdoc /> /// <inheritdoc />
public Interval(float seconds) : base(seconds) public Interval(float seconds) : base(seconds)
{ {

View file

@ -16,8 +16,15 @@ namespace SM.Base.Time
private static List<Stopwatch> _activeStopwatches = new List<Stopwatch>(); private static List<Stopwatch> _activeStopwatches = new List<Stopwatch>();
private bool _paused = false; private bool _paused = false;
/// <summary>
/// If true, the stopwatch was started.
/// <para>This doesn't changed when paused.</para>
/// </summary>
public bool Active { get; private set; } = false; public bool Active { get; private set; } = false;
/// <summary>
/// Gets/Sets if the stopwatch is paused.
/// </summary>
public bool Paused public bool Paused
{ {
get => _paused; get => _paused;
@ -30,6 +37,9 @@ namespace SM.Base.Time
} }
} }
/// <summary>
/// If true, the stopwatch is active and not paused... (who would have guessed...)
/// </summary>
public bool Running => Active && !Paused; public bool Running => Active && !Paused;
/// <summary> /// <summary>

View file

@ -1,20 +1,90 @@
namespace SM.Base.Types using System;
using OpenTK;
namespace SM.Base.Types
{ {
/// <summary>
/// A One-dimensional Vector (also known as <see cref="float"/>), in a class.
/// </summary>
public class CVector1 public class CVector1
{ {
/// <summary>
/// X - Component
/// </summary>
public float X { get; set; } public float X { get; set; }
/// <summary>
/// The length/magnitute of the vector.
/// </summary>
public float Length => GetLength();
/// <summary>
/// Gets the square of the vector length (magnitude).
/// </summary>
/// <remarks>
/// This property avoids the costly square root operation required by the Length property. This makes it more suitable
/// for comparisons.
/// </remarks>
public float LengthSquared => GetLength(true);
/// <summary>
/// Creates a class vector
/// </summary>
/// <param name="x">X-Component</param>
public CVector1(float x) public CVector1(float x)
{ {
X = x; X = x;
} }
/// <summary>
/// Sets the X-Component.
/// </summary>
/// <param name="x">X-Component</param>
public virtual void Set(float x) public virtual void Set(float x)
{ {
X = x; X = x;
} }
/// <summary>
/// Get the length of the vector.
/// </summary>
/// <param name="squared">If true, it will return the squared product.</param>
/// <returns></returns>
public float GetLength(bool squared = false)
{
float length = GetLengthProcess();
if (squared) return length;
return (float) Math.Sqrt(length);
}
/// <summary>
/// Normalizes the vector.
/// </summary>
public void Normalize()
{
float length = GetLength();
NormalizationProcess(length);
}
/// <summary>
/// Conversion into <see cref="float"/>
/// </summary>
public static implicit operator float(CVector1 vector1) => vector1.X; public static implicit operator float(CVector1 vector1) => vector1.X;
/// <summary>
/// Conversion from <see cref="float"/> to One-dimensional Vector.
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
public static implicit operator CVector1(float f) => new CVector1(f); public static implicit operator CVector1(float f) => new CVector1(f);
private protected virtual float GetLengthProcess()
{
return X * X;
}
private protected virtual void NormalizationProcess(float length)
{
X *= length;
}
} }
} }

View file

@ -2,38 +2,81 @@
namespace SM.Base.Types namespace SM.Base.Types
{ {
/// <summary>
/// A two-dimensional vector.
/// </summary>
public class CVector2 : CVector1 public class CVector2 : CVector1
{ {
/// <summary>
/// Y-component
/// </summary>
public float Y { get; set; } public float Y { get; set; }
/// <summary>
/// Creates a vector, where each component is the same value.
/// </summary>
/// <param name="uniform">The Value</param>
public CVector2(float uniform) : base(uniform) public CVector2(float uniform) : base(uniform)
{ {
Y = uniform; Y = uniform;
} }
/// <summary>
/// Creates a vector
/// </summary>
public CVector2(float x, float y) : base(x) public CVector2(float x, float y) : base(x)
{ {
Y = y; Y = y;
} }
public virtual void Set(float uniform) private protected override float GetLengthProcess()
{
return base.GetLengthProcess() + Y * Y;
}
private protected override void NormalizationProcess(float length)
{
base.NormalizationProcess(length);
Y *= length;
}
/// <summary>
/// Sets each component to the same value
/// </summary>
/// <param name="uniform"></param>
public override void Set(float uniform)
{ {
base.Set(uniform); base.Set(uniform);
Y = uniform; Y = uniform;
} }
/// <summary>
/// Sets each component to the <see cref="Vector2"/> counter-part.
/// </summary>
/// <param name="vector"></param>
public void Set(Vector2 vector) public void Set(Vector2 vector)
{ {
Set(vector.X, vector.Y); Set(vector.X, vector.Y);
} }
/// <summary>
/// Sets the a own value to each component.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void Set(float x, float y) public void Set(float x, float y)
{ {
base.Set(x); base.Set(x);
Y = y; Y = y;
} }
/// <summary>
/// Converts to <see cref="Vector2"/>
/// </summary>
public static implicit operator Vector2(CVector2 vector2) => new Vector2(vector2.X, vector2.Y); public static implicit operator Vector2(CVector2 vector2) => new Vector2(vector2.X, vector2.Y);
/// <summary>
/// Converts from <see cref="Vector2"/> to <see cref="CVector2"/>.
/// </summary>
public static implicit operator CVector2(Vector2 vector2) => new CVector2(vector2.X, vector2.Y); public static implicit operator CVector2(Vector2 vector2) => new CVector2(vector2.X, vector2.Y);
} }
} }

View file

@ -2,38 +2,74 @@
namespace SM.Base.Types namespace SM.Base.Types
{ {
/// <summary>
/// A three-dimensional vector.
/// </summary>
public class CVector3 : CVector2 public class CVector3 : CVector2
{ {
/// <summary>
/// Z-component
/// </summary>
public float Z { get; set; } public float Z { get; set; }
/// <summary>
/// Creates a vector, where each component is the same value.
/// </summary>
/// <param name="uniform">The Value</param>
public CVector3(float uniform) : base(uniform) public CVector3(float uniform) : base(uniform)
{ {
Z = uniform; Z = uniform;
} }
/// <summary>
/// Creates a vector
/// </summary>
public CVector3(float x, float y, float z) : base(x, y) public CVector3(float x, float y, float z) : base(x, y)
{ {
Z = z; Z = z;
} }
private protected override float GetLengthProcess()
{
return base.GetLengthProcess() + Z * Z;
}
private protected override void NormalizationProcess(float length)
{
base.NormalizationProcess(length);
Z *= length;
}
/// <inheritdoc />
public override void Set(float uniform) public override void Set(float uniform)
{ {
base.Set(uniform); base.Set(uniform);
Z = uniform; Z = uniform;
} }
/// <summary>
/// Sets the a own value to each component.
/// </summary>
public void Set(float x, float y, float z) public void Set(float x, float y, float z)
{ {
base.Set(x,y); base.Set(x,y);
Z = z; Z = z;
} }
/// <summary>
/// Sets each component to the <see cref="Vector3"/> counter-part.
/// </summary>
/// <param name="vector"></param>
public void Set(Vector3 vector) public void Set(Vector3 vector)
{ {
Set(vector.X, vector.Y, vector.Z); Set(vector.X, vector.Y, vector.Z);
} }
/// <summary>
/// Converts to <see cref="Vector3"/>
/// </summary>
public static implicit operator Vector3(CVector3 vector) => new Vector3(vector.X, vector.Y, vector.Z); public static implicit operator Vector3(CVector3 vector) => new Vector3(vector.X, vector.Y, vector.Z);
/// <summary>
/// Converts from <see cref="Vector3"/> to <see cref="CVector3"/>.
/// </summary>
public static implicit operator CVector3(Vector3 vector) => new CVector3(vector.X, vector.Y, vector.Z); public static implicit operator CVector3(Vector3 vector) => new CVector3(vector.X, vector.Y, vector.Z);
} }
} }

View file

@ -84,6 +84,9 @@ namespace SM.Utility
return (float) Randomizer.NextDouble() * (max - min) + min; return (float) Randomizer.NextDouble() * (max - min) + min;
} }
/// <summary>
/// Gets a random item from the provided list.
/// </summary>
public static TSource GetRandomItem<TSource>(this IList<TSource> list) public static TSource GetRandomItem<TSource>(this IList<TSource> list)
{ {
return list[GetInt(0, list.Count - 1)]; return list[GetInt(0, list.Count - 1)];

View file

@ -182,6 +182,9 @@ namespace SM.Base
if (!CursorVisible) CursorVisible = true; if (!CursorVisible) CursorVisible = true;
} }
/// <summary>
/// Create a bitmap from the framebuffer.
/// </summary>
public Bitmap TakeScreenshot(Framebuffer framebuffer, ReadBufferMode readBuffer, int x, int y, int width, int height) public Bitmap TakeScreenshot(Framebuffer framebuffer, ReadBufferMode readBuffer, int x, int y, int width, int height)
{ {
GL.GetInteger(GetPName.FramebufferBinding, out int prevFBId); GL.GetInteger(GetPName.FramebufferBinding, out int prevFBId);

View file

@ -16,8 +16,14 @@ namespace SM.Base
/// </summary> /// </summary>
public abstract class RenderPipeline public abstract class RenderPipeline
{ {
/// <summary>
/// If true, this pipeline was already once activated.
/// </summary>
public bool IsInitialized { get; private set; } = false; public bool IsInitialized { get; private set; } = false;
/// <summary>
/// The window the pipeline is connected to.
/// </summary>
protected GenericWindow _window { get; private set; } protected GenericWindow _window { get; private set; }
/// <summary> /// <summary>
@ -76,6 +82,10 @@ namespace SM.Base
} }
/// <summary>
/// Occurs, when the pipeline was connected to a window the first time.
/// </summary>
/// <param name="window"></param>
protected internal virtual void Initialization(GenericWindow window) protected internal virtual void Initialization(GenericWindow window)
{ {
@ -88,6 +98,10 @@ namespace SM.Base
{ {
} }
/// <summary>
/// Creates a framebuffer, that has specific (often) required settings already applied.
/// </summary>
/// <returns></returns>
public static Framebuffer CreateWindowFramebuffer() public static Framebuffer CreateWindowFramebuffer()
{ {
Framebuffer framebuffer = new Framebuffer(window: SMRenderer.CurrentWindow); Framebuffer framebuffer = new Framebuffer(window: SMRenderer.CurrentWindow);
@ -112,6 +126,10 @@ namespace SM.Base
context.ActivePipeline = this; context.ActivePipeline = this;
} }
/// <summary>
/// Event, that triggers, when the scene in the current window changes.
/// </summary>
/// <param name="scene"></param>
protected internal virtual void SceneChanged(TScene scene) protected internal virtual void SceneChanged(TScene scene)
{ {

View file

@ -37,22 +37,22 @@ namespace SM.OGL.Mesh
/// The primitive type, that determinants how the mesh is drawn. /// The primitive type, that determinants how the mesh is drawn.
/// <para>Default: Triangles</para> /// <para>Default: Triangles</para>
/// </summary> /// </summary>
public virtual PrimitiveType PrimitiveType { get; } = PrimitiveType.Triangles; public virtual PrimitiveType PrimitiveType { get; protected set; } = PrimitiveType.Triangles;
/// <summary> /// <summary>
/// Contains the vertices for the mesh. /// Contains the vertices for the mesh.
/// </summary> /// </summary>
public virtual VBO Vertex { get; } public virtual VBO Vertex { get; protected set; }
/// <summary> /// <summary>
/// Contains the texture coords for the mesh. /// Contains the texture coords for the mesh.
/// </summary> /// </summary>
public virtual VBO UVs { get; } public virtual VBO UVs { get; protected set; }
/// <summary> /// <summary>
/// Contains the normals for the mesh. /// Contains the normals for the mesh.
/// </summary> /// </summary>
public virtual VBO Normals { get; } public virtual VBO Normals { get; protected set; }
/// <summary> /// <summary>
/// Represents the bounding box. /// Represents the bounding box.

View file

@ -22,8 +22,6 @@ namespace SM2D.Light
public override void Draw(Framebuffer main, Framebuffer target) public override void Draw(Framebuffer main, Framebuffer target)
{ {
base.Draw(main, target);
_shader.Draw(main.ColorAttachments["color"], collection => _shader.Draw(main.ColorAttachments["color"], collection =>
{ {
collection["FragSize"].SetUniform2((SMRenderer.CurrentWindow as GLWindow2D).WorldScale); collection["FragSize"].SetUniform2((SMRenderer.CurrentWindow as GLWindow2D).WorldScale);

View file

@ -14,7 +14,7 @@ namespace SM2D.Object
{ {
public class Polygon : Mesh public class Polygon : Mesh
{ {
public Polygon(ICollection<Vector2> vertices) public Polygon(ICollection<Vector2> vertices) : base(PrimitiveType.TriangleFan)
{ {
foreach (var vertex in vertices) foreach (var vertex in vertices)
{ {
@ -25,7 +25,7 @@ namespace SM2D.Object
foreach (var vertex in vertices) AddUV(vertex); foreach (var vertex in vertices) AddUV(vertex);
} }
public Polygon(ICollection<PolygonVertex> vertices) public Polygon(ICollection<PolygonVertex> vertices) : base(PrimitiveType.TriangleFan)
{ {
foreach (var polygonVertex in vertices) foreach (var polygonVertex in vertices)
{ {
@ -36,11 +36,11 @@ namespace SM2D.Object
foreach (var vertex in vertices) AddUV(vertex.Vertex); foreach (var vertex in vertices) AddUV(vertex.Vertex);
} }
public override VBO Vertex { get; } = new VBO(); public override VBO Vertex { get; protected set; } = new VBO();
public override VBO UVs { get; } = new VBO(pointerSize: 2); public override VBO UVs { get; protected set; } = new VBO(pointerSize: 2);
public override VBO Color { get; } = new VBO(pointerSize: 4); public override VBO Color { get; protected set; } = new VBO(pointerSize: 4);
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.TriangleFan; public override PrimitiveType PrimitiveType { get; protected set; } = PrimitiveType.TriangleFan;
private void AddVertex(Vector2 vertex) private void AddVertex(Vector2 vertex)
{ {