30.09.2020

+ Mouse support
+ Started to add summaries to SM.Base
This commit is contained in:
Michel Fedde 2020-09-30 21:26:42 +02:00
parent 16366fa015
commit 7acdba92f8
21 changed files with 325 additions and 58 deletions

View file

@ -0,0 +1,39 @@
using System;
using OpenTK;
using OpenTK.Input;
namespace SM.Base.Controls
{
/// <summary>
/// Mouse controller
/// </summary>
/// <typeparam name="TWindow">The type of window this controller is connected to.</typeparam>
public class Mouse<TWindow>
where TWindow : GenericWindow
{
/// <summary>
/// The window it is connected to.
/// </summary>
protected TWindow _window;
/// <summary>
/// The current position of the mouse in the screen.
/// </summary>
public Vector2 InScreen { get; private set; }
/// <summary>
/// The current position of the mouse in the screen from 0..1.
/// </summary>
public Vector2 InScreenNormalized { get; private set; }
protected internal Mouse(TWindow window)
{
_window = window;
}
protected void MouseMoveEvent(MouseMoveEventArgs mmea)
{
InScreen = new Vector2(mmea.X, mmea.Y);
InScreenNormalized = new Vector2(mmea.X / (float)_window.Width, mmea.Y / (float)_window.Height);
}
}
}

View file

@ -1,12 +1,13 @@
using SM.Base.Objects; using SM.Base.Objects;
using SM.Base.Objects.Static; using SM.Base.Objects.Static;
using SM.Base.Scene; using SM.Base.Scene;
using SM.OGL.Mesh;
namespace SM.Base namespace SM.Base
{ {
public class Defaults public class Defaults
{ {
public static IShader DefaultShader; public static IShader DefaultShader;
public static Mesh DefaultMesh = Plate.Object; public static GenericMesh DefaultMesh = Plate.Object;
} }
} }

View file

