20.09.2020

+ Instance Drawing
+ Text and Font

~ Made "DrawBackground" forced Background for 2D

- DrawEmpty
This commit is contained in:
Michel Fedde 2020-09-20 18:29:09 +02:00
parent acccf5f0e7
commit c4a0847567
29 changed files with 365 additions and 85 deletions

View file

@ -4,7 +4,7 @@ using SM.OGL.Mesh;
namespace SM.Base.Scene
{
public class DrawingBasis : IShowItem
public abstract class DrawingBasis : IShowItem
{
protected Material _material = new Material();
protected Mesh _mesh = Plate.Object;
@ -22,10 +22,9 @@ namespace SM.Base.Scene
context.Mesh = _mesh;
}
}
public class DrawingBasis<TTransformation> : DrawingBasis
public abstract class DrawingBasis<TTransformation> : DrawingBasis
where TTransformation : GenericTransformation, new()
{
public TTransformation Transform = new TTransformation();
}
}

View file

@ -7,6 +7,5 @@ namespace SM.Base.Scene
public interface IShader
{
void Draw(DrawContext context);
void DrawInstanced(DrawContext context, ICollection<Matrix4> instanceCollection);
}
}

View file

@ -0,0 +1,11 @@
using OpenTK;
namespace SM.Base.Scene
{
public struct Instance
{
public Matrix4 ModelMatrix;
public Vector2 TexturePosition;
public Vector2 TextureScale;
}
}

View file

@ -7,7 +7,7 @@ namespace SM.Base.Scene
public class Material
{
public TextureBase Texture;
public Color4 Tint;
public Color4 Tint = Color4.White;
public IShader Shader = Shaders.Default;
}

View file

@ -47,13 +47,19 @@
<ItemGroup>
<Compile Include="Drawing\DrawingBasis.cs" />
<Compile Include="Drawing\GenericTransformation.cs" />
<Compile Include="Drawing\Instance.cs" />
<Compile Include="Drawing\IShader.cs" />
<Compile Include="Drawing\IShowCollection.cs" />
<Compile Include="Drawing\IShowItem.cs" />
<Compile Include="Scene\IShowCollection.cs" />
<Compile Include="Scene\IShowItem.cs" />
<Compile Include="Drawing\Material.cs" />
<Compile Include="Scene\IBackgroundItem.cs" />
<Compile Include="Shader\InstanceShader.cs" />
<Compile Include="Shader\Shaders.cs" />
<Compile Include="Textures\Texture.cs" />
<Compile Include="Text\CharParameter.cs" />
<Compile Include="Text\Font.cs" />
<Compile Include="Text\FontCharStorage.cs" />
<Compile Include="Text\TextDrawingBasis.cs" />
<Compile Include="Types\Vector.cs" />
<Compile Include="Types\Vector2.cs" />
<Compile Include="Types\Vector3.cs" />

View file

