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