@ -4,9 +4,18 @@ using SM.OGL.Mesh;
namespace SM.Base.Scene namespace SM.Base.Scene
{ {
/// <summary>
/// Contains general basis systems for drawing objects.
/// </summary>
public abstract class DrawingBasis : IShowItem public abstract class DrawingBasis : IShowItem
{ {
/// <summary>
/// The material it should use.
/// </summary>
protected Material _material = new Material(); protected Material _material = new Material();
/// <summary>
/// The mesh it should use.
/// </summary>
protected GenericMesh _mesh = Defaults.DefaultMesh; protected GenericMesh _mesh = Defaults.DefaultMesh;
public virtual void Update(UpdateContext context) public virtual void Update(UpdateContext context)
{ {
@ -14,14 +23,25 @@ namespace SM.Base.Scene
} }
public virtual void Draw(DrawContext context) public virtual void Draw(DrawContext context)
{ } {
}
/// <summary>
/// Applies the current settings to the context.
/// </summary>
/// <param name="context"></param>
protected void ApplyContext(ref DrawContext context) protected void ApplyContext(ref DrawContext context)
{ {
_material.Shader ??= Defaults.DefaultShader;
context.Material = _material; context.Material = _material;
context.Mesh = _mesh; context.Mesh = _mesh;
} }
} }
/// <summary>
/// Contains general basis systems for drawing objects.
/// </summary>
/// <typeparam name="TTransformation">The transformation type</typeparam>
public abstract class DrawingBasis<TTransformation> : DrawingBasis public abstract class DrawingBasis<TTransformation> : DrawingBasis
where TTransformation : GenericTransformation, new() where TTransformation : GenericTransformation, new()
{ {

View file

@ -2,8 +2,15 @@
namespace SM.Base.Scene namespace SM.Base.Scene
{ {
/// <summary>
/// Contains methods for using transformations right.
/// </summary>
public abstract class GenericTransformation public abstract class GenericTransformation
{ {
/// <summary>
/// Calculates the current matrix.
/// </summary>
/// <returns>The current matrix.</returns>
public abstract Matrix4 GetMatrix(); public abstract Matrix4 GetMatrix();
} }
} }

View file

@ -4,8 +4,15 @@ using SM.Base.Contexts;
namespace SM.Base.Scene namespace SM.Base.Scene
{ {
/// <summary>
/// A general interface to work with material shaders properly.
/// </summary>
public interface IShader public interface IShader
{ {
/// <summary>
/// Draws the context.
/// </summary>
/// <param name="context">The context</param>
void Draw(DrawContext context); void Draw(DrawContext context);
} }
} }

View file

@ -2,10 +2,22 @@
namespace SM.Base.Scene namespace SM.Base.Scene
{ {
/// <summary>
/// This represens a drawing instance.
/// </summary>
public struct Instance public struct Instance
{ {
/// <summary>
/// The model matrix.
/// </summary>
public Matrix4 ModelMatrix; public Matrix4 ModelMatrix;
/// <summary>
/// The texture offset.
/// </summary>
public Vector2 TexturePosition; public Vector2 TexturePosition;
/// <summary>
/// The texture scale.
/// </summary>
public Vector2 TextureScale; public Vector2 TextureScale;
} }
} }

View file

@ -3,11 +3,24 @@ using SM.OGL.Texture;
namespace SM.Base.Scene namespace SM.Base.Scene
{ {
/// <summary>
/// Represents a material.
/// </summary>
public class Material public class Material
{ {
/// <summary>
/// The base texture. (aka. Diffuse Texture)
/// </summary>
public TextureBase Texture; public TextureBase Texture;
/// <summary>
/// The tint or color.
/// </summary>
public Color4 Tint = Color4.White; public Color4 Tint = Color4.White;
/// <summary>
/// A shader, that is used to draw this material.
/// <para>Default: Defaults.DefaultShaders </para>
/// </summary>
public IShader Shader = Defaults.DefaultShader; public IShader Shader = Defaults.DefaultShader;
} }
} }

View file

@ -1,11 +1,18 @@
using OpenTK; using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4; using OpenTK.Graphics.OpenGL4;
using SM.OGL.Mesh; using SM.OGL.Mesh;
namespace SM.Base.Objects.Static namespace SM.Base.Objects.Static
{ {
public class Plate : Mesh /// <summary>
/// A basic plate
/// </summary>
public class Plate : GenericMesh
{ {
/// <summary>
/// The object.
/// </summary>
public static Plate Object = new Plate(); public static Plate Object = new Plate();
public override VBO Vertex { get; } = new VBO() public override VBO Vertex { get; } = new VBO()

View file

@ -47,6 +47,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Controls\Mouse.cs" />
<Compile Include="Defaults.cs" /> <Compile Include="Defaults.cs" />
<Compile Include="Drawing\DrawingBasis.cs" /> <Compile Include="Drawing\DrawingBasis.cs" />
<Compile Include="Drawing\GenericTransformation.cs" /> <Compile Include="Drawing\GenericTransformation.cs" />

View file

@ -19,7 +19,7 @@ namespace SM.Base.Scene
public virtual void Draw(DrawContext context) public virtual void Draw(DrawContext context)
{ {
if (!context.ForceViewport && Camera != null) context.View = Camera.ViewMatrix; if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix();
DrawContext backgroundDrawContext = context; DrawContext backgroundDrawContext = context;
backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix(); backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix();
@ -33,5 +33,12 @@ namespace SM.Base.Scene
HUD[i].Draw(context); HUD[i].Draw(context);
} }
internal void Activate()
{
OnActivating();
}
protected virtual void OnActivating()
{ }
} }
} }

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Configuration.Assemblies;
using OpenTK; using OpenTK;
namespace SM.Base.Types namespace SM.Base.Types
@ -41,7 +42,64 @@ namespace SM.Base.Types
_z = z; _z = z;
_w = w; _w = w;
} }
#region Set
protected void Set(float x, float y)
{
_X = x;
_Y = y;
}
protected void Set(OpenTK.Vector2 vector)
{
Set(vector.X, vector.Y);
}
protected void Set(float x, float y, float z)
{
Set(x,y);
_Z = z;
}
protected void Set(OpenTK.Vector3 vector)
{
Set(vector.X, vector.Y, vector.Z);
}
protected void Set(float x, float y, float z, float w)
{
Set(x,y,z);
_W = w;
}
protected void Set(OpenTK.Vector4 vector)
{
Set(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Add
protected void Add(OpenTK.Vector2 vector)
{
_X += vector.X;
_Y += vector.Y;
}
protected void Add(OpenTK.Vector3 vector)
{
_X += vector.X;
_Y += vector.Y;
_Z += vector.Z;
}
protected void Add(OpenTK.Vector4 vector)
{
_X += vector.X;
_Y += vector.Y;
_Z += vector.Z;
_W += vector.W;
}
#endregion
public static implicit operator OpenTK.Vector2(Vector v) => new OpenTK.Vector2(v._x, v._y); public static implicit operator OpenTK.Vector2(Vector v) => new OpenTK.Vector2(v._x, v._y);
public static implicit operator OpenTK.Vector3(Vector v) => new OpenTK.Vector3(v._x, v._y, v._z); public static implicit operator OpenTK.Vector3(Vector v) => new OpenTK.Vector3(v._x, v._y, v._z);
public static implicit operator OpenTK.Vector4(Vector v) => new OpenTK.Vector4(v._x, v._y, v._z, v._w); public static implicit operator OpenTK.Vector4(Vector v) => new OpenTK.Vector4(v._x, v._y, v._z, v._w);

View file

@ -23,6 +23,10 @@
public Vector2(float x, float y) : base(x,y, default, default) {} public Vector2(float x, float y) : base(x,y, default, default) {}
protected Vector2(float x, float y, float z, float w) : base(x, y, z, w) {} protected Vector2(float x, float y, float z, float w) : base(x, y, z, w) {}
public new void Set(float x, float y) => base.Set(x, y);
public new void Set(OpenTK.Vector2 vector) => base.Set(vector);
public new void Add(OpenTK.Vector2 vector) => base.Add(vector);
public static implicit operator Vector2(OpenTK.Vector2 v) => new Vector2(v.X, v.Y); public static implicit operator Vector2(OpenTK.Vector2 v) => new Vector2(v.X, v.Y);
} }

View file

@ -3,32 +3,28 @@ using System.IO;
using OpenTK; using OpenTK;
using OpenTK.Graphics; using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4; using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using SM.Base.Contexts; using SM.Base.Contexts;
using SM.Base.Objects.Static; using SM.Base.Objects.Static;
using SM.Base.Scene; using SM.Base.Scene;
using SM.OGL; using SM.OGL;
using SM.OGL.Shaders; using SM.OGL.Shaders;
using SM.Utility;
namespace SM.Base namespace SM.Base
{ {
public class GenericWindow<TScene, TItem, TCamera> : GameWindow public abstract class GenericWindow : GameWindow
where TScene : GenericScene<TCamera, TItem>, new()
where TItem : IShowItem
where TCamera : GenericCamera, new()
{ {
private TCamera _viewportCamera; private bool _loading = false;
public TScene CurrentScene { get; private set; }
public bool ForceViewportCamera { get; set; } = false; public bool ForceViewportCamera { get; set; } = false;
public Vector2? Scaling { get; set; } protected Vector2 _worldScale = Vector2.Zero;
public Vector2 WorldScale { get; private set; }= Vector2.Zero;
public float Aspect { get; private set; } = 0f; public float Aspect { get; private set; } = 0f;
public GenericWindow() : base(1280, 720, GraphicsMode.Default, "Testing", GameWindowFlags.Default, DisplayDevice.Default, 0, 0, GraphicsContextFlags.Default, null, true) protected GenericWindow() : base(1280, 720, GraphicsMode.Default, "Generic OGL Title", GameWindowFlags.Default,
{ DisplayDevice.Default, 0, 0, GraphicsContextFlags.Default, null, true)
_viewportCamera = new TCamera(); { }
}
protected override void OnLoad(EventArgs e) protected override void OnLoad(EventArgs e)
{ {
@ -44,18 +40,74 @@ namespace SM.Base
$"----------------------------------\n"); $"----------------------------------\n");
base.OnLoad(e); base.OnLoad(e);
_loading = true;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Aspect = (float)Width / Height;
_worldScale = new Vector2(Width, Height);
SetWorldScale();
GL.Viewport(ClientRectangle);
if (_loading)
{
_loading = false;
OnLoaded();
}
}
protected virtual void OnLoaded()
{
}
protected virtual void SetWorldScale() { }
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
Deltatime.UpdateDelta = (float)e.Time;
}
public void GrabCursor(bool makeItInvisible = true)
{
CursorGrabbed = true;
CursorVisible = !makeItInvisible;
}
public void UngrabCursor()
{
CursorGrabbed = false;
if (!CursorVisible) CursorVisible = true;
}
}
public abstract class GenericWindow<TScene, TItem, TCamera> : GenericWindow
where TScene : GenericScene<TCamera, TItem>, new()
where TItem : IShowItem
where TCamera : GenericCamera, new()
{
public TCamera ViewportCamera { get; }
public TScene CurrentScene { get; private set; }
protected GenericWindow()
{
ViewportCamera = new TCamera();
} }
protected override void OnRenderFrame(FrameEventArgs e) protected override void OnRenderFrame(FrameEventArgs e)
{ {
DrawContext drawContext = new DrawContext() DrawContext drawContext = new DrawContext()
{ {
World = _viewportCamera.World, World = ViewportCamera.World,
View = _viewportCamera.CalculateViewMatrix(), View = ViewportCamera.CalculateViewMatrix(),
Instances = new[] { new Instance {ModelMatrix = Matrix4.Identity, TexturePosition = Vector2.Zero, TextureScale = Vector2.One } }, Instances = new[] { new Instance {ModelMatrix = Matrix4.Identity, TexturePosition = Vector2.Zero, TextureScale = Vector2.One } },
Mesh = Plate.Object, Mesh = Plate.Object,
ForceViewport = ForceViewportCamera, ForceViewport = ForceViewportCamera,
WorldScale = WorldScale WorldScale = _worldScale
}; };
base.OnRenderFrame(e); base.OnRenderFrame(e);
@ -71,30 +123,13 @@ namespace SM.Base
{ {
base.OnResize(e); base.OnResize(e);
Aspect = (float)Width / Height; ViewportCamera.RecalculateWorld(_worldScale, Aspect);
WorldScale = new Vector2(Width, Height);
if (Scaling.HasValue)
{
if (Scaling.Value.X > 0 && Scaling.Value.Y > 0) WorldScale = Scaling.Value;
else if(Scaling.Value.X > 0) WorldScale = new Vector2(Scaling.Value.X, Scaling.Value.X / Aspect);
else if(Scaling.Value.Y > 0) WorldScale = new Vector2(Aspect * Scaling.Value.Y, Scaling.Value.Y);
}
GL.Viewport(ClientRectangle);
_viewportCamera.RecalculateWorld(WorldScale, Aspect);
} }
public virtual void SetScene(TScene scene) public virtual void SetScene(TScene scene)
{ {
CurrentScene = scene; CurrentScene = scene;
scene.Activate();
} }
} }
public enum WindowScaling
{
None,
Width,
Height,
FixedSize
}
} }

View file

@ -43,7 +43,7 @@ namespace SM.OGL.Mesh
/// <summary> /// <summary>
/// Connects the different buffer objects with ids. /// Connects the different buffer objects with ids.
/// </summary> /// </summary>
protected Dictionary<int, VBO> AttribDataIndex { get; } public Dictionary<int, VBO> AttribDataIndex { get; }
/// <summary> /// <summary>
/// Stores indices for a more performance friendly method to draw objects. /// Stores indices for a more performance friendly method to draw objects.

View file

@ -0,0 +1,22 @@
using OpenTK;
using OpenTK.Input;
using SM.Base;
using SM.Base.Controls;
using SM2D.Scene;
namespace SM2D.Controls
{
public class Mouse2D : Mouse<GLWindow2D>
{
protected internal Mouse2D(GLWindow2D window) : base(window)
{ }
internal new void MouseMoveEvent(MouseMoveEventArgs mmea) => base.MouseMoveEvent(mmea);
public Vector2 InWorld(Camera cam)
{
Vector2 res = _window.WorldScale;
return InScreenNormalized * res - res / 2;
}
}
}

View file

@ -1,4 +1,5 @@
using OpenTK.Graphics; using OpenTK.Graphics;
using SM.Base;
using SM.Base.Contexts; using SM.Base.Contexts;
using SM.Base.Scene; using SM.Base.Scene;
using SM2D.Scene; using SM2D.Scene;

View file

@ -1,15 +1,27 @@
using System; using System;
using OpenTK; using OpenTK;
using OpenTK.Graphics.OpenGL4; using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using SM.Base; using SM.Base;
using SM.Base.Controls;
using SM2D.Controls;
using SM2D.Scene; using SM2D.Scene;
using SM2D.Shader; using SM2D.Shader;
using Vector2 = OpenTK.Vector2;
namespace SM2D namespace SM2D
{ {
public class GLWindow2D : GenericWindow<Scene.Scene, I2DShowItem, Camera> public class GLWindow2D : GenericWindow<Scene.Scene, I2DShowItem, Camera>
{ {
public Vector2? Scaling { get; set; }
public Vector2 WorldScale => _worldScale;
public Mouse2D Mouse { get; }
public GLWindow2D()
{
Mouse = new Mouse2D(this);
}
protected override void OnLoad(EventArgs e) protected override void OnLoad(EventArgs e)
{ {
@ -25,5 +37,21 @@ namespace SM2D
GL.Disable(EnableCap.DepthTest); GL.Disable(EnableCap.DepthTest);
base.OnRenderFrame(e); base.OnRenderFrame(e);
} }
protected override void SetWorldScale()
{
if (Scaling.HasValue)
{
if (Scaling.Value.X > 0 && Scaling.Value.Y > 0) _worldScale = Scaling.Value;
else if (Scaling.Value.X > 0) _worldScale = new Vector2(Scaling.Value.X, Scaling.Value.X / Aspect);
else if (Scaling.Value.Y > 0) _worldScale = new Vector2(Aspect * Scaling.Value.Y, Scaling.Value.Y);
}
}
protected override void OnMouseMove(MouseMoveEventArgs e)
{
base.OnMouseMove(e);
Mouse.MouseMoveEvent(e);
}
} }
} }

View file

@ -58,5 +58,19 @@ namespace SM2D.Object
Vector2 uv = Vector2.Divide(vertex, BoundingBox.Max.Xy) + BoundingBox.Min.Xy; Vector2 uv = Vector2.Divide(vertex, BoundingBox.Max.Xy) + BoundingBox.Min.Xy;
UVs.Add(uv); UVs.Add(uv);
} }
public static Polygon GenerateCircle(int secments = 32)
{
List<Vector2> vertices = new List<Vector2>() {Vector2.Zero};
float step = 360f / secments;
for (int i = 0; i < secments + 1; i++)
{
Vector2 vertex = new Vector2( 0.5f * (float)Math.Cos(step * i * Math.PI / 180f), 0.5f * (float)Math.Sin(step * i * Math.PI / 180f));
vertices.Add(vertex);
}
return new Polygon(vertices);
}
} }
} }

