From 2aa12f8d25ed88280b8287468966b89c8430ce8d Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Sun, 27 Sep 2020 11:58:14 +0200 Subject: [PATCH] 27.09.2020 ~ Moved Default-Shader to 2D to provied 2D-specific feature ~ Fixed UVs in Polygon --- SMCode/SM.Base/Defaults.cs | 2 - SMCode/SM.Base/Drawing/DrawingBasis.cs | 2 +- SMCode/SM.Base/Drawing/Material.cs | 3 +- SMCode/SM.Base/SM.Base.csproj | 8 ++-- SMCode/SM.Base/Shader/InstanceShader.cs | 39 --------------- SMCode/SM.Base/Shader/Shaders.cs | 27 ----------- SMCode/SM.Base/Utility/Assembly.cs | 29 ++++++++++++ SMCode/SM.Base/Utility/BezierCurve.cs | 23 +++++++++ SMCode/SM.Base/Utility/Deltatime.cs | 21 +++++++++ SMCode/SM.Base/Utility/Randomize.cs | 22 +++++++++ SMCode/SM.OGL/Mesh/BoundingBox.cs | 2 +- SMCode/SM2D/Drawing/DrawBackground.cs | 4 ++ SMCode/SM2D/Drawing/DrawColor.cs | 2 +- SMCode/SM2D/GLWindow2D.cs | 3 ++ SMCode/SM2D/SM2D.csproj | 3 ++ SMCode/SM2D/Shader/Default2DShader.cs | 47 +++++++++++++++++++ .../Shader/ShaderFiles}/default.frag | 0 .../Shader/ShaderFiles}/default.vert | 0 SM_TEST/Program.cs | 12 ++--- 19 files changed, 166 insertions(+), 83 deletions(-) delete mode 100644 SMCode/SM.Base/Shader/InstanceShader.cs delete mode 100644 SMCode/SM.Base/Shader/Shaders.cs create mode 100644 SMCode/SM.Base/Utility/Assembly.cs create mode 100644 SMCode/SM.Base/Utility/BezierCurve.cs create mode 100644 SMCode/SM.Base/Utility/Deltatime.cs create mode 100644 SMCode/SM.Base/Utility/Randomize.cs create mode 100644 SMCode/SM2D/Shader/Default2DShader.cs rename SMCode/{SM.Base/Shader/Files => SM2D/Shader/ShaderFiles}/default.frag (100%) rename SMCode/{SM.Base/Shader/Files => SM2D/Shader/ShaderFiles}/default.vert (100%) diff --git a/SMCode/SM.Base/Defaults.cs b/SMCode/SM.Base/Defaults.cs index c4bd22d..5e76af0 100644 --- a/SMCode/SM.Base/Defaults.cs +++ b/SMCode/SM.Base/Defaults.cs @@ -1,8 +1,6 @@ using SM.Base.Objects; using SM.Base.Objects.Static; using SM.Base.Scene; -using SM.Base.Shader; -using SM.OGL.Mesh; namespace SM.Base { diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs index 7ca9359..d823ee7 100644 --- a/SMCode/SM.Base/Drawing/DrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -7,7 +7,7 @@ namespace SM.Base.Scene public abstract class DrawingBasis : IShowItem { protected Material _material = new Material(); - protected GenericMesh _mesh = Plate.Object; + protected GenericMesh _mesh = Defaults.DefaultMesh; public virtual void Update(UpdateContext context) { diff --git a/SMCode/SM.Base/Drawing/Material.cs b/SMCode/SM.Base/Drawing/Material.cs index 4b86414..c313e25 100644 --- a/SMCode/SM.Base/Drawing/Material.cs +++ b/SMCode/SM.Base/Drawing/Material.cs @@ -1,5 +1,4 @@ using OpenTK.Graphics; -using SM.Base.Shader; using SM.OGL.Texture; namespace SM.Base.Scene @@ -9,6 +8,6 @@ namespace SM.Base.Scene public TextureBase Texture; public Color4 Tint = Color4.White; - public IShader Shader = Shaders.Default; + public IShader Shader = Defaults.DefaultShader; } } \ No newline at end of file diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index a15b790..d40e145 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -58,8 +58,6 @@ - - @@ -69,6 +67,10 @@ + + + + @@ -80,8 +82,6 @@ - - diff --git a/SMCode/SM.Base/Shader/InstanceShader.cs b/SMCode/SM.Base/Shader/InstanceShader.cs deleted file mode 100644 index a93fe55..0000000 --- a/SMCode/SM.Base/Shader/InstanceShader.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using OpenTK; -using OpenTK.Graphics.OpenGL4; -using SM.Base.Contexts; -using SM.Base.Scene; -using SM.OGL.Shaders; - -namespace SM.Base.Shader -{ - public class InstanceShader : GenericShader, IShader - { - protected override bool AutoCompile { get; } = true; - - public Action SetUniformVertex; - public Action SetUniformFragment; - - public InstanceShader(string vertex, string fragment) : base(new ShaderFileCollection(vertex, fragment)) - { - } - public void Draw(DrawContext context) - { - GL.UseProgram(this); - - Uniforms["MVP"].SetMatrix4(context.View * context.World); - Uniforms["HasVColor"].SetUniform1(context.Mesh.AttribDataIndex.ContainsKey(3) && context.Mesh.AttribDataIndex[3] != null); - - 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); - - CleanUp(); - - GL.UseProgram(0); - } - } -} \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/Shaders.cs b/SMCode/SM.Base/Shader/Shaders.cs deleted file mode 100644 index 1b813e2..0000000 --- a/SMCode/SM.Base/Shader/Shaders.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.IO; -using System.Reflection; -using OpenTK.Graphics.OpenGL4; -using SM.OGL.Shaders; - -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()) - { - - SetUniformFragment = (u, context) => - { - 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/Utility/Assembly.cs b/SMCode/SM.Base/Utility/Assembly.cs new file mode 100644 index 0000000..1fe6639 --- /dev/null +++ b/SMCode/SM.Base/Utility/Assembly.cs @@ -0,0 +1,29 @@ +using System; +using System.IO; +using System.Reflection; + +namespace SM.Utility +{ + public class AssemblyUtility + { + /// + /// Read a file that is saved in a assembly + /// + /// The assembly that contains the file + /// The path to the file inside the assembly + /// + public static string ReadAssemblyFile(Assembly ass, string path) { return new StreamReader(GetAssemblyStream(ass, path)).ReadToEnd(); } + + /// + /// Read a file that is saved in the calling assembly + /// + /// The path to the file inside the assembly + /// + public static string ReadAssemblyFile(string path) { return ReadAssemblyFile(Assembly.GetCallingAssembly(), path); } + + + public static Stream GetAssemblyStream(Assembly ass, string path) { return ass.GetManifestResourceStream(ass.GetName().Name + "." + path) ?? throw new InvalidOperationException("Assembly couldn't find resource at path '" + path + "'."); } + + public static Stream GetAssemblyStream(string path) { return GetAssemblyStream(Assembly.GetCallingAssembly(), path); } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Utility/BezierCurve.cs b/SMCode/SM.Base/Utility/BezierCurve.cs new file mode 100644 index 0000000..6076a86 --- /dev/null +++ b/SMCode/SM.Base/Utility/BezierCurve.cs @@ -0,0 +1,23 @@ +using System; + +namespace SM.Utility +{ + public class BezierCurve + { + public static float Calculate(float t, params float[] points) + { + int pointAmount = points.Length; + int itterations = pointAmount - 1; + + double x = Math.Pow(1 - t, itterations) * points[0]; + for (int i = 1; i < itterations; i++) + { + if (i % 2 == 0) x += itterations * (1 - t) * Math.Pow(t, itterations - 1) * points[i]; + else x += itterations * Math.Pow(1 - t, itterations - 1) * t * points[i]; + } + + x += Math.Pow(t, itterations) * points[pointAmount - 1]; + return (float)x; + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Utility/Deltatime.cs b/SMCode/SM.Base/Utility/Deltatime.cs new file mode 100644 index 0000000..f8427be --- /dev/null +++ b/SMCode/SM.Base/Utility/Deltatime.cs @@ -0,0 +1,21 @@ +namespace SM.Utility +{ + public class Deltatime + { + public static float UpdateDelta { get; internal set; } + public static float RenderDelta { get; internal set; } + + public bool UseRender; + public float Scale; + + public float DeltaTime => (UseRender ? RenderDelta : UpdateDelta) * Scale; + + public Deltatime(float scale = 1, bool useRender = false) + { + UseRender = useRender; + Scale = scale; + } + + + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Utility/Randomize.cs b/SMCode/SM.Base/Utility/Randomize.cs new file mode 100644 index 0000000..61ae0f5 --- /dev/null +++ b/SMCode/SM.Base/Utility/Randomize.cs @@ -0,0 +1,22 @@ +using System; + +namespace SM.Utility +{ + public class Randomize + { + public static Random Randomizer = new Random(); + + public static void SetSeed(int seed) { Randomizer = new Random(seed); } + + public static bool GetBool(float tolerance) { return Randomizer.NextDouble() > tolerance; } + + public static int GetInt() { return Randomizer.Next(); } + public static int GetInt(int max) { return Randomizer.Next(max); } + public static int GetInt(int min, int max) { return Randomizer.Next(min, max); } + + public static float GetFloat() { return (float)Randomizer.NextDouble(); } + public static float GetFloat(float max) { return (float)Randomizer.NextDouble() * max; } + public static float GetFloat(float min, float max) { return (float)Randomizer.NextDouble() * max + min; } + + } +} \ No newline at end of file diff --git a/SMCode/SM.OGL/Mesh/BoundingBox.cs b/SMCode/SM.OGL/Mesh/BoundingBox.cs index f106c3a..db765a8 100644 --- a/SMCode/SM.OGL/Mesh/BoundingBox.cs +++ b/SMCode/SM.OGL/Mesh/BoundingBox.cs @@ -24,7 +24,7 @@ namespace SM.OGL.Mesh for (int i = 0; i < 2; i++) { Min[i] = Math.Min(Min[i], vector[i]); - Max[i] = Math.Max(Min[i], vector[i]); + Max[i] = Math.Max(Max[i], vector[i]); } } diff --git a/SMCode/SM2D/Drawing/DrawBackground.cs b/SMCode/SM2D/Drawing/DrawBackground.cs index 7bf716e..2f1eab5 100644 --- a/SMCode/SM2D/Drawing/DrawBackground.cs +++ b/SMCode/SM2D/Drawing/DrawBackground.cs @@ -1,11 +1,13 @@ using System.Drawing; using OpenTK; using OpenTK.Graphics; +using SM.Base; using SM.Base.Contexts; using SM.Base.Objects.Static; using SM.Base.Scene; using SM.Base.Textures; using SM.OGL.Texture; +using SM2D.Shader; using SM2D.Types; namespace SM2D.Drawing @@ -48,6 +50,8 @@ namespace SM2D.Drawing public void Draw(DrawContext context) { + if (_material.Shader == null) _material.Shader = Defaults.DefaultShader; + context.Material = _material; context.Mesh = Plate.Object; diff --git a/SMCode/SM2D/Drawing/DrawColor.cs b/SMCode/SM2D/Drawing/DrawColor.cs index 7a77165..2269fd9 100644 --- a/SMCode/SM2D/Drawing/DrawColor.cs +++ b/SMCode/SM2D/Drawing/DrawColor.cs @@ -8,7 +8,7 @@ namespace SM2D.Drawing { public class DrawColor : DrawingBasis, I2DShowItem { - public Color4 Tint + public Color4 Color { get => _material.Tint; set => _material.Tint = value; diff --git a/SMCode/SM2D/GLWindow2D.cs b/SMCode/SM2D/GLWindow2D.cs index cb847df..3f5e73d 100644 --- a/SMCode/SM2D/GLWindow2D.cs +++ b/SMCode/SM2D/GLWindow2D.cs @@ -3,6 +3,7 @@ using OpenTK; using OpenTK.Graphics.OpenGL4; using SM.Base; using SM2D.Scene; +using SM2D.Shader; namespace SM2D { @@ -12,6 +13,8 @@ namespace SM2D protected override void OnLoad(EventArgs e) { + Defaults.DefaultShader = new Default2DShader(); + base.OnLoad(e); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); diff --git a/SMCode/SM2D/SM2D.csproj b/SMCode/SM2D/SM2D.csproj index 77aa34c..4284ee1 100644 --- a/SMCode/SM2D/SM2D.csproj +++ b/SMCode/SM2D/SM2D.csproj @@ -59,6 +59,7 @@ + @@ -74,6 +75,8 @@ + + diff --git a/SMCode/SM2D/Shader/Default2DShader.cs b/SMCode/SM2D/Shader/Default2DShader.cs new file mode 100644 index 0000000..9ac6abc --- /dev/null +++ b/SMCode/SM2D/Shader/Default2DShader.cs @@ -0,0 +1,47 @@ +using OpenTK.Graphics.OpenGL4; +using SM.Base.Contexts; +using SM.Base.Scene; +using SM.OGL.Shaders; +using SM.Utility; + +namespace SM2D.Shader +{ + public class Default2DShader : GenericShader, IShader + { + protected override bool AutoCompile { get; } = true; + + public Default2DShader() : base(new ShaderFileCollection( + AssemblyUtility.ReadAssemblyFile("Shader.ShaderFiles.default.vert"), + AssemblyUtility.ReadAssemblyFile("Shader.ShaderFiles.default.frag"))) + { + + } + public void Draw(DrawContext context) + { + GL.UseProgram(this); + + GL.BindVertexArray(context.Mesh); + + // Vertex Uniforms + Uniforms["MVP"].SetMatrix4(context.View * context.World); + Uniforms["HasVColor"].SetUniform1(context.Mesh.AttribDataIndex.ContainsKey(3) && context.Mesh.AttribDataIndex[3] != null); + + for (int i = 0; i < context.Instances.Length; i++) + { + GL.UniformMatrix4(Uniforms["ModelMatrix"] + i, false, ref context.Instances[i].ModelMatrix); + GL.Uniform2(Uniforms["TextureOffset"] + i, context.Instances[i].TexturePosition); + GL.Uniform2(Uniforms["TextureScale"] + i, context.Instances[i].TextureScale); + } + + // Fragment Uniforms + Uniforms["Tint"].SetUniform4(context.Material.Tint); + Uniforms["Texture"].SetTexture(context.Material.Texture, Uniforms["UseTexture"]); + + DrawObject(context.Mesh, context.Instances.Length); + + CleanUp(); + + GL.UseProgram(0); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/Files/default.frag b/SMCode/SM2D/Shader/ShaderFiles/default.frag similarity index 100% rename from SMCode/SM.Base/Shader/Files/default.frag rename to SMCode/SM2D/Shader/ShaderFiles/default.frag diff --git a/SMCode/SM.Base/Shader/Files/default.vert b/SMCode/SM2D/Shader/ShaderFiles/default.vert similarity index 100% rename from SMCode/SM.Base/Shader/Files/default.vert rename to SMCode/SM2D/Shader/ShaderFiles/default.vert diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index 2434c45..a24f552 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -24,7 +24,7 @@ namespace SM_TEST FontSize = 32 }; - GLWindow2D window = new GLWindow2D {Scaling = new Vector2(0, 500)}; + GLWindow2D window = new GLWindow2D {Scaling = new Vector2(0, 1000)}; window.SetScene(scene = new Scene()); window.Load += WindowOnLoad; window.UpdateFrame += WindowOnUpdateFrame; @@ -50,7 +50,7 @@ namespace SM_TEST { ZIndex = 1 }); - col.Objects.Add(new DrawColor(Color4.Aqua) + col.Objects.Add(new DrawColor(Color4.Black) { Transform = { Rotation = 45, Position = new SM.Base.Types.Vector2(0, 25) }, }); @@ -82,8 +82,8 @@ namespace SM_TEST new PolygonVertex(new Vector2(1, .75f), Color4.White), new PolygonVertex(new Vector2(.75f, 1), Color4.White), new PolygonVertex(new Vector2(.25f, 1), Color4.White), - new PolygonVertex(new Vector2(0, .75f), Color4.Gray), - new PolygonVertex(new Vector2(0, .25f), Color4.Gray) + new PolygonVertex(new Vector2(0, .75f), new Color4(10,10,10,255)), + new PolygonVertex(new Vector2(0, .25f), new Color4(10,10,10,255)) }), Color4.LawnGreen) { Transform = {Position = new SM.Base.Types.Vector2(50,0)} @@ -96,8 +96,8 @@ namespace SM_TEST new PolygonVertex(new Vector2(1, .75f), Color4.White), new PolygonVertex(new Vector2(.75f, 1), Color4.White), new PolygonVertex(new Vector2(.25f, 1), Color4.White), - new PolygonVertex(new Vector2(0, .75f), Color4.Gray), - new PolygonVertex(new Vector2(0, .25f), Color4.Gray) + new PolygonVertex(new Vector2(0, .75f), new Color4(10,10,10,255)), + new PolygonVertex(new Vector2(0, .25f), new Color4(10,10,10,255)) }), new Bitmap("soldier_logo.png")) { Transform = {Position = new SM.Base.Types.Vector2(-50,0)}