26.09.2020

+ Added BoundingBoxes to Meshes
+ SM.Base.Objects.Mesh
+ Vertex Colors
+ ShowItem Collections + 2D Equlivant
+ Default Class to store Default Values
+ SM.OGL.GLSystem to store OpenGL specific system information

+ SM2D.DrawColor // not working yet
+ SM2D.DrawComplex to allow access to all features.
+ SM2D.DrawPolygon
+ Polygon system // for 2D only yet

~ Renamed SM.OGL.Mesh to SM.OGL.GenericMesh
This commit is contained in:
Michel Fedde 2020-09-26 23:40:16 +02:00
parent c4a0847567
commit 617a7ef044
39 changed files with 598 additions and 66 deletions

View file

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

View file

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

View file

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

View file

@ -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() {}
}
}

View file

@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -29,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="OpenTK, Version=3.2.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
@ -45,14 +47,17 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Defaults.cs" />
<Compile Include="Drawing\DrawingBasis.cs" />
<Compile Include="Drawing\GenericTransformation.cs" />
<Compile Include="Drawing\Instance.cs" />
<Compile Include="Drawing\IShader.cs" />
<Compile Include="Objects\Mesh.cs" />
<Compile Include="Scene\IShowCollection.cs" />
<Compile Include="Scene\IShowItem.cs" />
<Compile Include="Drawing\Material.cs" />
<Compile Include="Scene\IBackgroundItem.cs" />
<Compile Include="Scene\GenericItemCollection.cs" />
<Compile Include="Shader\InstanceShader.cs" />
<Compile Include="Shader\Shaders.cs" />
<Compile Include="Textures\Texture.cs" />
@ -70,7 +75,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scene\GenericCamera.cs" />
<Compile Include="Scene\GenericScene.cs" />
<Compile Include="StaticObjects\Plate.cs" />
<Compile Include="Objects\Static\Plate.cs" />
</ItemGroup>
<ItemGroup>
<None Include="OpenTK.dll.config" />

View file

@ -0,0 +1,25 @@
using System.Collections.Generic;
using SM.Base.Contexts;
namespace SM.Base.Scene
{
public abstract class GenericItemCollection<TItem, TTransformation> : IShowItem, IShowCollection<TItem>
where TItem : IShowItem
where TTransformation : GenericTransformation, new()
{
public List<TItem> Objects { get; } = new List<TItem>();
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);
}
}
}

View file

@ -4,18 +4,20 @@ using SM.Base.Contexts;
namespace SM.Base.Scene
{
public abstract class GenericScene<TCamera> : IShowCollection
public abstract class GenericScene<TCamera, TItem> : IShowCollection<TItem>
where TCamera : GenericCamera, new()
where TItem : IShowItem
{
protected IBackgroundItem _background;
public List<IShowItem> HUD { get; } = new List<IShowItem>();
public List<IShowItem> Objects { get; } = new List<IShowItem>();
public List<TItem> HUD { get; } = new List<TItem>();
public List<TItem> Objects { get; } = new List<TItem>();
public TCamera Camera { get; set; }
public TCamera BackgroundCamera { get; set; } = new TCamera();
public TCamera HUDCamera { get; set; } = new TCamera();
public Dictionary<string, TCamera> Cameras = new Dictionary<string, TCamera>();
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);
}

View file

@ -1,9 +1,12 @@
using System.Collections.Generic;
using SM.Base.Contexts;
namespace SM.Base.Scene
{
public interface IShowCollection
public interface IShowCollection<TItem> where TItem : IShowItem
{
List<IShowItem> Objects { get; }
List<TItem> Objects { get; }
void Draw(DrawContext context);
}
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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<TScene, TCamera> : GameWindow
where TScene : GenericScene<TCamera>, new()
public class GenericWindow<TScene, TItem, TCamera> : GameWindow
where TScene : GenericScene<TCamera, TItem>, 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);
}

View file

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

View file

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

33
SMCode/SM.OGL/GLSystem.cs Normal file
View file

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

View file

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

View file

@ -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<int, VBO> AttribDataIndex { get; }
public Mesh()
public virtual int[] Indices { get; set; }
protected GenericMesh()
{
AttribDataIndex = new Dictionary<int, VBO>()
{

View file

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

View file

@ -46,7 +46,9 @@
<ItemGroup>
<Compile Include="GLDebugging.cs" />
<Compile Include="GLObject.cs" />
<Compile Include="Mesh\Mesh.cs" />
<Compile Include="GLSystem.cs" />
<Compile Include="Mesh\BoundingBox.cs" />
<Compile Include="Mesh\GenericMesh.cs" />
<Compile Include="Mesh\TypeDefinition.cs" />
<Compile Include="Mesh\VBO.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@ -56,6 +58,7 @@
<Compile Include="Shaders\Uniform.cs" />
<Compile Include="Shaders\UniformCollection.cs" />
<Compile Include="Texture\TextureBase.cs" />
<Compile Include="Version.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>

View file

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

31
SMCode/SM.OGL/Version.cs Normal file
View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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<Transformation>
public class DrawText : TextDrawingBasis<Transformation>, 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; }
}
}

View file

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

View file

@ -6,7 +6,7 @@ using SM2D.Scene;
namespace SM2D
{
public class GLWindow2D : GenericWindow<Scene.Scene, Camera>
public class GLWindow2D : GenericWindow<Scene.Scene, I2DShowItem, Camera>
{

View file

@ -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<Vector2> vertices)
{
foreach (Vector2 vertex in vertices)
{
Color.Add(Color4.White);
AddVertex(vertex);
}
foreach (Vector2 vertex in vertices)
{
AddUV(vertex);
}
}
public Polygon(ICollection<PolygonVertex> 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);
}
}
}

View file

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

View file

@ -46,11 +46,18 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Drawing\DrawBackground.cs" />
<Compile Include="Drawing\DrawColor.cs" />
<Compile Include="Drawing\DrawComplex.cs" />
<Compile Include="Drawing\DrawPolygon.cs" />
<Compile Include="Drawing\DrawText.cs" />
<Compile Include="Drawing\DrawTexture.cs" />
<Compile Include="GLWindow2D.cs" />
<Compile Include="Object\Polygon.cs" />
<Compile Include="Object\PolygonVertex.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="Types\Transformation.cs" />
</ItemGroup>

View file

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

View file

@ -0,0 +1,10 @@
using SM.Base.Scene;
namespace SM2D.Scene
{
public interface I2DShowItem : IShowItem
{
int ZIndex { get; set; }
}
}

View file

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

View file

@ -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<Camera>
public class Scene : GenericScene<Camera, I2DShowItem>
{
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);
}
}
}

View file

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