19.09.2020

+ Vector-classes
+ Added Background
~ Changed OpenTK.Vector2 to SM.Base.Types.Vector2
This commit is contained in:
Michel Fedde 2020-09-19 19:04:19 +02:00
parent a603ecc417
commit acccf5f0e7
22 changed files with 295 additions and 27 deletions

View file

@ -4,13 +4,10 @@ using SM.OGL.Mesh;
namespace SM.Base.Scene
{
public class DrawingBasis<TTransformation> : 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<TTransformation> : DrawingBasis
where TTransformation : GenericTransformation, new()
{
public TTransformation Transform = new TTransformation();
}
}

View file

@ -54,6 +54,10 @@
<Compile Include="Shader\InstanceShader.cs" />
<Compile Include="Shader\Shaders.cs" />
<Compile Include="Textures\Texture.cs" />
<Compile Include="Types\Vector.cs" />
<Compile Include="Types\Vector2.cs" />
<Compile Include="Types\Vector3.cs" />
<Compile Include="Types\Vector4.cs" />
<Compile Include="Window\Contexts\DrawContext.cs" />
<Compile Include="Window\Contexts\UpdateContext.cs" />
<Compile Include="Window\GenericWindow.cs" />
@ -74,8 +78,6 @@
<Name>SM.OGL</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Types\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -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);
}
}

View file

@ -1,22 +1,34 @@
using System.Collections.Generic;
using OpenTK;
using SM.Base.Contexts;
namespace SM.Base.Scene
{
public abstract class GenericScene<TCamera> : IShowCollection
where TCamera : GenericCamera
where TCamera : GenericCamera, new()
{
public IShowItem Background;
public List<IShowItem> HUD { get; } = new List<IShowItem>();
public List<IShowItem> Objects { get; } = new List<IShowItem>();
public TCamera Camera { get; set; }
public TCamera BackgroundCamera { get; set; } = new TCamera();
public Dictionary<string, TCamera> Cameras = new Dictionary<string, TCamera>();
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);
}
}

View file

@ -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);
}

View file

@ -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"]);
}));
}
}

View file

@ -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; }
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -14,5 +14,7 @@ namespace SM.Base.Contexts
public Mesh Mesh;
public Material Material;
public Vector2 WorldScale;
}
}

View file

@ -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
}
}

View file

@ -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)

View file

@ -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; }
}
}

View file

@ -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);
}
}
}

View file

@ -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<Transformation>
{
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);

View file

@ -1,4 +1,7 @@
using SM.Base;
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using SM.Base;
using SM2D.Scene;
namespace SM2D
@ -6,5 +9,18 @@ namespace SM2D
public class GLWindow2D : GenericWindow<Scene.Scene, Camera>
{
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);
}
}
}

View file

@ -45,6 +45,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Drawing\DrawBackground.cs" />
<Compile Include="Drawing\DrawEmpty.cs" />
<Compile Include="Drawing\DrawTexture.cs" />
<Compile Include="GLWindow2D.cs" />

View file

@ -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);
}
}
}

View file

@ -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;

View file

@ -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);
}
}
}