diff --git a/SMCode/SM.Base/Defaults.cs b/SMCode/SM.Base/Defaults.cs new file mode 100644 index 0000000..c4bd22d --- /dev/null +++ b/SMCode/SM.Base/Defaults.cs @@ -0,0 +1,14 @@ +using SM.Base.Objects; +using SM.Base.Objects.Static; +using SM.Base.Scene; +using SM.Base.Shader; +using SM.OGL.Mesh; + +namespace SM.Base +{ + public class Defaults + { + public static IShader DefaultShader; + public static Mesh DefaultMesh = Plate.Object; + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs index 3bde646..7ca9359 100644 --- a/SMCode/SM.Base/Drawing/DrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -1,5 +1,5 @@ using SM.Base.Contexts; -using SM.Base.StaticObjects; +using SM.Base.Objects.Static; using SM.OGL.Mesh; namespace SM.Base.Scene @@ -7,7 +7,7 @@ namespace SM.Base.Scene public abstract class DrawingBasis : IShowItem { protected Material _material = new Material(); - protected Mesh _mesh = Plate.Object; + protected GenericMesh _mesh = Plate.Object; public virtual void Update(UpdateContext context) { diff --git a/SMCode/SM.Base/Objects/Mesh.cs b/SMCode/SM.Base/Objects/Mesh.cs new file mode 100644 index 0000000..e29c01a --- /dev/null +++ b/SMCode/SM.Base/Objects/Mesh.cs @@ -0,0 +1,14 @@ +using SM.OGL.Mesh; + +namespace SM.Base.Objects +{ + public class Mesh : GenericMesh + { + public virtual VBO Color { get; } + + protected Mesh() + { + AttribDataIndex.Add(3, Color); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/StaticObjects/Plate.cs b/SMCode/SM.Base/Objects/Static/Plate.cs similarity index 67% rename from SMCode/SM.Base/StaticObjects/Plate.cs rename to SMCode/SM.Base/Objects/Static/Plate.cs index c80664b..01061dd 100644 --- a/SMCode/SM.Base/StaticObjects/Plate.cs +++ b/SMCode/SM.Base/Objects/Static/Plate.cs @@ -1,7 +1,8 @@ -using OpenTK.Graphics.OpenGL4; +using OpenTK; +using OpenTK.Graphics.OpenGL4; using SM.OGL.Mesh; -namespace SM.Base.StaticObjects +namespace SM.Base.Objects.Static { public class Plate : Mesh { @@ -13,8 +14,6 @@ namespace SM.Base.StaticObjects {-.5f, .5f, 0}, {.5f, .5f, 0}, {.5f, -.5f, 0}, - {0,0,0}, - {0,0,0}, }; public override VBO UVs { get; } = new VBO(pointerSize: 2) @@ -23,12 +22,14 @@ namespace SM.Base.StaticObjects {0, 1}, {1, 1}, {1, 0}, - {0, 0}, - {0, 0}, }; public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Quads; + public override BoundingBox BoundingBox { get; } = new BoundingBox(new Vector3(-.5f, -.5f, 0), new Vector3(.5f, .5f, 0)); + + //public override int[] Indices { get; set; } = new[] {0, 1, 2, 3}; + private Plate() {} } } \ No newline at end of file diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index 9d0b979..a15b790 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + latest pdbonly @@ -29,6 +30,7 @@ TRACE prompt 4 + latest @@ -45,14 +47,17 @@ + + + @@ -70,7 +75,7 @@ - + diff --git a/SMCode/SM.Base/Scene/GenericItemCollection.cs b/SMCode/SM.Base/Scene/GenericItemCollection.cs new file mode 100644 index 0000000..cc117a0 --- /dev/null +++ b/SMCode/SM.Base/Scene/GenericItemCollection.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; +using SM.Base.Contexts; + +namespace SM.Base.Scene +{ + public abstract class GenericItemCollection : IShowItem, IShowCollection + where TItem : IShowItem + where TTransformation : GenericTransformation, new() + { + public List Objects { get; } = new List(); + public TTransformation Transform = new TTransformation(); + public void Update(UpdateContext context) + { + throw new System.NotImplementedException(); + } + + public virtual void Draw(DrawContext context) + { + context.View = Transform.GetMatrix() * context.View; + + for (int i = 0; i < Objects.Count; i++) + Objects[i].Draw(context); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/GenericScene.cs b/SMCode/SM.Base/Scene/GenericScene.cs index 68d861d..161fa2c 100644 --- a/SMCode/SM.Base/Scene/GenericScene.cs +++ b/SMCode/SM.Base/Scene/GenericScene.cs @@ -4,18 +4,20 @@ using SM.Base.Contexts; namespace SM.Base.Scene { - public abstract class GenericScene : IShowCollection + public abstract class GenericScene : IShowCollection where TCamera : GenericCamera, new() + where TItem : IShowItem { protected IBackgroundItem _background; - public List HUD { get; } = new List(); - public List Objects { get; } = new List(); + public List HUD { get; } = new List(); + public List Objects { get; } = new List(); public TCamera Camera { get; set; } public TCamera BackgroundCamera { get; set; } = new TCamera(); + public TCamera HUDCamera { get; set; } = new TCamera(); public Dictionary Cameras = new Dictionary(); - public void Draw(DrawContext context) + public virtual void Draw(DrawContext context) { if (!context.ForceViewport && Camera != null) context.View = Camera.ViewMatrix; @@ -26,7 +28,7 @@ namespace SM.Base.Scene for(int i = 0; i < Objects.Count; i++) Objects[i].Draw(context); - context.View = Matrix4.Identity; + context.View = HUDCamera.CalculateViewMatrix(); for (int i = 0; i < HUD.Count; i++) HUD[i].Draw(context); } diff --git a/SMCode/SM.Base/Scene/IShowCollection.cs b/SMCode/SM.Base/Scene/IShowCollection.cs index e7e7ad1..83d53a7 100644 --- a/SMCode/SM.Base/Scene/IShowCollection.cs +++ b/SMCode/SM.Base/Scene/IShowCollection.cs @@ -1,9 +1,12 @@ using System.Collections.Generic; +using SM.Base.Contexts; namespace SM.Base.Scene { - public interface IShowCollection + public interface IShowCollection where TItem : IShowItem { - List Objects { get; } + List Objects { get; } + + void Draw(DrawContext context); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/Files/default.frag b/SMCode/SM.Base/Shader/Files/default.frag index e6c308c..fc8964f 100644 --- a/SMCode/SM.Base/Shader/Files/default.frag +++ b/SMCode/SM.Base/Shader/Files/default.frag @@ -1,6 +1,7 @@ #version 330 in vec2 vTexture; +in vec4 vColor; uniform vec4 Tint; uniform bool UseTexture; @@ -9,6 +10,6 @@ uniform sampler2D Texture; layout(location = 0) out vec4 color; void main() { - color = Tint; + color = vColor * Tint; if (UseTexture) color *= texture(Texture, vTexture); } \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/Files/default.vert b/SMCode/SM.Base/Shader/Files/default.vert index cdfd85c..8923148 100644 --- a/SMCode/SM.Base/Shader/Files/default.vert +++ b/SMCode/SM.Base/Shader/Files/default.vert @@ -2,16 +2,22 @@ #define maxInstances 32 layout(location = 0) in vec3 aPos; layout(location = 1) in vec2 aTex; +layout(location = 3) in vec4 aColor; uniform mat4 MVP; +uniform bool HasVColor; uniform mat4 ModelMatrix[maxInstances]; uniform vec2 TextureOffset[maxInstances]; uniform vec2 TextureScale[maxInstances]; out vec2 vTexture; +out vec4 vColor; void main() { vTexture = aTex * TextureScale[gl_InstanceID] + TextureOffset[gl_InstanceID]; + if (HasVColor) vColor = aColor; + else vColor = vec4(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 0fa00aa..a93fe55 100644 --- a/SMCode/SM.Base/Shader/InstanceShader.cs +++ b/SMCode/SM.Base/Shader/InstanceShader.cs @@ -4,7 +4,6 @@ using OpenTK; using OpenTK.Graphics.OpenGL4; using SM.Base.Contexts; using SM.Base.Scene; -using SM.Base.StaticObjects; using SM.OGL.Shaders; namespace SM.Base.Shader @@ -24,6 +23,7 @@ namespace SM.Base.Shader 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); @@ -31,6 +31,8 @@ namespace SM.Base.Shader DrawObject(context.Mesh, context.Instances.Length, true); + CleanUp(); + GL.UseProgram(0); } } diff --git a/SMCode/SM.Base/Text/TextDrawingBasis.cs b/SMCode/SM.Base/Text/TextDrawingBasis.cs index 826a93b..2301d0a 100644 --- a/SMCode/SM.Base/Text/TextDrawingBasis.cs +++ b/SMCode/SM.Base/Text/TextDrawingBasis.cs @@ -39,6 +39,8 @@ namespace SM.Base.Text set => _material.Tint = value; } + public float Spacing = 1; + protected TextDrawingBasis(Font font) { _material.Texture = font; @@ -57,8 +59,15 @@ namespace SM.Base.Text _modelMatrixs = new Instance[_text.Length]; float x = 0; + CharParameter _last = new CharParameter(); for (var i = 0; i < _text.Length; i++) { + if (_text[i] == 32) + { + x += _last.Width * Spacing; + continue; + } + CharParameter parameter; try { @@ -66,17 +75,20 @@ namespace SM.Base.Text } catch { - throw new Exception("Font doesn't contain '"+_text[i]+"'"); + throw new Exception("Font doesn't contain '" + _text[i] + "'"); } - Matrix4 matrix = Matrix4.CreateScale(parameter.Width, Font.Height, 1) * Matrix4.CreateTranslation(x, 0, 0); + 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; + + x += parameter.Width * Spacing; + _last = parameter; } } } diff --git a/SMCode/SM.Base/Window/Contexts/DrawContext.cs b/SMCode/SM.Base/Window/Contexts/DrawContext.cs index e51b40a..a8adb02 100644 --- a/SMCode/SM.Base/Window/Contexts/DrawContext.cs +++ b/SMCode/SM.Base/Window/Contexts/DrawContext.cs @@ -14,7 +14,7 @@ namespace SM.Base.Contexts public Matrix4 View; public Instance[] Instances; - public Mesh Mesh; + public GenericMesh Mesh; public Material Material; public Vector2 WorldScale; diff --git a/SMCode/SM.Base/Window/GenericWindow.cs b/SMCode/SM.Base/Window/GenericWindow.cs index e849b6d..b859aaf 100644 --- a/SMCode/SM.Base/Window/GenericWindow.cs +++ b/SMCode/SM.Base/Window/GenericWindow.cs @@ -4,14 +4,16 @@ using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using SM.Base.Contexts; +using SM.Base.Objects.Static; using SM.Base.Scene; -using SM.Base.StaticObjects; +using SM.OGL; using SM.OGL.Shaders; namespace SM.Base { - public class GenericWindow : GameWindow - where TScene : GenericScene, new() + public class GenericWindow : GameWindow + where TScene : GenericScene, new() + where TItem : IShowItem where TCamera : GenericCamera, new() { private TCamera _viewportCamera; @@ -30,6 +32,17 @@ namespace SM.Base protected override void OnLoad(EventArgs e) { + GLSystem.INIT_SYSTEM(); + + Console.Write("----------------------\n" + + "--- OpenGL Loading ---\n" + + "----------------------------------\n" + + $"--- {"DeviceVersion",14}: {GLSystem.DeviceVersion,-10} ---\n" + + $"--- {"ForcedVersion",14}: {GLSystem.ForcedVersion,-10} ---\n" + + $"--- {"ShadingVersion",14}: {GLSystem.ShadingVersion,-10} ---\n" + + $"--- {"Debugging",14}: {GLSystem.Debugging,-10} ---\n" + + $"----------------------------------\n"); + base.OnLoad(e); } diff --git a/SMCode/SM.OGL/GLDebugging.cs b/SMCode/SM.OGL/GLDebugging.cs index 7250f02..4cc20a0 100644 --- a/SMCode/SM.OGL/GLDebugging.cs +++ b/SMCode/SM.OGL/GLDebugging.cs @@ -32,10 +32,10 @@ namespace SM.OGL GL.Enable(EnableCap.DebugOutput); GL.Enable(EnableCap.DebugOutputSynchronous); } - catch + catch (AccessViolationException) { Console.WriteLine("Enableing proper GLDebugging failed. \n" + - "Often it fails, because your hardware doesn't provied proper OpenGL 4 \n" + + "Often it fails, because your hardware doesn't provide proper OpenGL 4 \n" + " or KHR_debug extension support."); } diff --git a/SMCode/SM.OGL/GLObject.cs b/SMCode/SM.OGL/GLObject.cs index c0d369c..fd16793 100644 --- a/SMCode/SM.OGL/GLObject.cs +++ b/SMCode/SM.OGL/GLObject.cs @@ -13,7 +13,7 @@ namespace SM.OGL { get { - if (AutoCompile && _id < 0) Compile(); + if (AutoCompile && !WasCompiled) Compile(); return _id; } } @@ -27,7 +27,7 @@ namespace SM.OGL public void Name(string name) { - GL.ObjectLabel(TypeIdentifier, _id, name.Length, name); + if (GLSystem.Debugging) GL.ObjectLabel(TypeIdentifier, _id, name.Length, name); } public static implicit operator int(GLObject glo) => glo.ID; diff --git a/SMCode/SM.OGL/GLSystem.cs b/SMCode/SM.OGL/GLSystem.cs new file mode 100644 index 0000000..5a41b89 --- /dev/null +++ b/SMCode/SM.OGL/GLSystem.cs @@ -0,0 +1,33 @@ +using System.Linq; +using OpenTK.Graphics.OpenGL4; + +namespace SM.OGL +{ + public class GLSystem + { + private static bool _init = false; + + public static Version DeviceVersion { get; private set; } + public static Version ForcedVersion { get; set; } = new Version(); + + public static Version ShadingVersion { get; private set; } + public static string[] Extensions { get; private set; } + + public static bool Debugging { get; private set; } + + public static void INIT_SYSTEM() + { + if (_init) return; + + DeviceVersion = Version.CreateGLVersion(GL.GetString(StringName.Version)); + + ShadingVersion = Version.CreateGLVersion(GL.GetString(StringName.ShadingLanguageVersion)); + Extensions = GL.GetString(StringName.Extensions).Split(' '); + + Debugging = Extensions.Contains("KHR_debug"); + if (Debugging) GLDebugging.EnableDebugging(); + + _init = true; + } + } +} \ No newline at end of file diff --git a/SMCode/SM.OGL/Mesh/BoundingBox.cs b/SMCode/SM.OGL/Mesh/BoundingBox.cs new file mode 100644 index 0000000..f106c3a --- /dev/null +++ b/SMCode/SM.OGL/Mesh/BoundingBox.cs @@ -0,0 +1,40 @@ +using System; +using System.Runtime.CompilerServices; +using OpenTK; + +namespace SM.OGL.Mesh +{ + public class BoundingBox + { + public Vector3 Max = Vector3.Zero; + public Vector3 Min = Vector3.Zero; + + public Vector3 this[bool x, bool y, bool z] => new Vector3(x ? Max.X : Min.X, y ? Max.Y : Min.Y, z ? Max.Z : Min.Z); + + public BoundingBox() {} + + public BoundingBox(Vector3 min, Vector3 max) + { + Min = min; + Max = max; + } + + public void Update(Vector2 vector) + { + for (int i = 0; i < 2; i++) + { + Min[i] = Math.Min(Min[i], vector[i]); + Max[i] = Math.Max(Min[i], vector[i]); + } + } + + public void Update(Vector3 vector) + { + for (int i = 0; i < 3; i++) + { + Min[i] = Math.Min(Min[i], vector[i]); + Max[i] = Math.Max(Min[i], vector[i]); + } + } + } +} \ No newline at end of file diff --git a/SMCode/SM.OGL/Mesh/Mesh.cs b/SMCode/SM.OGL/Mesh/GenericMesh.cs similarity index 82% rename from SMCode/SM.OGL/Mesh/Mesh.cs rename to SMCode/SM.OGL/Mesh/GenericMesh.cs index edc9de5..4bee19a 100644 --- a/SMCode/SM.OGL/Mesh/Mesh.cs +++ b/SMCode/SM.OGL/Mesh/GenericMesh.cs @@ -1,13 +1,12 @@ using System; using System.Collections.Generic; using OpenTK.Graphics.OpenGL4; +using Buffer = OpenTK.Graphics.OpenGL4.Buffer; namespace SM.OGL.Mesh { - public class Mesh : GLObject + public abstract class GenericMesh : GLObject { - public static int BufferSizeMultiplier = 3; - protected override bool AutoCompile { get; } = true; public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.VertexArray; @@ -17,9 +16,13 @@ namespace SM.OGL.Mesh public virtual VBO UVs { get; } public virtual VBO Normals { get; } + public virtual BoundingBox BoundingBox { get; } = new BoundingBox(); + public virtual Dictionary AttribDataIndex { get; } - public Mesh() + public virtual int[] Indices { get; set; } + + protected GenericMesh() { AttribDataIndex = new Dictionary() { diff --git a/SMCode/SM.OGL/Mesh/VBO.cs b/SMCode/SM.OGL/Mesh/VBO.cs index b832bed..d84b03e 100644 --- a/SMCode/SM.OGL/Mesh/VBO.cs +++ b/SMCode/SM.OGL/Mesh/VBO.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Linq; using OpenTK; +using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; namespace SM.OGL.Mesh @@ -29,6 +31,12 @@ namespace SM.OGL.Mesh public void Add(float x, float y) => AddRange(new[] {x,y}); public void Add(float x, float y, float z) => AddRange(new[] {x,y,z}); public void Add(float x, float y, float z, float w) => AddRange(new[] {x,y,z,w}); + public void Add(Vector2 vector) => Add(vector.X, vector.Y); + public void Add(Vector2 vector, float z) => Add(vector.X, vector.Y, z); + public void Add(Vector2 vector, float z, float w) => Add(vector.X, vector.Y, z, w); + public void Add(Vector3 vector) => Add(vector.X, vector.Y, vector.Z); + public void Add(Vector4 vector) => Add(vector.X, vector.Y, vector.Z, vector.W); + public void Add(Color4 color) => Add(color.R, color.G, color.B, color.A); public void BindBuffer(int attribID) { @@ -36,7 +44,7 @@ namespace SM.OGL.Mesh int buffer = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, buffer); - GL.BufferData(BufferTarget.ArrayBuffer, data.Length * Mesh.BufferSizeMultiplier, data, BufferUsageHint); + GL.BufferData(BufferTarget.ArrayBuffer, data.Length * sizeof(float), data, BufferUsageHint); GL.VertexAttribPointer(attribID, PointerSize, PointerType, Normalised, PointerStride, PointerOffset); GL.EnableVertexAttribArray(attribID); diff --git a/SMCode/SM.OGL/SM.OGL.csproj b/SMCode/SM.OGL/SM.OGL.csproj index 36858f0..b5726f2 100644 --- a/SMCode/SM.OGL/SM.OGL.csproj +++ b/SMCode/SM.OGL/SM.OGL.csproj @@ -46,7 +46,9 @@ - + + + @@ -56,6 +58,7 @@ + diff --git a/SMCode/SM.OGL/Shaders/GenericShader.cs b/SMCode/SM.OGL/Shaders/GenericShader.cs index 0d0c406..ed9ed4f 100644 --- a/SMCode/SM.OGL/Shaders/GenericShader.cs +++ b/SMCode/SM.OGL/Shaders/GenericShader.cs @@ -48,10 +48,22 @@ namespace SM.OGL.Shaders Load(); } - public void DrawObject(Mesh.Mesh mesh, int amount, bool bindVAO = false) + public void DrawObject(Mesh.GenericMesh mesh, int amount, bool bindVAO = false) { if (bindVAO) GL.BindVertexArray(mesh); - GL.DrawArraysInstanced(mesh.PrimitiveType, 0, mesh.Vertex.Count, amount); + + if (mesh.Indices != null) + GL.DrawElementsInstanced(mesh.PrimitiveType, 0, DrawElementsType.UnsignedInt, mesh.Indices, amount); + else + GL.DrawArraysInstanced(mesh.PrimitiveType, 0, mesh.Vertex.Count, amount); + } + + protected void CleanUp() + { + Uniforms.NextTexture = 0; + + GL.BindTexture(TextureTarget.Texture2D, 0); + GL.BindVertexArray(0); } } } \ No newline at end of file diff --git a/SMCode/SM.OGL/Version.cs b/SMCode/SM.OGL/Version.cs new file mode 100644 index 0000000..bd14606 --- /dev/null +++ b/SMCode/SM.OGL/Version.cs @@ -0,0 +1,31 @@ +namespace SM.OGL +{ + public struct Version + { + public int MajorVersion; + public int MinorVersion; + + public Version(int majorVersion, int minorVersion) + { + MinorVersion = minorVersion; + MajorVersion = majorVersion; + } + + public Version(string version) + { + string[] splits = version.Trim().Split(new []{'.'}, 2); + MajorVersion = int.Parse(splits[0]); + MinorVersion = int.Parse(splits[1]); + } + + public override string ToString() + { + return $"{MajorVersion}.{MinorVersion}"; + } + + public static Version CreateGLVersion(string version) + { + return new Version(version.Substring(0, 3)); + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawBackground.cs b/SMCode/SM2D/Drawing/DrawBackground.cs index a5a9fef..7bf716e 100644 --- a/SMCode/SM2D/Drawing/DrawBackground.cs +++ b/SMCode/SM2D/Drawing/DrawBackground.cs @@ -2,8 +2,8 @@ using OpenTK; using OpenTK.Graphics; using SM.Base.Contexts; +using SM.Base.Objects.Static; using SM.Base.Scene; -using SM.Base.StaticObjects; using SM.Base.Textures; using SM.OGL.Texture; using SM2D.Types; diff --git a/SMCode/SM2D/Drawing/DrawColor.cs b/SMCode/SM2D/Drawing/DrawColor.cs new file mode 100644 index 0000000..7a77165 --- /dev/null +++ b/SMCode/SM2D/Drawing/DrawColor.cs @@ -0,0 +1,35 @@ +using OpenTK.Graphics; +using SM.Base.Contexts; +using SM.Base.Scene; +using SM2D.Scene; +using SM2D.Types; + +namespace SM2D.Drawing +{ + public class DrawColor : DrawingBasis, I2DShowItem + { + public Color4 Tint + { + get => _material.Tint; + set => _material.Tint = value; + } + + public int ZIndex { get; set; } + + public DrawColor() {} + + public DrawColor(Color4 color) + { + _material.Tint = color; + } + + public override void Draw(DrawContext context) + { + base.Draw(context); + ApplyContext(ref context); + context.Instances[0].ModelMatrix = Transform.GetMatrix(); + + _material.Shader.Draw(context); + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawComplex.cs b/SMCode/SM2D/Drawing/DrawComplex.cs new file mode 100644 index 0000000..a6f9370 --- /dev/null +++ b/SMCode/SM2D/Drawing/DrawComplex.cs @@ -0,0 +1,35 @@ +using SM.Base.Contexts; +using SM.Base.Scene; +using SM.OGL.Mesh; +using SM2D.Scene; +using SM2D.Types; + +namespace SM2D.Drawing +{ + public class DrawComplex: DrawingBasis, I2DShowItem + { + public int ZIndex { get; set; } + + public Material Material + { + get => _material; + set => _material = value; + } + + public GenericMesh Mesh + { + get => _mesh; + set => _mesh = value; + } + + public override void Draw(DrawContext context) + { + base.Draw(context); + ApplyContext(ref context); + + context.Instances[0].ModelMatrix = Transform.GetMatrix(); + + _material.Shader.Draw(context); + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawPolygon.cs b/SMCode/SM2D/Drawing/DrawPolygon.cs new file mode 100644 index 0000000..60f18cf --- /dev/null +++ b/SMCode/SM2D/Drawing/DrawPolygon.cs @@ -0,0 +1,38 @@ +using System.Drawing; +using OpenTK.Graphics; +using SM.Base.Textures; +using SM.OGL.Texture; +using SM2D.Object; + +namespace SM2D.Drawing +{ + public class DrawPolygon : DrawColor + { + public Polygon Polygon + { + get => (Polygon)_mesh; + set => _mesh = value; + } + + public Texture Texture + { + get => (Texture)_material.Texture; + set => _material.Texture = value; + } + + public DrawPolygon(Polygon polygon) {} + + public DrawPolygon(Polygon polygon, Bitmap map) : this(polygon, map, Color4.White) {} + + public DrawPolygon(Polygon polygon, Color4 color) : base(color) + { + _mesh = polygon; + } + public DrawPolygon(Polygon polygon, Bitmap map, Color4 tint) : base(tint) + { + _mesh = polygon; + + _material.Texture = new Texture(map); + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawText.cs b/SMCode/SM2D/Drawing/DrawText.cs index 3f19ab6..9558f78 100644 --- a/SMCode/SM2D/Drawing/DrawText.cs +++ b/SMCode/SM2D/Drawing/DrawText.cs @@ -1,14 +1,17 @@ using SM.Base.Contexts; using SM.Base.Text; +using SM.Base.Types; +using SM2D.Scene; using SM2D.Types; namespace SM2D.Drawing { - public class DrawText : TextDrawingBasis + public class DrawText : TextDrawingBasis, I2DShowItem { public DrawText(Font font, string text) : base(font) { _text = text; + Transform.Size = new Vector2(1); } public override void Draw(DrawContext context) @@ -21,5 +24,7 @@ namespace SM2D.Drawing _material.Shader.Draw(context); } + + public int ZIndex { get; set; } } } \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawTexture.cs b/SMCode/SM2D/Drawing/DrawTexture.cs index c8b431e..53412ec 100644 --- a/SMCode/SM2D/Drawing/DrawTexture.cs +++ b/SMCode/SM2D/Drawing/DrawTexture.cs @@ -5,48 +5,40 @@ using SM.Base.Contexts; using SM.Base.Scene; using SM.Base.Textures; using SM.Base.Types; +using SM2D.Scene; using SM2D.Types; using Vector2 = SM.Base.Types.Vector2; namespace SM2D.Drawing { - public class DrawTexture : DrawingBasis + public class DrawTexture : DrawColor { public static float MasterScale = .25f; + public float Scale = 1; public Texture Texture { get => (Texture) _material.Texture; set => _material.Texture = value; } - public Color4 Tint - { - get => _material.Tint; - set => _material.Tint = value; - } - - public float Scale = 1; + public DrawTexture() {} + protected DrawTexture(Color4 color) : base(color) { } public DrawTexture(Bitmap map) : this(map, Color4.White) { } - public DrawTexture(Bitmap map, Color4 tint) + public DrawTexture(Bitmap map, Color4 color) { _material.Texture = new Texture(map); - _material.Tint = tint; + _material.Tint = color; } public override void Draw(DrawContext context) { + Transform.Size = new Vector2(Texture.Map.Width * MasterScale * Scale, Texture.Map.Height * MasterScale * Scale); base.Draw(context); - ApplyContext(ref context); - - Transform.Size = new Vector2(Texture.Map.Width * MasterScale * Scale, Texture.Map.Height * MasterScale * Scale); - context.Instances[0].ModelMatrix = Transform.GetMatrix(); - - _material.Shader.Draw(context); } } } \ No newline at end of file diff --git a/SMCode/SM2D/GLWindow2D.cs b/SMCode/SM2D/GLWindow2D.cs index 7f65370..cb847df 100644 --- a/SMCode/SM2D/GLWindow2D.cs +++ b/SMCode/SM2D/GLWindow2D.cs @@ -6,7 +6,7 @@ using SM2D.Scene; namespace SM2D { - public class GLWindow2D : GenericWindow + public class GLWindow2D : GenericWindow { diff --git a/SMCode/SM2D/Object/Polygon.cs b/SMCode/SM2D/Object/Polygon.cs new file mode 100644 index 0000000..999aa18 --- /dev/null +++ b/SMCode/SM2D/Object/Polygon.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using OpenTK; +using OpenTK.Graphics; +using OpenTK.Graphics.OpenGL4; +using SM.Base.Objects; +using SM.OGL.Mesh; + +namespace SM2D.Object +{ + public class Polygon : Mesh + { + public override VBO Vertex { get; } = new VBO(); + public override VBO UVs { get; } = new VBO(pointerSize:2); + public override VBO Color { get; } = new VBO(pointerSize: 4); + + public override PrimitiveType PrimitiveType { get; } = PrimitiveType.TriangleFan; + + + + public Polygon(ICollection vertices) + { + foreach (Vector2 vertex in vertices) + { + Color.Add(Color4.White); + AddVertex(vertex); + } + + foreach (Vector2 vertex in vertices) + { + AddUV(vertex); + } + } + + public Polygon(ICollection vertices) + { + foreach (PolygonVertex polygonVertex in vertices) + { + Color.Add(polygonVertex.Color); + AddVertex(polygonVertex.Vertex); + } + + foreach (PolygonVertex vertex in vertices) + { + AddUV(vertex.Vertex); + } + } + + private void AddVertex(Vector2 vertex) + { + BoundingBox.Update(vertex); + Vertex.Add(vertex, 0); + } + + private void AddUV(Vector2 vertex) + { + Vector2 uv = Vector2.Divide(vertex, BoundingBox.Max.Xy) + BoundingBox.Min.Xy; + UVs.Add(uv); + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Object/PolygonVertex.cs b/SMCode/SM2D/Object/PolygonVertex.cs new file mode 100644 index 0000000..77eb8e8 --- /dev/null +++ b/SMCode/SM2D/Object/PolygonVertex.cs @@ -0,0 +1,17 @@ +using OpenTK; +using OpenTK.Graphics; + +namespace SM2D.Object +{ + public struct PolygonVertex + { + public Vector2 Vertex; + public Color4 Color; + + public PolygonVertex(Vector2 vertex = default, Color4 color = default) + { + Vertex = vertex; + Color = color; + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/SM2D.csproj b/SMCode/SM2D/SM2D.csproj index e79a991..77aa34c 100644 --- a/SMCode/SM2D/SM2D.csproj +++ b/SMCode/SM2D/SM2D.csproj @@ -46,11 +46,18 @@ + + + + + + + diff --git a/SMCode/SM2D/Scene/Camera.cs b/SMCode/SM2D/Scene/Camera.cs index 84359ff..ffd8de3 100644 --- a/SMCode/SM2D/Scene/Camera.cs +++ b/SMCode/SM2D/Scene/Camera.cs @@ -12,12 +12,12 @@ namespace SM2D.Scene protected override Matrix4 ViewCalculation() { - return Matrix4.LookAt(Position.X, Position.Y, -1, Position.X, Position.Y, 0, 0, 1, 0); + return Matrix4.LookAt(Position.X, Position.Y, 2, Position.X, Position.Y, 0, 0, 1, 0); } public override void RecalculateWorld(OpenTK.Vector2 world, float aspect) { - OrthographicWorld = Matrix4.CreateOrthographicOffCenter(world.X / 2, -world.X / 2, world.Y / 2, -world.Y / 2, 0.1f, 100); + OrthographicWorld = Matrix4.CreateOrthographicOffCenter(-world.X / 2, world.X / 2, world.Y / 2, -world.Y / 2, 0.1f, 4f); } } } \ No newline at end of file diff --git a/SMCode/SM2D/Scene/I2DShowItem.cs b/SMCode/SM2D/Scene/I2DShowItem.cs new file mode 100644 index 0000000..60b0f21 --- /dev/null +++ b/SMCode/SM2D/Scene/I2DShowItem.cs @@ -0,0 +1,10 @@ +using SM.Base.Scene; + +namespace SM2D.Scene +{ + public interface I2DShowItem : IShowItem + { + int ZIndex { get; set; } + + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Scene/ItemCollection.cs b/SMCode/SM2D/Scene/ItemCollection.cs new file mode 100644 index 0000000..bacad5e --- /dev/null +++ b/SMCode/SM2D/Scene/ItemCollection.cs @@ -0,0 +1,24 @@ +using SM.Base.Contexts; +using SM.Base.Scene; +using SM.Base.Types; +using SM2D.Types; + +namespace SM2D.Scene +{ + public class ItemCollection : GenericItemCollection, I2DShowItem + { + public ItemCollection() + { + Transform.Size = new Vector2(1); + } + + public override void Draw(DrawContext context) + { + Objects.Sort((x, y) => x.ZIndex - y.ZIndex); + + base.Draw(context); + } + + public int ZIndex { get; set; } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Scene/Scene.cs b/SMCode/SM2D/Scene/Scene.cs index 67c5b87..17f784f 100644 --- a/SMCode/SM2D/Scene/Scene.cs +++ b/SMCode/SM2D/Scene/Scene.cs @@ -1,10 +1,11 @@ using OpenTK.Graphics; +using SM.Base.Contexts; using SM.Base.Scene; using SM2D.Drawing; namespace SM2D.Scene { - public class Scene : GenericScene + public class Scene : GenericScene { public DrawBackground Background => (DrawBackground)_background; @@ -12,5 +13,11 @@ namespace SM2D.Scene { _background = new DrawBackground(Color4.Black); } + + public override void Draw(DrawContext context) + { + Objects.Sort((x,y) => x.ZIndex - y.ZIndex); + base.Draw(context); + } } } \ No newline at end of file diff --git a/SMCode/SM2D/Types/Transformation.cs b/SMCode/SM2D/Types/Transformation.cs index 1c2d90c..1f8b74d 100644 --- a/SMCode/SM2D/Types/Transformation.cs +++ b/SMCode/SM2D/Types/Transformation.cs @@ -9,12 +9,13 @@ namespace SM2D.Types public Vector2 Position = new Vector2(0); public Vector2 Size = new Vector2(50); public float Rotation; + public int ZIndex = 0; public override Matrix4 GetMatrix() { return Matrix4.CreateScale(Size.X, Size.Y, 1) * Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) * - Matrix4.CreateTranslation(Position.X, Position.Y, 1); + Matrix4.CreateTranslation(Position.X, Position.Y, ZIndex); } } } \ No newline at end of file diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index 59463ed..2434c45 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -1,8 +1,11 @@ using System; +using System.Collections.Generic; using System.Drawing; +using OpenTK; using OpenTK.Graphics; using SM2D; using SM2D.Drawing; +using SM2D.Object; using SM2D.Scene; using Font = SM.Base.Text.Font; using Vector2 = OpenTK.Vector2; @@ -13,32 +16,100 @@ namespace SM_TEST { static Scene scene; private static Font font; + private static ItemCollection col; static void Main(string[] args) { font = new Font(@"C:\Windows\Fonts\Arial.ttf") { - FontSize = 64 + FontSize = 32 }; - GLWindow2D window = new GLWindow2D {Scaling = new Vector2(0, 1000)}; + GLWindow2D window = new GLWindow2D {Scaling = new Vector2(0, 500)}; window.SetScene(scene = new Scene()); window.Load += WindowOnLoad; + window.UpdateFrame += WindowOnUpdateFrame; window.Run(); } + private static void WindowOnUpdateFrame(object sender, FrameEventArgs e) + { + float speed = 40; + + //col.Transform.Position.Y += (float)e.Time * speed; + } + 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")) + col = new ItemCollection() { - Transform = { Position = new Vector2(100), Rotation = 45}, + Transform = { Position = new SM.Base.Types.Vector2(0, -400) }, + ZIndex = 1 + }; + + col.Objects.Add(new DrawTexture(new Bitmap("soldier_logo.png")) + { + ZIndex = 1 }); + col.Objects.Add(new DrawColor(Color4.Aqua) + { + Transform = { Rotation = 45, Position = new SM.Base.Types.Vector2(0, 25) }, + }); + + scene.Objects.Add(col); scene.Objects.Add(new DrawText(font, "Testing...") { - Transform = {Size = new Vector2(1), Position = new SM.Base.Types.Vector2(0, -400)}, + Transform = { Position = new SM.Base.Types.Vector2(0, -400)}, Color = Color4.Black }); + + scene.Objects.Add(new DrawPolygon(new Polygon(new[] + { + new Vector2(.25f, 0), + new Vector2(.75f, 0), + new Vector2(1, .25f), + new Vector2(1, .75f), + new Vector2(.75f, 1), + new Vector2(.25f, 1), + new Vector2(0, .75f), + new Vector2(0, .25f) + }),Color4.Blue) + ); + scene.Objects.Add(new DrawPolygon(new Polygon(new[] + { + new PolygonVertex(new Vector2(.25f, 0), Color4.White), + new PolygonVertex(new Vector2(.75f, 0), Color4.White), + new PolygonVertex(new Vector2(1, .25f), Color4.White), + 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) + }), Color4.LawnGreen) + { + Transform = {Position = new SM.Base.Types.Vector2(50,0)} + }); + scene.Objects.Add(new DrawPolygon(new Polygon(new[] + { + new PolygonVertex(new Vector2(.25f, 0), Color4.White), + new PolygonVertex(new Vector2(.75f, 0), Color4.White), + new PolygonVertex(new Vector2(1, .25f), Color4.White), + 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 Bitmap("soldier_logo.png")) + { + Transform = {Position = new SM.Base.Types.Vector2(-50,0)} + }); + scene.Background.Color = Color4.Beige; + + /*scene.HUD.Add(new DrawText(font, "GIVE ME A HUD HUG!") + { + Color = Color4.Black, + Spacing = .75f + });*/ } } } \ No newline at end of file