From c4a0847567aecf9284cdf257cb255f170c60ece0 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Sun, 20 Sep 2020 18:29:09 +0200 Subject: [PATCH] 20.09.2020 + Instance Drawing + Text and Font ~ Made "DrawBackground" forced Background for 2D - DrawEmpty --- SMCode/SM.Base/Drawing/DrawingBasis.cs | 5 +- SMCode/SM.Base/Drawing/IShader.cs | 1 - SMCode/SM.Base/Drawing/Instance.cs | 11 +++ SMCode/SM.Base/Drawing/Material.cs | 2 +- SMCode/SM.Base/SM.Base.csproj | 10 +- SMCode/SM.Base/Scene/GenericScene.cs | 4 +- SMCode/SM.Base/Scene/IBackgroundItem.cs | 7 ++ .../{Drawing => Scene}/IShowCollection.cs | 0 .../SM.Base/{Drawing => Scene}/IShowItem.cs | 0 SMCode/SM.Base/Shader/Files/default.vert | 9 +- SMCode/SM.Base/Shader/InstanceShader.cs | 19 ++-- SMCode/SM.Base/Shader/Shaders.cs | 18 +++- SMCode/SM.Base/Text/CharParameter.cs | 16 ++++ SMCode/SM.Base/Text/Font.cs | 95 +++++++++++++++++++ SMCode/SM.Base/Text/FontCharStorage.cs | 14 +++ SMCode/SM.Base/Text/TextDrawingBasis.cs | 83 ++++++++++++++++ SMCode/SM.Base/Textures/Texture.cs | 32 ++++--- SMCode/SM.Base/Window/Contexts/DrawContext.cs | 6 +- SMCode/SM.Base/Window/GenericWindow.cs | 2 +- SMCode/SM.OGL/GLObject.cs | 2 + SMCode/SM.OGL/Shaders/GenericShader.cs | 9 +- SMCode/SM.OGL/Shaders/Uniform.cs | 2 + SMCode/SM2D/Drawing/DrawBackground.cs | 17 +++- SMCode/SM2D/Drawing/DrawEmpty.cs | 24 ----- SMCode/SM2D/Drawing/DrawText.cs | 25 +++++ SMCode/SM2D/Drawing/DrawTexture.cs | 2 +- SMCode/SM2D/SM2D.csproj | 2 +- SMCode/SM2D/Scene/Scene.cs | 11 ++- SM_TEST/Program.cs | 22 +++-- 29 files changed, 365 insertions(+), 85 deletions(-) create mode 100644 SMCode/SM.Base/Drawing/Instance.cs create mode 100644 SMCode/SM.Base/Scene/IBackgroundItem.cs rename SMCode/SM.Base/{Drawing => Scene}/IShowCollection.cs (100%) rename SMCode/SM.Base/{Drawing => Scene}/IShowItem.cs (100%) create mode 100644 SMCode/SM.Base/Text/CharParameter.cs create mode 100644 SMCode/SM.Base/Text/Font.cs create mode 100644 SMCode/SM.Base/Text/FontCharStorage.cs create mode 100644 SMCode/SM.Base/Text/TextDrawingBasis.cs delete mode 100644 SMCode/SM2D/Drawing/DrawEmpty.cs create mode 100644 SMCode/SM2D/Drawing/DrawText.cs diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs index 3659374..3bde646 100644 --- a/SMCode/SM.Base/Drawing/DrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -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 : DrawingBasis + public abstract class DrawingBasis : DrawingBasis where TTransformation : GenericTransformation, new() { - public TTransformation Transform = new TTransformation(); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/IShader.cs b/SMCode/SM.Base/Drawing/IShader.cs index aabd2cf..e6df907 100644 --- a/SMCode/SM.Base/Drawing/IShader.cs +++ b/SMCode/SM.Base/Drawing/IShader.cs @@ -7,6 +7,5 @@ namespace SM.Base.Scene public interface IShader { void Draw(DrawContext context); - void DrawInstanced(DrawContext context, ICollection instanceCollection); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/Instance.cs b/SMCode/SM.Base/Drawing/Instance.cs new file mode 100644 index 0000000..6d15f60 --- /dev/null +++ b/SMCode/SM.Base/Drawing/Instance.cs @@ -0,0 +1,11 @@ +using OpenTK; + +namespace SM.Base.Scene +{ + public struct Instance + { + public Matrix4 ModelMatrix; + public Vector2 TexturePosition; + public Vector2 TextureScale; + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/Material.cs b/SMCode/SM.Base/Drawing/Material.cs index e716b05..4b86414 100644 --- a/SMCode/SM.Base/Drawing/Material.cs +++ b/SMCode/SM.Base/Drawing/Material.cs @@ -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; } diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index 0fec514..9d0b979 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -47,13 +47,19 @@ + - - + + + + + + + diff --git a/SMCode/SM.Base/Scene/GenericScene.cs b/SMCode/SM.Base/Scene/GenericScene.cs index fb2555c..68d861d 100644 --- a/SMCode/SM.Base/Scene/GenericScene.cs +++ b/SMCode/SM.Base/Scene/GenericScene.cs @@ -8,7 +8,7 @@ namespace SM.Base.Scene where TCamera : GenericCamera, new() { - public IShowItem Background; + protected IBackgroundItem _background; public List HUD { get; } = new List(); public List Objects { get; } = new List(); 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); diff --git a/SMCode/SM.Base/Scene/IBackgroundItem.cs b/SMCode/SM.Base/Scene/IBackgroundItem.cs new file mode 100644 index 0000000..bedd535 --- /dev/null +++ b/SMCode/SM.Base/Scene/IBackgroundItem.cs @@ -0,0 +1,7 @@ +namespace SM.Base.Scene +{ + public interface IBackgroundItem : IShowItem + { + + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/IShowCollection.cs b/SMCode/SM.Base/Scene/IShowCollection.cs similarity index 100% rename from SMCode/SM.Base/Drawing/IShowCollection.cs rename to SMCode/SM.Base/Scene/IShowCollection.cs diff --git a/SMCode/SM.Base/Drawing/IShowItem.cs b/SMCode/SM.Base/Scene/IShowItem.cs similarity index 100% rename from SMCode/SM.Base/Drawing/IShowItem.cs rename to SMCode/SM.Base/Scene/IShowItem.cs diff --git a/SMCode/SM.Base/Shader/Files/default.vert b/SMCode/SM.Base/Shader/Files/default.vert index 2a49e46..cdfd85c 100644 --- a/SMCode/SM.Base/Shader/Files/default.vert +++ b/SMCode/SM.Base/Shader/Files/default.vert @@ -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); } \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/InstanceShader.cs b/SMCode/SM.Base/Shader/InstanceShader.cs index 067284f..0fa00aa 100644 --- a/SMCode/SM.Base/Shader/InstanceShader.cs +++ b/SMCode/SM.Base/Shader/InstanceShader.cs @@ -13,26 +13,25 @@ namespace SM.Base.Shader { protected override bool AutoCompile { get; } = true; - public Action SetUniform; + public Action SetUniformVertex; + public Action SetUniformFragment; - public InstanceShader(string vertex, string fragment, Action 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 instanceCollection) - { - throw new NotImplementedException(); - } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/Shaders.cs b/SMCode/SM.Base/Shader/Shaders.cs index 38be41e..1b813e2 100644 --- a/SMCode/SM.Base/Shader/Shaders.cs +++ b/SMCode/SM.Base/Shader/Shaders.cs @@ -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); + } + }; } } \ No newline at end of file diff --git a/SMCode/SM.Base/Text/CharParameter.cs b/SMCode/SM.Base/Text/CharParameter.cs new file mode 100644 index 0000000..e21f2b2 --- /dev/null +++ b/SMCode/SM.Base/Text/CharParameter.cs @@ -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; + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Text/Font.cs b/SMCode/SM.Base/Text/Font.cs new file mode 100644 index 0000000..71396e9 --- /dev/null +++ b/SMCode/SM.Base/Text/Font.cs @@ -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 Positions = new Dictionary(); + + 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 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 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(); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Text/FontCharStorage.cs b/SMCode/SM.Base/Text/FontCharStorage.cs new file mode 100644 index 0000000..38739d3 --- /dev/null +++ b/SMCode/SM.Base/Text/FontCharStorage.cs @@ -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', '{', '|', '}', '~' + }; + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Text/TextDrawingBasis.cs b/SMCode/SM.Base/Text/TextDrawingBasis.cs new file mode 100644 index 0000000..826a93b --- /dev/null +++ b/SMCode/SM.Base/Text/TextDrawingBasis.cs @@ -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 : DrawingBasis + 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; + } + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Textures/Texture.cs b/SMCode/SM.Base/Textures/Texture.cs index 13524bd..24c607d 100644 --- a/SMCode/SM.Base/Textures/Texture.cs +++ b/SMCode/SM.Base/Textures/Texture.cs @@ -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,29 +24,38 @@ 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); - GL.TexImage2D(TextureTarget.Texture2D, 0, + bool transparenz = map.PixelFormat == PixelFormat.Format32bppArgb; + + GL.TexImage2D(TextureTarget.Texture2D, 0, transparenz ? PixelInternalFormat.Rgba : PixelInternalFormat.Rgb, data.Width, data.Height, 0, 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); diff --git a/SMCode/SM.Base/Window/Contexts/DrawContext.cs b/SMCode/SM.Base/Window/Contexts/DrawContext.cs index 9a2ec4f..e51b40a 100644 --- a/SMCode/SM.Base/Window/Contexts/DrawContext.cs +++ b/SMCode/SM.Base/Window/Contexts/DrawContext.cs @@ -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; diff --git a/SMCode/SM.Base/Window/GenericWindow.cs b/SMCode/SM.Base/Window/GenericWindow.cs index 2c8ec1d..e849b6d 100644 --- a/SMCode/SM.Base/Window/GenericWindow.cs +++ b/SMCode/SM.Base/Window/GenericWindow.cs @@ -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 diff --git a/SMCode/SM.OGL/GLObject.cs b/SMCode/SM.OGL/GLObject.cs index f448a08..c0d369c 100644 --- a/SMCode/SM.OGL/GLObject.cs +++ b/SMCode/SM.OGL/GLObject.cs @@ -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 diff --git a/SMCode/SM.OGL/Shaders/GenericShader.cs b/SMCode/SM.OGL/Shaders/GenericShader.cs index 28d9ec5..0d0c406 100644 --- a/SMCode/SM.OGL/Shaders/GenericShader.cs +++ b/SMCode/SM.OGL/Shaders/GenericShader.cs @@ -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); + } } } \ No newline at end of file diff --git a/SMCode/SM.OGL/Shaders/Uniform.cs b/SMCode/SM.OGL/Shaders/Uniform.cs index d9dd56b..5ee3b0e 100644 --- a/SMCode/SM.OGL/Shaders/Uniform.cs +++ b/SMCode/SM.OGL/Shaders/Uniform.cs @@ -181,5 +181,7 @@ namespace SM.OGL.Shaders GL.BindTexture(TextureTarget.Texture2D, texture); SetUniform1(texturePos); } + + public static implicit operator int(Uniform u) => u.Location; } } \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawBackground.cs b/SMCode/SM2D/Drawing/DrawBackground.cs index ebdfbc4..a5a9fef 100644 --- a/SMCode/SM2D/Drawing/DrawBackground.cs +++ b/SMCode/SM2D/Drawing/DrawBackground.cs @@ -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); } } diff --git a/SMCode/SM2D/Drawing/DrawEmpty.cs b/SMCode/SM2D/Drawing/DrawEmpty.cs deleted file mode 100644 index 4fbc996..0000000 --- a/SMCode/SM2D/Drawing/DrawEmpty.cs +++ /dev/null @@ -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); - } - } -} \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawText.cs b/SMCode/SM2D/Drawing/DrawText.cs new file mode 100644 index 0000000..3f19ab6 --- /dev/null +++ b/SMCode/SM2D/Drawing/DrawText.cs @@ -0,0 +1,25 @@ +using SM.Base.Contexts; +using SM.Base.Text; +using SM2D.Types; + +namespace SM2D.Drawing +{ + public class DrawText : TextDrawingBasis + { + 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); + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawTexture.cs b/SMCode/SM2D/Drawing/DrawTexture.cs index bfa6cc1..c8b431e 100644 --- a/SMCode/SM2D/Drawing/DrawTexture.cs +++ b/SMCode/SM2D/Drawing/DrawTexture.cs @@ -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); } diff --git a/SMCode/SM2D/SM2D.csproj b/SMCode/SM2D/SM2D.csproj index 57168e0..e79a991 100644 --- a/SMCode/SM2D/SM2D.csproj +++ b/SMCode/SM2D/SM2D.csproj @@ -46,7 +46,7 @@ - + diff --git a/SMCode/SM2D/Scene/Scene.cs b/SMCode/SM2D/Scene/Scene.cs index e6835d3..67c5b87 100644 --- a/SMCode/SM2D/Scene/Scene.cs +++ b/SMCode/SM2D/Scene/Scene.cs @@ -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 { - + public DrawBackground Background => (DrawBackground)_background; + + public Scene() + { + _background = new DrawBackground(Color4.Black); + } } } \ No newline at end of file diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index c48d7af..59463ed 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -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; } } } \ No newline at end of file