07.10.2020

+ Parent, Name and Flags to objects.

~ Improved Matrix calculations
This commit is contained in:
Michel Fedde 2020-10-08 12:25:20 +02:00
parent f865496414
commit 2c00dbd31a
21 changed files with 383 additions and 42 deletions

View file

@ -1,4 +1,5 @@
using SM.Base.Contexts;
using System.Collections.Generic;
using SM.Base.Contexts;
using SM.Base.Objects.Static;
using SM.OGL.Mesh;
@ -16,7 +17,16 @@ namespace SM.Base.Scene
/// <summary>
/// The mesh it should use.
/// </summary>
protected GenericMesh _mesh = Defaults.DefaultMesh;
protected GenericMesh _mesh = SMRenderer.DefaultMesh;
/// <inheritdoc />
public object Parent { get; set; }
/// <inheritdoc />
public string Name { get; set; } = "Unnamed draw object";
/// <inheritdoc />
public ICollection<string> Flags { get; set; }
/// <inheritdoc />
public virtual void Update(UpdateContext context)
@ -33,6 +43,18 @@ namespace SM.Base.Scene
DrawContext(ref context);
}
/// <inheritdoc />
public virtual void OnAdded(object sender)
{
}
/// <inheritdoc />
public virtual void OnRemoved(object sender)
{
}
/// <summary>
/// Draws the context, that was given to them.
/// </summary>

View file

@ -7,10 +7,34 @@ namespace SM.Base.Scene
/// </summary>
public abstract class GenericTransformation
{
/// <summary>
/// Contains the current model matrix.
/// </summary>
protected Matrix4 _modelMatrix { get; private set; }
/// <summary>
/// Contains the last frame the matrix was calculated.
/// </summary>
protected ulong _lastFrame { get; private set; }
/// <summary>
/// Returns the current model matrix.
/// </summary>
/// <returns></returns>
public Matrix4 GetMatrix()
{
if (_lastFrame != SMRenderer.CurrentFrame)
{
_lastFrame = SMRenderer.CurrentFrame;
_modelMatrix = RequestMatrix();
}
return _modelMatrix;
}
/// <summary>
/// Calculates the current matrix.
/// </summary>
/// <returns>The current matrix.</returns>
public abstract Matrix4 GetMatrix();
protected abstract Matrix4 RequestMatrix();
}
}

View file

@ -0,0 +1,16 @@
using System.Collections.Generic;
using SM.OGL.Framebuffer;
namespace SM.Base.PostProcessing
{
public abstract class PostProcessingEffect
{
public virtual Dictionary<string, int> AdditionalFramebufferOutputs { get; }
public virtual ICollection<Framebuffer> AdditionalFramebuffers { get; }
public void ApplyOutputs(Framebuffer mainbuffer)
{
mainbuffer.Append();
}
}
}

View file

@ -52,18 +52,19 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Controls\Mouse.cs" />
<Compile Include="Defaults.cs" />
<Compile Include="Drawing\DrawingBasis.cs" />
<Compile Include="Drawing\GenericTransformation.cs" />
<Compile Include="Drawing\Instance.cs" />
<Compile Include="Drawing\IShader.cs" />
<Compile Include="Log.cs" />
<Compile Include="Objects\Mesh.cs" />
<Compile Include="PostProcessing\PostProcessingEffect.cs" />
<Compile Include="Scene\IShowCollection.cs" />
<Compile Include="Scene\IShowItem.cs" />
<Compile Include="Drawing\Material.cs" />
<Compile Include="Scene\IBackgroundItem.cs" />
<Compile Include="Scene\GenericItemCollection.cs" />
<Compile Include="SMRenderer.cs" />
<Compile Include="Textures\Texture.cs" />
<Compile Include="Text\CharParameter.cs" />
<Compile Include="Text\Font.cs" />
@ -79,6 +80,7 @@
<Compile Include="Utility\Assembly.cs" />
<Compile Include="Utility\Deltatime.cs" />
<Compile Include="Utility\Randomize.cs" />
<Compile Include="Utility\RotationUtility.cs" />
<Compile Include="Window\Contexts\DrawContext.cs" />
<Compile Include="Window\Contexts\UpdateContext.cs" />
<Compile Include="Window\GenericWindow.cs" />

View file

