diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs index a0325f5..d28dfc3 100644 --- a/SMCode/SM.Base/Drawing/DrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -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 /// /// The mesh it should use. /// - protected GenericMesh _mesh = Defaults.DefaultMesh; + protected GenericMesh _mesh = SMRenderer.DefaultMesh; + + /// + public object Parent { get; set; } + + /// + public string Name { get; set; } = "Unnamed draw object"; + + /// + public ICollection Flags { get; set; } /// public virtual void Update(UpdateContext context) @@ -33,6 +43,18 @@ namespace SM.Base.Scene DrawContext(ref context); } + /// + public virtual void OnAdded(object sender) + { + + } + + /// + public virtual void OnRemoved(object sender) + { + + } + /// /// Draws the context, that was given to them. /// diff --git a/SMCode/SM.Base/Drawing/GenericTransformation.cs b/SMCode/SM.Base/Drawing/GenericTransformation.cs index f6c0e83..ab4ddb4 100644 --- a/SMCode/SM.Base/Drawing/GenericTransformation.cs +++ b/SMCode/SM.Base/Drawing/GenericTransformation.cs @@ -7,10 +7,34 @@ namespace SM.Base.Scene /// public abstract class GenericTransformation { + /// + /// Contains the current model matrix. + /// + protected Matrix4 _modelMatrix { get; private set; } + /// + /// Contains the last frame the matrix was calculated. + /// + protected ulong _lastFrame { get; private set; } + + /// + /// Returns the current model matrix. + /// + /// + public Matrix4 GetMatrix() + { + if (_lastFrame != SMRenderer.CurrentFrame) + { + _lastFrame = SMRenderer.CurrentFrame; + _modelMatrix = RequestMatrix(); + } + + return _modelMatrix; + } + /// /// Calculates the current matrix. /// /// The current matrix. - public abstract Matrix4 GetMatrix(); + protected abstract Matrix4 RequestMatrix(); } } \ No newline at end of file diff --git a/SMCode/SM.Base/PostProcessing/PostProcessingEffect.cs b/SMCode/SM.Base/PostProcessing/PostProcessingEffect.cs new file mode 100644 index 0000000..85f01be --- /dev/null +++ b/SMCode/SM.Base/PostProcessing/PostProcessingEffect.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using SM.OGL.Framebuffer; + +namespace SM.Base.PostProcessing +{ + public abstract class PostProcessingEffect + { + public virtual Dictionary AdditionalFramebufferOutputs { get; } + public virtual ICollection AdditionalFramebuffers { get; } + + public void ApplyOutputs(Framebuffer mainbuffer) + { + mainbuffer.Append(); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index 1810f80..a6860d9 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -52,18 +52,19 @@ - + + @@ -79,6 +80,7 @@ + diff --git a/SMCode/SM.Base/Defaults.cs b/SMCode/SM.Base/SMRenderer.cs similarity index 67% rename from SMCode/SM.Base/Defaults.cs rename to SMCode/SM.Base/SMRenderer.cs index 5c6e62f..ac2f055 100644 --- a/SMCode/SM.Base/Defaults.cs +++ b/SMCode/SM.Base/SMRenderer.cs @@ -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 { /// - /// The default options. + /// Contains different information about this renderer. /// - public class Defaults + public class SMRenderer { /// /// The default mesh. @@ -26,5 +24,10 @@ namespace SM.Base /// The default deltatime helper. /// public static Deltatime DefaultDeltatime = new Deltatime(); + + /// + /// Current Frame + /// + public static ulong CurrentFrame { get; internal set; } = 0; } } \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/GenericItemCollection.cs b/SMCode/SM.Base/Scene/GenericItemCollection.cs index 30f7882..3bfb483 100644 --- a/SMCode/SM.Base/Scene/GenericItemCollection.cs +++ b/SMCode/SM.Base/Scene/GenericItemCollection.cs @@ -14,7 +14,37 @@ namespace SM.Base.Scene public List Objects => this; /// - public void Update(UpdateContext context) + public object Parent { get; set; } + + /// + public string Name { get; set; } = "Unnamed Item Collection"; + + /// + public ICollection Flags { get; set; } = new[] {"collection"}; + + /// + /// Adds a item. + /// + public new void Add(TItem item) + { + base.Add(item); + item.Parent = this; + item.OnAdded(this); + } + + /// + /// Removes a item. + /// + /// + public new void Remove(TItem item) + { + base.Remove(item); + item.Parent = null; + item.OnRemoved(this); + } + + /// + 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); } + + /// + public virtual void OnAdded(object sender) + { + + } + + /// + public virtual void OnRemoved(object sender) + { } + + /// + /// Returns a object with this name or the default, if not available. + /// Not reclusive. + /// + /// + /// + 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; + } + + /// + /// Returns a object with this name or the default if not available. + /// Not reclusive. + /// + /// Type of return + public TGetItem GetItemByName(string name) + where TGetItem : TItem + { + return (TGetItem)GetItemByName(name); + } + + /// + /// Returns all object that have this flag. + /// Only in this list. + /// + public ICollection GetItemsWithFlag(string flag) + { + List list = new List(); + for (var i = 0; i < this.Count; i++) + { + TItem obj = this[i]; + if (obj.Flags.Contains(flag)) list.Add(obj); + } + + return list; + } } /// @@ -45,7 +133,7 @@ namespace SM.Base.Scene /// public override void Draw(DrawContext context) { - context.View = Transform.GetMatrix() * context.View; + context.ModelMaster = Transform.GetMatrix() * context.ModelMaster; base.Draw(context); } diff --git a/SMCode/SM.Base/Scene/GenericScene.cs b/SMCode/SM.Base/Scene/GenericScene.cs index 53ed232..05b907b 100644 --- a/SMCode/SM.Base/Scene/GenericScene.cs +++ b/SMCode/SM.Base/Scene/GenericScene.cs @@ -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 /// public abstract class GenericScene { + private IBackgroundItem _background; + /// /// This contains the background. /// - protected IBackgroundItem _background; + protected IBackgroundItem _Background + { + get => _background; + set + { + value.Parent = this; + _background = value; + } + } + + /// + /// Updates this scene. + /// + /// + public virtual void Update(UpdateContext context) + { + + } /// /// Draws this scene. @@ -48,6 +68,9 @@ namespace SM.Base.Scene where TCollection : GenericItemCollection, new() where TItem : IShowItem { + private TCollection _objectCollection = new TCollection(); + private TCollection _hud = new TCollection(); + /// /// The active camera, that is used if the context doesn't force the viewport camera. /// If none set, it automaticly uses the viewport camera. @@ -65,16 +88,42 @@ namespace SM.Base.Scene /// /// Objects inside the scene. /// - public TCollection Objects { get; set; } = new TCollection(); + public TCollection Objects + { + get => _objectCollection; + set + { + value.Parent = this; + _objectCollection = value; + } + } + /// /// This defines the HUD objects. /// - public TCollection HUD { get; } = new TCollection(); + public TCollection HUD + { + get => _hud; + set + { + value.Parent = this; + _hud = value; + } + } /// /// A collection for cameras to switch easier to different cameras. /// public Dictionary Cameras = new Dictionary(); + + /// + public override void Update(UpdateContext context) + { + _Background?.Update(context); + _objectCollection.Update(context); + _hud.Update(context); + } + /// 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); } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/IShowItem.cs b/SMCode/SM.Base/Scene/IShowItem.cs index 41b021e..677606d 100644 --- a/SMCode/SM.Base/Scene/IShowItem.cs +++ b/SMCode/SM.Base/Scene/IShowItem.cs @@ -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 /// public interface IShowItem { + /// + /// Parent of the object. + /// + object Parent { get; set; } + + /// + /// Contains the name for the object. + /// + string Name { get; set; } + + /// + /// Contains specific flags for the object. + /// + ICollection Flags { get; set; } + /// /// Tells the object to update own systems. /// @@ -17,5 +33,14 @@ namespace SM.Base.Scene /// /// void Draw(DrawContext context); + + /// + /// Action, that is called, when the object was added to a GenericItemCollection. + /// + void OnAdded(object sender); + /// + /// Action, that is called, when the object was removed from a GenericItemCollection. + /// + void OnRemoved(object sender); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Utility/RotationUtility.cs b/SMCode/SM.Base/Utility/RotationUtility.cs new file mode 100644 index 0000000..751452e --- /dev/null +++ b/SMCode/SM.Base/Utility/RotationUtility.cs @@ -0,0 +1,20 @@ +using System; +using OpenTK; + +namespace SM.Utility +{ + /// + /// Utilitys for rotations + /// + public class RotationUtility + { + /// + /// Angle towards an coordinate. + /// + /// + public static float TurnTowards(Vector2 origin, Vector2 target) + { + return MathHelper.RadiansToDegrees((float)Math.Atan2(target.Y - origin.Y, target.X - origin.X)); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Window/Contexts/DrawContext.cs b/SMCode/SM.Base/Window/Contexts/DrawContext.cs index 406eede..4004433 100644 --- a/SMCode/SM.Base/Window/Contexts/DrawContext.cs +++ b/SMCode/SM.Base/Window/Contexts/DrawContext.cs @@ -24,6 +24,10 @@ namespace SM.Base.Contexts /// public Matrix4 View; /// + /// The master model matrix. + /// + public Matrix4 ModelMaster; + /// /// The drawing instances. /// If there is only one, it's index 0 /// diff --git a/SMCode/SM.Base/Window/Contexts/UpdateContext.cs b/SMCode/SM.Base/Window/Contexts/UpdateContext.cs index b20189a..e67c1fc 100644 --- a/SMCode/SM.Base/Window/Contexts/UpdateContext.cs +++ b/SMCode/SM.Base/Window/Contexts/UpdateContext.cs @@ -11,7 +11,7 @@ namespace SM.Base.Contexts /// /// The delta time. /// - public float Deltatime => Defaults.DefaultDeltatime.DeltaTime; + public float Deltatime => SMRenderer.DefaultDeltatime.DeltaTime; /// /// The current keyboard state. diff --git a/SMCode/SM.Base/Window/GenericWindow.cs b/SMCode/SM.Base/Window/GenericWindow.cs index 1c56bad..41450a4 100644 --- a/SMCode/SM.Base/Window/GenericWindow.cs +++ b/SMCode/SM.Base/Window/GenericWindow.cs @@ -95,6 +95,16 @@ namespace SM.Base MouseState = Mouse.GetState() }; + Update(e, ref context); + } + + /// + /// Updates the system. + /// + /// + /// + protected virtual void Update(FrameEventArgs e, ref UpdateContext context) + { Stopwatch.PerformTicks(context); } @@ -152,14 +162,24 @@ namespace SM.Base ViewportCamera = new TCamera(); } + /// + protected override void Update(FrameEventArgs e, ref UpdateContext context) + { + base.Update(e, ref context); + CurrentScene?.Update(context); + } + /// 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, diff --git a/SMCode/SM2D/Controls/Mouse2D.cs b/SMCode/SM2D/Controls/Mouse2D.cs index 5661462..0278b61 100644 --- a/SMCode/SM2D/Controls/Mouse2D.cs +++ b/SMCode/SM2D/Controls/Mouse2D.cs @@ -13,10 +13,20 @@ namespace SM2D.Controls internal new void MouseMoveEvent(MouseMoveEventArgs mmea) => base.MouseMoveEvent(mmea); - public Vector2 InWorld(Camera cam) + public Vector2 InWorld() { Vector2 res = _window.WorldScale; return InScreenNormalized * res - res / 2; } + + public Vector2 InWorld(Camera cam) + { + return InWorld() + cam.Position; + } + + public Vector2 InWorld(Vector2 position) + { + return InWorld() + position; + } } } \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawBackground.cs b/SMCode/SM2D/Drawing/DrawBackground.cs index bdc17c8..a59ef2f 100644 --- a/SMCode/SM2D/Drawing/DrawBackground.cs +++ b/SMCode/SM2D/Drawing/DrawBackground.cs @@ -1,4 +1,5 @@ -using System.Drawing; +using System.Collections.Generic; +using System.Drawing; using OpenTK; using OpenTK.Graphics; using SM.Base; @@ -43,10 +44,12 @@ namespace SM2D.Drawing Texture = (Texture) texture; } + public object Parent { get; set; } + public string Name { get; set; } = "Background"; + public ICollection Flags { get; set; } = new string[0]; + public void Update(UpdateContext context) - { - throw new System.NotImplementedException(); - } + { } public void Draw(DrawContext context) { @@ -56,5 +59,13 @@ namespace SM2D.Drawing context.Instances[0].ModelMatrix = Matrix4.CreateScale(context.WorldScale.X, context.WorldScale.Y, 1); context.Shader.Draw(context); } + + public void OnAdded(object sender) + { + } + + public void OnRemoved(object sender) + { + } } } \ No newline at end of file diff --git a/SMCode/SM2D/Pipelines/Adv2DPipeline.cs b/SMCode/SM2D/Pipelines/Adv2DPipeline.cs new file mode 100644 index 0000000..47de7fc --- /dev/null +++ b/SMCode/SM2D/Pipelines/Adv2DPipeline.cs @@ -0,0 +1,7 @@ +namespace SM2D.Pipelines +{ + public class Adv2DPipeline + { + + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Pipelines/Basic2DPipeline.cs b/SMCode/SM2D/Pipelines/Basic2DPipeline.cs index d70a156..5ff1d8a 100644 --- a/SMCode/SM2D/Pipelines/Basic2DPipeline.cs +++ b/SMCode/SM2D/Pipelines/Basic2DPipeline.cs @@ -17,7 +17,7 @@ namespace SM2D.Pipelines Framebuffer.Screen.Activate(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); - scene.Draw(context); + scene?.Draw(context); } } } \ No newline at end of file diff --git a/SMCode/SM2D/SM2D.csproj b/SMCode/SM2D/SM2D.csproj index dd1d2da..29c4707 100644 --- a/SMCode/SM2D/SM2D.csproj +++ b/SMCode/SM2D/SM2D.csproj @@ -55,6 +55,7 @@ + diff --git a/SMCode/SM2D/Scene/Scene.cs b/SMCode/SM2D/Scene/Scene.cs index 9a94156..23bdc12 100644 --- a/SMCode/SM2D/Scene/Scene.cs +++ b/SMCode/SM2D/Scene/Scene.cs @@ -7,11 +7,11 @@ namespace SM2D.Scene { public class Scene : GenericScene { - public DrawBackground Background => (DrawBackground)_background; + public DrawBackground Background => (DrawBackground)_Background; public Scene() { - _background = new DrawBackground(Color4.Black); + _Background = new DrawBackground(Color4.Black); } } } \ No newline at end of file diff --git a/SMCode/SM2D/Shader/Default2DShader.cs b/SMCode/SM2D/Shader/Default2DShader.cs index 854f00f..40e0def 100644 --- a/SMCode/SM2D/Shader/Default2DShader.cs +++ b/SMCode/SM2D/Shader/Default2DShader.cs @@ -25,7 +25,7 @@ namespace SM2D.Shader GL.BindVertexArray(context.Mesh); // Vertex Uniforms - Uniforms["MVP"].SetMatrix4(context.View * context.World); + Uniforms["MVP"].SetMatrix4(context.ModelMaster * context.View * context.World); Uniforms["HasVColor"].SetUniform1(context.Mesh.AttribDataIndex.ContainsKey(3) && context.Mesh.AttribDataIndex[3] != null); for (int i = 0; i < context.Instances.Length; i++) diff --git a/SMCode/SM2D/Types/Transformation.cs b/SMCode/SM2D/Types/Transformation.cs index 6e27787..bd1005a 100644 --- a/SMCode/SM2D/Types/Transformation.cs +++ b/SMCode/SM2D/Types/Transformation.cs @@ -1,20 +1,54 @@ -using OpenTK; +using System; +using System.Configuration.Assemblies; +using OpenTK; using SM.Base.Scene; using SM.Base.Types; +using SM.Utility; namespace SM2D.Types { public class Transformation : GenericTransformation { - public CVector2 Position = new CVector2(0); - public CVector2 Size = new CVector2(50); - public float Rotation; + private float _eulerRotation = 0; + private CVector2 _position = new CVector2(0); + private CVector2 _scale = new CVector2(50); - public override Matrix4 GetMatrix() + public CVector2 Position { - return Matrix4.CreateScale(Size.X, Size.Y, 1) * - Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) * - Matrix4.CreateTranslation(Position.X, Position.Y, 0); + get => _position; + set => _position = value; + } + + public CVector2 Size + { + get => _scale; + set => _scale = value; + } + public float Rotation + { + get => _eulerRotation; + set => _eulerRotation = value; + } + + protected override Matrix4 RequestMatrix() + { + return Matrix4.CreateScale(_scale.X, _scale.Y, 1) * + Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(_eulerRotation)) * + Matrix4.CreateTranslation(_position.X, _position.Y, 0); + } + + public void TurnTo(Vector2 v) + { + _eulerRotation = RotationUtility.TurnTowards(Position, v); + } + + public Vector2 LookAtVector() + { + if (_modelMatrix.Determinant < 0.0001) return new Vector2(0); + + Vector3 vec = Vector3.TransformNormal(Vector3.UnitX, _modelMatrix); + vec.Normalize(); + return vec.Xy; } } } \ No newline at end of file diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index ad6816d..1d3b16a 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Drawing; using System.Runtime.InteropServices; +using System.Security.Authentication.ExtendedProtection.Configuration; using OpenTK; using OpenTK.Graphics; using SM.Base; @@ -41,7 +42,11 @@ namespace SM_TEST private static void WindowOnUpdateFrame(object sender, FrameEventArgs e) { - polyogn.Transform.Position.Set(window.Mouse.InWorld(window.ViewportCamera)); + Vector2 mousepos = window.Mouse.InWorld(); + //polyogn.Transform.Position.Set(mousepos); + polyogn.Transform.TurnTo(mousepos); + + Log.Write(LogType.Info, polyogn.Transform.LookAtVector()); } private static void WindowOnLoad(object sender, EventArgs e) @@ -52,7 +57,7 @@ namespace SM_TEST col = new ItemCollection() { - Transform = { Position = new SM.Base.Types.CVector2(0, -400) }, + Transform = { Position = new SM.Base.Types.CVector2(0, 400) }, ZIndex = 1 }; @@ -69,12 +74,12 @@ namespace SM_TEST scene.Objects.Add(col); scene.Objects.Add(new DrawText(font, "Testing...") { - Transform = { Position = new SM.Base.Types.CVector2(0, -400)}, + Transform = { Position = new SM.Base.Types.CVector2(0, 400)}, Color = Color4.Black }); - scene.Objects.Add(polyogn = new DrawPolygon(Polygon.GenerateCircle(),Color4.Blue)); - scene.Objects.Add(new DrawPolygon(new Polygon(new[] + scene.Objects.Add(new DrawPolygon(Polygon.GenerateCircle(),Color4.Blue)); + scene.Objects.Add(polyogn = new DrawPolygon(new Polygon(new[] { new PolygonVertex(new Vector2(.25f, 0), Color4.White), new PolygonVertex(new Vector2(.75f, 0), Color4.White),