@ -8,7 +8,7 @@ namespace SM.Base.Scene
where TCamera : GenericCamera, new()
{
public IShowItem Background;
protected IBackgroundItem _background;
public List<IShowItem> HUD { get; } = new List<IShowItem>();
public List<IShowItem> Objects { get; } = new List<IShowItem>();
public TCamera Camera { get; set; }
@ -21,7 +21,7 @@ namespace SM.Base.Scene
DrawContext backgroundDrawContext = context;
backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix();
Background?.Draw(backgroundDrawContext);
_background?.Draw(backgroundDrawContext);
for(int i = 0; i < Objects.Count; i++)
Objects[i].Draw(context);

View file

@ -0,0 +1,7 @@
namespace SM.Base.Scene
{
public interface IBackgroundItem : IShowItem
{
}
}

View file

@ -1,14 +1,17 @@
#version 330
#define maxInstances 32
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTex;
uniform mat4 MVP;
uniform mat4 ModelMatrix;
uniform mat4 ModelMatrix[maxInstances];
uniform vec2 TextureOffset[maxInstances];
uniform vec2 TextureScale[maxInstances];
out vec2 vTexture;
void main() {
vTexture = aTex;
vTexture = aTex * TextureScale[gl_InstanceID] + TextureOffset[gl_InstanceID];
gl_Position = MVP * ModelMatrix * vec4(aPos, 1);
gl_Position = MVP * ModelMatrix[gl_InstanceID] * vec4(aPos, 1);
}

View file

@ -13,26 +13,25 @@ namespace SM.Base.Shader
{
protected override bool AutoCompile { get; } = true;
public Action<UniformCollection, DrawContext> SetUniform;
public Action<UniformCollection, DrawContext, int> SetUniformVertex;
public Action<UniformCollection, DrawContext> SetUniformFragment;
public InstanceShader(string vertex, string fragment, Action<UniformCollection, DrawContext> setUniform) : base(new ShaderFileCollection(vertex, fragment))
public InstanceShader(string vertex, string fragment) : base(new ShaderFileCollection(vertex, fragment))
{
SetUniform = setUniform;
}
public void Draw(DrawContext context)
{
GL.UseProgram(this);
SetUniform.Invoke(Uniforms, context);
Uniforms["MVP"].SetMatrix4(context.View * context.World);
DrawObject(context.Mesh, true);
for (int i = 0; i < context.Instances.Length; i++) SetUniformVertex?.Invoke(Uniforms, context, i);
SetUniformFragment?.Invoke(Uniforms, context);
DrawObject(context.Mesh, context.Instances.Length, true);
GL.UseProgram(0);
}
public void DrawInstanced(DrawContext context, ICollection<Matrix4> instanceCollection)
{
throw new NotImplementedException();
}
}
}

View file

@ -1,5 +1,6 @@
using System.IO;
using System.Reflection;
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Shaders;
namespace SM.Base.Shader
@ -7,13 +8,20 @@ namespace SM.Base.Shader
public class Shaders
{
public static InstanceShader Default = new InstanceShader(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SM.Base.Shader.Files.default.vert")).ReadToEnd(),
new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SM.Base.Shader.Files.default.frag")).ReadToEnd(),
((u, context) =>
new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SM.Base.Shader.Files.default.frag")).ReadToEnd())
{
SetUniformFragment = (u, context) =>
{
u["MVP"].SetMatrix4(context.View * context.World);
u["ModelMatrix"].SetMatrix4(context.ModelMatrix);
u["Tint"].SetUniform4(context.Material.Tint);
u["Texture"].SetTexture(context.Material.Texture, 0, u["UseTexture"]);
}));
},
SetUniformVertex = (u, context, i) =>
{
GL.UniformMatrix4(u["ModelMatrix"] + i, false, ref context.Instances[i].ModelMatrix);
GL.Uniform2(u["TextureOffset"] + i, context.Instances[i].TexturePosition);
GL.Uniform2(u["TextureScale"] + i, context.Instances[i].TextureScale);
}
};
}
}

View file

@ -0,0 +1,16 @@
using System;
using System.Configuration.Assemblies;
using OpenTK;
namespace SM.Data.Fonts
{
[Serializable]
public class CharParameter
{
public int X;
public float Width;
public float RelativeX;
public float RelativeWidth;
}
}

View file

@ -0,0 +1,95 @@
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Security.Policy;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using SM.Data.Fonts;
using SM.OGL.Texture;
namespace SM.Base.Text
{
public class Font : TextureBase
{
public Dictionary<char, CharParameter> Positions = new Dictionary<char, CharParameter>();
public override TextureMinFilter Filter { get; set; } = TextureMinFilter.Linear;
public override TextureWrapMode WrapMode { get; set; } = TextureWrapMode.ClampToEdge;
public FontFamily FontFamily;
public FontStyle FontStyle = FontStyle.Regular;
public float FontSize = 12;
public ICollection<char> CharSet = FontCharStorage.SimpleUTF8;
public int Width { get; private set; }
public int Height { get; private set; }
public Bitmap Texture { get; private set; }
public Font(string path)
{
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(path);
FontFamily = pfc.Families[0];
}
public Font(FontFamily font)
{
FontFamily = font;
}
public void RegenerateTexture()
{
Bitmap map = new Bitmap(1000,20);
Width = 0;
Height = 0;
using (System.Drawing.Font f = new System.Drawing.Font(FontFamily, FontSize, FontStyle))
{
using (Graphics g = Graphics.FromImage(map))
{
g.Clear(Color.Transparent);
foreach (char c in CharSet)
{
string s = c.ToString();
SizeF size = g.MeasureString(s, f);
try
{
Positions.Add(c, new CharParameter() { Width = size.Width, X = Width });
}
catch
{
// ignored
}
if (Height < size.Height) Height = (int)size.Height;
Width += (int)size.Width;
}
}
map = new Bitmap(Width, Height);
using (Graphics g = Graphics.FromImage(map))
{
foreach (KeyValuePair<char, CharParameter> keyValuePair in Positions)
{
keyValuePair.Value.RelativeX = (keyValuePair.Value.X + 0.00001f) / Width;
keyValuePair.Value.RelativeWidth = (keyValuePair.Value.Width) / Width;
g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, keyValuePair.Value.X, 0);
}
}
}
Texture = map;
_id = SM.Base.Textures.Texture.GenerateTexture(map, Filter, WrapMode);
}
protected override void Compile()
{
base.Compile();
RegenerateTexture();
}
}
}

View file

@ -0,0 +1,14 @@
namespace SM.Data.Fonts
{
public class FontCharStorage
{
public static readonly char[] SimpleUTF8 = new char[]
{
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'
};
}
}

View file

@ -0,0 +1,83 @@
using System;
using OpenTK;
using OpenTK.Graphics;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.Data.Fonts;
namespace SM.Base.Text
{
public abstract class TextDrawingBasis<TTransform> : DrawingBasis<TTransform>
where TTransform : GenericTransformation, new()
{
protected Instance[] _modelMatrixs;
protected string _text;
public Font Font
{
get => (Font) _material.Texture;
set
{
_material.Texture = value;
GenerateMatrixes();
}
}
public string Text
{
get => _text;
set
{
_text = value;
GenerateMatrixes();
}
}
public Color4 Color
{
get => _material.Tint;
set => _material.Tint = value;
}
protected TextDrawingBasis(Font font)
{
_material.Texture = font;
}
public override void Draw(DrawContext context)
{
base.Draw(context);
if (_modelMatrixs == null) GenerateMatrixes();
}
public void GenerateMatrixes()
{
if (!Font.WasCompiled) Font.RegenerateTexture();
_modelMatrixs = new Instance[_text.Length];
float x = 0;
for (var i = 0; i < _text.Length; i++)
{
CharParameter parameter;
try
{
parameter = Font.Positions[_text[i]];
}
catch
{
throw new Exception("Font doesn't contain '"+_text[i]+"'");
}
Matrix4 matrix = Matrix4.CreateScale(parameter.Width, Font.Height, 1) * Matrix4.CreateTranslation(x, 0, 0);
_modelMatrixs[i] = new Instance
{
ModelMatrix = matrix,
TexturePosition = new Vector2(parameter.RelativeX, 0),
TextureScale = new Vector2(parameter.RelativeWidth, 1)
};
x += parameter.Width;
}
}
}
}

View file

@ -9,6 +9,7 @@ namespace SM.Base.Textures
public class Texture : TextureBase
{
public Bitmap Map;
public bool AutoDispose = false;
public Texture(Bitmap map) : this(map, TextureMinFilter.Linear, TextureWrapMode.Repeat) {}
@ -23,13 +24,18 @@ namespace SM.Base.Textures
{
base.Compile();
_id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, _id);
_id = GenerateTexture(Map, Filter, WrapMode, AutoDispose);
}
BitmapData data = Map.LockBits(new Rectangle(0, 0, Map.Width, Map.Height), ImageLockMode.ReadOnly,
Map.PixelFormat);
public static int GenerateTexture(Bitmap map, TextureMinFilter filter, TextureWrapMode wrapMode, bool dispose = false)
{
int id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id);
bool transparenz = Map.PixelFormat == PixelFormat.Format32bppArgb;
BitmapData data = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadOnly,
map.PixelFormat);
bool transparenz = map.PixelFormat == PixelFormat.Format32bppArgb;
GL.TexImage2D(TextureTarget.Texture2D, 0,
transparenz ? PixelInternalFormat.Rgba : PixelInternalFormat.Rgb,
@ -37,15 +43,19 @@ namespace SM.Base.Textures
transparenz ? OpenTK.Graphics.OpenGL4.PixelFormat.Bgra : OpenTK.Graphics.OpenGL4.PixelFormat.Bgr,
PixelType.UnsignedByte, data.Scan0);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)Filter);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)Filter);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) WrapMode);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) WrapMode);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)filter);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)filter);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)wrapMode);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)wrapMode);
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, 0);
Map.UnlockBits(data);
map.UnlockBits(data);
if (dispose) map.Dispose();
return id;
}
public static implicit operator Texture(Bitmap map) => new Texture(map);

View file

@ -1,4 +1,5 @@
using OpenTK;
using System.Collections.Generic;
using OpenTK;
using SM.Base.Scene;
using SM.OGL.Mesh;
@ -7,10 +8,11 @@ namespace SM.Base.Contexts
public struct DrawContext
{
public bool ForceViewport;
public bool Instancing;
public Matrix4 World;
public Matrix4 View;
public Matrix4 ModelMatrix;
public Instance[] Instances;
public Mesh Mesh;
public Material Material;

View file

@ -39,7 +39,7 @@ namespace SM.Base
{
World = _viewportCamera.World,
View = _viewportCamera.CalculateViewMatrix(),
ModelMatrix = Matrix4.Identity,
Instances = new[] { new Instance {ModelMatrix = Matrix4.Identity, TexturePosition = Vector2.Zero, TextureScale = Vector2.One } },
Mesh = Plate.Object,
ForceViewport = ForceViewportCamera,
WorldScale = WorldScale

View file

@ -7,6 +7,8 @@ namespace SM.OGL
protected int _id = -1;
protected virtual bool AutoCompile { get; } = false;
public bool WasCompiled => _id > 0;
public virtual int ID
{
get

View file

@ -36,7 +36,8 @@ namespace SM.OGL.Shaders
string key = GL.GetActiveUniform(_id, i, out _, out _);
int loc = GL.GetUniformLocation(_id, key);
if (key.StartsWith("[")) key = key.Split('[', ']')[0];
if (key.EndsWith("]"))
key = key.Split('[', ']')[0];
Uniforms.Add(key, loc);
}
@ -47,10 +48,10 @@ namespace SM.OGL.Shaders
Load();
}
public void DrawObject(Mesh.Mesh mesh, bool bindVAO = false)
public void DrawObject(Mesh.Mesh mesh, int amount, bool bindVAO = false)
{
if (bindVAO) GL.BindVertexArray(mesh);
GL.DrawArrays(mesh.PrimitiveType, 0, mesh.Vertex.Count);
GL.DrawArraysInstanced(mesh.PrimitiveType, 0, mesh.Vertex.Count, amount);
}
}
}

View file

@ -181,5 +181,7 @@ namespace SM.OGL.Shaders
GL.BindTexture(TextureTarget.Texture2D, texture);
SetUniform1(texturePos);
}
public static implicit operator int(Uniform u) => u.Location;
}
}

View file

@ -3,14 +3,17 @@ using OpenTK;
using OpenTK.Graphics;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.Base.StaticObjects;
using SM.Base.Textures;
using SM.OGL.Texture;
using SM2D.Types;
namespace SM2D.Drawing
{
public class DrawBackground : DrawingBasis
public class DrawBackground : IBackgroundItem
{
private Material _material = new Material();
public Color4 Color
{
get => _material.Tint;
@ -38,11 +41,17 @@ namespace SM2D.Drawing
Texture = (Texture) texture;
}
public override void Draw(DrawContext context)
public void Update(UpdateContext context)
{
ApplyContext(ref context);
throw new System.NotImplementedException();
}
context.ModelMatrix = Matrix4.CreateScale(context.WorldScale.X, context.WorldScale.Y, 1);
public void Draw(DrawContext context)
{
context.Material = _material;
context.Mesh = Plate.Object;
context.Instances[0].ModelMatrix = Matrix4.CreateScale(context.WorldScale.X, context.WorldScale.Y, 1);
_material.Shader.Draw(context);
}
}

View file

@ -1,24 +0,0 @@
using OpenTK;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.Base.Shader;
using SM.Base.StaticObjects;
using SM.OGL.Mesh;
namespace SM2D.Drawing
{
public class DrawEmpty : IShowItem
{
public void Update(UpdateContext context)
{
throw new System.NotImplementedException();
}
public void Draw(DrawContext context)
{
context.ModelMatrix = Matrix4.CreateScale(100, 100, 1);
Shaders.Default.Draw(context);
}
}
}

View file

@ -0,0 +1,25 @@
using SM.Base.Contexts;
using SM.Base.Text;
using SM2D.Types;
namespace SM2D.Drawing
{
public class DrawText : TextDrawingBasis<Transformation>
{
public DrawText(Font font, string text) : base(font)
{
_text = text;
}
public override void Draw(DrawContext context)
{
base.Draw(context);
context.Instances = _modelMatrixs;
ApplyContext(ref context);
context.View = Transform.GetMatrix() * context.View;
_material.Shader.Draw(context);
}
}
}

View file

@ -44,7 +44,7 @@ namespace SM2D.Drawing
ApplyContext(ref context);
Transform.Size = new Vector2(Texture.Map.Width * MasterScale * Scale, Texture.Map.Height * MasterScale * Scale);
context.ModelMatrix = Transform.GetMatrix();
context.Instances[0].ModelMatrix = Transform.GetMatrix();
_material.Shader.Draw(context);
}

View file

@ -46,7 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Drawing\DrawBackground.cs" />
<Compile Include="Drawing\DrawEmpty.cs" />
<Compile Include="Drawing\DrawText.cs" />
<Compile Include="Drawing\DrawTexture.cs" />
<Compile Include="GLWindow2D.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View file

@ -1,9 +1,16 @@
using SM.Base.Scene;
using OpenTK.Graphics;
using SM.Base.Scene;
using SM2D.Drawing;
namespace SM2D.Scene
{
public class Scene : GenericScene<Camera>
{
public DrawBackground Background => (DrawBackground)_background;
public Scene()
{
_background = new DrawBackground(Color4.Black);
}
}
}

View file

@ -1,23 +1,25 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics;
using SM.Base;
using SM2D;
using SM2D.Drawing;
using SM2D.Scene;
using Font = SM.Base.Text.Font;
using Vector2 = OpenTK.Vector2;
namespace SM_TEST
{
class Program
{
static Scene scene;
private static Font font;
static void Main(string[] args)
{
font = new Font(@"C:\Windows\Fonts\Arial.ttf")
{
FontSize = 64
};
GLWindow2D window = new GLWindow2D {Scaling = new Vector2(0, 1000)};
window.SetScene(scene = new Scene());
window.Load += WindowOnLoad;
@ -26,13 +28,17 @@ namespace SM_TEST
private static void WindowOnLoad(object sender, EventArgs e)
{
scene.Objects.Add(new DrawTexture(new Bitmap("soldier_logo.png")));
scene.Objects.Add(new DrawTexture(new Bitmap("soldier_logo.png"))
{
Transform = { Position = new Vector2(100), Rotation = 45},
});
scene.Background = new DrawBackground(Color4.Beige);
scene.Objects.Add(new DrawText(font, "Testing...")
{
Transform = {Size = new Vector2(1), Position = new SM.Base.Types.Vector2(0, -400)},
Color = Color4.Black
});
scene.Background.Color = Color4.Beige;
}
}
}