diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs index 8ab8869..3659374 100644 --- a/SMCode/SM.Base/Drawing/DrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -4,13 +4,10 @@ using SM.OGL.Mesh; namespace SM.Base.Scene { - public class DrawingBasis : IShowItem - where TTransformation : GenericTransformation, new() + public class DrawingBasis : IShowItem { protected Material _material = new Material(); protected Mesh _mesh = Plate.Object; - - public TTransformation Transform = new TTransformation(); public virtual void Update(UpdateContext context) { @@ -25,4 +22,10 @@ namespace SM.Base.Scene context.Mesh = _mesh; } } + public class DrawingBasis : DrawingBasis + where TTransformation : GenericTransformation, new() + { + + public TTransformation Transform = new TTransformation(); + } } \ No newline at end of file diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index e40f4f9..0fec514 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -54,6 +54,10 @@ + + + + @@ -74,8 +78,6 @@ SM.OGL - - - + \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/GenericCamera.cs b/SMCode/SM.Base/Scene/GenericCamera.cs index ec3649b..70dc17d 100644 --- a/SMCode/SM.Base/Scene/GenericCamera.cs +++ b/SMCode/SM.Base/Scene/GenericCamera.cs @@ -18,9 +18,9 @@ namespace SM.Base.Scene return ViewMatrix; } - public abstract Matrix4 ViewCalculation(); + protected abstract Matrix4 ViewCalculation(); public abstract bool Orthographic { get; } - public abstract void RecalculateWorld(float width, float height); + public abstract void RecalculateWorld(Vector2 world, float aspect); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/GenericScene.cs b/SMCode/SM.Base/Scene/GenericScene.cs index d1da11e..fb2555c 100644 --- a/SMCode/SM.Base/Scene/GenericScene.cs +++ b/SMCode/SM.Base/Scene/GenericScene.cs @@ -1,22 +1,34 @@ using System.Collections.Generic; +using OpenTK; using SM.Base.Contexts; namespace SM.Base.Scene { public abstract class GenericScene : IShowCollection - where TCamera : GenericCamera + where TCamera : GenericCamera, new() { + public IShowItem Background; + 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 Dictionary Cameras = new Dictionary(); public void Draw(DrawContext context) { if (!context.ForceViewport && Camera != null) context.View = Camera.ViewMatrix; + DrawContext backgroundDrawContext = context; + backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix(); + Background?.Draw(backgroundDrawContext); + for(int i = 0; i < Objects.Count; i++) Objects[i].Draw(context); + + context.View = Matrix4.Identity; + for (int i = 0; i < HUD.Count; i++) + HUD[i].Draw(context); } } diff --git a/SMCode/SM.Base/Shader/Files/default.frag b/SMCode/SM.Base/Shader/Files/default.frag index 8fd1eff..e6c308c 100644 --- a/SMCode/SM.Base/Shader/Files/default.frag +++ b/SMCode/SM.Base/Shader/Files/default.frag @@ -3,10 +3,12 @@ in vec2 vTexture; uniform vec4 Tint; +uniform bool UseTexture; uniform sampler2D Texture; layout(location = 0) out vec4 color; void main() { - color = Tint * texture(Texture, vTexture); + color = Tint; + if (UseTexture) color *= texture(Texture, vTexture); } \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/Shaders.cs b/SMCode/SM.Base/Shader/Shaders.cs index 438e9bb..38be41e 100644 --- a/SMCode/SM.Base/Shader/Shaders.cs +++ b/SMCode/SM.Base/Shader/Shaders.cs @@ -13,7 +13,7 @@ namespace SM.Base.Shader 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["Texture"].SetTexture(context.Material.Texture, 0, u["UseTexture"]); })); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Textures/Texture.cs b/SMCode/SM.Base/Textures/Texture.cs index 6bda402..13524bd 100644 --- a/SMCode/SM.Base/Textures/Texture.cs +++ b/SMCode/SM.Base/Textures/Texture.cs @@ -47,5 +47,9 @@ namespace SM.Base.Textures GL.BindTexture(TextureTarget.Texture2D, 0); Map.UnlockBits(data); } + + public static implicit operator Texture(Bitmap map) => new Texture(map); + public override TextureMinFilter Filter { get; set; } + public override TextureWrapMode WrapMode { get; set; } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Types/Vector.cs b/SMCode/SM.Base/Types/Vector.cs new file mode 100644 index 0000000..6edfcb7 --- /dev/null +++ b/SMCode/SM.Base/Types/Vector.cs @@ -0,0 +1,52 @@ +using System; +using OpenTK; + +namespace SM.Base.Types +{ + public abstract class Vector + { + private float _x = default; + private float _y = default; + private float _z = default; + private float _w = default; + + protected float _X + { + get => _x; + set => _x = value; + } + protected float _Y + { + get => _y; + set => _y = value; + } + protected float _Z + { + get => _z; + set => _z = value; + } + protected float _W + { + get => _w; + set => _w = value; + } + + protected Vector(float uniform) : this(uniform, uniform, uniform, uniform) + { } + + protected Vector(float x, float y, float z, float w) + { + _x = x; + _y = y; + _z = z; + _w = w; + } + + public static implicit operator OpenTK.Vector2(Vector v) => new OpenTK.Vector2(v._x, v._y); + public static implicit operator OpenTK.Vector3(Vector v) => new OpenTK.Vector3(v._x, v._y, v._z); + public static implicit operator OpenTK.Vector4(Vector v) => new OpenTK.Vector4(v._x, v._y, v._z, v._w); + + + + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/Vector2.cs b/SMCode/SM.Base/Types/Vector2.cs new file mode 100644 index 0000000..c686177 --- /dev/null +++ b/SMCode/SM.Base/Types/Vector2.cs @@ -0,0 +1,29 @@ +namespace SM.Base.Types +{ + public class Vector2 : Vector + { + public float X + { + get => _X; + set => _X = value; + } + public float Y + { + get => _Y; + set => _Y = value; + } + + public Vector2() : this(0) + {} + + public Vector2(float uniform) : base(uniform) + { + + } + public Vector2(float x, float y) : base(x,y, default, default) {} + protected Vector2(float x, float y, float z, float w) : base(x, y, z, w) {} + + + public static implicit operator Vector2(OpenTK.Vector2 v) => new Vector2(v.X, v.Y); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/Vector3.cs b/SMCode/SM.Base/Types/Vector3.cs new file mode 100644 index 0000000..748b9d0 --- /dev/null +++ b/SMCode/SM.Base/Types/Vector3.cs @@ -0,0 +1,23 @@ +using System.Runtime.InteropServices; + +namespace SM.Base.Types +{ + public class Vector3 : Vector2 + { + public float Z + { + get => _Z; + set => _Z = value; + } + + public Vector3(float uniform) : base(uniform) + { } + + public Vector3(float x, float y, float z) : base(x, y, z, default) + { } + + protected Vector3(float x, float y, float z, float w) : base(x, y, z, w) { } + + public static implicit operator Vector3(OpenTK.Vector3 v) => new Vector3(v.X, v.Y, v.Z); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/Vector4.cs b/SMCode/SM.Base/Types/Vector4.cs new file mode 100644 index 0000000..5c8bf7f --- /dev/null +++ b/SMCode/SM.Base/Types/Vector4.cs @@ -0,0 +1,21 @@ +namespace SM.Base.Types +{ + public class Vector4 : Vector3 + { + public float W + { + get => _W; + set => _W = value; + } + + public Vector4(float uniform) : base(uniform) + { + } + + public Vector4(float x, float y, float z, float w) : base(x, y, z, w) + { + } + + public static implicit operator Vector4(OpenTK.Vector4 v) => new Vector4(v.X, v.Y, v.Z, v.W); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Window/Contexts/DrawContext.cs b/SMCode/SM.Base/Window/Contexts/DrawContext.cs index 3a59f0e..9a2ec4f 100644 --- a/SMCode/SM.Base/Window/Contexts/DrawContext.cs +++ b/SMCode/SM.Base/Window/Contexts/DrawContext.cs @@ -14,5 +14,7 @@ namespace SM.Base.Contexts public Mesh Mesh; public Material Material; + + public Vector2 WorldScale; } } \ No newline at end of file diff --git a/SMCode/SM.Base/Window/GenericWindow.cs b/SMCode/SM.Base/Window/GenericWindow.cs index 969f91c..2c8ec1d 100644 --- a/SMCode/SM.Base/Window/GenericWindow.cs +++ b/SMCode/SM.Base/Window/GenericWindow.cs @@ -19,6 +19,10 @@ namespace SM.Base public TScene CurrentScene { get; private set; } public bool ForceViewportCamera { get; set; } = false; + public Vector2? Scaling { get; set; } + public Vector2 WorldScale { get; private set; }= Vector2.Zero; + public float Aspect { get; private set; } = 0f; + public GenericWindow() : base(1280, 720, GraphicsMode.Default, "Testing", GameWindowFlags.Default, DisplayDevice.Default, 0, 0, GraphicsContextFlags.Default, null, true) { _viewportCamera = new TCamera(); @@ -37,7 +41,8 @@ namespace SM.Base View = _viewportCamera.CalculateViewMatrix(), ModelMatrix = Matrix4.Identity, Mesh = Plate.Object, - ForceViewport = ForceViewportCamera + ForceViewport = ForceViewportCamera, + WorldScale = WorldScale }; base.OnRenderFrame(e); @@ -53,8 +58,17 @@ namespace SM.Base { base.OnResize(e); + Aspect = (float)Width / Height; + WorldScale = new Vector2(Width, Height); + if (Scaling.HasValue) + { + if (Scaling.Value.X > 0 && Scaling.Value.Y > 0) WorldScale = Scaling.Value; + else if(Scaling.Value.X > 0) WorldScale = new Vector2(Scaling.Value.X, Scaling.Value.X / Aspect); + else if(Scaling.Value.Y > 0) WorldScale = new Vector2(Aspect * Scaling.Value.Y, Scaling.Value.Y); + } + GL.Viewport(ClientRectangle); - _viewportCamera.RecalculateWorld(Width, Height); + _viewportCamera.RecalculateWorld(WorldScale, Aspect); } public virtual void SetScene(TScene scene) @@ -62,4 +76,12 @@ namespace SM.Base CurrentScene = scene; } } + + public enum WindowScaling + { + None, + Width, + Height, + FixedSize + } } \ No newline at end of file diff --git a/SMCode/SM.OGL/Shaders/Uniform.cs b/SMCode/SM.OGL/Shaders/Uniform.cs index 97821d2..d9dd56b 100644 --- a/SMCode/SM.OGL/Shaders/Uniform.cs +++ b/SMCode/SM.OGL/Shaders/Uniform.cs @@ -162,6 +162,17 @@ namespace SM.OGL.Shaders #endregion + public void SetTexture(TextureBase texture, Uniform checkUniform) + { + checkUniform.SetUniform1(texture != null); + if (texture != null) SetTexture(texture); + } + + public void SetTexture(TextureBase texture, int pos, Uniform checkUniform) + { + checkUniform.SetUniform1(texture != null); + if (texture != null) SetTexture(texture); + } public void SetTexture(TextureBase texture) => SetTexture(texture, Parent.NextTexture++); public void SetTexture(TextureBase texture, int texturePos) diff --git a/SMCode/SM.OGL/Texture/TextureBase.cs b/SMCode/SM.OGL/Texture/TextureBase.cs index 9c0d7d8..b95b173 100644 --- a/SMCode/SM.OGL/Texture/TextureBase.cs +++ b/SMCode/SM.OGL/Texture/TextureBase.cs @@ -2,12 +2,12 @@ namespace SM.OGL.Texture { - public class TextureBase : GLObject + public abstract class TextureBase : GLObject { protected override bool AutoCompile { get; } = true; public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Texture; - public virtual TextureMinFilter Filter { get; set; } - public virtual TextureWrapMode WrapMode { get; set; } + public abstract TextureMinFilter Filter { get; set; } + public abstract TextureWrapMode WrapMode { get; set; } } } \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawBackground.cs b/SMCode/SM2D/Drawing/DrawBackground.cs new file mode 100644 index 0000000..ebdfbc4 --- /dev/null +++ b/SMCode/SM2D/Drawing/DrawBackground.cs @@ -0,0 +1,49 @@ +using System.Drawing; +using OpenTK; +using OpenTK.Graphics; +using SM.Base.Contexts; +using SM.Base.Scene; +using SM.Base.Textures; +using SM.OGL.Texture; +using SM2D.Types; + +namespace SM2D.Drawing +{ + public class DrawBackground : DrawingBasis + { + public Color4 Color + { + get => _material.Tint; + set => _material.Tint = value; + } + + public TextureBase Texture + { + get => _material.Texture; + set => _material.Texture = value; + } + public DrawBackground(Color4 color) + { + Color = color; + } + + public DrawBackground(Bitmap texture) + { + Texture = (Texture)texture; + } + + public DrawBackground(Bitmap texture, Color4 tint) + { + Color = tint; + Texture = (Texture) texture; + } + + public override void Draw(DrawContext context) + { + ApplyContext(ref context); + + context.ModelMatrix = Matrix4.CreateScale(context.WorldScale.X, context.WorldScale.Y, 1); + _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 31f4d23..bfa6cc1 100644 --- a/SMCode/SM2D/Drawing/DrawTexture.cs +++ b/SMCode/SM2D/Drawing/DrawTexture.cs @@ -1,14 +1,19 @@ using System.Drawing; +using OpenTK; using OpenTK.Graphics; using SM.Base.Contexts; using SM.Base.Scene; using SM.Base.Textures; +using SM.Base.Types; using SM2D.Types; +using Vector2 = SM.Base.Types.Vector2; namespace SM2D.Drawing { public class DrawTexture : DrawingBasis { + public static float MasterScale = .25f; + public Texture Texture { get => (Texture) _material.Texture; @@ -21,6 +26,9 @@ namespace SM2D.Drawing set => _material.Tint = value; } + public float Scale = 1; + + public DrawTexture(Bitmap map) : this(map, Color4.White) { } @@ -35,6 +43,7 @@ namespace SM2D.Drawing base.Draw(context); ApplyContext(ref context); + Transform.Size = new Vector2(Texture.Map.Width * MasterScale * Scale, Texture.Map.Height * MasterScale * Scale); context.ModelMatrix = Transform.GetMatrix(); _material.Shader.Draw(context); diff --git a/SMCode/SM2D/GLWindow2D.cs b/SMCode/SM2D/GLWindow2D.cs index da3b4f5..7f65370 100644 --- a/SMCode/SM2D/GLWindow2D.cs +++ b/SMCode/SM2D/GLWindow2D.cs @@ -1,10 +1,26 @@ -using SM.Base; +using System; +using OpenTK; +using OpenTK.Graphics.OpenGL4; +using SM.Base; using SM2D.Scene; namespace SM2D { public class GLWindow2D : GenericWindow { - + + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + GL.Enable(EnableCap.Blend); + GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); + } + + protected override void OnRenderFrame(FrameEventArgs e) + { + GL.Disable(EnableCap.DepthTest); + base.OnRenderFrame(e); + } } } \ No newline at end of file diff --git a/SMCode/SM2D/SM2D.csproj b/SMCode/SM2D/SM2D.csproj index 39c6cd7..57168e0 100644 --- a/SMCode/SM2D/SM2D.csproj +++ b/SMCode/SM2D/SM2D.csproj @@ -45,6 +45,7 @@ + diff --git a/SMCode/SM2D/Scene/Camera.cs b/SMCode/SM2D/Scene/Camera.cs index 81a9941..84359ff 100644 --- a/SMCode/SM2D/Scene/Camera.cs +++ b/SMCode/SM2D/Scene/Camera.cs @@ -1,5 +1,6 @@ using OpenTK; using SM.Base.Scene; +using Vector2 = SM.Base.Types.Vector2; namespace SM2D.Scene { @@ -7,16 +8,16 @@ namespace SM2D.Scene { public override bool Orthographic { get; } = true; - public Vector2 Position; + public Vector2 Position = new Vector2(0); - public override Matrix4 ViewCalculation() + protected override Matrix4 ViewCalculation() { return Matrix4.LookAt(Position.X, Position.Y, -1, Position.X, Position.Y, 0, 0, 1, 0); } - public override void RecalculateWorld(float width, float height) + public override void RecalculateWorld(OpenTK.Vector2 world, float aspect) { - OrthographicWorld = Matrix4.CreateOrthographicOffCenter(-width / 2, width / 2, height / 2, -height / 2, 0.1f, 100); + OrthographicWorld = Matrix4.CreateOrthographicOffCenter(world.X / 2, -world.X / 2, world.Y / 2, -world.Y / 2, 0.1f, 100); } } } \ No newline at end of file diff --git a/SMCode/SM2D/Types/Transformation.cs b/SMCode/SM2D/Types/Transformation.cs index dad78b0..1c2d90c 100644 --- a/SMCode/SM2D/Types/Transformation.cs +++ b/SMCode/SM2D/Types/Transformation.cs @@ -1,11 +1,12 @@ using OpenTK; using SM.Base.Scene; +using Vector2 = SM.Base.Types.Vector2; namespace SM2D.Types { public class Transformation : GenericTransformation { - public Vector2 Position; + public Vector2 Position = new Vector2(0); public Vector2 Size = new Vector2(50); public float Rotation; diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index b4214fa..c48d7af 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -4,6 +4,8 @@ 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; @@ -16,7 +18,7 @@ namespace SM_TEST static Scene scene; static void Main(string[] args) { - GLWindow2D window = new GLWindow2D(); + GLWindow2D window = new GLWindow2D {Scaling = new Vector2(0, 1000)}; window.SetScene(scene = new Scene()); window.Load += WindowOnLoad; window.Run(); @@ -24,7 +26,13 @@ namespace SM_TEST private static void WindowOnLoad(object sender, EventArgs e) { - scene.Objects.Add(new DrawTexture(new Bitmap("draconier_logo.png"))); + + 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); } } -} +} \ No newline at end of file