Loads and loads of small improvements I added while developing on my game
This commit is contained in:
parent
41421b1df9
commit
a7c71e7ea1
107 changed files with 2278 additions and 1023 deletions
|
|
@ -1,41 +1,63 @@
|
|||
#region usings
|
||||
|
||||
using System.Collections.Generic;
|
||||
using OpenTK;
|
||||
using OpenTK.Input;
|
||||
using SM.Base.Controls;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.Scene;
|
||||
using SM2D.Scene;
|
||||
using SM2D.Types;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace SM2D.Controls
|
||||
{
|
||||
public class Mouse2D : Mouse<IGLWindow2D>
|
||||
public class Mouse2D
|
||||
{
|
||||
protected internal Mouse2D(IGLWindow2D window) : base(window)
|
||||
public static Vector2 InWorld(Vector2 worldScale)
|
||||
{
|
||||
}
|
||||
|
||||
internal new void MouseMoveEvent(MouseMoveEventArgs mmea)
|
||||
{
|
||||
base.MouseMoveEvent(mmea);
|
||||
}
|
||||
|
||||
public Vector2 InWorld()
|
||||
{
|
||||
var res = _window.WorldScale;
|
||||
var res = worldScale;
|
||||
res.Y *= -1;
|
||||
return InScreenNormalized * res - res / 2;
|
||||
return Mouse.InScreenNormalized * res - res / 2;
|
||||
}
|
||||
|
||||
public Vector2 InWorld(Camera cam)
|
||||
public static Vector2 InWorld(Camera cam)
|
||||
{
|
||||
return InWorld() + cam.Position;
|
||||
return InWorld(cam.WorldScale) + cam.Position;
|
||||
}
|
||||
|
||||
public Vector2 InWorld(Vector2 position)
|
||||
public static Vector2 InWorld(Vector2 worldScale, Vector2 position)
|
||||
{
|
||||
return InWorld() + position;
|
||||
return InWorld(worldScale) + position;
|
||||
}
|
||||
|
||||
public static bool MouseOver<TObject>(Vector2 mousePos, params TObject[] checkingObjects)
|
||||
where TObject : IModelItem, ITransformItem<Transformation>
|
||||
=> MouseOver(mousePos, out _, checkingObjects);
|
||||
|
||||
public static bool MouseOver<TObject>(Vector2 mousePos, ICollection<TObject> checkingObjects)
|
||||
where TObject : IModelItem, ITransformItem<Transformation>
|
||||
=> MouseOver<TObject>(mousePos, out _, checkingObjects);
|
||||
|
||||
public static bool MouseOver<TObject>(Vector2 mousePos, out TObject clicked, params TObject[] checkingObjects)
|
||||
where TObject : IModelItem, ITransformItem<Transformation>
|
||||
=> MouseOver<TObject>(mousePos, out clicked, (ICollection<TObject>)checkingObjects);
|
||||
|
||||
public static bool MouseOver<TObject>(Vector2 mousePos, out TObject clickedObj, ICollection<TObject> checkingObjects)
|
||||
where TObject : IModelItem, ITransformItem<Transformation>
|
||||
{
|
||||
clickedObj = default;
|
||||
|
||||
foreach (TObject obj in checkingObjects)
|
||||
{
|
||||
Vector3 min = obj.Mesh.BoundingBox.Get(obj.Transform.MergeMatrix(obj.Transform.LastMaster), false);
|
||||
Vector3 max = obj.Mesh.BoundingBox.Get(obj.Transform.MergeMatrix(obj.Transform.LastMaster), true);
|
||||
|
||||
if (mousePos.X > min.X && mousePos.X < max.X &&
|
||||
mousePos.Y > min.Y && mousePos.Y < max.Y)
|
||||
{
|
||||
clickedObj = obj;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,20 +4,38 @@ using System.Collections.Generic;
|
|||
using System.Drawing;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.Objects.Static;
|
||||
using SM.Base.Scene;
|
||||
using SM.Base.Textures;
|
||||
using SM.Base.Windows;
|
||||
using SM.OGL.Texture;
|
||||
using SM2D.Scene;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace SM2D.Drawing
|
||||
{
|
||||
public class DrawBackground : IBackgroundItem
|
||||
public class DrawBackground : DrawingBasis, IBackgroundItem
|
||||
{
|
||||
private Material _material = new Material();
|
||||
public Color4 Color
|
||||
{
|
||||
get => Material.Tint;
|
||||
set => Material.Tint = value;
|
||||
}
|
||||
|
||||
public TextureBase Texture
|
||||
{
|
||||
get => Material.Texture;
|
||||
set
|
||||
{
|
||||
if (Material.Tint == Color4.Black) Material.Tint = Color4.White;
|
||||
Material.Texture = value;
|
||||
}
|
||||
}
|
||||
|
||||
public DrawBackground() : this(Color4.Black) {}
|
||||
|
||||
public DrawBackground(Color4 color)
|
||||
{
|
||||
|
|
@ -35,41 +53,12 @@ namespace SM2D.Drawing
|
|||
Texture = (Texture) texture;
|
||||
}
|
||||
|
||||
public Color4 Color
|
||||
|
||||
protected override void DrawContext(ref DrawContext context)
|
||||
{
|
||||
get => _material.Tint;
|
||||
set => _material.Tint = value;
|
||||
}
|
||||
|
||||
public TextureBase Texture
|
||||
{
|
||||
get => _material.Texture;
|
||||
set => _material.Texture = value;
|
||||
}
|
||||
|
||||
public object Parent { get; set; }
|
||||
public string Name { get; set; } = "Background";
|
||||
public ICollection<string> Flags { get; set; } = new string[0];
|
||||
|
||||
public void Update(UpdateContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void Draw(DrawContext context)
|
||||
{
|
||||
context.Material = _material;
|
||||
context.Mesh = Plate.Object;
|
||||
|
||||
context.ModelMaster = Matrix4.CreateScale(context.WorldScale.X, context.WorldScale.Y, 1);
|
||||
base.DrawContext(ref context);
|
||||
context.ModelMatrix = Matrix4.CreateScale((context.UseCamera as Camera).WorldScale.X, (context.UseCamera as Camera).WorldScale.Y, 1);
|
||||
context.Shader.Draw(context);
|
||||
}
|
||||
|
||||
public void OnAdded(object sender)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnRemoved(object sender)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,30 +2,30 @@
|
|||
using System.Drawing;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.Objects;
|
||||
using SM.Base.Textures;
|
||||
using SM.Base.Windows;
|
||||
using SM.OGL.Mesh;
|
||||
using SM2D.Object;
|
||||
using SM2D.Scene;
|
||||
using SM2D.Types;
|
||||
|
||||
namespace SM2D.Drawing
|
||||
{
|
||||
public class DrawObject2D : DrawingBasis<Transformation>, I2DShowItem
|
||||
public class DrawObject2D : DrawingBasis<Transformation>
|
||||
{
|
||||
public int ZIndex { get; set; }
|
||||
|
||||
public Texture Texture
|
||||
{
|
||||
get => (Texture) _material.Texture;
|
||||
set => _material.Texture = value;
|
||||
get => (Texture) Material.Texture;
|
||||
set => Material.Texture = value;
|
||||
}
|
||||
|
||||
public Color4 Color
|
||||
{
|
||||
get => _material.Tint;
|
||||
set => _material.Tint = value;
|
||||
get => Material.Tint;
|
||||
set => Material.Tint = value;
|
||||
}
|
||||
|
||||
protected override void DrawContext(ref DrawContext context)
|
||||
|
|
@ -33,37 +33,32 @@ namespace SM2D.Drawing
|
|||
base.DrawContext(ref context);
|
||||
context.Shader.Draw(context);
|
||||
}
|
||||
|
||||
public void SetShader(MaterialShader shader) => Material.CustomShader = shader;
|
||||
|
||||
public Material GetMaterialReference() => _material;
|
||||
public void SetMaterialReference(Material material) => _material = material;
|
||||
|
||||
public void SetShader(MaterialShader shader) => _material.CustomShader = shader;
|
||||
|
||||
public Polygon ApplyPolygon(ICollection<Vector2> vertices)
|
||||
public Polygon ApplyPolygon(ICollection<Vector2> vertices, bool centerUVs = false)
|
||||
{
|
||||
Polygon polygon = new Polygon(vertices);
|
||||
_mesh = polygon;
|
||||
Mesh = polygon;
|
||||
return polygon;
|
||||
}
|
||||
public Polygon ApplyPolygon(ICollection<PolygonVertex> vertices)
|
||||
public Polygon ApplyPolygon(ICollection<PolygonVertex> vertices, bool centerUVs = false)
|
||||
{
|
||||
Polygon polygon = new Polygon(vertices);
|
||||
_mesh = polygon;
|
||||
Mesh = polygon;
|
||||
return polygon;
|
||||
}
|
||||
public void ApplyPolygon(Polygon polygon)
|
||||
{
|
||||
_mesh = polygon;
|
||||
Mesh = polygon;
|
||||
}
|
||||
|
||||
public void ApplyMesh(Mesh mesh) => _mesh = mesh;
|
||||
|
||||
public Polygon ApplyCircle(int segments = 32)
|
||||
public Polygon ApplyCircle(int segments = 32, bool centerUVs = false)
|
||||
{
|
||||
Polygon pol = Polygon.GenerateCircle(segments);
|
||||
_mesh = pol;
|
||||
Mesh = pol;
|
||||
return pol;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -7,9 +7,8 @@ using SM2D.Types;
|
|||
|
||||
namespace SM2D.Drawing
|
||||
{
|
||||
public class DrawParticles : ParticleDrawingBasis<Transformation, Vector2>, I2DShowItem
|
||||
public class DrawParticles : ParticleDrawingBasis<Transformation, Vector2>
|
||||
{
|
||||
public int ZIndex { get; set; }
|
||||
public override Func<Vector2, ParticleContext, Vector2> MovementCalculation { get; set; } = ParticleMovement.Default2D;
|
||||
|
||||
public DrawParticles(TimeSpan duration) : base(duration)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#region usings
|
||||
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base;
|
||||
using SM.Base.Drawing.Text;
|
||||
using SM.Base.Types;
|
||||
using SM.Base.Windows;
|
||||
using SM2D.Scene;
|
||||
using SM2D.Types;
|
||||
|
||||
|
|
@ -10,7 +11,7 @@ using SM2D.Types;
|
|||
|
||||
namespace SM2D.Drawing
|
||||
{
|
||||
public class DrawText : TextDrawingBasis<Transformation>, I2DShowItem
|
||||
public class DrawText : TextDrawingBasis<Transformation>
|
||||
{
|
||||
public DrawText(Font font, string text) : base(font)
|
||||
{
|
||||
|
|
@ -18,8 +19,6 @@ namespace SM2D.Drawing
|
|||
Transform.Size = new CVector2(1);
|
||||
}
|
||||
|
||||
public int ZIndex { get; set; }
|
||||
|
||||
protected override void DrawContext(ref DrawContext context)
|
||||
{
|
||||
base.DrawContext(ref context);
|
||||
|
|
|
|||
35
SMCode/SM2D/Object/PolyLine.cs
Normal file
35
SMCode/SM2D/Object/PolyLine.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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 enum PolyLineType
|
||||
{
|
||||
NotConnected = 1,
|
||||
Connected = 3,
|
||||
ConnectedLoop = 2
|
||||
}
|
||||
|
||||
public class PolyLine : Polygon, ILineMesh
|
||||
{
|
||||
public float LineWidth { get; set; } = 1;
|
||||
|
||||
public PolyLine(ICollection<Vector2> vertices, PolyLineType lineType = PolyLineType.NotConnected) : base(vertices)
|
||||
{
|
||||
UVs.Active = false;
|
||||
|
||||
PrimitiveType = (PrimitiveType)lineType;
|
||||
}
|
||||
|
||||
public PolyLine(ICollection<PolygonVertex> vertices, PolyLineType lineType = PolyLineType.NotConnected) : base(vertices)
|
||||
{
|
||||
UVs.Active = false;
|
||||
PrimitiveType = (PrimitiveType)lineType;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -16,13 +16,16 @@ namespace SM2D.Object
|
|||
{
|
||||
public Polygon(ICollection<Vector2> vertices) : base(PrimitiveType.TriangleFan)
|
||||
{
|
||||
Color.Active = false;
|
||||
|
||||
foreach (var vertex in vertices)
|
||||
{
|
||||
Color.Add(Color4.White);
|
||||
AddVertex(vertex);
|
||||
Vertex.Add(vertex, 0);
|
||||
}
|
||||
|
||||
foreach (var vertex in vertices) AddUV(vertex);
|
||||
UpdateBoundingBox();
|
||||
|
||||
if (UVs.Active) foreach (var vertex in vertices) AddUV(vertex);
|
||||
}
|
||||
|
||||
public Polygon(ICollection<PolygonVertex> vertices) : base(PrimitiveType.TriangleFan)
|
||||
|
|
@ -30,10 +33,12 @@ namespace SM2D.Object
|
|||
foreach (var polygonVertex in vertices)
|
||||
{
|
||||
Color.Add(polygonVertex.Color);
|
||||
AddVertex(polygonVertex.Vertex);
|
||||
Vertex.Add(polygonVertex.Vertex, 0);
|
||||
}
|
||||
|
||||
foreach (var vertex in vertices) AddUV(vertex.Vertex);
|
||||
UpdateBoundingBox();
|
||||
|
||||
if (UVs.Active) foreach (var vertex in vertices) AddUV(vertex.Vertex);
|
||||
}
|
||||
|
||||
public override VBO Vertex { get; protected set; } = new VBO();
|
||||
|
|
@ -42,12 +47,6 @@ namespace SM2D.Object
|
|||
|
||||
public override PrimitiveType PrimitiveType { get; protected set; } = PrimitiveType.TriangleFan;
|
||||
|
||||
private void AddVertex(Vector2 vertex)
|
||||
{
|
||||
BoundingBox.Update(vertex);
|
||||
Vertex.Add(vertex, 0);
|
||||
}
|
||||
|
||||
private void AddUV(Vector2 vertex)
|
||||
{
|
||||
var uv = Vector2.Divide(vertex - BoundingBox.Min.Xy, BoundingBox.Max.Xy - BoundingBox.Min.Xy);
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
namespace SM2D.Pipelines
|
||||
{
|
||||
public class Adv2DPipeline
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +1,23 @@
|
|||
using System;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.Windows;
|
||||
using SM.OGL.Framebuffer;
|
||||
using SM2D.Shader;
|
||||
|
||||
namespace SM2D.Pipelines
|
||||
{
|
||||
public class Basic2DPipeline : RenderPipeline<Scene.Scene>
|
||||
public class Basic2DPipeline : RenderPipeline
|
||||
{
|
||||
public static Basic2DPipeline Pipeline = new Basic2DPipeline();
|
||||
|
||||
public override MaterialShader DefaultShader { get; protected set; } = ShaderCollection.Instanced;
|
||||
|
||||
private Basic2DPipeline()
|
||||
{
|
||||
_defaultShader = Basic2DShader.Shader;
|
||||
}
|
||||
|
||||
protected override void RenderProcess(ref DrawContext context, Scene.Scene scene)
|
||||
protected override void RenderProcess(ref DrawContext context)
|
||||
{
|
||||
scene?.Draw(context);
|
||||
context.Scene?.Draw(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
#region usings
|
||||
|
||||
using System.Collections.Generic;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base.Scene;
|
||||
using SM.OGL.Framebuffer;
|
||||
using SM2D.Shader;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace SM2D.Pipelines
|
||||
{
|
||||
public class Default2DPipeline : RenderPipeline<Scene.Scene>
|
||||
{
|
||||
public static Default2DPipeline Pipeline = new Default2DPipeline();
|
||||
|
||||
|
||||
private Default2DPipeline()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void Initialization(IGenericWindow window)
|
||||
{
|
||||
MainFramebuffer = CreateWindowFramebuffer();
|
||||
}
|
||||
|
||||
protected override void RenderProcess(ref DrawContext context, Scene.Scene scene)
|
||||
{
|
||||
if (scene != null)
|
||||
{
|
||||
scene.DrawBackground(context);
|
||||
|
||||
scene.DrawMainObjects(context);
|
||||
|
||||
scene.DrawHUD(context);
|
||||
scene.DrawDebug(context);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void SceneChanged(Scene.Scene scene)
|
||||
{
|
||||
base.SceneChanged(scene);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -45,22 +45,19 @@
|
|||
<Compile Include="Drawing\DrawObject2D.cs" />
|
||||
<Compile Include="Drawing\DrawParticles.cs" />
|
||||
<Compile Include="Drawing\DrawText.cs" />
|
||||
<Compile Include="Window\GLWindow2D.cs" />
|
||||
<Compile Include="Window\GLWPFWindow2D.cs" />
|
||||
<Compile Include="Object\Polygon.cs" />
|
||||
<Compile Include="Object\PolygonVertex.cs" />
|
||||
<Compile Include="Pipelines\Adv2DPipeline.cs" />
|
||||
<Compile Include="Object\PolyLine.cs" />
|
||||
<Compile Include="Pipelines\Basic2DPipeline.cs" />
|
||||
<Compile Include="Pipelines\Default2DPipeline.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Scene\Camera.cs" />
|
||||
<Compile Include="Scene\I2DShowItem.cs" />
|
||||
<Compile Include="Scene\ItemCollection.cs" />
|
||||
<Compile Include="Scene\Scene.cs" />
|
||||
<Compile Include="Shader\Basic2DShader.cs" />
|
||||
<Compile Include="Shader\Default2DShader.cs" />
|
||||
<Compile Include="Shader\ShaderCollection.cs" />
|
||||
<Compile Include="Types\Transformation.cs" />
|
||||
<Compile Include="Window\IGLWindow2D.cs" />
|
||||
<Compile Include="Window\Window2DSetup.cs" />
|
||||
<Compile Include="Window\I2DSetup.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SM.Base\SM.Base.csproj">
|
||||
|
|
@ -72,9 +69,6 @@
|
|||
<Name>SM.OGL</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Shader\ShaderFiles\default.glsl" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="OpenTK">
|
||||
<Version>3.3.1</Version>
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=light_005Clightobjects/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=shader_005Cshaderfiles/@EntryIndexedValue">False</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=window/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
#region usings
|
||||
|
||||
using System;
|
||||
using OpenTK;
|
||||
using SM.Base;
|
||||
using SM.Base.Scene;
|
||||
using SM.Base.Types;
|
||||
using SM.Base.Windows;
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
@ -10,18 +13,73 @@ namespace SM2D.Scene
|
|||
{
|
||||
public class Camera : GenericCamera
|
||||
{
|
||||
internal static int ResizeCounter = 0;
|
||||
|
||||
private int _resizeCounter = 0;
|
||||
private bool _updateWorldScale = false;
|
||||
private Vector2? _requestedWorldScale = null;
|
||||
|
||||
public Vector2? RequestedWorldScale
|
||||
{
|
||||
get => _requestedWorldScale;
|
||||
set
|
||||
{
|
||||
_requestedWorldScale = value;
|
||||
_updateWorldScale = true;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 WorldScale { get; private set; } = Vector2.Zero;
|
||||
|
||||
public event Action<Camera> WorldScaleChanged;
|
||||
|
||||
public CVector2 Position = new CVector2(0);
|
||||
public override bool Orthographic { get; } = true;
|
||||
|
||||
protected override Matrix4 ViewCalculation()
|
||||
protected override Matrix4 ViewCalculation(IGenericWindow window)
|
||||
{
|
||||
return Matrix4.LookAt(Position.X, Position.Y, 2, Position.X, Position.Y, 0, 0, 1, 0);
|
||||
return Matrix4.LookAt(Position.X, Position.Y, 1, Position.X, Position.Y, 0, 0, 1, 0);
|
||||
}
|
||||
|
||||
public override void RecalculateWorld(Vector2 world, float aspect)
|
||||
protected override bool WorldCalculation(IGenericWindow window, out Matrix4 world)
|
||||
{
|
||||
OrthographicWorld =
|
||||
Matrix4.CreateOrthographic(world.X, world.Y, 0.1f, 100f);
|
||||
world = Matrix4.Identity;
|
||||
if (ResizeCounter != _resizeCounter || _updateWorldScale)
|
||||
{
|
||||
_updateWorldScale = false;
|
||||
_resizeCounter = ResizeCounter;
|
||||
CalculateWorldScale(window);
|
||||
|
||||
world = Matrix4.CreateOrthographic(WorldScale.X, WorldScale.Y, .0001f, 1.5f);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void CalculateWorldScale(IGenericWindow window)
|
||||
{
|
||||
if (RequestedWorldScale.HasValue)
|
||||
{
|
||||
float aspect = window.Width > window.Height ? window.AspectRatio : window.AspectRatioReverse;
|
||||
Vector2 requested = RequestedWorldScale.Value;
|
||||
|
||||
if (requested.X > 0 && requested.Y > 0)
|
||||
{
|
||||
float requestRatio = requested.X / requested.Y;
|
||||
|
||||
if (requestRatio > aspect) WorldScale = new Vector2(requested.X, requested.X / aspect);
|
||||
else WorldScale = new Vector2(aspect * requested.Y, requested.Y);
|
||||
}
|
||||
else if (requested.X > 0) WorldScale = new Vector2(requested.X, requested.X / aspect);
|
||||
else if (requested.Y > 0) WorldScale = new Vector2(aspect * requested.Y, requested.Y);
|
||||
}
|
||||
else
|
||||
{
|
||||
WorldScale = window.WindowSize;
|
||||
}
|
||||
|
||||
WorldScaleChanged?.Invoke(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +1,16 @@
|
|||
#region usings
|
||||
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base;
|
||||
using SM.Base.Scene;
|
||||
using SM.Base.Types;
|
||||
using SM.Base.Windows;
|
||||
using SM2D.Types;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace SM2D.Scene
|
||||
{
|
||||
public class ItemCollection : GenericItemCollection<I2DShowItem, Transformation>, I2DShowItem
|
||||
public class ItemCollection : GenericItemCollection<Transformation>
|
||||
{
|
||||
public ItemCollection()
|
||||
{
|
||||
|
|
@ -18,11 +19,7 @@ namespace SM2D.Scene
|
|||
|
||||
public override void Draw(DrawContext context)
|
||||
{
|
||||
Sort((x, y) => x.ZIndex - y.ZIndex);
|
||||
|
||||
base.Draw(context);
|
||||
}
|
||||
|
||||
public int ZIndex { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -2,16 +2,17 @@
|
|||
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base;
|
||||
using SM.Base.Objects.Static;
|
||||
using SM.Base.Scene;
|
||||
using SM.Base.Windows;
|
||||
using SM2D.Drawing;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace SM2D.Scene
|
||||
{
|
||||
public class Scene : GenericScene<Camera, ItemCollection, I2DShowItem>
|
||||
public class Scene : GenericScene<Camera, ItemCollection>
|
||||
{
|
||||
private static DrawObject2D _axisHelper;
|
||||
|
||||
|
|
@ -19,16 +20,24 @@ namespace SM2D.Scene
|
|||
static Scene()
|
||||
{
|
||||
_axisHelper = new DrawObject2D();
|
||||
_axisHelper.ApplyMesh(AxisHelper.Object);
|
||||
_axisHelper.Mesh = AxisHelper.Object;
|
||||
}
|
||||
|
||||
public Scene()
|
||||
{
|
||||
_Background = new DrawBackground(Color4.Black);
|
||||
Objects = new ItemCollection();
|
||||
|
||||
BackgroundCamera = new Camera();
|
||||
HUDCamera = new Camera();
|
||||
}
|
||||
|
||||
|
||||
public DrawBackground Background => (DrawBackground) _Background;
|
||||
public DrawBackground Background
|
||||
{
|
||||
get => (DrawBackground) _Background;
|
||||
set => _Background = value;
|
||||
}
|
||||
|
||||
public override void DrawDebug(DrawContext context)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
using SM.Base.Contexts;
|
||||
using SM.Base.Drawing;
|
||||
using SM.OGL.Shaders;
|
||||
using SM.Utility;
|
||||
|
||||
namespace SM2D.Shader
|
||||
{
|
||||
public class Basic2DShader : MaterialShader
|
||||
{
|
||||
|
||||
public static Basic2DShader Shader = new Basic2DShader();
|
||||
private Basic2DShader() : base(AssemblyUtility.ReadAssemblyFile("SM2D.Shader.ShaderFiles.basic.glsl"))
|
||||
{
|
||||
}
|
||||
|
||||
protected override void DrawProcess(DrawContext context)
|
||||
{
|
||||
// Vertex Uniforms
|
||||
Uniforms["MVP"].SetMatrix4(context.ModelMaster * context.View * context.World);
|
||||
Uniforms["HasVColor"]
|
||||
.SetUniform1(context.Mesh.Attributes["color"] != null);
|
||||
|
||||
UniformArray instances = Uniforms.GetArray("Instances");
|
||||
for (int i = 0; i < context.Instances.Count; i++)
|
||||
{
|
||||
var shaderInstance = instances[i];
|
||||
var instance = context.Instances[i];
|
||||
shaderInstance["ModelMatrix"].SetMatrix4(instance.ModelMatrix);
|
||||
shaderInstance["TextureOffset"].SetUniform2(instance.TexturePosition);
|
||||
shaderInstance["TextureScale"].SetUniform2(instance.TextureScale);
|
||||
}
|
||||
|
||||
// Fragment Uniforms
|
||||
Uniforms["Tint"].SetUniform4(context.Material.Tint);
|
||||
Uniforms["Texture"].SetTexture(context.Material.Texture, Uniforms["UseTexture"]);
|
||||
|
||||
DrawObject(context.Mesh, context.Instances.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
#region usings
|
||||
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.Scene;
|
||||
using SM.OGL.Shaders;
|
||||
using SM.Utility;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace SM2D.Shader
|
||||
{
|
||||
public class Default2DShader : MaterialShader
|
||||
{
|
||||
public static Default2DShader MaterialShader = new Default2DShader();
|
||||
|
||||
|
||||
private Default2DShader() : base(AssemblyUtility.ReadAssemblyFile("SM2D.Shader.ShaderFiles.default.glsl"))
|
||||
{
|
||||
}
|
||||
|
||||
protected override void DrawProcess(DrawContext context)
|
||||
{
|
||||
// Vertex Uniforms
|
||||
Uniforms["MVP"].SetMatrix4(context.ModelMaster * context.View * context.World);
|
||||
Uniforms["HasVColor"]
|
||||
.SetUniform1(context.Mesh.Attributes["color"] != null);
|
||||
Uniforms["occlude"].SetUniform1(context.ShaderArguments.ContainsKey("occluder"));
|
||||
|
||||
UniformArray instances = Uniforms.GetArray("Instances");
|
||||
for (int i = 0; i < context.Instances.Count; i++)
|
||||
{
|
||||
var shaderInstance = instances[i];
|
||||
var instance = context.Instances[i];
|
||||
shaderInstance["ModelMatrix"].SetMatrix4(instance.ModelMatrix);
|
||||
shaderInstance["TextureOffset"].SetUniform2(instance.TexturePosition);
|
||||
shaderInstance["TextureScale"].SetUniform2(instance.TextureScale);
|
||||
}
|
||||
|
||||
// Fragment Uniforms
|
||||
Uniforms["Tint"].SetUniform4(context.Material.Tint);
|
||||
Uniforms["Texture"].SetTexture(context.Material.Texture, Uniforms["UseTexture"]);
|
||||
|
||||
DrawObject(context.Mesh, context.Instances.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
SMCode/SM2D/Shader/ShaderCollection.cs
Normal file
19
SMCode/SM2D/Shader/ShaderCollection.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using SM.Base.Drawing;
|
||||
using SM.Base.Windows;
|
||||
using SM.OGL.Shaders;
|
||||
using SM.Utility;
|
||||
|
||||
namespace SM2D.Shader
|
||||
{
|
||||
public class ShaderCollection
|
||||
{
|
||||
public static SimpleShader Basic = new SimpleShader("basic", AssemblyUtility.ReadAssemblyFile("SM2D.Shader.ShaderFiles.basic.glsl"), SetUniforms);
|
||||
public static SimpleShader Instanced = new SimpleShader("instanced", AssemblyUtility.ReadAssemblyFile("SM2D.Shader.ShaderFiles.basic.glsl"), SetUniforms);
|
||||
|
||||
static void SetUniforms(UniformCollection uniforms, DrawContext context)
|
||||
{
|
||||
uniforms["Tint"].SetUniform4(context.Material.Tint);
|
||||
uniforms["Texture"].SetTexture(context.Material.Texture, uniforms["UseTexture"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +1,6 @@
|
|||
#version 330
|
||||
//# region vertex
|
||||
|
||||
//# import SM_base_vertex_basic
|
||||
void ApplyTexModifier();
|
||||
void CheckVertexColor();
|
||||
void ApplyModelTransformation();
|
||||
|
||||
void vmain() {
|
||||
ApplyTexModifier();
|
||||
CheckVertexColor();
|
||||
ApplyModelTransformation();
|
||||
}
|
||||
|
||||
//# region fragment
|
||||
in vec2 vTexture;
|
||||
in vec4 vColor;
|
||||
in vec2 v_TexCoords;
|
||||
in vec4 v_Color;
|
||||
|
||||
uniform vec4 Tint;
|
||||
uniform bool UseTexture;
|
||||
|
|
@ -22,7 +8,9 @@ uniform sampler2D Texture;
|
|||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
void fmain() {
|
||||
color = vColor * Tint;
|
||||
if (UseTexture) color *= texture(Texture, vTexture);
|
||||
void main() {
|
||||
color = vec4(v_TexCoords, 0, 1);
|
||||
return;
|
||||
color = v_Color * Tint;
|
||||
if (UseTexture) color = texture(Texture, v_TexCoords);
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
#version 330
|
||||
|
||||
//# region vertex
|
||||
|
||||
//# import SM_base_vertex_basic
|
||||
void ApplyTexModifier();
|
||||
void CheckVertexColor();
|
||||
void ApplyModelTransformation();
|
||||
|
||||
void vmain() {
|
||||
ApplyTexModifier();
|
||||
CheckVertexColor();
|
||||
ApplyModelTransformation();
|
||||
}
|
||||
|
||||
//# region fragment
|
||||
in vec2 vTexture;
|
||||
in vec4 vColor;
|
||||
|
||||
uniform bool occlude;
|
||||
|
||||
uniform vec4 Tint;
|
||||
uniform bool UseTexture;
|
||||
uniform sampler2D Texture;
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
layout(location = 1) out vec4 bloom;
|
||||
|
||||
void fmain() {
|
||||
color = vColor * Tint;
|
||||
if (UseTexture) color *= texture(Texture, vTexture);
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
#region usings
|
||||
|
||||
using System;
|
||||
using System.Drawing.Drawing2D;
|
||||
using OpenTK;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.Scene;
|
||||
|
|
@ -14,22 +15,32 @@ namespace SM2D.Types
|
|||
{
|
||||
public class Transformation : GenericTransformation
|
||||
{
|
||||
public static int ZIndexPercision = 300;
|
||||
|
||||
public CVector2 Position { get; set; } = new CVector2(0);
|
||||
|
||||
public CVector2 Size { get; set; } = new CVector2(50);
|
||||
|
||||
public float Rotation { get; set; }
|
||||
public CVector1 Rotation { get; set; } = new CVector1(0);
|
||||
|
||||
public bool HorizontalFlip { get; set; } = false;
|
||||
|
||||
public bool VerticalFlip { get; set; } = false;
|
||||
|
||||
public int ZIndex { get; set; }
|
||||
|
||||
protected override Matrix4 RequestMatrix()
|
||||
{
|
||||
return Matrix4.CreateScale(Size.X, Size.Y, 1) *
|
||||
Matrix4.CreateRotationX(MathHelper.DegreesToRadians(HorizontalFlip ? 180 : 0)) *
|
||||
Matrix4.CreateRotationY(MathHelper.DegreesToRadians(VerticalFlip ? 180 : 0)) *
|
||||
Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) *
|
||||
Matrix4.CreateTranslation(Position.X, Position.Y, 0);
|
||||
Matrix4.CreateTranslation(Position.X, Position.Y, -(1 / (float)ZIndexPercision * ZIndex));
|
||||
}
|
||||
|
||||
public void TurnTo(Vector2 v)
|
||||
{
|
||||
Rotation = RotationUtility.TurnTowards(Position, v);
|
||||
Rotation.Set(RotationUtility.TurnTowards(Position, v));
|
||||
}
|
||||
|
||||
public Vector2 LookAtVector()
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
using System;
|
||||
using System.Windows.Media;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics.ES11;
|
||||
using SM.Base;
|
||||
using SM2D.Controls;
|
||||
using SM2D.Pipelines;
|
||||
using SM2D.Scene;
|
||||
using SM2D.Shader;
|
||||
|
||||
namespace SM2D
|
||||
{
|
||||
public class GLWPFWindow2D : GenericWPFWindow<Scene.Scene, Camera>, IGLWindow2D
|
||||
{
|
||||
public GLWPFWindow2D()
|
||||
{
|
||||
Mouse = new Mouse2D(this);
|
||||
}
|
||||
|
||||
public Vector2? Scaling { get; set; }
|
||||
public Mouse2D Mouse { get; }
|
||||
|
||||
protected override void Init()
|
||||
{
|
||||
base.Init();
|
||||
|
||||
SMRenderer.DefaultMaterialShader = Default2DShader.MaterialShader;
|
||||
|
||||
//SetRenderPipeline(Default2DPipeline.Pipeline);
|
||||
}
|
||||
|
||||
protected override void Rendering(TimeSpan delta)
|
||||
{
|
||||
GL.Disable(EnableCap.DepthTest);
|
||||
base.Rendering(delta);
|
||||
}
|
||||
|
||||
public override void SetWorldScale()
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
#region usings
|
||||
|
||||
using System;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using OpenTK.Input;
|
||||
using SM.Base;
|
||||
using SM2D.Controls;
|
||||
using SM2D.Pipelines;
|
||||
using SM2D.Scene;
|
||||
using SM2D.Shader;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace SM2D
|
||||
{
|
||||
public class GLWindow2D : GenericWindow<Scene.Scene, Camera>, IGLWindow2D
|
||||
{
|
||||
public GLWindow2D()
|
||||
{
|
||||
Mouse = new Mouse2D(this);
|
||||
}
|
||||
|
||||
public Vector2? Scaling { get; set; }
|
||||
|
||||
public Mouse2D Mouse { get; }
|
||||
|
||||
protected override void OnLoad(EventArgs e)
|
||||
{
|
||||
base.OnLoad(e);
|
||||
GL.Enable(EnableCap.Blend);
|
||||
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
|
||||
}
|
||||
|
||||
protected override void OnLoaded()
|
||||
{
|
||||
base.OnLoaded();
|
||||
SMRenderer.DefaultMaterialShader = Default2DShader.MaterialShader;
|
||||
|
||||
SetRenderPipeline(Default2DPipeline.Pipeline);
|
||||
}
|
||||
|
||||
protected override void OnRenderFrame(FrameEventArgs e)
|
||||
{
|
||||
GL.Disable(EnableCap.DepthTest);
|
||||
base.OnRenderFrame(e);
|
||||
}
|
||||
|
||||
public override void SetWorldScale()
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseMove(MouseMoveEventArgs e)
|
||||
{
|
||||
base.OnMouseMove(e);
|
||||
Mouse.MouseMoveEvent(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
SMCode/SM2D/Window/I2DSetup.cs
Normal file
10
SMCode/SM2D/Window/I2DSetup.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using OpenTK;
|
||||
using SM.Base.Windows;
|
||||
|
||||
namespace SM2D
|
||||
{
|
||||
public interface I2DSetup : ISetup
|
||||
{
|
||||
Vector2? WorldScale { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
using OpenTK;
|
||||
using SM.Base;
|
||||
using SM2D.Controls;
|
||||
using SM2D.Scene;
|
||||
|
||||
namespace SM2D
|
||||
{
|
||||
public interface IGLWindow2D : IGenericWindow<Scene.Scene, Camera>
|
||||
{
|
||||
Vector2? Scaling { get; set; }
|
||||
|
||||
Mouse2D Mouse { get; }
|
||||
}
|
||||
}
|
||||
38
SMCode/SM2D/Window/Window2DSetup.cs
Normal file
38
SMCode/SM2D/Window/Window2DSetup.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
using System.Drawing.Drawing2D;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base;
|
||||
using SM.Base.PostProcess;
|
||||
using SM.Base.Windows;
|
||||
using SM2D.Scene;
|
||||
using SM2D.Shader;
|
||||
|
||||
namespace SM2D
|
||||
{
|
||||
public struct Window2DSetup : I2DSetup
|
||||
{
|
||||
public Vector2? WorldScale { get; set; }
|
||||
|
||||
public void Applied(IGenericWindow window)
|
||||
{
|
||||
window.ViewportCamera = new Camera();
|
||||
|
||||
SMRenderer.DefaultMaterialShader = ShaderCollection.Instanced;
|
||||
}
|
||||
|
||||
public void Load(IGenericWindow window)
|
||||
{
|
||||
(window.ViewportCamera as Camera).RequestedWorldScale = WorldScale;
|
||||
GL.Enable(EnableCap.DepthTest);
|
||||
}
|
||||
|
||||
public void Loaded(IGenericWindow window)
|
||||
{
|
||||
}
|
||||
|
||||
public void Resize(IGenericWindow window)
|
||||
{
|
||||
Camera.ResizeCounter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue