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