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.Static;
using SM.Base.Scene;
using SM.OGL.Mesh;
namespace SM.Base
{
public class Defaults
{
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
{
/// <summary>
/// Contains general basis systems for drawing objects.
/// </summary>
public abstract class DrawingBasis : IShowItem
{
/// <summary>
/// The material it should use.
/// </summary>
protected Material _material = new Material();
/// <summary>
/// The mesh it should use.
/// </summary>
protected GenericMesh _mesh = Defaults.DefaultMesh;
public virtual void Update(UpdateContext context)
{
@ -14,14 +23,25 @@ namespace SM.Base.Scene
}
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)
{
_material.Shader ??= Defaults.DefaultShader;
context.Material = _material;
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
where TTransformation : GenericTransformation, new()
{

View file

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

View file

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

View file

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

View file

@ -3,11 +3,24 @@ using SM.OGL.Texture;
namespace SM.Base.Scene
{
/// <summary>
/// Represents a material.
/// </summary>
public class Material
{
/// <summary>
/// The base texture. (aka. Diffuse Texture)
/// </summary>
public TextureBase Texture;
/// <summary>
/// The tint or color.
/// </summary>
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;
}
}

View file

@ -1,11 +1,18 @@
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Mesh;
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 override VBO Vertex { get; } = new VBO()

View file

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

View file

@ -19,7 +19,7 @@ namespace SM.Base.Scene
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;
backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix();
@ -33,5 +33,12 @@ namespace SM.Base.Scene
HUD[i].Draw(context);
}
internal void Activate()
{
OnActivating();
}
protected virtual void OnActivating()
{ }
}
}

View file

@ -1,4 +1,5 @@
using System;
using System.Configuration.Assemblies;
using OpenTK;
namespace SM.Base.Types
@ -41,7 +42,64 @@ namespace SM.Base.Types
_z = z;
_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.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);

View file

@ -23,6 +23,10 @@
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) {}
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);
}

View file

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