Loads and loads of small improvements I added while developing on my game

This commit is contained in:
Michel Fedde 2021-03-02 19:54:19 +01:00
parent 41421b1df9
commit a7c71e7ea1
107 changed files with 2278 additions and 1023 deletions

View file

@ -1,8 +1,9 @@
#region usings
using System.Collections.Generic;
using SM.Base.Contexts;
using SM.Base;
using SM.Base.Scene;
using SM.Base.Windows;
using SM.OGL.Mesh;
#endregion
@ -12,17 +13,21 @@ namespace SM.Base.Drawing
/// <summary>
/// Contains general basis systems for drawing objects.
/// </summary>
public abstract class DrawingBasis : IShowItem
public abstract class DrawingBasis : IShowItem, IModelItem
{
/// <summary>
/// The material it should use.
/// </summary>
protected Material _material = new Material();
public Material Material = new Material();
/// <summary>
/// The mesh it should use.
/// </summary>
protected GenericMesh _mesh = SMRenderer.DefaultMesh;
public GenericMesh Mesh { get; set; } = SMRenderer.DefaultMesh;
public ShaderArguments ShaderArguments => Material.ShaderArguments;
public TextureTransformation TextureTransform = new TextureTransformation();
/// <inheritdoc />
public object Parent { get; set; }
@ -36,15 +41,13 @@ namespace SM.Base.Drawing
/// <summary>
/// This value determents if the object should draw something.
/// </summary>
public bool Active = true;
public bool Active { get; set; } = true;
/// <inheritdoc />
public void Draw(DrawContext context)
{
if (!Active) return;
context.Material = _material;
context.Mesh = _mesh;
context.Material = Material;
context.Mesh = Mesh;
DrawContext(ref context);
}
@ -65,6 +68,7 @@ namespace SM.Base.Drawing
/// <param name="context"></param>
protected virtual void DrawContext(ref DrawContext context)
{
context.TextureMatrix *= TextureTransform.GetMatrix();
}
}
@ -72,19 +76,20 @@ namespace SM.Base.Drawing
/// 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, IShowTransformItem<TTransformation>
where TTransformation : GenericTransformation, new()
{
/// <summary>
/// The current transformation.
/// </summary>
public TTransformation Transform = new TTransformation();
public TTransformation Transform { get; set; } = new TTransformation();
/// <inheritdoc />
protected override void DrawContext(ref DrawContext context)
{
base.DrawContext(ref context);
context.ModelMaster *= Transform.GetMatrix();
Transform.LastMaster = context.ModelMatrix;
context.ModelMatrix = Transform.MergeMatrix(context.ModelMatrix);
}
}
}

View file

@ -16,6 +16,8 @@ namespace SM.Base.Drawing
/// </summary>
public bool Ignore = false;
public Matrix4 LastMaster { get; internal set; }
/// <summary>
/// Contains the current model matrix.
/// </summary>
@ -43,6 +45,11 @@ namespace SM.Base.Drawing
return _modelMatrix;
}
public Matrix4 MergeMatrix(Matrix4 matrix)
{
return GetMatrix() * matrix;
}
/// <summary>
/// Calculates the current matrix.
/// </summary>

View file

@ -16,14 +16,6 @@ namespace SM.Base.Drawing
/// </summary>
public Matrix4 ModelMatrix = Matrix4.Identity;
/// <summary>
/// The texture offset.
/// </summary>
public Vector2 TexturePosition = Vector2.Zero;
/// <summary>
/// The texture scale.
/// </summary>
public Vector2 TextureScale = Vector2.One;
public Matrix3 TextureMatrix = Matrix3.Identity;
}
}

View file

@ -1,5 +1,6 @@
#region usings
using System.Collections.Generic;
using OpenTK.Graphics;
using SM.OGL.Texture;
@ -26,5 +27,9 @@ namespace SM.Base.Drawing
/// The tint or color.
/// </summary>
public Color4 Tint = Color4.White;
public ShaderArguments ShaderArguments { get; internal set; } = new ShaderArguments();
public bool Blending = false;
}
}

View file

@ -1,55 +0,0 @@
#region usings
using OpenTK.Graphics.OpenGL4;
using SM.Base.Contexts;
using SM.OGL.Shaders;
#endregion
namespace SM.Base.Drawing
{
/// <summary>
/// A general class to work with material shaders properly.
/// </summary>
public abstract class MaterialShader : GenericShader
{
/// <inheritdoc />
protected MaterialShader(string combinedData) : base(combinedData)
{}
/// <inheritdoc />
protected MaterialShader(string vertex, string fragment) : base(vertex, fragment)
{
}
/// <inheritdoc />
protected MaterialShader(ShaderFileCollection shaderFileFiles) : base(shaderFileFiles)
{
}
/// <summary>
/// Prepares the context for the drawing.
/// </summary>
/// <param name="context">The context</param>
public virtual void Draw(DrawContext context)
{
GL.UseProgram(this);
GL.BindVertexArray(context.Mesh);
DrawProcess(context);
CleanUp();
GL.UseProgram(0);
context.ShaderArguments.Clear();
}
/// <summary>
/// Draws the context.
/// </summary>
/// <param name="context"></param>
protected abstract void DrawProcess(DrawContext context);
}
}