@ -1,6 +1,4 @@
using SM.Base.Objects;
using SM.Base.Objects.Static;
using SM.Base.Scene;
using SM.Base.Objects.Static;
using SM.Base.Text;
using SM.OGL.Mesh;
using SM.Utility;
@ -8,9 +6,9 @@ using SM.Utility;
namespace SM.Base
{
/// <summary>
/// The default options.
/// Contains different information about this renderer.
/// </summary>
public class Defaults
public class SMRenderer
{
/// <summary>
/// The default mesh.
@ -26,5 +24,10 @@ namespace SM.Base
/// The default deltatime helper.
/// </summary>
public static Deltatime DefaultDeltatime = new Deltatime();
/// <summary>
/// Current Frame
/// </summary>
public static ulong CurrentFrame { get; internal set; } = 0;
}
}

View file

@ -14,7 +14,37 @@ namespace SM.Base.Scene
public List<TItem> Objects => this;
/// <inheritdoc />
public void Update(UpdateContext context)
public object Parent { get; set; }
/// <inheritdoc />
public string Name { get; set; } = "Unnamed Item Collection";
/// <inheritdoc />
public ICollection<string> Flags { get; set; } = new[] {"collection"};
/// <summary>
/// Adds a item.
/// </summary>
public new void Add(TItem item)
{
base.Add(item);
item.Parent = this;
item.OnAdded(this);
}
/// <summary>
/// Removes a item.
/// </summary>
/// <param name="item"></param>
public new void Remove(TItem item)
{
base.Remove(item);
item.Parent = null;
item.OnRemoved(this);
}
/// <inheritdoc />
public virtual void Update(UpdateContext context)
{
for(int i = 0; i < Objects.Count; i++)
this[i].Update(context);
@ -26,6 +56,64 @@ namespace SM.Base.Scene
for (int i = 0; i < Objects.Count; i++)
this[i].Draw(context);
}
/// <inheritdoc />
public virtual void OnAdded(object sender)
{
}
/// <inheritdoc />
public virtual void OnRemoved(object sender)
{ }
/// <summary>
/// Returns a object with this name or the default, if not available.
/// <para>Not reclusive.</para>
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public TItem GetItemByName(string name)
{
TItem obj = default;
for (var i = 0; i < this.Count; i++)
{
if (this[i].Name == name)
{
obj = this[i];
break;
}
}
return obj;
}
/// <summary>
/// Returns a object with this name or the default if not available.
/// <para>Not reclusive.</para>
/// </summary>
/// <typeparam name="TGetItem">Type of return</typeparam>
public TGetItem GetItemByName<TGetItem>(string name)
where TGetItem : TItem
{
return (TGetItem)GetItemByName(name);
}
/// <summary>
/// Returns all object that have this flag.
/// <para>Only in this list.</para>
/// </summary>
public ICollection<TItem> GetItemsWithFlag(string flag)
{
List<TItem> list = new List<TItem>();
for (var i = 0; i < this.Count; i++)
{
TItem obj = this[i];
if (obj.Flags.Contains(flag)) list.Add(obj);
}
return list;
}
}
/// <summary>
@ -45,7 +133,7 @@ namespace SM.Base.Scene
/// <inheritdoc />
public override void Draw(DrawContext context)
{
context.View = Transform.GetMatrix() * context.View;
context.ModelMaster = Transform.GetMatrix() * context.ModelMaster;
base.Draw(context);
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using OpenTK;
using SM.Base.Contexts;
@ -9,10 +10,29 @@ namespace SM.Base.Scene
/// </summary>
public abstract class GenericScene
{
private IBackgroundItem _background;
/// <summary>
/// This contains the background.
/// </summary>
protected IBackgroundItem _background;
protected IBackgroundItem _Background
{
get => _background;
set
{
value.Parent = this;
_background = value;
}
}
/// <summary>
/// Updates this scene.
/// </summary>
/// <param name="context"></param>
public virtual void Update(UpdateContext context)
{
}
/// <summary>
/// Draws this scene.
@ -48,6 +68,9 @@ namespace SM.Base.Scene
where TCollection : GenericItemCollection<TItem>, new()
where TItem : IShowItem
{
private TCollection _objectCollection = new TCollection();
private TCollection _hud = new TCollection();
/// <summary>
/// The active camera, that is used if the context doesn't force the viewport camera.
/// <para>If none set, it automaticly uses the viewport camera.</para>
@ -65,16 +88,42 @@ namespace SM.Base.Scene
/// <summary>
/// Objects inside the scene.
/// </summary>
public TCollection Objects { get; set; } = new TCollection();
public TCollection Objects
{
get => _objectCollection;
set
{
value.Parent = this;
_objectCollection = value;
}
}
/// <summary>
/// This defines the HUD objects.
/// </summary>
public TCollection HUD { get; } = new TCollection();
public TCollection HUD
{
get => _hud;
set
{
value.Parent = this;
_hud = value;
}
}
/// <summary>
/// A collection for cameras to switch easier to different cameras.
/// </summary>
public Dictionary<string, TCamera> Cameras = new Dictionary<string, TCamera>();
/// <inheritdoc />
public override void Update(UpdateContext context)
{
_Background?.Update(context);
_objectCollection.Update(context);
_hud.Update(context);
}
/// <inheritdoc />
public override void Draw(DrawContext context)
{
@ -82,12 +131,12 @@ namespace SM.Base.Scene
DrawContext backgroundDrawContext = context;
backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix();
_background?.Draw(backgroundDrawContext);
_Background?.Draw(backgroundDrawContext);
Objects.Draw(context);
_objectCollection.Draw(context);
context.View = HUDCamera.CalculateViewMatrix();
HUD.Draw(context);
_hud.Draw(context);
}
}
}

View file

@ -1,4 +1,5 @@
using SM.Base.Contexts;
using System.Collections.Generic;
using SM.Base.Contexts;
namespace SM.Base.Scene
{
@ -7,6 +8,21 @@ namespace SM.Base.Scene
/// </summary>
public interface IShowItem
{
/// <summary>
/// Parent of the object.
/// </summary>
object Parent { get; set; }
/// <summary>
/// Contains the name for the object.
/// </summary>
string Name { get; set; }
/// <summary>
/// Contains specific flags for the object.
/// </summary>
ICollection<string> Flags { get; set; }
/// <summary>
/// Tells the object to update own systems.
/// </summary>
@ -17,5 +33,14 @@ namespace SM.Base.Scene
/// </summary>
/// <param name="context"></param>
void Draw(DrawContext context);
/// <summary>
/// Action, that is called, when the object was added to a GenericItemCollection.
/// </summary>
void OnAdded(object sender);
/// <summary>
/// Action, that is called, when the object was removed from a GenericItemCollection.
/// </summary>
void OnRemoved(object sender);
}
}

View file

@ -0,0 +1,20 @@
using System;
using OpenTK;
namespace SM.Utility
{
/// <summary>
/// Utilitys for rotations
/// </summary>
public class RotationUtility
{
/// <summary>
/// Angle towards an coordinate.
/// </summary>
/// <returns></returns>
public static float TurnTowards(Vector2 origin, Vector2 target)
{
return MathHelper.RadiansToDegrees((float)Math.Atan2(target.Y - origin.Y, target.X - origin.X));
}
}
}

View file

@ -24,6 +24,10 @@ namespace SM.Base.Contexts
/// </summary>
public Matrix4 View;
/// <summary>
/// The master model matrix.
/// </summary>
public Matrix4 ModelMaster;
/// <summary>
/// The drawing instances.
/// <para>If there is only one, it's index 0</para>
/// </summary>

View file

@ -11,7 +11,7 @@ namespace SM.Base.Contexts
/// <summary>
/// The delta time.
/// </summary>
public float Deltatime => Defaults.DefaultDeltatime.DeltaTime;
public float Deltatime => SMRenderer.DefaultDeltatime.DeltaTime;
/// <summary>
/// The current keyboard state.

View file

@ -95,6 +95,16 @@ namespace SM.Base
MouseState = Mouse.GetState()
};
Update(e, ref context);
}
/// <summary>
/// Updates the system.
/// </summary>
/// <param name="e"></param>
/// <param name="context"></param>
protected virtual void Update(FrameEventArgs e, ref UpdateContext context)
{
Stopwatch.PerformTicks(context);
}
@ -152,14 +162,24 @@ namespace SM.Base
ViewportCamera = new TCamera();
}
/// <inheritdoc />
protected override void Update(FrameEventArgs e, ref UpdateContext context)
{
base.Update(e, ref context);
CurrentScene?.Update(context);
}
/// <inheritdoc />
protected override void OnRenderFrame(FrameEventArgs e)
{
SMRenderer.CurrentFrame++;
Deltatime.RenderDelta = (float)e.Time;
DrawContext drawContext = new DrawContext()
{
World = ViewportCamera.World,
View = ViewportCamera.CalculateViewMatrix(),
ModelMaster = Matrix4.Identity,
Instances = new[] { new Instance {ModelMatrix = Matrix4.Identity, TexturePosition = Vector2.Zero, TextureScale = Vector2.One } },
Mesh = Plate.Object,
ForceViewport = ForceViewportCamera,