View file

@ -45,6 +45,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Controls\Mouse2D.cs" />
<Compile Include="Drawing\DrawBackground.cs" /> <Compile Include="Drawing\DrawBackground.cs" />
<Compile Include="Drawing\DrawColor.cs" /> <Compile Include="Drawing\DrawColor.cs" />
<Compile Include="Drawing\DrawComplex.cs" /> <Compile Include="Drawing\DrawComplex.cs" />

View file

@ -9,13 +9,12 @@ namespace SM2D.Types
public Vector2 Position = new Vector2(0); public Vector2 Position = new Vector2(0);
public Vector2 Size = new Vector2(50); public Vector2 Size = new Vector2(50);
public float Rotation; public float Rotation;
public int ZIndex = 0;
public override Matrix4 GetMatrix() public override Matrix4 GetMatrix()
{ {
return Matrix4.CreateScale(Size.X, Size.Y, 1) * return Matrix4.CreateScale(Size.X, Size.Y, 1) *
Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) * Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) *
Matrix4.CreateTranslation(Position.X, Position.Y, ZIndex); Matrix4.CreateTranslation(Position.X, Position.Y, 0);
} }
} }
} }

View file

@ -17,6 +17,8 @@ namespace SM_TEST
static Scene scene; static Scene scene;
private static Font font; private static Font font;
private static ItemCollection col; private static ItemCollection col;
private static DrawPolygon polyogn;
private static GLWindow2D window;
static void Main(string[] args) static void Main(string[] args)
{ {
font = new Font(@"C:\Windows\Fonts\Arial.ttf") font = new Font(@"C:\Windows\Fonts\Arial.ttf")
@ -24,18 +26,17 @@ namespace SM_TEST
FontSize = 32 FontSize = 32
}; };
GLWindow2D window = new GLWindow2D {Scaling = new Vector2(0, 1000)}; window = new GLWindow2D {Scaling = new Vector2(0, 1000)};
window.GrabCursor();
window.SetScene(scene = new Scene()); window.SetScene(scene = new Scene());
window.Load += WindowOnLoad; window.Load += WindowOnLoad;
window.UpdateFrame += WindowOnUpdateFrame; window.RenderFrame += WindowOnUpdateFrame;
window.Run(); window.Run();
} }
private static void WindowOnUpdateFrame(object sender, FrameEventArgs e) private static void WindowOnUpdateFrame(object sender, FrameEventArgs e)
{ {
float speed = 40; polyogn.Transform.Position.Set(window.Mouse.InWorld(window.ViewportCamera));
//col.Transform.Position.Y += (float)e.Time * speed;
} }
private static void WindowOnLoad(object sender, EventArgs e) private static void WindowOnLoad(object sender, EventArgs e)
@ -53,6 +54,7 @@ namespace SM_TEST
col.Objects.Add(new DrawColor(Color4.Black) col.Objects.Add(new DrawColor(Color4.Black)
{ {
Transform = { Rotation = 45, Position = new SM.Base.Types.Vector2(0, 25) }, Transform = { Rotation = 45, Position = new SM.Base.Types.Vector2(0, 25) },
ZIndex = 2
}); });
scene.Objects.Add(col); scene.Objects.Add(col);
@ -62,18 +64,7 @@ namespace SM_TEST
Color = Color4.Black Color = Color4.Black
}); });
scene.Objects.Add(new DrawPolygon(new Polygon(new[] scene.Objects.Add(polyogn = new DrawPolygon(Polygon.GenerateCircle(),Color4.Blue));
{
new Vector2(.25f, 0),
new Vector2(.75f, 0),
new Vector2(1, .25f),
new Vector2(1, .75f),
new Vector2(.75f, 1),
new Vector2(.25f, 1),
new Vector2(0, .75f),
new Vector2(0, .25f)
}),Color4.Blue)
);
scene.Objects.Add(new DrawPolygon(new Polygon(new[] scene.Objects.Add(new DrawPolygon(new Polygon(new[]
{ {
new PolygonVertex(new Vector2(.25f, 0), Color4.White), new PolygonVertex(new Vector2(.25f, 0), Color4.White),