View file

@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using OpenTK;
using SM.Base.Contexts;
using SM.Base;
using SM.Base.Scene;
using SM.Base.Time;
using SM.Base.Types;
using SM.Base.Windows;
using SM.OGL.Shaders;
namespace SM.Base.Drawing.Particles

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
namespace SM.Base.Drawing
{
public class ShaderArguments : Dictionary<string, object>
{
public TType Get<TType>(string name, TType defaultValue = default)
{
return ContainsKey(name) ? (TType)this[name] : defaultValue;
}
}
}

View file

@ -32,6 +32,8 @@ namespace SM.Base.Drawing.Text
/// </summary>
public float FontSize = 12;
public float Spacing = 1;
/// <summary>
/// The font style.
/// <para>Default: <see cref="System.Drawing.FontStyle.Regular" /></para>

View file

@ -3,7 +3,8 @@
using System;
using OpenTK;
using OpenTK.Graphics;
using SM.Base.Contexts;
using SM.Base;
using SM.Base.Windows;
#endregion
@ -32,13 +33,19 @@ namespace SM.Base.Drawing.Text
/// </summary>
public float Spacing = 1;
public float ActualSpacing => Spacing * Font.Spacing;
public float Width;
public float Height;
/// <summary>
/// Creates a text object with a font.
/// </summary>
/// <param name="font">The font.</param>
protected TextDrawingBasis(Font font)
{
_material.Texture = font;
Material.Texture = font;
Material.Blending = true;
}
/// <summary>
@ -46,10 +53,10 @@ namespace SM.Base.Drawing.Text
/// </summary>
public Font Font
{
get => (Font) _material.Texture;
get => (Font) Material.Texture;
set
{
_material.Texture = value;
Material.Texture = value;
GenerateMatrixes();
}
}
@ -72,8 +79,8 @@ namespace SM.Base.Drawing.Text
/// </summary>
public Color4 Color
{
get => _material.Tint;
set => _material.Tint = value;
get => Material.Tint;
set => Material.Tint = value;
}
@ -92,15 +99,26 @@ namespace SM.Base.Drawing.Text
{
if (!Font.WasCompiled) Font.RegenerateTexture();
_text = _text.Replace("\r\n", "\n").Replace("\t", " ");
_instances = new Instance[_text.Length];
float x = 0;
float y = 0;
var _last = new CharParameter();
for (var i = 0; i < _text.Length; i++)
{
if (_text[i] == 32)
if (_text[i] == ' ')
{
x += _last.Width * Spacing;
x += Font.FontSize * ActualSpacing;
continue;
}
if (_text[i] == '\n')
{
y += Font.Height;
Width = Math.Max(Width, x);
x = 0;
continue;
}
@ -115,17 +133,19 @@ namespace SM.Base.Drawing.Text
}
var matrix = Matrix4.CreateScale(parameter.Width, Font.Height, 1) *
Matrix4.CreateTranslation(x, 0, 0);
Matrix4.CreateTranslation(x, -y, 0);
_instances[i] = new Instance
{
ModelMatrix = matrix,
TexturePosition = new Vector2(parameter.NormalizedX, 0),
TextureScale = new Vector2(parameter.NormalizedWidth, 1)
TextureMatrix = TextureTransformation.CalculateMatrix(new Vector2(parameter.NormalizedX, 0), new Vector2(parameter.NormalizedWidth, 1), 0),
};
x += parameter.Width * Spacing;
x += parameter.Width * ActualSpacing;
_last = parameter;
}
Width = Math.Max(Width, x);
Height = y + Font.Height;
}
}
}

View file

@ -0,0 +1,26 @@
using System;
using OpenTK;
using SM.Base.Types;
namespace SM.Base.Drawing
{
public class TextureTransformation
{
public CVector2 Offset = new CVector2(0);
public CVector2 Scale = new CVector2(1);
public CVector1 Rotation = new CVector1(0);
public Matrix3 GetMatrix()
{
return CalculateMatrix(Offset, Scale, Rotation);
}
public static Matrix3 CalculateMatrix(Vector2 offset, Vector2 scale, float rotation)
{
float radians = MathHelper.DegreesToRadians(rotation);
Matrix3 result = Matrix3.CreateScale(scale.X, scale.Y, 1) * Matrix3.CreateRotationZ(radians);
result.Row2 = new Vector3(offset.X, offset.Y, 1);
return result;
}
}
}